gdb/corelow.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Core dump and executable file functions below target vector, for GDB.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include <signal.h>
  17. #include <fcntl.h>
  18. #ifdef HAVE_SYS_FILE_H
  19. #include <sys/file.h>                /* needed for F_OK and friends */
  20. #endif
  21. #include "frame.h"                /* required by inferior.h */
  22. #include "inferior.h"
  23. #include "infrun.h"
  24. #include "symtab.h"
  25. #include "command.h"
  26. #include "bfd.h"
  27. #include "target.h"
  28. #include "gdbcore.h"
  29. #include "gdbthread.h"
  30. #include "regcache.h"
  31. #include "regset.h"
  32. #include "symfile.h"
  33. #include "exec.h"
  34. #include "readline/readline.h"
  35. #include "solib.h"
  36. #include "filenames.h"
  37. #include "progspace.h"
  38. #include "objfiles.h"
  39. #include "gdb_bfd.h"
  40. #include "completer.h"
  41. #include "filestuff.h"

  42. #ifndef O_LARGEFILE
  43. #define O_LARGEFILE 0
  44. #endif

  45. /* List of all available core_fns.  On gdb startup, each core file
  46.    register reader calls deprecated_add_core_fns() to register
  47.    information on each core format it is prepared to read.  */

  48. static struct core_fns *core_file_fns = NULL;

  49. /* The core_fns for a core file handler that is prepared to read the
  50.    core file currently open on core_bfd.  */

  51. static struct core_fns *core_vec = NULL;

  52. /* FIXME: kettenis/20031023: Eventually this variable should
  53.    disappear.  */

  54. static struct gdbarch *core_gdbarch = NULL;

  55. /* Per-core data.  Currently, only the section table.  Note that these
  56.    target sections are *not* mapped in the current address spaces' set
  57.    of target sections --- those should come only from pure executable
  58.    or shared library bfds.  The core bfd sections are an
  59.    implementation detail of the core target, just like ptrace is for
  60.    unix child targets.  */
  61. static struct target_section_table *core_data;

  62. static void core_files_info (struct target_ops *);

  63. static struct core_fns *sniff_core_bfd (bfd *);

  64. static int gdb_check_format (bfd *);

  65. static void core_close (struct target_ops *self);

  66. static void core_close_cleanup (void *ignore);

  67. static void add_to_thread_list (bfd *, asection *, void *);

  68. static void init_core_ops (void);

  69. void _initialize_corelow (void);

  70. static struct target_ops core_ops;

  71. /* An arbitrary identifier for the core inferior.  */
  72. #define CORELOW_PID 1

  73. /* Link a new core_fns into the global core_file_fns list.  Called on
  74.    gdb startup by the _initialize routine in each core file register
  75.    reader, to register information about each format the reader is
  76.    prepared to handle.  */

  77. void
  78. deprecated_add_core_fns (struct core_fns *cf)
  79. {
  80.   cf->next = core_file_fns;
  81.   core_file_fns = cf;
  82. }

  83. /* The default function that core file handlers can use to examine a
  84.    core file BFD and decide whether or not to accept the job of
  85.    reading the core file.  */

  86. int
  87. default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
  88. {
  89.   int result;

  90.   result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
  91.   return (result);
  92. }

  93. /* Walk through the list of core functions to find a set that can
  94.    handle the core file open on ABFD.  Returns pointer to set that is
  95.    selected.  */

  96. static struct core_fns *
  97. sniff_core_bfd (bfd *abfd)
  98. {
  99.   struct core_fns *cf;
  100.   struct core_fns *yummy = NULL;
  101.   int matches = 0;;

  102.   /* Don't sniff if we have support for register sets in
  103.      CORE_GDBARCH.  */
  104.   if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
  105.     return NULL;

  106.   for (cf = core_file_fns; cf != NULL; cf = cf->next)
  107.     {
  108.       if (cf->core_sniffer (cf, abfd))
  109.         {
  110.           yummy = cf;
  111.           matches++;
  112.         }
  113.     }
  114.   if (matches > 1)
  115.     {
  116.       warning (_("\"%s\": ambiguous core format, %d handlers match"),
  117.                bfd_get_filename (abfd), matches);
  118.     }
  119.   else if (matches == 0)
  120.     error (_("\"%s\": no core file handler recognizes format"),
  121.            bfd_get_filename (abfd));

  122.   return (yummy);
  123. }

  124. /* The default is to reject every core file format we see.  Either
  125.    BFD has to recognize it, or we have to provide a function in the
  126.    core file handler that recognizes it.  */

  127. int
  128. default_check_format (bfd *abfd)
  129. {
  130.   return (0);
  131. }

  132. /* Attempt to recognize core file formats that BFD rejects.  */

  133. static int
  134. gdb_check_format (bfd *abfd)
  135. {
  136.   struct core_fns *cf;

  137.   for (cf = core_file_fns; cf != NULL; cf = cf->next)
  138.     {
  139.       if (cf->check_format (abfd))
  140.         {
  141.           return (1);
  142.         }
  143.     }
  144.   return (0);
  145. }

  146. /* Discard all vestiges of any previous core file and mark data and
  147.    stack spaces as empty.  */

  148. static void
  149. core_close (struct target_ops *self)
  150. {
  151.   if (core_bfd)
  152.     {
  153.       int pid = ptid_get_pid (inferior_ptid);
  154.       inferior_ptid = null_ptid;    /* Avoid confusion from thread
  155.                                        stuff.  */
  156.       if (pid != 0)
  157.         exit_inferior_silent (pid);

  158.       /* Clear out solib state while the bfd is still open.  See
  159.          comments in clear_solib in solib.c.  */
  160.       clear_solib ();

  161.       if (core_data)
  162.         {
  163.           xfree (core_data->sections);
  164.           xfree (core_data);
  165.           core_data = NULL;
  166.         }

  167.       gdb_bfd_unref (core_bfd);
  168.       core_bfd = NULL;
  169.     }
  170.   core_vec = NULL;
  171.   core_gdbarch = NULL;
  172. }

  173. static void
  174. core_close_cleanup (void *ignore)
  175. {
  176.   core_close (NULL);
  177. }

  178. /* Look for sections whose names start with `.reg/' so that we can
  179.    extract the list of threads in a core file.  */

  180. static void
  181. add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
  182. {
  183.   ptid_t ptid;
  184.   int core_tid;
  185.   int pid, lwpid;
  186.   asection *reg_sect = (asection *) reg_sect_arg;
  187.   int fake_pid_p = 0;
  188.   struct inferior *inf;

  189.   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
  190.     return;

  191.   core_tid = atoi (bfd_section_name (abfd, asect) + 5);

  192.   pid = bfd_core_file_pid (core_bfd);
  193.   if (pid == 0)
  194.     {
  195.       fake_pid_p = 1;
  196.       pid = CORELOW_PID;
  197.     }

  198.   lwpid = core_tid;

  199.   inf = current_inferior ();
  200.   if (inf->pid == 0)
  201.     {
  202.       inferior_appeared (inf, pid);
  203.       inf->fake_pid_p = fake_pid_p;
  204.     }

  205.   ptid = ptid_build (pid, lwpid, 0);

  206.   add_thread (ptid);

  207. /* Warning, Will Robinson, looking at BFD private data! */

  208.   if (reg_sect != NULL
  209.       && asect->filepos == reg_sect->filepos)        /* Did we find .reg?  */
  210.     inferior_ptid = ptid;                        /* Yes, make it current.  */
  211. }

  212. /* This routine opens and sets up the core file bfd.  */

  213. static void
  214. core_open (const char *arg, int from_tty)
  215. {
  216.   const char *p;
  217.   int siggy;
  218.   struct cleanup *old_chain;
  219.   char *temp;
  220.   bfd *temp_bfd;
  221.   int scratch_chan;
  222.   int flags;
  223.   volatile struct gdb_exception except;
  224.   char *filename;

  225.   target_preopen (from_tty);
  226.   if (!arg)
  227.     {
  228.       if (core_bfd)
  229.         error (_("No core file specified.  (Use `detach' "
  230.                  "to stop debugging a core file.)"));
  231.       else
  232.         error (_("No core file specified."));
  233.     }

  234.   filename = tilde_expand (arg);
  235.   if (!IS_ABSOLUTE_PATH (filename))
  236.     {
  237.       temp = concat (current_directory, "/",
  238.                      filename, (char *) NULL);
  239.       xfree (filename);
  240.       filename = temp;
  241.     }

  242.   old_chain = make_cleanup (xfree, filename);

  243.   flags = O_BINARY | O_LARGEFILE;
  244.   if (write_files)
  245.     flags |= O_RDWR;
  246.   else
  247.     flags |= O_RDONLY;
  248.   scratch_chan = gdb_open_cloexec (filename, flags, 0);
  249.   if (scratch_chan < 0)
  250.     perror_with_name (filename);

  251.   temp_bfd = gdb_bfd_fopen (filename, gnutarget,
  252.                             write_files ? FOPEN_RUB : FOPEN_RB,
  253.                             scratch_chan);
  254.   if (temp_bfd == NULL)
  255.     perror_with_name (filename);

  256.   if (!bfd_check_format (temp_bfd, bfd_core)
  257.       && !gdb_check_format (temp_bfd))
  258.     {
  259.       /* Do it after the err msg */
  260.       /* FIXME: should be checking for errors from bfd_close (for one
  261.          thing, on error it does not free all the storage associated
  262.          with the bfd).  */
  263.       make_cleanup_bfd_unref (temp_bfd);
  264.       error (_("\"%s\" is not a core dump: %s"),
  265.              filename, bfd_errmsg (bfd_get_error ()));
  266.     }

  267.   /* Looks semi-reasonable.  Toss the old core file and work on the
  268.      new.  */

  269.   do_cleanups (old_chain);
  270.   unpush_target (&core_ops);
  271.   core_bfd = temp_bfd;
  272.   old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);

  273.   core_gdbarch = gdbarch_from_bfd (core_bfd);

  274.   /* Find a suitable core file handler to munch on core_bfd */
  275.   core_vec = sniff_core_bfd (core_bfd);

  276.   validate_files ();

  277.   core_data = XCNEW (struct target_section_table);

  278.   /* Find the data section */
  279.   if (build_section_table (core_bfd,
  280.                            &core_data->sections,
  281.                            &core_data->sections_end))
  282.     error (_("\"%s\": Can't find sections: %s"),
  283.            bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));

  284.   /* If we have no exec file, try to set the architecture from the
  285.      core file.  We don't do this unconditionally since an exec file
  286.      typically contains more information that helps us determine the
  287.      architecture than a core file.  */
  288.   if (!exec_bfd)
  289.     set_gdbarch_from_file (core_bfd);

  290.   push_target (&core_ops);
  291.   discard_cleanups (old_chain);

  292.   /* Do this before acknowledging the inferior, so if
  293.      post_create_inferior throws (can happen easilly if you're loading
  294.      a core file with the wrong exec), we aren't left with threads
  295.      from the previous inferior.  */
  296.   init_thread_list ();

  297.   inferior_ptid = null_ptid;

  298.   /* Need to flush the register cache (and the frame cache) from a
  299.      previous debug session.  If inferior_ptid ends up the same as the
  300.      last debug session --- e.g., b foo; run; gcore core1; step; gcore
  301.      core2; core core1; core core2 --- then there's potential for
  302.      get_current_regcache to return the cached regcache of the
  303.      previous session, and the frame cache being stale.  */
  304.   registers_changed ();

  305.   /* Build up thread list from BFD sections, and possibly set the
  306.      current thread to the .reg/NN section matching the .reg
  307.      section.  */
  308.   bfd_map_over_sections (core_bfd, add_to_thread_list,
  309.                          bfd_get_section_by_name (core_bfd, ".reg"));

  310.   if (ptid_equal (inferior_ptid, null_ptid))
  311.     {
  312.       /* Either we found no .reg/NN section, and hence we have a
  313.          non-threaded core (single-threaded, from gdb's perspective),
  314.          or for some reason add_to_thread_list couldn't determine
  315.          which was the "main" thread.  The latter case shouldn't
  316.          usually happen, but we're dealing with input here, which can
  317.          always be broken in different ways.  */
  318.       struct thread_info *thread = first_thread_of_process (-1);

  319.       if (thread == NULL)
  320.         {
  321.           inferior_appeared (current_inferior (), CORELOW_PID);
  322.           inferior_ptid = pid_to_ptid (CORELOW_PID);
  323.           add_thread_silent (inferior_ptid);
  324.         }
  325.       else
  326.         switch_to_thread (thread->ptid);
  327.     }

  328.   post_create_inferior (&core_ops, from_tty);

  329.   /* Now go through the target stack looking for threads since there
  330.      may be a thread_stratum target loaded on top of target core by
  331.      now.  The layer above should claim threads found in the BFD
  332.      sections.  */
  333.   TRY_CATCH (except, RETURN_MASK_ERROR)
  334.     {
  335.       target_update_thread_list ();
  336.     }

  337.   if (except.reason < 0)
  338.     exception_print (gdb_stderr, except);

  339.   p = bfd_core_file_failing_command (core_bfd);
  340.   if (p)
  341.     printf_filtered (_("Core was generated by `%s'.\n"), p);

  342.   /* Clearing any previous state of convenience variables.  */
  343.   clear_exit_convenience_vars ();

  344.   siggy = bfd_core_file_failing_signal (core_bfd);
  345.   if (siggy > 0)
  346.     {
  347.       /* If we don't have a CORE_GDBARCH to work with, assume a native
  348.          core (map gdb_signal from host signals).  If we do have
  349.          CORE_GDBARCH to work with, but no gdb_signal_from_target
  350.          implementation for that gdbarch, as a fallback measure,
  351.          assume the host signal mapping.  It'll be correct for native
  352.          cores, but most likely incorrect for cross-cores.  */
  353.       enum gdb_signal sig = (core_gdbarch != NULL
  354.                              && gdbarch_gdb_signal_from_target_p (core_gdbarch)
  355.                              ? gdbarch_gdb_signal_from_target (core_gdbarch,
  356.                                                                siggy)
  357.                              : gdb_signal_from_host (siggy));

  358.       printf_filtered (_("Program terminated with signal %s, %s.\n"),
  359.                        gdb_signal_to_name (sig), gdb_signal_to_string (sig));

  360.       /* Set the value of the internal variable $_exitsignal,
  361.          which holds the signal uncaught by the inferior.  */
  362.       set_internalvar_integer (lookup_internalvar ("_exitsignal"),
  363.                                siggy);
  364.     }

  365.   /* Fetch all registers from core file.  */
  366.   target_fetch_registers (get_current_regcache (), -1);

  367.   /* Now, set up the frame cache, and print the top of stack.  */
  368.   reinit_frame_cache ();
  369.   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  370. }

  371. static void
  372. core_detach (struct target_ops *ops, const char *args, int from_tty)
  373. {
  374.   if (args)
  375.     error (_("Too many arguments"));
  376.   unpush_target (ops);
  377.   reinit_frame_cache ();
  378.   if (from_tty)
  379.     printf_filtered (_("No core file now.\n"));
  380. }

  381. /* Try to retrieve registers from a section in core_bfd, and supply
  382.    them to core_vec->core_read_registers, as the register set numbered
  383.    WHICH.

  384.    If inferior_ptid's lwp member is zero, do the single-threaded
  385.    thing: look for a section named NAME.  If inferior_ptid's lwp
  386.    member is non-zero, do the multi-threaded thing: look for a section
  387.    named "NAME/LWP", where LWP is the shortest ASCII decimal
  388.    representation of inferior_ptid's lwp member.

  389.    HUMAN_NAME is a human-readable name for the kind of registers the
  390.    NAME section contains, for use in error messages.

  391.    If REQUIRED is non-zero, print an error if the core file doesn't
  392.    have a section by the appropriate name.  Otherwise, just do
  393.    nothing.  */

  394. static void
  395. get_core_register_section (struct regcache *regcache,
  396.                            const struct regset *regset,
  397.                            const char *name,
  398.                            int min_size,
  399.                            int which,
  400.                            const char *human_name,
  401.                            int required)
  402. {
  403.   static char *section_name = NULL;
  404.   struct bfd_section *section;
  405.   bfd_size_type size;
  406.   char *contents;

  407.   xfree (section_name);

  408.   if (ptid_get_lwp (inferior_ptid))
  409.     section_name = xstrprintf ("%s/%ld", name,
  410.                                ptid_get_lwp (inferior_ptid));
  411.   else
  412.     section_name = xstrdup (name);

  413.   section = bfd_get_section_by_name (core_bfd, section_name);
  414.   if (! section)
  415.     {
  416.       if (required)
  417.         warning (_("Couldn't find %s registers in core file."),
  418.                  human_name);
  419.       return;
  420.     }

  421.   size = bfd_section_size (core_bfd, section);
  422.   if (size < min_size)
  423.     {
  424.       warning (_("Section `%s' in core file too small."), section_name);
  425.       return;
  426.     }

  427.   contents = alloca (size);
  428.   if (! bfd_get_section_contents (core_bfd, section, contents,
  429.                                   (file_ptr) 0, size))
  430.     {
  431.       warning (_("Couldn't read %s registers from `%s' section in core file."),
  432.                human_name, name);
  433.       return;
  434.     }

  435.   if (regset != NULL)
  436.     {
  437.       regset->supply_regset (regset, regcache, -1, contents, size);
  438.       return;
  439.     }

  440.   gdb_assert (core_vec);
  441.   core_vec->core_read_registers (regcache, contents, size, which,
  442.                                  ((CORE_ADDR)
  443.                                   bfd_section_vma (core_bfd, section)));
  444. }

  445. /* Callback for get_core_registers that handles a single core file
  446.    register note section. */

  447. static void
  448. get_core_registers_cb (const char *sect_name, int size,
  449.                        const struct regset *regset,
  450.                        const char *human_name, void *cb_data)
  451. {
  452.   struct regcache *regcache = (struct regcache *) cb_data;
  453.   int required = 0;

  454.   if (strcmp (sect_name, ".reg") == 0)
  455.     {
  456.       required = 1;
  457.       if (human_name == NULL)
  458.         human_name = "general-purpose";
  459.     }
  460.   else if (strcmp (sect_name, ".reg2") == 0)
  461.     {
  462.       if (human_name == NULL)
  463.         human_name = "floating-point";
  464.     }

  465.   /* The 'which' parameter is only used when no regset is provided.
  466.      Thus we just set it to -1. */
  467.   get_core_register_section (regcache, regset, sect_name,
  468.                              size, -1, human_name, required);
  469. }

  470. /* Get the registers out of a core file.  This is the machine-
  471.    independent part.  Fetch_core_registers is the machine-dependent
  472.    part, typically implemented in the xm-file for each
  473.    architecture.  */

  474. /* We just get all the registers, so we don't use regno.  */

  475. static void
  476. get_core_registers (struct target_ops *ops,
  477.                     struct regcache *regcache, int regno)
  478. {
  479.   int i;
  480.   struct gdbarch *gdbarch;

  481.   if (!(core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
  482.       && (core_vec == NULL || core_vec->core_read_registers == NULL))
  483.     {
  484.       fprintf_filtered (gdb_stderr,
  485.                      "Can't fetch registers from this type of core file\n");
  486.       return;
  487.     }

  488.   gdbarch = get_regcache_arch (regcache);
  489.   if (gdbarch_iterate_over_regset_sections_p (gdbarch))
  490.     gdbarch_iterate_over_regset_sections (gdbarch,
  491.                                           get_core_registers_cb,
  492.                                           (void *) regcache, NULL);
  493.   else
  494.     {
  495.       get_core_register_section (regcache, NULL,
  496.                                  ".reg", 0, 0, "general-purpose", 1);
  497.       get_core_register_section (regcache, NULL,
  498.                                  ".reg2", 0, 2, "floating-point", 0);
  499.     }

  500.   /* Mark all registers not found in the core as unavailable.  */
  501.   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
  502.     if (regcache_register_status (regcache, i) == REG_UNKNOWN)
  503.       regcache_raw_supply (regcache, i, NULL);
  504. }

  505. static void
  506. core_files_info (struct target_ops *t)
  507. {
  508.   print_section_info (core_data, core_bfd);
  509. }

  510. struct spuid_list
  511. {
  512.   gdb_byte *buf;
  513.   ULONGEST offset;
  514.   LONGEST len;
  515.   ULONGEST pos;
  516.   ULONGEST written;
  517. };

  518. static void
  519. add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
  520. {
  521.   struct spuid_list *list = list_p;
  522.   enum bfd_endian byte_order
  523.     = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
  524.   int fd, pos = 0;

  525.   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
  526.   if (pos == 0)
  527.     return;

  528.   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
  529.     {
  530.       store_unsigned_integer (list->buf + list->pos - list->offset,
  531.                               4, byte_order, fd);
  532.       list->written += 4;
  533.     }
  534.   list->pos += 4;
  535. }

  536. /* Read siginfo data from the core, if possible.  Returns -1 on
  537.    failure.  Otherwise, returns the number of bytes read.  ABFD is the
  538.    core file's BFD; READBUF, OFFSET, and LEN are all as specified by
  539.    the to_xfer_partial interface.  */

  540. static LONGEST
  541. get_core_siginfo (bfd *abfd, gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
  542. {
  543.   asection *section;
  544.   char *section_name;
  545.   const char *name = ".note.linuxcore.siginfo";

  546.   if (ptid_get_lwp (inferior_ptid))
  547.     section_name = xstrprintf ("%s/%ld", name,
  548.                                ptid_get_lwp (inferior_ptid));
  549.   else
  550.     section_name = xstrdup (name);

  551.   section = bfd_get_section_by_name (abfd, section_name);
  552.   xfree (section_name);
  553.   if (section == NULL)
  554.     return -1;

  555.   if (!bfd_get_section_contents (abfd, section, readbuf, offset, len))
  556.     return -1;

  557.   return len;
  558. }

  559. static enum target_xfer_status
  560. core_xfer_partial (struct target_ops *ops, enum target_object object,
  561.                    const char *annex, gdb_byte *readbuf,
  562.                    const gdb_byte *writebuf, ULONGEST offset,
  563.                    ULONGEST len, ULONGEST *xfered_len)
  564. {
  565.   switch (object)
  566.     {
  567.     case TARGET_OBJECT_MEMORY:
  568.       return section_table_xfer_memory_partial (readbuf, writebuf,
  569.                                                 offset, len, xfered_len,
  570.                                                 core_data->sections,
  571.                                                 core_data->sections_end,
  572.                                                 NULL);

  573.     case TARGET_OBJECT_AUXV:
  574.       if (readbuf)
  575.         {
  576.           /* When the aux vector is stored in core file, BFD
  577.              represents this with a fake section called ".auxv".  */

  578.           struct bfd_section *section;
  579.           bfd_size_type size;

  580.           section = bfd_get_section_by_name (core_bfd, ".auxv");
  581.           if (section == NULL)
  582.             return TARGET_XFER_E_IO;

  583.           size = bfd_section_size (core_bfd, section);
  584.           if (offset >= size)
  585.             return TARGET_XFER_EOF;
  586.           size -= offset;
  587.           if (size > len)
  588.             size = len;

  589.           if (size == 0)
  590.             return TARGET_XFER_EOF;
  591.           if (!bfd_get_section_contents (core_bfd, section, readbuf,
  592.                                          (file_ptr) offset, size))
  593.             {
  594.               warning (_("Couldn't read NT_AUXV note in core file."));
  595.               return TARGET_XFER_E_IO;
  596.             }

  597.           *xfered_len = (ULONGEST) size;
  598.           return TARGET_XFER_OK;
  599.         }
  600.       return TARGET_XFER_E_IO;

  601.     case TARGET_OBJECT_WCOOKIE:
  602.       if (readbuf)
  603.         {
  604.           /* When the StackGhost cookie is stored in core file, BFD
  605.              represents this with a fake section called
  606.              ".wcookie".  */

  607.           struct bfd_section *section;
  608.           bfd_size_type size;

  609.           section = bfd_get_section_by_name (core_bfd, ".wcookie");
  610.           if (section == NULL)
  611.             return TARGET_XFER_E_IO;

  612.           size = bfd_section_size (core_bfd, section);
  613.           if (offset >= size)
  614.             return TARGET_XFER_EOF;
  615.           size -= offset;
  616.           if (size > len)
  617.             size = len;

  618.           if (size == 0)
  619.             return TARGET_XFER_EOF;
  620.           if (!bfd_get_section_contents (core_bfd, section, readbuf,
  621.                                          (file_ptr) offset, size))
  622.             {
  623.               warning (_("Couldn't read StackGhost cookie in core file."));
  624.               return TARGET_XFER_E_IO;
  625.             }

  626.           *xfered_len = (ULONGEST) size;
  627.           return TARGET_XFER_OK;

  628.         }
  629.       return TARGET_XFER_E_IO;

  630.     case TARGET_OBJECT_LIBRARIES:
  631.       if (core_gdbarch
  632.           && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
  633.         {
  634.           if (writebuf)
  635.             return TARGET_XFER_E_IO;
  636.           else
  637.             {
  638.               *xfered_len = gdbarch_core_xfer_shared_libraries (core_gdbarch,
  639.                                                                 readbuf,
  640.                                                                 offset, len);

  641.               if (*xfered_len == 0)
  642.                 return TARGET_XFER_EOF;
  643.               else
  644.                 return TARGET_XFER_OK;
  645.             }
  646.         }
  647.       /* FALL THROUGH */

  648.     case TARGET_OBJECT_LIBRARIES_AIX:
  649.       if (core_gdbarch
  650.           && gdbarch_core_xfer_shared_libraries_aix_p (core_gdbarch))
  651.         {
  652.           if (writebuf)
  653.             return TARGET_XFER_E_IO;
  654.           else
  655.             {
  656.               *xfered_len
  657.                 = gdbarch_core_xfer_shared_libraries_aix (core_gdbarch,
  658.                                                           readbuf, offset,
  659.                                                           len);

  660.               if (*xfered_len == 0)
  661.                 return TARGET_XFER_EOF;
  662.               else
  663.                 return TARGET_XFER_OK;
  664.             }
  665.         }
  666.       /* FALL THROUGH */

  667.     case TARGET_OBJECT_SPU:
  668.       if (readbuf && annex)
  669.         {
  670.           /* When the SPU contexts are stored in a core file, BFD
  671.              represents this with a fake section called
  672.              "SPU/<annex>".  */

  673.           struct bfd_section *section;
  674.           bfd_size_type size;
  675.           char sectionstr[100];

  676.           xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);

  677.           section = bfd_get_section_by_name (core_bfd, sectionstr);
  678.           if (section == NULL)
  679.             return TARGET_XFER_E_IO;

  680.           size = bfd_section_size (core_bfd, section);
  681.           if (offset >= size)
  682.             return TARGET_XFER_EOF;
  683.           size -= offset;
  684.           if (size > len)
  685.             size = len;

  686.           if (size == 0)
  687.             return TARGET_XFER_EOF;
  688.           if (!bfd_get_section_contents (core_bfd, section, readbuf,
  689.                                          (file_ptr) offset, size))
  690.             {
  691.               warning (_("Couldn't read SPU section in core file."));
  692.               return TARGET_XFER_E_IO;
  693.             }

  694.           *xfered_len = (ULONGEST) size;
  695.           return TARGET_XFER_OK;
  696.         }
  697.       else if (readbuf)
  698.         {
  699.           /* NULL annex requests list of all present spuids.  */
  700.           struct spuid_list list;

  701.           list.buf = readbuf;
  702.           list.offset = offset;
  703.           list.len = len;
  704.           list.pos = 0;
  705.           list.written = 0;
  706.           bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);

  707.           if (list.written == 0)
  708.             return TARGET_XFER_EOF;
  709.           else
  710.             {
  711.               *xfered_len = (ULONGEST) list.written;
  712.               return TARGET_XFER_OK;
  713.             }
  714.         }
  715.       return TARGET_XFER_E_IO;

  716.     case TARGET_OBJECT_SIGNAL_INFO:
  717.       if (readbuf)
  718.         {
  719.           LONGEST l = get_core_siginfo (core_bfd, readbuf, offset, len);

  720.           if (l > 0)
  721.             {
  722.               *xfered_len = len;
  723.               return TARGET_XFER_OK;
  724.             }
  725.         }
  726.       return TARGET_XFER_E_IO;

  727.     default:
  728.       return ops->beneath->to_xfer_partial (ops->beneath, object,
  729.                                             annex, readbuf,
  730.                                             writebuf, offset, len,
  731.                                             xfered_len);
  732.     }
  733. }


  734. /* If mourn is being called in all the right places, this could be say
  735.    `gdb internal error' (since generic_mourn calls
  736.    breakpoint_init_inferior).  */

  737. static int
  738. ignore (struct target_ops *ops, struct gdbarch *gdbarch,
  739.         struct bp_target_info *bp_tgt)
  740. {
  741.   return 0;
  742. }


  743. /* Okay, let's be honest: threads gleaned from a core file aren't
  744.    exactly lively, are they?  On the other hand, if we don't claim
  745.    that each & every one is alive, then we don't get any of them
  746.    to appear in an "info thread" command, which is quite a useful
  747.    behaviour.
  748. */
  749. static int
  750. core_thread_alive (struct target_ops *ops, ptid_t ptid)
  751. {
  752.   return 1;
  753. }

  754. /* Ask the current architecture what it knows about this core file.
  755.    That will be used, in turn, to pick a better architecture.  This
  756.    wrapper could be avoided if targets got a chance to specialize
  757.    core_ops.  */

  758. static const struct target_desc *
  759. core_read_description (struct target_ops *target)
  760. {
  761.   if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
  762.     {
  763.       const struct target_desc *result;

  764.       result = gdbarch_core_read_description (core_gdbarch,
  765.                                               target, core_bfd);
  766.       if (result != NULL)
  767.         return result;
  768.     }

  769.   return target->beneath->to_read_description (target->beneath);
  770. }

  771. static char *
  772. core_pid_to_str (struct target_ops *ops, ptid_t ptid)
  773. {
  774.   static char buf[64];
  775.   struct inferior *inf;
  776.   int pid;

  777.   /* The preferred way is to have a gdbarch/OS specific
  778.      implementation.  */
  779.   if (core_gdbarch
  780.       && gdbarch_core_pid_to_str_p (core_gdbarch))
  781.     return gdbarch_core_pid_to_str (core_gdbarch, ptid);

  782.   /* Otherwise, if we don't have one, we'll just fallback to
  783.      "process", with normal_pid_to_str.  */

  784.   /* Try the LWPID field first.  */
  785.   pid = ptid_get_lwp (ptid);
  786.   if (pid != 0)
  787.     return normal_pid_to_str (pid_to_ptid (pid));

  788.   /* Otherwise, this isn't a "threaded" core -- use the PID field, but
  789.      only if it isn't a fake PID.  */
  790.   inf = find_inferior_ptid (ptid);
  791.   if (inf != NULL && !inf->fake_pid_p)
  792.     return normal_pid_to_str (ptid);

  793.   /* No luck.  We simply don't have a valid PID to print.  */
  794.   xsnprintf (buf, sizeof buf, "<main task>");
  795.   return buf;
  796. }

  797. static int
  798. core_has_memory (struct target_ops *ops)
  799. {
  800.   return (core_bfd != NULL);
  801. }

  802. static int
  803. core_has_stack (struct target_ops *ops)
  804. {
  805.   return (core_bfd != NULL);
  806. }

  807. static int
  808. core_has_registers (struct target_ops *ops)
  809. {
  810.   return (core_bfd != NULL);
  811. }

  812. /* Implement the to_info_proc method.  */

  813. static void
  814. core_info_proc (struct target_ops *ops, const char *args,
  815.                 enum info_proc_what request)
  816. {
  817.   struct gdbarch *gdbarch = get_current_arch ();

  818.   /* Since this is the core file target, call the 'core_info_proc'
  819.      method on gdbarch, not 'info_proc'.  */
  820.   if (gdbarch_core_info_proc_p (gdbarch))
  821.     gdbarch_core_info_proc (gdbarch, args, request);
  822. }

  823. /* Fill in core_ops with its defined operations and properties.  */

  824. static void
  825. init_core_ops (void)
  826. {
  827.   core_ops.to_shortname = "core";
  828.   core_ops.to_longname = "Local core dump file";
  829.   core_ops.to_doc =
  830.     "Use a core file as a target.  Specify the filename of the core file.";
  831.   core_ops.to_open = core_open;
  832.   core_ops.to_close = core_close;
  833.   core_ops.to_detach = core_detach;
  834.   core_ops.to_fetch_registers = get_core_registers;
  835.   core_ops.to_xfer_partial = core_xfer_partial;
  836.   core_ops.to_files_info = core_files_info;
  837.   core_ops.to_insert_breakpoint = ignore;
  838.   core_ops.to_remove_breakpoint = ignore;
  839.   core_ops.to_thread_alive = core_thread_alive;
  840.   core_ops.to_read_description = core_read_description;
  841.   core_ops.to_pid_to_str = core_pid_to_str;
  842.   core_ops.to_stratum = process_stratum;
  843.   core_ops.to_has_memory = core_has_memory;
  844.   core_ops.to_has_stack = core_has_stack;
  845.   core_ops.to_has_registers = core_has_registers;
  846.   core_ops.to_info_proc = core_info_proc;
  847.   core_ops.to_magic = OPS_MAGIC;

  848.   if (core_target)
  849.     internal_error (__FILE__, __LINE__,
  850.                     _("init_core_ops: core target already exists (\"%s\")."),
  851.                     core_target->to_longname);
  852.   core_target = &core_ops;
  853. }

  854. void
  855. _initialize_corelow (void)
  856. {
  857.   init_core_ops ();

  858.   add_target_with_completer (&core_ops, filename_completer);
  859. }