target_ops::beneath -> target_ops::beneath()
[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 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       int pid = ptid_get_pid (inferior_ptid);
264       inferior_ptid = null_ptid;    /* Avoid confusion from thread
265                                        stuff.  */
266       if (pid != 0)
267         exit_inferior_silent (pid);
268
269       /* Clear out solib state while the bfd is still open.  See
270          comments in clear_solib in solib.c.  */
271       clear_solib ();
272
273       current_program_space->cbfd.reset (nullptr);
274     }
275
276   /* Core targets are heap-allocated (see core_target_open), so here
277      we delete ourselves.  */
278   delete this;
279 }
280
281 /* Look for sections whose names start with `.reg/' so that we can
282    extract the list of threads in a core file.  */
283
284 static void
285 add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
286 {
287   ptid_t ptid;
288   int core_tid;
289   int pid, lwpid;
290   asection *reg_sect = (asection *) reg_sect_arg;
291   int fake_pid_p = 0;
292   struct inferior *inf;
293
294   if (!startswith (bfd_section_name (abfd, asect), ".reg/"))
295     return;
296
297   core_tid = atoi (bfd_section_name (abfd, asect) + 5);
298
299   pid = bfd_core_file_pid (core_bfd);
300   if (pid == 0)
301     {
302       fake_pid_p = 1;
303       pid = CORELOW_PID;
304     }
305
306   lwpid = core_tid;
307
308   inf = current_inferior ();
309   if (inf->pid == 0)
310     {
311       inferior_appeared (inf, pid);
312       inf->fake_pid_p = fake_pid_p;
313     }
314
315   ptid = ptid_build (pid, lwpid, 0);
316
317   add_thread (ptid);
318
319 /* Warning, Will Robinson, looking at BFD private data! */
320
321   if (reg_sect != NULL
322       && asect->filepos == reg_sect->filepos)   /* Did we find .reg?  */
323     inferior_ptid = ptid;                       /* Yes, make it current.  */
324 }
325
326 /* Issue a message saying we have no core to debug, if FROM_TTY.  */
327
328 static void
329 maybe_say_no_core_file_now (int from_tty)
330 {
331   if (from_tty)
332     printf_filtered (_("No core file now.\n"));
333 }
334
335 /* Backward compatability with old way of specifying core files.  */
336
337 void
338 core_file_command (const char *filename, int from_tty)
339 {
340   dont_repeat ();               /* Either way, seems bogus.  */
341
342   if (filename == NULL)
343     {
344       if (core_bfd != NULL)
345         {
346           target_detach (current_inferior (), from_tty);
347           gdb_assert (core_bfd == NULL);
348         }
349       else
350         maybe_say_no_core_file_now (from_tty);
351     }
352   else
353     core_target_open (filename, from_tty);
354 }
355
356 /* See gdbcore.h.  */
357
358 void
359 core_target_open (const char *arg, int from_tty)
360 {
361   const char *p;
362   int siggy;
363   struct cleanup *old_chain;
364   int scratch_chan;
365   int flags;
366
367   target_preopen (from_tty);
368   if (!arg)
369     {
370       if (core_bfd)
371         error (_("No core file specified.  (Use `detach' "
372                  "to stop debugging a core file.)"));
373       else
374         error (_("No core file specified."));
375     }
376
377   gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
378   if (!IS_ABSOLUTE_PATH (filename.get ()))
379     filename.reset (concat (current_directory, "/",
380                             filename.get (), (char *) NULL));
381
382   flags = O_BINARY | O_LARGEFILE;
383   if (write_files)
384     flags |= O_RDWR;
385   else
386     flags |= O_RDONLY;
387   scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
388   if (scratch_chan < 0)
389     perror_with_name (filename.get ());
390
391   gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
392                                            write_files ? FOPEN_RUB : FOPEN_RB,
393                                            scratch_chan));
394   if (temp_bfd == NULL)
395     perror_with_name (filename.get ());
396
397   if (!bfd_check_format (temp_bfd.get (), bfd_core)
398       && !gdb_check_format (temp_bfd.get ()))
399     {
400       /* Do it after the err msg */
401       /* FIXME: should be checking for errors from bfd_close (for one
402          thing, on error it does not free all the storage associated
403          with the bfd).  */
404       error (_("\"%s\" is not a core dump: %s"),
405              filename.get (), bfd_errmsg (bfd_get_error ()));
406     }
407
408   current_program_space->cbfd = std::move (temp_bfd);
409
410   core_target *target = new core_target ();
411
412   /* Own the target until it is successfully pushed.  */
413   target_ops_up target_holder (target);
414
415   validate_files ();
416
417   /* If we have no exec file, try to set the architecture from the
418      core file.  We don't do this unconditionally since an exec file
419      typically contains more information that helps us determine the
420      architecture than a core file.  */
421   if (!exec_bfd)
422     set_gdbarch_from_file (core_bfd);
423
424   push_target (target);
425   target_holder.release ();
426
427   /* Do this before acknowledging the inferior, so if
428      post_create_inferior throws (can happen easilly if you're loading
429      a core file with the wrong exec), we aren't left with threads
430      from the previous inferior.  */
431   init_thread_list ();
432
433   inferior_ptid = null_ptid;
434
435   /* Need to flush the register cache (and the frame cache) from a
436      previous debug session.  If inferior_ptid ends up the same as the
437      last debug session --- e.g., b foo; run; gcore core1; step; gcore
438      core2; core core1; core core2 --- then there's potential for
439      get_current_regcache to return the cached regcache of the
440      previous session, and the frame cache being stale.  */
441   registers_changed ();
442
443   /* Build up thread list from BFD sections, and possibly set the
444      current thread to the .reg/NN section matching the .reg
445      section.  */
446   bfd_map_over_sections (core_bfd, add_to_thread_list,
447                          bfd_get_section_by_name (core_bfd, ".reg"));
448
449   if (ptid_equal (inferior_ptid, null_ptid))
450     {
451       /* Either we found no .reg/NN section, and hence we have a
452          non-threaded core (single-threaded, from gdb's perspective),
453          or for some reason add_to_thread_list couldn't determine
454          which was the "main" thread.  The latter case shouldn't
455          usually happen, but we're dealing with input here, which can
456          always be broken in different ways.  */
457       struct thread_info *thread = first_thread_of_process (-1);
458
459       if (thread == NULL)
460         {
461           inferior_appeared (current_inferior (), CORELOW_PID);
462           inferior_ptid = pid_to_ptid (CORELOW_PID);
463           add_thread_silent (inferior_ptid);
464         }
465       else
466         switch_to_thread (thread->ptid);
467     }
468
469   post_create_inferior (target, from_tty);
470
471   /* Now go through the target stack looking for threads since there
472      may be a thread_stratum target loaded on top of target core by
473      now.  The layer above should claim threads found in the BFD
474      sections.  */
475   TRY
476     {
477       target_update_thread_list ();
478     }
479
480   CATCH (except, RETURN_MASK_ERROR)
481     {
482       exception_print (gdb_stderr, except);
483     }
484   END_CATCH
485
486   p = bfd_core_file_failing_command (core_bfd);
487   if (p)
488     printf_filtered (_("Core was generated by `%s'.\n"), p);
489
490   /* Clearing any previous state of convenience variables.  */
491   clear_exit_convenience_vars ();
492
493   siggy = bfd_core_file_failing_signal (core_bfd);
494   if (siggy > 0)
495     {
496       gdbarch *core_gdbarch = target->core_gdbarch ();
497
498       /* If we don't have a CORE_GDBARCH to work with, assume a native
499          core (map gdb_signal from host signals).  If we do have
500          CORE_GDBARCH to work with, but no gdb_signal_from_target
501          implementation for that gdbarch, as a fallback measure,
502          assume the host signal mapping.  It'll be correct for native
503          cores, but most likely incorrect for cross-cores.  */
504       enum gdb_signal sig = (core_gdbarch != NULL
505                              && gdbarch_gdb_signal_from_target_p (core_gdbarch)
506                              ? gdbarch_gdb_signal_from_target (core_gdbarch,
507                                                                siggy)
508                              : gdb_signal_from_host (siggy));
509
510       printf_filtered (_("Program terminated with signal %s, %s.\n"),
511                        gdb_signal_to_name (sig), gdb_signal_to_string (sig));
512
513       /* Set the value of the internal variable $_exitsignal,
514          which holds the signal uncaught by the inferior.  */
515       set_internalvar_integer (lookup_internalvar ("_exitsignal"),
516                                siggy);
517     }
518
519   /* Fetch all registers from core file.  */
520   target_fetch_registers (get_current_regcache (), -1);
521
522   /* Now, set up the frame cache, and print the top of stack.  */
523   reinit_frame_cache ();
524   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
525
526   /* Current thread should be NUM 1 but the user does not know that.
527      If a program is single threaded gdb in general does not mention
528      anything about threads.  That is why the test is >= 2.  */
529   if (thread_count () >= 2)
530     {
531       TRY
532         {
533           thread_command (NULL, from_tty);
534         }
535       CATCH (except, RETURN_MASK_ERROR)
536         {
537           exception_print (gdb_stderr, except);
538         }
539       END_CATCH
540     }
541 }
542
543 void
544 core_target::detach (inferior *inf, int from_tty)
545 {
546   /* Note that 'this' is dangling after this call.  unpush_target
547      closes the target, and our close implementation deletes
548      'this'.  */
549   unpush_target (this);
550
551   reinit_frame_cache ();
552   maybe_say_no_core_file_now (from_tty);
553 }
554
555 /* Try to retrieve registers from a section in core_bfd, and supply
556    them to m_core_vec->core_read_registers, as the register set
557    numbered WHICH.
558
559    If ptid's lwp member is zero, do the single-threaded
560    thing: look for a section named NAME.  If ptid's lwp
561    member is non-zero, do the multi-threaded thing: look for a section
562    named "NAME/LWP", where LWP is the shortest ASCII decimal
563    representation of ptid's lwp member.
564
565    HUMAN_NAME is a human-readable name for the kind of registers the
566    NAME section contains, for use in error messages.
567
568    If REQUIRED is true, print an error if the core file doesn't have a
569    section by the appropriate name.  Otherwise, just do nothing.  */
570
571 void
572 core_target::get_core_register_section (struct regcache *regcache,
573                                         const struct regset *regset,
574                                         const char *name,
575                                         int min_size,
576                                         int which,
577                                         const char *human_name,
578                                         bool required)
579 {
580   struct bfd_section *section;
581   bfd_size_type size;
582   char *contents;
583   bool variable_size_section = (regset != NULL
584                                 && regset->flags & REGSET_VARIABLE_SIZE);
585
586   thread_section_name section_name (name, regcache->ptid ());
587
588   section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
589   if (! section)
590     {
591       if (required)
592         warning (_("Couldn't find %s registers in core file."),
593                  human_name);
594       return;
595     }
596
597   size = bfd_section_size (core_bfd, section);
598   if (size < min_size)
599     {
600       warning (_("Section `%s' in core file too small."),
601                section_name.c_str ());
602       return;
603     }
604   if (size != min_size && !variable_size_section)
605     {
606       warning (_("Unexpected size of section `%s' in core file."),
607                section_name.c_str ());
608     }
609
610   contents = (char *) alloca (size);
611   if (! bfd_get_section_contents (core_bfd, section, contents,
612                                   (file_ptr) 0, size))
613     {
614       warning (_("Couldn't read %s registers from `%s' section in core file."),
615                human_name, section_name.c_str ());
616       return;
617     }
618
619   if (regset != NULL)
620     {
621       regset->supply_regset (regset, regcache, -1, contents, size);
622       return;
623     }
624
625   gdb_assert (m_core_vec != nullptr);
626   m_core_vec->core_read_registers (regcache, contents, size, which,
627                                    ((CORE_ADDR)
628                                     bfd_section_vma (core_bfd, section)));
629 }
630
631 /* Data passed to gdbarch_iterate_over_regset_sections's callback.  */
632 struct get_core_registers_cb_data
633 {
634   core_target *target;
635   struct regcache *regcache;
636 };
637
638 /* Callback for get_core_registers that handles a single core file
639    register note section. */
640
641 static void
642 get_core_registers_cb (const char *sect_name, int size,
643                        const struct regset *regset,
644                        const char *human_name, void *cb_data)
645 {
646   auto *data = (get_core_registers_cb_data *) cb_data;
647   bool required = false;
648
649   if (strcmp (sect_name, ".reg") == 0)
650     {
651       required = true;
652       if (human_name == NULL)
653         human_name = "general-purpose";
654     }
655   else if (strcmp (sect_name, ".reg2") == 0)
656     {
657       if (human_name == NULL)
658         human_name = "floating-point";
659     }
660
661   /* The 'which' parameter is only used when no regset is provided.
662      Thus we just set it to -1. */
663   data->target->get_core_register_section (data->regcache, regset, sect_name,
664                                            size, -1, human_name, required);
665 }
666
667 /* Get the registers out of a core file.  This is the machine-
668    independent part.  Fetch_core_registers is the machine-dependent
669    part, typically implemented in the xm-file for each
670    architecture.  */
671
672 /* We just get all the registers, so we don't use regno.  */
673
674 void
675 core_target::fetch_registers (struct regcache *regcache, int regno)
676 {
677   int i;
678   struct gdbarch *gdbarch;
679
680   if (!(m_core_gdbarch != nullptr
681         && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
682       && (m_core_vec == NULL || m_core_vec->core_read_registers == NULL))
683     {
684       fprintf_filtered (gdb_stderr,
685                      "Can't fetch registers from this type of core file\n");
686       return;
687     }
688
689   gdbarch = regcache->arch ();
690   if (gdbarch_iterate_over_regset_sections_p (gdbarch))
691     {
692       get_core_registers_cb_data data = { this, regcache };
693       gdbarch_iterate_over_regset_sections (gdbarch,
694                                             get_core_registers_cb,
695                                             (void *) &data, NULL);
696     }
697   else
698     {
699       get_core_register_section (regcache, NULL,
700                                  ".reg", 0, 0, "general-purpose", 1);
701       get_core_register_section (regcache, NULL,
702                                  ".reg2", 0, 2, "floating-point", 0);
703     }
704
705   /* Mark all registers not found in the core as unavailable.  */
706   for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
707     if (regcache->get_register_status (i) == REG_UNKNOWN)
708       regcache->raw_supply (i, NULL);
709 }
710
711 void
712 core_target::files_info ()
713 {
714   print_section_info (&m_core_section_table, core_bfd);
715 }
716 \f
717 struct spuid_list
718 {
719   gdb_byte *buf;
720   ULONGEST offset;
721   LONGEST len;
722   ULONGEST pos;
723   ULONGEST written;
724 };
725
726 static void
727 add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
728 {
729   struct spuid_list *list = (struct spuid_list *) list_p;
730   enum bfd_endian byte_order
731     = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
732   int fd, pos = 0;
733
734   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
735   if (pos == 0)
736     return;
737
738   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
739     {
740       store_unsigned_integer (list->buf + list->pos - list->offset,
741                               4, byte_order, fd);
742       list->written += 4;
743     }
744   list->pos += 4;
745 }
746
747 enum target_xfer_status
748 core_target::xfer_partial (enum target_object object, const char *annex,
749                            gdb_byte *readbuf, const gdb_byte *writebuf,
750                            ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
751 {
752   switch (object)
753     {
754     case TARGET_OBJECT_MEMORY:
755       return (section_table_xfer_memory_partial
756               (readbuf, writebuf,
757                offset, len, xfered_len,
758                m_core_section_table.sections,
759                m_core_section_table.sections_end,
760                NULL));
761
762     case TARGET_OBJECT_AUXV:
763       if (readbuf)
764         {
765           /* When the aux vector is stored in core file, BFD
766              represents this with a fake section called ".auxv".  */
767
768           struct bfd_section *section;
769           bfd_size_type size;
770
771           section = bfd_get_section_by_name (core_bfd, ".auxv");
772           if (section == NULL)
773             return TARGET_XFER_E_IO;
774
775           size = bfd_section_size (core_bfd, section);
776           if (offset >= size)
777             return TARGET_XFER_EOF;
778           size -= offset;
779           if (size > len)
780             size = len;
781
782           if (size == 0)
783             return TARGET_XFER_EOF;
784           if (!bfd_get_section_contents (core_bfd, section, readbuf,
785                                          (file_ptr) offset, size))
786             {
787               warning (_("Couldn't read NT_AUXV note in core file."));
788               return TARGET_XFER_E_IO;
789             }
790
791           *xfered_len = (ULONGEST) size;
792           return TARGET_XFER_OK;
793         }
794       return TARGET_XFER_E_IO;
795
796     case TARGET_OBJECT_WCOOKIE:
797       if (readbuf)
798         {
799           /* When the StackGhost cookie is stored in core file, BFD
800              represents this with a fake section called
801              ".wcookie".  */
802
803           struct bfd_section *section;
804           bfd_size_type size;
805
806           section = bfd_get_section_by_name (core_bfd, ".wcookie");
807           if (section == NULL)
808             return TARGET_XFER_E_IO;
809
810           size = bfd_section_size (core_bfd, section);
811           if (offset >= size)
812             return TARGET_XFER_EOF;
813           size -= offset;
814           if (size > len)
815             size = len;
816
817           if (size == 0)
818             return TARGET_XFER_EOF;
819           if (!bfd_get_section_contents (core_bfd, section, readbuf,
820                                          (file_ptr) offset, size))
821             {
822               warning (_("Couldn't read StackGhost cookie in core file."));
823               return TARGET_XFER_E_IO;
824             }
825
826           *xfered_len = (ULONGEST) size;
827           return TARGET_XFER_OK;
828
829         }
830       return TARGET_XFER_E_IO;
831
832     case TARGET_OBJECT_LIBRARIES:
833       if (m_core_gdbarch != nullptr
834           && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
835         {
836           if (writebuf)
837             return TARGET_XFER_E_IO;
838           else
839             {
840               *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
841                                                                 readbuf,
842                                                                 offset, len);
843
844               if (*xfered_len == 0)
845                 return TARGET_XFER_EOF;
846               else
847                 return TARGET_XFER_OK;
848             }
849         }
850       /* FALL THROUGH */
851
852     case TARGET_OBJECT_LIBRARIES_AIX:
853       if (m_core_gdbarch != nullptr
854           && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
855         {
856           if (writebuf)
857             return TARGET_XFER_E_IO;
858           else
859             {
860               *xfered_len
861                 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
862                                                           readbuf, offset,
863                                                           len);
864
865               if (*xfered_len == 0)
866                 return TARGET_XFER_EOF;
867               else
868                 return TARGET_XFER_OK;
869             }
870         }
871       /* FALL THROUGH */
872
873     case TARGET_OBJECT_SPU:
874       if (readbuf && annex)
875         {
876           /* When the SPU contexts are stored in a core file, BFD
877              represents this with a fake section called
878              "SPU/<annex>".  */
879
880           struct bfd_section *section;
881           bfd_size_type size;
882           char sectionstr[100];
883
884           xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
885
886           section = bfd_get_section_by_name (core_bfd, sectionstr);
887           if (section == NULL)
888             return TARGET_XFER_E_IO;
889
890           size = bfd_section_size (core_bfd, section);
891           if (offset >= size)
892             return TARGET_XFER_EOF;
893           size -= offset;
894           if (size > len)
895             size = len;
896
897           if (size == 0)
898             return TARGET_XFER_EOF;
899           if (!bfd_get_section_contents (core_bfd, section, readbuf,
900                                          (file_ptr) offset, size))
901             {
902               warning (_("Couldn't read SPU section in core file."));
903               return TARGET_XFER_E_IO;
904             }
905
906           *xfered_len = (ULONGEST) size;
907           return TARGET_XFER_OK;
908         }
909       else if (readbuf)
910         {
911           /* NULL annex requests list of all present spuids.  */
912           struct spuid_list list;
913
914           list.buf = readbuf;
915           list.offset = offset;
916           list.len = len;
917           list.pos = 0;
918           list.written = 0;
919           bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
920
921           if (list.written == 0)
922             return TARGET_XFER_EOF;
923           else
924             {
925               *xfered_len = (ULONGEST) list.written;
926               return TARGET_XFER_OK;
927             }
928         }
929       return TARGET_XFER_E_IO;
930
931     case TARGET_OBJECT_SIGNAL_INFO:
932       if (readbuf)
933         {
934           if (m_core_gdbarch != nullptr
935               && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
936             {
937               LONGEST l = gdbarch_core_xfer_siginfo  (m_core_gdbarch, readbuf,
938                                                       offset, len);
939
940               if (l >= 0)
941                 {
942                   *xfered_len = l;
943                   if (l == 0)
944                     return TARGET_XFER_EOF;
945                   else
946                     return TARGET_XFER_OK;
947                 }
948             }
949         }
950       return TARGET_XFER_E_IO;
951
952     default:
953       return this->beneath ()->xfer_partial (object, annex, readbuf,
954                                              writebuf, offset, len,
955                                              xfered_len);
956     }
957 }
958
959 \f
960
961 /* Okay, let's be honest: threads gleaned from a core file aren't
962    exactly lively, are they?  On the other hand, if we don't claim
963    that each & every one is alive, then we don't get any of them
964    to appear in an "info thread" command, which is quite a useful
965    behaviour.
966  */
967 bool
968 core_target::thread_alive (ptid_t ptid)
969 {
970   return true;
971 }
972
973 /* Ask the current architecture what it knows about this core file.
974    That will be used, in turn, to pick a better architecture.  This
975    wrapper could be avoided if targets got a chance to specialize
976    core_target.  */
977
978 const struct target_desc *
979 core_target::read_description ()
980 {
981   if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
982     {
983       const struct target_desc *result;
984
985       result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
986       if (result != NULL)
987         return result;
988     }
989
990   return this->beneath ()->read_description ();
991 }
992
993 const char *
994 core_target::pid_to_str (ptid_t ptid)
995 {
996   static char buf[64];
997   struct inferior *inf;
998   int pid;
999
1000   /* The preferred way is to have a gdbarch/OS specific
1001      implementation.  */
1002   if (m_core_gdbarch != nullptr
1003       && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1004     return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
1005
1006   /* Otherwise, if we don't have one, we'll just fallback to
1007      "process", with normal_pid_to_str.  */
1008
1009   /* Try the LWPID field first.  */
1010   pid = ptid_get_lwp (ptid);
1011   if (pid != 0)
1012     return normal_pid_to_str (pid_to_ptid (pid));
1013
1014   /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1015      only if it isn't a fake PID.  */
1016   inf = find_inferior_ptid (ptid);
1017   if (inf != NULL && !inf->fake_pid_p)
1018     return normal_pid_to_str (ptid);
1019
1020   /* No luck.  We simply don't have a valid PID to print.  */
1021   xsnprintf (buf, sizeof buf, "<main task>");
1022   return buf;
1023 }
1024
1025 const char *
1026 core_target::thread_name (struct thread_info *thr)
1027 {
1028   if (m_core_gdbarch != nullptr
1029       && gdbarch_core_thread_name_p (m_core_gdbarch))
1030     return gdbarch_core_thread_name (m_core_gdbarch, thr);
1031   return NULL;
1032 }
1033
1034 bool
1035 core_target::has_memory ()
1036 {
1037   return (core_bfd != NULL);
1038 }
1039
1040 bool
1041 core_target::has_stack ()
1042 {
1043   return (core_bfd != NULL);
1044 }
1045
1046 bool
1047 core_target::has_registers ()
1048 {
1049   return (core_bfd != NULL);
1050 }
1051
1052 /* Implement the to_info_proc method.  */
1053
1054 bool
1055 core_target::info_proc (const char *args, enum info_proc_what request)
1056 {
1057   struct gdbarch *gdbarch = get_current_arch ();
1058
1059   /* Since this is the core file target, call the 'core_info_proc'
1060      method on gdbarch, not 'info_proc'.  */
1061   if (gdbarch_core_info_proc_p (gdbarch))
1062     gdbarch_core_info_proc (gdbarch, args, request);
1063
1064   return true;
1065 }
1066
1067 void
1068 _initialize_corelow (void)
1069 {
1070   add_target (core_target_info, core_target_open, filename_completer);
1071 }