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