462103a80b27d6d6a01f850a58234cee888adbec
[external/binutils.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2
3    Copyright (C) 1986-2019 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <signal.h>
23 #include <fcntl.h>
24 #include "frame.h"              /* required by inferior.h */
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "symtab.h"
28 #include "command.h"
29 #include "bfd.h"
30 #include "target.h"
31 #include "process-stratum-target.h"
32 #include "gdbcore.h"
33 #include "gdbthread.h"
34 #include "regcache.h"
35 #include "regset.h"
36 #include "symfile.h"
37 #include "exec.h"
38 #include "readline/readline.h"
39 #include "solib.h"
40 #include "filenames.h"
41 #include "progspace.h"
42 #include "objfiles.h"
43 #include "gdb_bfd.h"
44 #include "completer.h"
45 #include "filestuff.h"
46
47 #ifndef O_LARGEFILE
48 #define O_LARGEFILE 0
49 #endif
50
51 static core_fns *sniff_core_bfd (gdbarch *core_gdbarch,
52                                  bfd *abfd);
53
54 /* The core file target.  */
55
56 static const target_info core_target_info = {
57   "core",
58   N_("Local core dump file"),
59   N_("Use a core file as a target.  Specify the filename of the core file.")
60 };
61
62 class core_target final : public process_stratum_target
63 {
64 public:
65   core_target ();
66   ~core_target () override;
67
68   const target_info &info () const override
69   { return core_target_info; }
70
71   void close () override;
72   void detach (inferior *, int) override;
73   void fetch_registers (struct regcache *, int) override;
74
75   enum target_xfer_status xfer_partial (enum target_object object,
76                                         const char *annex,
77                                         gdb_byte *readbuf,
78                                         const gdb_byte *writebuf,
79                                         ULONGEST offset, ULONGEST len,
80                                         ULONGEST *xfered_len) override;
81   void files_info () override;
82
83   bool thread_alive (ptid_t ptid) override;
84   const struct target_desc *read_description () override;
85
86   const char *pid_to_str (ptid_t) override;
87
88   const char *thread_name (struct thread_info *) override;
89
90   bool has_all_memory () override { return false; }
91   bool has_memory () override;
92   bool has_stack () override;
93   bool has_registers () override;
94   bool has_execution (ptid_t) override { return false; }
95
96   bool info_proc (const char *, enum info_proc_what) override;
97
98   /* A few helpers.  */
99
100   /* Getter, see variable definition.  */
101   struct gdbarch *core_gdbarch ()
102   {
103     return m_core_gdbarch;
104   }
105
106   /* See definition.  */
107   void get_core_register_section (struct regcache *regcache,
108                                   const struct regset *regset,
109                                   const char *name,
110                                   int section_min_size,
111                                   int which,
112                                   const char *human_name,
113                                   bool required);
114
115 private: /* per-core data */
116
117   /* The core's section table.  Note that these target sections are
118      *not* mapped in the current address spaces' set of target
119      sections --- those should come only from pure executable or
120      shared library bfds.  The core bfd sections are an implementation
121      detail of the core target, just like ptrace is for unix child
122      targets.  */
123   target_section_table m_core_section_table {};
124
125   /* The core_fns for a core file handler that is prepared to read the
126      core file currently open on core_bfd.  */
127   core_fns *m_core_vec = NULL;
128
129   /* FIXME: kettenis/20031023: Eventually this field should
130      disappear.  */
131   struct gdbarch *m_core_gdbarch = NULL;
132 };
133
134 core_target::core_target ()
135 {
136   m_core_gdbarch = gdbarch_from_bfd (core_bfd);
137
138   /* Find a suitable core file handler to munch on core_bfd */
139   m_core_vec = sniff_core_bfd (m_core_gdbarch, core_bfd);
140
141   /* Find the data section */
142   if (build_section_table (core_bfd,
143                            &m_core_section_table.sections,
144                            &m_core_section_table.sections_end))
145     error (_("\"%s\": Can't find sections: %s"),
146            bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
147 }
148
149 core_target::~core_target ()
150 {
151   xfree (m_core_section_table.sections);
152 }
153
154 /* List of all available core_fns.  On gdb startup, each core file
155    register reader calls deprecated_add_core_fns() to register
156    information on each core format it is prepared to read.  */
157
158 static struct core_fns *core_file_fns = NULL;
159
160 static int gdb_check_format (bfd *);
161
162 static void add_to_thread_list (bfd *, asection *, void *);
163
164 /* An arbitrary identifier for the core inferior.  */
165 #define CORELOW_PID 1
166
167 /* Link a new core_fns into the global core_file_fns list.  Called on
168    gdb startup by the _initialize routine in each core file register
169    reader, to register information about each format the reader is
170    prepared to handle.  */
171
172 void
173 deprecated_add_core_fns (struct core_fns *cf)
174 {
175   cf->next = core_file_fns;
176   core_file_fns = cf;
177 }
178
179 /* The default function that core file handlers can use to examine a
180    core file BFD and decide whether or not to accept the job of
181    reading the core file.  */
182
183 int
184 default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
185 {
186   int result;
187
188   result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
189   return (result);
190 }
191
192 /* Walk through the list of core functions to find a set that can
193    handle the core file open on ABFD.  Returns pointer to set that is
194    selected.  */
195
196 static struct core_fns *
197 sniff_core_bfd (struct gdbarch *core_gdbarch, bfd *abfd)
198 {
199   struct core_fns *cf;
200   struct core_fns *yummy = NULL;
201   int matches = 0;
202
203   /* Don't sniff if we have support for register sets in
204      CORE_GDBARCH.  */
205   if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
206     return NULL;
207
208   for (cf = core_file_fns; cf != NULL; cf = cf->next)
209     {
210       if (cf->core_sniffer (cf, abfd))
211         {
212           yummy = cf;
213           matches++;
214         }
215     }
216   if (matches > 1)
217     {
218       warning (_("\"%s\": ambiguous core format, %d handlers match"),
219                bfd_get_filename (abfd), matches);
220     }
221   else if (matches == 0)
222     error (_("\"%s\": no core file handler recognizes format"),
223            bfd_get_filename (abfd));
224
225   return (yummy);
226 }
227
228 /* The default is to reject every core file format we see.  Either
229    BFD has to recognize it, or we have to provide a function in the
230    core file handler that recognizes it.  */
231
232 int
233 default_check_format (bfd *abfd)
234 {
235   return (0);
236 }
237
238 /* Attempt to recognize core file formats that BFD rejects.  */
239
240 static int
241 gdb_check_format (bfd *abfd)
242 {
243   struct core_fns *cf;
244
245   for (cf = core_file_fns; cf != NULL; cf = cf->next)
246     {
247       if (cf->check_format (abfd))
248         {
249           return (1);
250         }
251     }
252   return (0);
253 }
254
255 /* Close the core target.  */
256
257 void
258 core_target::close ()
259 {
260   if (core_bfd)
261     {
262       inferior_ptid = null_ptid;    /* Avoid confusion from thread
263                                        stuff.  */
264       exit_inferior_silent (current_inferior ());
265
266       /* Clear out solib state while the bfd is still open.  See
267          comments in clear_solib in solib.c.  */
268       clear_solib ();
269
270       current_program_space->cbfd.reset (nullptr);
271     }
272
273   /* Core targets are heap-allocated (see core_target_open), so here
274      we delete ourselves.  */
275   delete this;
276 }
277
278 /* Look for sections whose names start with `.reg/' so that we can
279    extract the list of threads in a core file.  */
280
281 static void
282 add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
283 {
284   ptid_t ptid;
285   int core_tid;
286   int pid, lwpid;
287   asection *reg_sect = (asection *) reg_sect_arg;
288   int fake_pid_p = 0;
289   struct inferior *inf;
290
291   if (!startswith (bfd_section_name (abfd, asect), ".reg/"))
292     return;
293
294   core_tid = atoi (bfd_section_name (abfd, asect) + 5);
295
296   pid = bfd_core_file_pid (core_bfd);
297   if (pid == 0)
298     {
299       fake_pid_p = 1;
300       pid = CORELOW_PID;
301     }
302
303   lwpid = core_tid;
304
305   inf = current_inferior ();
306   if (inf->pid == 0)
307     {
308       inferior_appeared (inf, pid);
309       inf->fake_pid_p = fake_pid_p;
310     }
311
312   ptid = ptid_t (pid, lwpid, 0);
313
314   add_thread (ptid);
315
316 /* Warning, Will Robinson, looking at BFD private data! */
317
318   if (reg_sect != NULL
319       && asect->filepos == reg_sect->filepos)   /* Did we find .reg?  */
320     inferior_ptid = ptid;                       /* Yes, make it current.  */
321 }
322
323 /* Issue a message saying we have no core to debug, if FROM_TTY.  */
324
325 static void
326 maybe_say_no_core_file_now (int from_tty)
327 {
328   if (from_tty)
329     printf_filtered (_("No core file now.\n"));
330 }
331
332 /* Backward compatability with old way of specifying core files.  */
333
334 void
335 core_file_command (const char *filename, int from_tty)
336 {
337   dont_repeat ();               /* Either way, seems bogus.  */
338
339   if (filename == NULL)
340     {
341       if (core_bfd != NULL)
342         {
343           target_detach (current_inferior (), from_tty);
344           gdb_assert (core_bfd == NULL);
345         }
346       else
347         maybe_say_no_core_file_now (from_tty);
348     }
349   else
350     core_target_open (filename, from_tty);
351 }
352
353 /* See gdbcore.h.  */
354
355 void
356 core_target_open (const char *arg, int from_tty)
357 {
358   const char *p;
359   int siggy;
360   int scratch_chan;
361   int flags;
362
363   target_preopen (from_tty);
364   if (!arg)
365     {
366       if (core_bfd)
367         error (_("No core file specified.  (Use `detach' "
368                  "to stop debugging a core file.)"));
369       else
370         error (_("No core file specified."));
371     }
372
373   gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
374   if (!IS_ABSOLUTE_PATH (filename.get ()))
375     filename.reset (concat (current_directory, "/",
376                             filename.get (), (char *) NULL));
377
378   flags = O_BINARY | O_LARGEFILE;
379   if (write_files)
380     flags |= O_RDWR;
381   else
382     flags |= O_RDONLY;
383   scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
384   if (scratch_chan < 0)
385     perror_with_name (filename.get ());
386
387   gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
388                                            write_files ? FOPEN_RUB : FOPEN_RB,
389                                            scratch_chan));
390   if (temp_bfd == NULL)
391     perror_with_name (filename.get ());
392
393   if (!bfd_check_format (temp_bfd.get (), bfd_core)
394       && !gdb_check_format (temp_bfd.get ()))
395     {
396       /* Do it after the err msg */
397       /* FIXME: should be checking for errors from bfd_close (for one
398          thing, on error it does not free all the storage associated
399          with the bfd).  */
400       error (_("\"%s\" is not a core dump: %s"),
401              filename.get (), bfd_errmsg (bfd_get_error ()));
402     }
403
404   current_program_space->cbfd = std::move (temp_bfd);
405
406   core_target *target = new core_target ();
407
408   /* Own the target until it is successfully pushed.  */
409   target_ops_up target_holder (target);
410
411   validate_files ();
412
413   /* If we have no exec file, try to set the architecture from the
414      core file.  We don't do this unconditionally since an exec file
415      typically contains more information that helps us determine the
416      architecture than a core file.  */
417   if (!exec_bfd)
418     set_gdbarch_from_file (core_bfd);
419
420   push_target (target);
421   target_holder.release ();
422
423   inferior_ptid = null_ptid;
424
425   /* Need to flush the register cache (and the frame cache) from a
426      previous debug session.  If inferior_ptid ends up the same as the
427      last debug session --- e.g., b foo; run; gcore core1; step; gcore
428      core2; core core1; core core2 --- then there's potential for
429      get_current_regcache to return the cached regcache of the
430      previous session, and the frame cache being stale.  */
431   registers_changed ();
432
433   /* Build up thread list from BFD sections, and possibly set the
434      current thread to the .reg/NN section matching the .reg
435      section.  */
436   bfd_map_over_sections (core_bfd, add_to_thread_list,
437                          bfd_get_section_by_name (core_bfd, ".reg"));
438
439   if (inferior_ptid == null_ptid)
440     {
441       /* Either we found no .reg/NN section, and hence we have a
442          non-threaded core (single-threaded, from gdb's perspective),
443          or for some reason add_to_thread_list couldn't determine
444          which was the "main" thread.  The latter case shouldn't
445          usually happen, but we're dealing with input here, which can
446          always be broken in different ways.  */
447       thread_info *thread = first_thread_of_inferior (current_inferior ());
448
449       if (thread == NULL)
450         {
451           inferior_appeared (current_inferior (), CORELOW_PID);
452           inferior_ptid = ptid_t (CORELOW_PID);
453           add_thread_silent (inferior_ptid);
454         }
455       else
456         switch_to_thread (thread);
457     }
458
459   post_create_inferior (target, from_tty);
460
461   /* Now go through the target stack looking for threads since there
462      may be a thread_stratum target loaded on top of target core by
463      now.  The layer above should claim threads found in the BFD
464      sections.  */
465   TRY
466     {
467       target_update_thread_list ();
468     }
469
470   CATCH (except, RETURN_MASK_ERROR)
471     {
472       exception_print (gdb_stderr, except);
473     }
474   END_CATCH
475
476   p = bfd_core_file_failing_command (core_bfd);
477   if (p)
478     printf_filtered (_("Core was generated by `%s'.\n"), p);
479
480   /* Clearing any previous state of convenience variables.  */
481   clear_exit_convenience_vars ();
482
483   siggy = bfd_core_file_failing_signal (core_bfd);
484   if (siggy > 0)
485     {
486       gdbarch *core_gdbarch = target->core_gdbarch ();
487
488       /* If we don't have a CORE_GDBARCH to work with, assume a native
489          core (map gdb_signal from host signals).  If we do have
490          CORE_GDBARCH to work with, but no gdb_signal_from_target
491          implementation for that gdbarch, as a fallback measure,
492          assume the host signal mapping.  It'll be correct for native
493          cores, but most likely incorrect for cross-cores.  */
494       enum gdb_signal sig = (core_gdbarch != NULL
495                              && gdbarch_gdb_signal_from_target_p (core_gdbarch)
496                              ? gdbarch_gdb_signal_from_target (core_gdbarch,
497                                                                siggy)
498                              : gdb_signal_from_host (siggy));
499
500       printf_filtered (_("Program terminated with signal %s, %s.\n"),
501                        gdb_signal_to_name (sig), gdb_signal_to_string (sig));
502
503       /* Set the value of the internal variable $_exitsignal,
504          which holds the signal uncaught by the inferior.  */
505       set_internalvar_integer (lookup_internalvar ("_exitsignal"),
506                                siggy);
507     }
508
509   /* Fetch all registers from core file.  */
510   target_fetch_registers (get_current_regcache (), -1);
511
512   /* Now, set up the frame cache, and print the top of stack.  */
513   reinit_frame_cache ();
514   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
515
516   /* Current thread should be NUM 1 but the user does not know that.
517      If a program is single threaded gdb in general does not mention
518      anything about threads.  That is why the test is >= 2.  */
519   if (thread_count () >= 2)
520     {
521       TRY
522         {
523           thread_command (NULL, from_tty);
524         }
525       CATCH (except, RETURN_MASK_ERROR)
526         {
527           exception_print (gdb_stderr, except);
528         }
529       END_CATCH
530     }
531 }
532
533 void
534 core_target::detach (inferior *inf, int from_tty)
535 {
536   /* Note that 'this' is dangling after this call.  unpush_target
537      closes the target, and our close implementation deletes
538      'this'.  */
539   unpush_target (this);
540
541   reinit_frame_cache ();
542   maybe_say_no_core_file_now (from_tty);
543 }
544
545 /* Try to retrieve registers from a section in core_bfd, and supply
546    them to m_core_vec->core_read_registers, as the register set
547    numbered WHICH.
548
549    If ptid's lwp member is zero, do the single-threaded
550    thing: look for a section named NAME.  If ptid's lwp
551    member is non-zero, do the multi-threaded thing: look for a section
552    named "NAME/LWP", where LWP is the shortest ASCII decimal
553    representation of ptid's lwp member.
554
555    HUMAN_NAME is a human-readable name for the kind of registers the
556    NAME section contains, for use in error messages.
557
558    If REQUIRED is true, print an error if the core file doesn't have a
559    section by the appropriate name.  Otherwise, just do nothing.  */
560
561 void
562 core_target::get_core_register_section (struct regcache *regcache,
563                                         const struct regset *regset,
564                                         const char *name,
565                                         int section_min_size,
566                                         int which,
567                                         const char *human_name,
568                                         bool required)
569 {
570   struct bfd_section *section;
571   bfd_size_type size;
572   char *contents;
573   bool variable_size_section = (regset != NULL
574                                 && regset->flags & REGSET_VARIABLE_SIZE);
575
576   thread_section_name section_name (name, regcache->ptid ());
577
578   section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
579   if (! section)
580     {
581       if (required)
582         warning (_("Couldn't find %s registers in core file."),
583                  human_name);
584       return;
585     }
586
587   size = bfd_section_size (core_bfd, section);
588   if (size < section_min_size)
589     {
590       warning (_("Section `%s' in core file too small."),
591                section_name.c_str ());
592       return;
593     }
594   if (size != section_min_size && !variable_size_section)
595     {
596       warning (_("Unexpected size of section `%s' in core file."),
597                section_name.c_str ());
598     }
599
600   contents = (char *) alloca (size);
601   if (! bfd_get_section_contents (core_bfd, section, contents,
602                                   (file_ptr) 0, size))
603     {
604       warning (_("Couldn't read %s registers from `%s' section in core file."),
605                human_name, section_name.c_str ());
606       return;
607     }
608
609   if (regset != NULL)
610     {
611       regset->supply_regset (regset, regcache, -1, contents, size);
612       return;
613     }
614
615   gdb_assert (m_core_vec != nullptr);
616   m_core_vec->core_read_registers (regcache, contents, size, which,
617                                    ((CORE_ADDR)
618                                     bfd_section_vma (core_bfd, section)));
619 }
620
621 /* Data passed to gdbarch_iterate_over_regset_sections's callback.  */
622 struct get_core_registers_cb_data
623 {
624   core_target *target;
625   struct regcache *regcache;
626 };
627
628 /* Callback for get_core_registers that handles a single core file
629    register note section. */
630
631 static void
632 get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
633                        const struct regset *regset,
634                        const char *human_name, void *cb_data)
635 {
636   auto *data = (get_core_registers_cb_data *) cb_data;
637   bool required = false;
638   bool variable_size_section = (regset != NULL
639                                 && regset->flags & REGSET_VARIABLE_SIZE);
640
641   if (!variable_size_section)
642     gdb_assert (supply_size == collect_size);
643
644   if (strcmp (sect_name, ".reg") == 0)
645     {
646       required = true;
647       if (human_name == NULL)
648         human_name = "general-purpose";
649     }
650   else if (strcmp (sect_name, ".reg2") == 0)
651     {
652       if (human_name == NULL)
653         human_name = "floating-point";
654     }
655
656   /* The 'which' parameter is only used when no regset is provided.
657      Thus we just set it to -1. */
658   data->target->get_core_register_section (data->regcache, regset, sect_name,
659                                            supply_size, -1, human_name,
660                                            required);
661 }
662
663 /* Get the registers out of a core file.  This is the machine-
664    independent part.  Fetch_core_registers is the machine-dependent
665    part, typically implemented in the xm-file for each
666    architecture.  */
667
668 /* We just get all the registers, so we don't use regno.  */
669
670 void
671 core_target::fetch_registers (struct regcache *regcache, int regno)
672 {
673   int i;
674   struct gdbarch *gdbarch;
675
676   if (!(m_core_gdbarch != nullptr
677         && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
678       && (m_core_vec == NULL || m_core_vec->core_read_registers == NULL))
679     {
680       fprintf_filtered (gdb_stderr,
681                      "Can't fetch registers from this type of core file\n");
682       return;
683     }
684
685   gdbarch = regcache->arch ();
686   if (gdbarch_iterate_over_regset_sections_p (gdbarch))
687     {
688       get_core_registers_cb_data data = { this, regcache };
689       gdbarch_iterate_over_regset_sections (gdbarch,
690                                             get_core_registers_cb,
691                                             (void *) &data, NULL);
692     }
693   else
694     {
695       get_core_register_section (regcache, NULL,
696                                  ".reg", 0, 0, "general-purpose", 1);
697       get_core_register_section (regcache, NULL,
698                                  ".reg2", 0, 2, "floating-point", 0);
699     }
700
701   /* Mark all registers not found in the core as unavailable.  */
702   for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
703     if (regcache->get_register_status (i) == REG_UNKNOWN)
704       regcache->raw_supply (i, NULL);
705 }
706
707 void
708 core_target::files_info ()
709 {
710   print_section_info (&m_core_section_table, core_bfd);
711 }
712 \f
713 struct spuid_list
714 {
715   gdb_byte *buf;
716   ULONGEST offset;
717   LONGEST len;
718   ULONGEST pos;
719   ULONGEST written;
720 };
721
722 static void
723 add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
724 {
725   struct spuid_list *list = (struct spuid_list *) list_p;
726   enum bfd_endian byte_order
727     = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
728   int fd, pos = 0;
729
730   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
731   if (pos == 0)
732     return;
733
734   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
735     {
736       store_unsigned_integer (list->buf + list->pos - list->offset,
737                               4, byte_order, fd);
738       list->written += 4;
739     }
740   list->pos += 4;
741 }
742
743 enum target_xfer_status
744 core_target::xfer_partial (enum target_object object, const char *annex,
745                            gdb_byte *readbuf, const gdb_byte *writebuf,
746                            ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
747 {
748   switch (object)
749     {
750     case TARGET_OBJECT_MEMORY:
751       return (section_table_xfer_memory_partial
752               (readbuf, writebuf,
753                offset, len, xfered_len,
754                m_core_section_table.sections,
755                m_core_section_table.sections_end,
756                NULL));
757
758     case TARGET_OBJECT_AUXV:
759       if (readbuf)
760         {
761           /* When the aux vector is stored in core file, BFD
762              represents this with a fake section called ".auxv".  */
763
764           struct bfd_section *section;
765           bfd_size_type size;
766
767           section = bfd_get_section_by_name (core_bfd, ".auxv");
768           if (section == NULL)
769             return TARGET_XFER_E_IO;
770
771           size = bfd_section_size (core_bfd, section);
772           if (offset >= size)
773             return TARGET_XFER_EOF;
774           size -= offset;
775           if (size > len)
776             size = len;
777
778           if (size == 0)
779             return TARGET_XFER_EOF;
780           if (!bfd_get_section_contents (core_bfd, section, readbuf,
781                                          (file_ptr) offset, size))
782             {
783               warning (_("Couldn't read NT_AUXV note in core file."));
784               return TARGET_XFER_E_IO;
785             }
786
787           *xfered_len = (ULONGEST) size;
788           return TARGET_XFER_OK;
789         }
790       return TARGET_XFER_E_IO;
791
792     case TARGET_OBJECT_WCOOKIE:
793       if (readbuf)
794         {
795           /* When the StackGhost cookie is stored in core file, BFD
796              represents this with a fake section called
797              ".wcookie".  */
798
799           struct bfd_section *section;
800           bfd_size_type size;
801
802           section = bfd_get_section_by_name (core_bfd, ".wcookie");
803           if (section == NULL)
804             return TARGET_XFER_E_IO;
805
806           size = bfd_section_size (core_bfd, section);
807           if (offset >= size)
808             return TARGET_XFER_EOF;
809           size -= offset;
810           if (size > len)
811             size = len;
812
813           if (size == 0)
814             return TARGET_XFER_EOF;
815           if (!bfd_get_section_contents (core_bfd, section, readbuf,
816                                          (file_ptr) offset, size))
817             {
818               warning (_("Couldn't read StackGhost cookie in core file."));
819               return TARGET_XFER_E_IO;
820             }
821
822           *xfered_len = (ULONGEST) size;
823           return TARGET_XFER_OK;
824
825         }
826       return TARGET_XFER_E_IO;
827
828     case TARGET_OBJECT_LIBRARIES:
829       if (m_core_gdbarch != nullptr
830           && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
831         {
832           if (writebuf)
833             return TARGET_XFER_E_IO;
834           else
835             {
836               *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
837                                                                 readbuf,
838                                                                 offset, len);
839
840               if (*xfered_len == 0)
841                 return TARGET_XFER_EOF;
842               else
843                 return TARGET_XFER_OK;
844             }
845         }
846       /* FALL THROUGH */
847
848     case TARGET_OBJECT_LIBRARIES_AIX:
849       if (m_core_gdbarch != nullptr
850           && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
851         {
852           if (writebuf)
853             return TARGET_XFER_E_IO;
854           else
855             {
856               *xfered_len
857                 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
858                                                           readbuf, offset,
859                                                           len);
860
861               if (*xfered_len == 0)
862                 return TARGET_XFER_EOF;
863               else
864                 return TARGET_XFER_OK;
865             }
866         }
867       /* FALL THROUGH */
868
869     case TARGET_OBJECT_SPU:
870       if (readbuf && annex)
871         {
872           /* When the SPU contexts are stored in a core file, BFD
873              represents this with a fake section called
874              "SPU/<annex>".  */
875
876           struct bfd_section *section;
877           bfd_size_type size;
878           char sectionstr[100];
879
880           xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
881
882           section = bfd_get_section_by_name (core_bfd, sectionstr);
883           if (section == NULL)
884             return TARGET_XFER_E_IO;
885
886           size = bfd_section_size (core_bfd, section);
887           if (offset >= size)
888             return TARGET_XFER_EOF;
889           size -= offset;
890           if (size > len)
891             size = len;
892
893           if (size == 0)
894             return TARGET_XFER_EOF;
895           if (!bfd_get_section_contents (core_bfd, section, readbuf,
896                                          (file_ptr) offset, size))
897             {
898               warning (_("Couldn't read SPU section in core file."));
899               return TARGET_XFER_E_IO;
900             }
901
902           *xfered_len = (ULONGEST) size;
903           return TARGET_XFER_OK;
904         }
905       else if (readbuf)
906         {
907           /* NULL annex requests list of all present spuids.  */
908           struct spuid_list list;
909
910           list.buf = readbuf;
911           list.offset = offset;
912           list.len = len;
913           list.pos = 0;
914           list.written = 0;
915           bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
916
917           if (list.written == 0)
918             return TARGET_XFER_EOF;
919           else
920             {
921               *xfered_len = (ULONGEST) list.written;
922               return TARGET_XFER_OK;
923             }
924         }
925       return TARGET_XFER_E_IO;
926
927     case TARGET_OBJECT_SIGNAL_INFO:
928       if (readbuf)
929         {
930           if (m_core_gdbarch != nullptr
931               && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
932             {
933               LONGEST l = gdbarch_core_xfer_siginfo  (m_core_gdbarch, readbuf,
934                                                       offset, len);
935
936               if (l >= 0)
937                 {
938                   *xfered_len = l;
939                   if (l == 0)
940                     return TARGET_XFER_EOF;
941                   else
942                     return TARGET_XFER_OK;
943                 }
944             }
945         }
946       return TARGET_XFER_E_IO;
947
948     default:
949       return this->beneath ()->xfer_partial (object, annex, readbuf,
950                                              writebuf, offset, len,
951                                              xfered_len);
952     }
953 }
954
955 \f
956
957 /* Okay, let's be honest: threads gleaned from a core file aren't
958    exactly lively, are they?  On the other hand, if we don't claim
959    that each & every one is alive, then we don't get any of them
960    to appear in an "info thread" command, which is quite a useful
961    behaviour.
962  */
963 bool
964 core_target::thread_alive (ptid_t ptid)
965 {
966   return true;
967 }
968
969 /* Ask the current architecture what it knows about this core file.
970    That will be used, in turn, to pick a better architecture.  This
971    wrapper could be avoided if targets got a chance to specialize
972    core_target.  */
973
974 const struct target_desc *
975 core_target::read_description ()
976 {
977   if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
978     {
979       const struct target_desc *result;
980
981       result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
982       if (result != NULL)
983         return result;
984     }
985
986   return this->beneath ()->read_description ();
987 }
988
989 const char *
990 core_target::pid_to_str (ptid_t ptid)
991 {
992   static char buf[64];
993   struct inferior *inf;
994   int pid;
995
996   /* The preferred way is to have a gdbarch/OS specific
997      implementation.  */
998   if (m_core_gdbarch != nullptr
999       && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1000     return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
1001
1002   /* Otherwise, if we don't have one, we'll just fallback to
1003      "process", with normal_pid_to_str.  */
1004
1005   /* Try the LWPID field first.  */
1006   pid = ptid.lwp ();
1007   if (pid != 0)
1008     return normal_pid_to_str (ptid_t (pid));
1009
1010   /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1011      only if it isn't a fake PID.  */
1012   inf = find_inferior_ptid (ptid);
1013   if (inf != NULL && !inf->fake_pid_p)
1014     return normal_pid_to_str (ptid);
1015
1016   /* No luck.  We simply don't have a valid PID to print.  */
1017   xsnprintf (buf, sizeof buf, "<main task>");
1018   return buf;
1019 }
1020
1021 const char *
1022 core_target::thread_name (struct thread_info *thr)
1023 {
1024   if (m_core_gdbarch != nullptr
1025       && gdbarch_core_thread_name_p (m_core_gdbarch))
1026     return gdbarch_core_thread_name (m_core_gdbarch, thr);
1027   return NULL;
1028 }
1029
1030 bool
1031 core_target::has_memory ()
1032 {
1033   return (core_bfd != NULL);
1034 }
1035
1036 bool
1037 core_target::has_stack ()
1038 {
1039   return (core_bfd != NULL);
1040 }
1041
1042 bool
1043 core_target::has_registers ()
1044 {
1045   return (core_bfd != NULL);
1046 }
1047
1048 /* Implement the to_info_proc method.  */
1049
1050 bool
1051 core_target::info_proc (const char *args, enum info_proc_what request)
1052 {
1053   struct gdbarch *gdbarch = get_current_arch ();
1054
1055   /* Since this is the core file target, call the 'core_info_proc'
1056      method on gdbarch, not 'info_proc'.  */
1057   if (gdbarch_core_info_proc_p (gdbarch))
1058     gdbarch_core_info_proc (gdbarch, args, request);
1059
1060   return true;
1061 }
1062
1063 void
1064 _initialize_corelow (void)
1065 {
1066   add_target (core_target_info, core_target_open, filename_completer);
1067 }