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