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