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