GDB (API)
/home/stan/gdb/src/gdb/corelow.c
Go to the documentation of this file.
00001 /* Core dump and executable file functions below target vector, for GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "arch-utils.h"
00022 #include "gdb_string.h"
00023 #include <errno.h>
00024 #include <signal.h>
00025 #include <fcntl.h>
00026 #ifdef HAVE_SYS_FILE_H
00027 #include <sys/file.h>           /* needed for F_OK and friends */
00028 #endif
00029 #include "frame.h"              /* required by inferior.h */
00030 #include "inferior.h"
00031 #include "symtab.h"
00032 #include "command.h"
00033 #include "bfd.h"
00034 #include "target.h"
00035 #include "gdbcore.h"
00036 #include "gdbthread.h"
00037 #include "regcache.h"
00038 #include "regset.h"
00039 #include "symfile.h"
00040 #include "exec.h"
00041 #include "readline/readline.h"
00042 #include "gdb_assert.h"
00043 #include "exceptions.h"
00044 #include "solib.h"
00045 #include "filenames.h"
00046 #include "progspace.h"
00047 #include "objfiles.h"
00048 #include "gdb_bfd.h"
00049 #include "completer.h"
00050 #include "filestuff.h"
00051 
00052 #ifndef O_LARGEFILE
00053 #define O_LARGEFILE 0
00054 #endif
00055 
00056 /* List of all available core_fns.  On gdb startup, each core file
00057    register reader calls deprecated_add_core_fns() to register
00058    information on each core format it is prepared to read.  */
00059 
00060 static struct core_fns *core_file_fns = NULL;
00061 
00062 /* The core_fns for a core file handler that is prepared to read the
00063    core file currently open on core_bfd.  */
00064 
00065 static struct core_fns *core_vec = NULL;
00066 
00067 /* FIXME: kettenis/20031023: Eventually this variable should
00068    disappear.  */
00069 
00070 static struct gdbarch *core_gdbarch = NULL;
00071 
00072 /* Per-core data.  Currently, only the section table.  Note that these
00073    target sections are *not* mapped in the current address spaces' set
00074    of target sections --- those should come only from pure executable
00075    or shared library bfds.  The core bfd sections are an
00076    implementation detail of the core target, just like ptrace is for
00077    unix child targets.  */
00078 static struct target_section_table *core_data;
00079 
00080 static void core_files_info (struct target_ops *);
00081 
00082 static struct core_fns *sniff_core_bfd (bfd *);
00083 
00084 static int gdb_check_format (bfd *);
00085 
00086 static void core_open (char *, int);
00087 
00088 static void core_detach (struct target_ops *ops, char *, int);
00089 
00090 static void core_close (void);
00091 
00092 static void core_close_cleanup (void *ignore);
00093 
00094 static void add_to_thread_list (bfd *, asection *, void *);
00095 
00096 static void init_core_ops (void);
00097 
00098 void _initialize_corelow (void);
00099 
00100 static struct target_ops core_ops;
00101 
00102 /* An arbitrary identifier for the core inferior.  */
00103 #define CORELOW_PID 1
00104 
00105 /* Link a new core_fns into the global core_file_fns list.  Called on
00106    gdb startup by the _initialize routine in each core file register
00107    reader, to register information about each format the reader is
00108    prepared to handle.  */
00109 
00110 void
00111 deprecated_add_core_fns (struct core_fns *cf)
00112 {
00113   cf->next = core_file_fns;
00114   core_file_fns = cf;
00115 }
00116 
00117 /* The default function that core file handlers can use to examine a
00118    core file BFD and decide whether or not to accept the job of
00119    reading the core file.  */
00120 
00121 int
00122 default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
00123 {
00124   int result;
00125 
00126   result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
00127   return (result);
00128 }
00129 
00130 /* Walk through the list of core functions to find a set that can
00131    handle the core file open on ABFD.  Returns pointer to set that is
00132    selected.  */
00133 
00134 static struct core_fns *
00135 sniff_core_bfd (bfd *abfd)
00136 {
00137   struct core_fns *cf;
00138   struct core_fns *yummy = NULL;
00139   int matches = 0;;
00140 
00141   /* Don't sniff if we have support for register sets in
00142      CORE_GDBARCH.  */
00143   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
00144     return NULL;
00145 
00146   for (cf = core_file_fns; cf != NULL; cf = cf->next)
00147     {
00148       if (cf->core_sniffer (cf, abfd))
00149         {
00150           yummy = cf;
00151           matches++;
00152         }
00153     }
00154   if (matches > 1)
00155     {
00156       warning (_("\"%s\": ambiguous core format, %d handlers match"),
00157                bfd_get_filename (abfd), matches);
00158     }
00159   else if (matches == 0)
00160     error (_("\"%s\": no core file handler recognizes format"),
00161            bfd_get_filename (abfd));
00162 
00163   return (yummy);
00164 }
00165 
00166 /* The default is to reject every core file format we see.  Either
00167    BFD has to recognize it, or we have to provide a function in the
00168    core file handler that recognizes it.  */
00169 
00170 int
00171 default_check_format (bfd *abfd)
00172 {
00173   return (0);
00174 }
00175 
00176 /* Attempt to recognize core file formats that BFD rejects.  */
00177 
00178 static int
00179 gdb_check_format (bfd *abfd)
00180 {
00181   struct core_fns *cf;
00182 
00183   for (cf = core_file_fns; cf != NULL; cf = cf->next)
00184     {
00185       if (cf->check_format (abfd))
00186         {
00187           return (1);
00188         }
00189     }
00190   return (0);
00191 }
00192 
00193 /* Discard all vestiges of any previous core file and mark data and
00194    stack spaces as empty.  */
00195 
00196 static void
00197 core_close (void)
00198 {
00199   if (core_bfd)
00200     {
00201       int pid = ptid_get_pid (inferior_ptid);
00202       inferior_ptid = null_ptid;    /* Avoid confusion from thread
00203                                        stuff.  */
00204       if (pid != 0)
00205         exit_inferior_silent (pid);
00206 
00207       /* Clear out solib state while the bfd is still open.  See
00208          comments in clear_solib in solib.c.  */
00209       clear_solib ();
00210 
00211       if (core_data)
00212         {
00213           xfree (core_data->sections);
00214           xfree (core_data);
00215           core_data = NULL;
00216         }
00217 
00218       gdb_bfd_unref (core_bfd);
00219       core_bfd = NULL;
00220     }
00221   core_vec = NULL;
00222   core_gdbarch = NULL;
00223 }
00224 
00225 static void
00226 core_close_cleanup (void *ignore)
00227 {
00228   core_close ();
00229 }
00230 
00231 /* Look for sections whose names start with `.reg/' so that we can
00232    extract the list of threads in a core file.  */
00233 
00234 static void
00235 add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
00236 {
00237   ptid_t ptid;
00238   int core_tid;
00239   int pid, lwpid;
00240   asection *reg_sect = (asection *) reg_sect_arg;
00241   int fake_pid_p = 0;
00242   struct inferior *inf;
00243 
00244   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
00245     return;
00246 
00247   core_tid = atoi (bfd_section_name (abfd, asect) + 5);
00248 
00249   pid = bfd_core_file_pid (core_bfd);
00250   if (pid == 0)
00251     {
00252       fake_pid_p = 1;
00253       pid = CORELOW_PID;
00254     }
00255 
00256   lwpid = core_tid;
00257 
00258   inf = current_inferior ();
00259   if (inf->pid == 0)
00260     {
00261       inferior_appeared (inf, pid);
00262       inf->fake_pid_p = fake_pid_p;
00263     }
00264 
00265   ptid = ptid_build (pid, lwpid, 0);
00266 
00267   add_thread (ptid);
00268 
00269 /* Warning, Will Robinson, looking at BFD private data! */
00270 
00271   if (reg_sect != NULL
00272       && asect->filepos == reg_sect->filepos)   /* Did we find .reg?  */
00273     inferior_ptid = ptid;                       /* Yes, make it current.  */
00274 }
00275 
00276 /* This routine opens and sets up the core file bfd.  */
00277 
00278 static void
00279 core_open (char *filename, int from_tty)
00280 {
00281   const char *p;
00282   int siggy;
00283   struct cleanup *old_chain;
00284   char *temp;
00285   bfd *temp_bfd;
00286   int scratch_chan;
00287   int flags;
00288   volatile struct gdb_exception except;
00289 
00290   target_preopen (from_tty);
00291   if (!filename)
00292     {
00293       if (core_bfd)
00294         error (_("No core file specified.  (Use `detach' "
00295                  "to stop debugging a core file.)"));
00296       else
00297         error (_("No core file specified."));
00298     }
00299 
00300   filename = tilde_expand (filename);
00301   if (!IS_ABSOLUTE_PATH (filename))
00302     {
00303       temp = concat (current_directory, "/",
00304                      filename, (char *) NULL);
00305       xfree (filename);
00306       filename = temp;
00307     }
00308 
00309   old_chain = make_cleanup (xfree, filename);
00310 
00311   flags = O_BINARY | O_LARGEFILE;
00312   if (write_files)
00313     flags |= O_RDWR;
00314   else
00315     flags |= O_RDONLY;
00316   scratch_chan = gdb_open_cloexec (filename, flags, 0);
00317   if (scratch_chan < 0)
00318     perror_with_name (filename);
00319 
00320   temp_bfd = gdb_bfd_fopen (filename, gnutarget, 
00321                             write_files ? FOPEN_RUB : FOPEN_RB,
00322                             scratch_chan);
00323   if (temp_bfd == NULL)
00324     perror_with_name (filename);
00325 
00326   if (!bfd_check_format (temp_bfd, bfd_core)
00327       && !gdb_check_format (temp_bfd))
00328     {
00329       /* Do it after the err msg */
00330       /* FIXME: should be checking for errors from bfd_close (for one
00331          thing, on error it does not free all the storage associated
00332          with the bfd).  */
00333       make_cleanup_bfd_unref (temp_bfd);
00334       error (_("\"%s\" is not a core dump: %s"),
00335              filename, bfd_errmsg (bfd_get_error ()));
00336     }
00337 
00338   /* Looks semi-reasonable.  Toss the old core file and work on the
00339      new.  */
00340 
00341   do_cleanups (old_chain);
00342   unpush_target (&core_ops);
00343   core_bfd = temp_bfd;
00344   old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
00345 
00346   core_gdbarch = gdbarch_from_bfd (core_bfd);
00347 
00348   /* Find a suitable core file handler to munch on core_bfd */
00349   core_vec = sniff_core_bfd (core_bfd);
00350 
00351   validate_files ();
00352 
00353   core_data = XZALLOC (struct target_section_table);
00354 
00355   /* Find the data section */
00356   if (build_section_table (core_bfd,
00357                            &core_data->sections,
00358                            &core_data->sections_end))
00359     error (_("\"%s\": Can't find sections: %s"),
00360            bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
00361 
00362   /* If we have no exec file, try to set the architecture from the
00363      core file.  We don't do this unconditionally since an exec file
00364      typically contains more information that helps us determine the
00365      architecture than a core file.  */
00366   if (!exec_bfd)
00367     set_gdbarch_from_file (core_bfd);
00368 
00369   push_target (&core_ops);
00370   discard_cleanups (old_chain);
00371 
00372   /* Do this before acknowledging the inferior, so if
00373      post_create_inferior throws (can happen easilly if you're loading
00374      a core file with the wrong exec), we aren't left with threads
00375      from the previous inferior.  */
00376   init_thread_list ();
00377 
00378   inferior_ptid = null_ptid;
00379 
00380   /* Need to flush the register cache (and the frame cache) from a
00381      previous debug session.  If inferior_ptid ends up the same as the
00382      last debug session --- e.g., b foo; run; gcore core1; step; gcore
00383      core2; core core1; core core2 --- then there's potential for
00384      get_current_regcache to return the cached regcache of the
00385      previous session, and the frame cache being stale.  */
00386   registers_changed ();
00387 
00388   /* Build up thread list from BFD sections, and possibly set the
00389      current thread to the .reg/NN section matching the .reg
00390      section.  */
00391   bfd_map_over_sections (core_bfd, add_to_thread_list,
00392                          bfd_get_section_by_name (core_bfd, ".reg"));
00393 
00394   if (ptid_equal (inferior_ptid, null_ptid))
00395     {
00396       /* Either we found no .reg/NN section, and hence we have a
00397          non-threaded core (single-threaded, from gdb's perspective),
00398          or for some reason add_to_thread_list couldn't determine
00399          which was the "main" thread.  The latter case shouldn't
00400          usually happen, but we're dealing with input here, which can
00401          always be broken in different ways.  */
00402       struct thread_info *thread = first_thread_of_process (-1);
00403 
00404       if (thread == NULL)
00405         {
00406           inferior_appeared (current_inferior (), CORELOW_PID);
00407           inferior_ptid = pid_to_ptid (CORELOW_PID);
00408           add_thread_silent (inferior_ptid);
00409         }
00410       else
00411         switch_to_thread (thread->ptid);
00412     }
00413 
00414   post_create_inferior (&core_ops, from_tty);
00415 
00416   /* Now go through the target stack looking for threads since there
00417      may be a thread_stratum target loaded on top of target core by
00418      now.  The layer above should claim threads found in the BFD
00419      sections.  */
00420   TRY_CATCH (except, RETURN_MASK_ERROR)
00421     {
00422       target_find_new_threads ();
00423     }
00424 
00425   if (except.reason < 0)
00426     exception_print (gdb_stderr, except);
00427 
00428   p = bfd_core_file_failing_command (core_bfd);
00429   if (p)
00430     printf_filtered (_("Core was generated by `%s'.\n"), p);
00431 
00432   /* Clearing any previous state of convenience variables.  */
00433   clear_exit_convenience_vars ();
00434 
00435   siggy = bfd_core_file_failing_signal (core_bfd);
00436   if (siggy > 0)
00437     {
00438       /* If we don't have a CORE_GDBARCH to work with, assume a native
00439          core (map gdb_signal from host signals).  If we do have
00440          CORE_GDBARCH to work with, but no gdb_signal_from_target
00441          implementation for that gdbarch, as a fallback measure,
00442          assume the host signal mapping.  It'll be correct for native
00443          cores, but most likely incorrect for cross-cores.  */
00444       enum gdb_signal sig = (core_gdbarch != NULL
00445                              && gdbarch_gdb_signal_from_target_p (core_gdbarch)
00446                              ? gdbarch_gdb_signal_from_target (core_gdbarch,
00447                                                                siggy)
00448                              : gdb_signal_from_host (siggy));
00449 
00450       printf_filtered (_("Program terminated with signal %s, %s.\n"),
00451                        gdb_signal_to_name (sig), gdb_signal_to_string (sig));
00452 
00453       /* Set the value of the internal variable $_exitsignal,
00454          which holds the signal uncaught by the inferior.  */
00455       set_internalvar_integer (lookup_internalvar ("_exitsignal"),
00456                                siggy);
00457     }
00458 
00459   /* Fetch all registers from core file.  */
00460   target_fetch_registers (get_current_regcache (), -1);
00461 
00462   /* Now, set up the frame cache, and print the top of stack.  */
00463   reinit_frame_cache ();
00464   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
00465 }
00466 
00467 static void
00468 core_detach (struct target_ops *ops, char *args, int from_tty)
00469 {
00470   if (args)
00471     error (_("Too many arguments"));
00472   unpush_target (ops);
00473   reinit_frame_cache ();
00474   if (from_tty)
00475     printf_filtered (_("No core file now.\n"));
00476 }
00477 
00478 /* Try to retrieve registers from a section in core_bfd, and supply
00479    them to core_vec->core_read_registers, as the register set numbered
00480    WHICH.
00481 
00482    If inferior_ptid's lwp member is zero, do the single-threaded
00483    thing: look for a section named NAME.  If inferior_ptid's lwp
00484    member is non-zero, do the multi-threaded thing: look for a section
00485    named "NAME/LWP", where LWP is the shortest ASCII decimal
00486    representation of inferior_ptid's lwp member.
00487 
00488    HUMAN_NAME is a human-readable name for the kind of registers the
00489    NAME section contains, for use in error messages.
00490 
00491    If REQUIRED is non-zero, print an error if the core file doesn't
00492    have a section by the appropriate name.  Otherwise, just do
00493    nothing.  */
00494 
00495 static void
00496 get_core_register_section (struct regcache *regcache,
00497                            const char *name,
00498                            int which,
00499                            const char *human_name,
00500                            int required)
00501 {
00502   static char *section_name = NULL;
00503   struct bfd_section *section;
00504   bfd_size_type size;
00505   char *contents;
00506 
00507   xfree (section_name);
00508 
00509   if (ptid_get_lwp (inferior_ptid))
00510     section_name = xstrprintf ("%s/%ld", name,
00511                                ptid_get_lwp (inferior_ptid));
00512   else
00513     section_name = xstrdup (name);
00514 
00515   section = bfd_get_section_by_name (core_bfd, section_name);
00516   if (! section)
00517     {
00518       if (required)
00519         warning (_("Couldn't find %s registers in core file."),
00520                  human_name);
00521       return;
00522     }
00523 
00524   size = bfd_section_size (core_bfd, section);
00525   contents = alloca (size);
00526   if (! bfd_get_section_contents (core_bfd, section, contents,
00527                                   (file_ptr) 0, size))
00528     {
00529       warning (_("Couldn't read %s registers from `%s' section in core file."),
00530                human_name, name);
00531       return;
00532     }
00533 
00534   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
00535     {
00536       const struct regset *regset;
00537 
00538       regset = gdbarch_regset_from_core_section (core_gdbarch,
00539                                                  name, size);
00540       if (regset == NULL)
00541         {
00542           if (required)
00543             warning (_("Couldn't recognize %s registers in core file."),
00544                      human_name);
00545           return;
00546         }
00547 
00548       regset->supply_regset (regset, regcache, -1, contents, size);
00549       return;
00550     }
00551 
00552   gdb_assert (core_vec);
00553   core_vec->core_read_registers (regcache, contents, size, which,
00554                                  ((CORE_ADDR)
00555                                   bfd_section_vma (core_bfd, section)));
00556 }
00557 
00558 
00559 /* Get the registers out of a core file.  This is the machine-
00560    independent part.  Fetch_core_registers is the machine-dependent
00561    part, typically implemented in the xm-file for each
00562    architecture.  */
00563 
00564 /* We just get all the registers, so we don't use regno.  */
00565 
00566 static void
00567 get_core_registers (struct target_ops *ops,
00568                     struct regcache *regcache, int regno)
00569 {
00570   struct core_regset_section *sect_list;
00571   int i;
00572 
00573   if (!(core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
00574       && (core_vec == NULL || core_vec->core_read_registers == NULL))
00575     {
00576       fprintf_filtered (gdb_stderr,
00577                      "Can't fetch registers from this type of core file\n");
00578       return;
00579     }
00580 
00581   sect_list = gdbarch_core_regset_sections (get_regcache_arch (regcache));
00582   if (sect_list)
00583     while (sect_list->sect_name != NULL)
00584       {
00585         if (strcmp (sect_list->sect_name, ".reg") == 0)
00586           get_core_register_section (regcache, sect_list->sect_name,
00587                                      0, sect_list->human_name, 1);
00588         else if (strcmp (sect_list->sect_name, ".reg2") == 0)
00589           get_core_register_section (regcache, sect_list->sect_name,
00590                                      2, sect_list->human_name, 0);
00591         else
00592           get_core_register_section (regcache, sect_list->sect_name,
00593                                      3, sect_list->human_name, 0);
00594 
00595         sect_list++;
00596       }
00597 
00598   else
00599     {
00600       get_core_register_section (regcache,
00601                                  ".reg", 0, "general-purpose", 1);
00602       get_core_register_section (regcache,
00603                                  ".reg2", 2, "floating-point", 0);
00604     }
00605 
00606   /* Mark all registers not found in the core as unavailable.  */
00607   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
00608     if (regcache_register_status (regcache, i) == REG_UNKNOWN)
00609       regcache_raw_supply (regcache, i, NULL);
00610 }
00611 
00612 static void
00613 core_files_info (struct target_ops *t)
00614 {
00615   print_section_info (core_data, core_bfd);
00616 }
00617 
00618 struct spuid_list
00619 {
00620   gdb_byte *buf;
00621   ULONGEST offset;
00622   LONGEST len;
00623   ULONGEST pos;
00624   ULONGEST written;
00625 };
00626 
00627 static void
00628 add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
00629 {
00630   struct spuid_list *list = list_p;
00631   enum bfd_endian byte_order
00632     = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
00633   int fd, pos = 0;
00634 
00635   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
00636   if (pos == 0)
00637     return;
00638 
00639   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
00640     {
00641       store_unsigned_integer (list->buf + list->pos - list->offset,
00642                               4, byte_order, fd);
00643       list->written += 4;
00644     }
00645   list->pos += 4;
00646 }
00647 
00648 /* Read siginfo data from the core, if possible.  Returns -1 on
00649    failure.  Otherwise, returns the number of bytes read.  ABFD is the
00650    core file's BFD; READBUF, OFFSET, and LEN are all as specified by
00651    the to_xfer_partial interface.  */
00652 
00653 static LONGEST
00654 get_core_siginfo (bfd *abfd, gdb_byte *readbuf, ULONGEST offset, LONGEST len)
00655 {
00656   asection *section;
00657   char *section_name;
00658   const char *name = ".note.linuxcore.siginfo";
00659 
00660   if (ptid_get_lwp (inferior_ptid))
00661     section_name = xstrprintf ("%s/%ld", name,
00662                                ptid_get_lwp (inferior_ptid));
00663   else
00664     section_name = xstrdup (name);
00665 
00666   section = bfd_get_section_by_name (abfd, section_name);
00667   xfree (section_name);
00668   if (section == NULL)
00669     return -1;
00670 
00671   if (!bfd_get_section_contents (abfd, section, readbuf, offset, len))
00672     return -1;
00673 
00674   return len;
00675 }
00676 
00677 static LONGEST
00678 core_xfer_partial (struct target_ops *ops, enum target_object object,
00679                    const char *annex, gdb_byte *readbuf,
00680                    const gdb_byte *writebuf, ULONGEST offset,
00681                    LONGEST len)
00682 {
00683   switch (object)
00684     {
00685     case TARGET_OBJECT_MEMORY:
00686       return section_table_xfer_memory_partial (readbuf, writebuf,
00687                                                 offset, len,
00688                                                 core_data->sections,
00689                                                 core_data->sections_end,
00690                                                 NULL);
00691 
00692     case TARGET_OBJECT_AUXV:
00693       if (readbuf)
00694         {
00695           /* When the aux vector is stored in core file, BFD
00696              represents this with a fake section called ".auxv".  */
00697 
00698           struct bfd_section *section;
00699           bfd_size_type size;
00700 
00701           section = bfd_get_section_by_name (core_bfd, ".auxv");
00702           if (section == NULL)
00703             return -1;
00704 
00705           size = bfd_section_size (core_bfd, section);
00706           if (offset >= size)
00707             return 0;
00708           size -= offset;
00709           if (size > len)
00710             size = len;
00711           if (size > 0
00712               && !bfd_get_section_contents (core_bfd, section, readbuf,
00713                                             (file_ptr) offset, size))
00714             {
00715               warning (_("Couldn't read NT_AUXV note in core file."));
00716               return -1;
00717             }
00718 
00719           return size;
00720         }
00721       return -1;
00722 
00723     case TARGET_OBJECT_WCOOKIE:
00724       if (readbuf)
00725         {
00726           /* When the StackGhost cookie is stored in core file, BFD
00727              represents this with a fake section called
00728              ".wcookie".  */
00729 
00730           struct bfd_section *section;
00731           bfd_size_type size;
00732 
00733           section = bfd_get_section_by_name (core_bfd, ".wcookie");
00734           if (section == NULL)
00735             return -1;
00736 
00737           size = bfd_section_size (core_bfd, section);
00738           if (offset >= size)
00739             return 0;
00740           size -= offset;
00741           if (size > len)
00742             size = len;
00743           if (size > 0
00744               && !bfd_get_section_contents (core_bfd, section, readbuf,
00745                                             (file_ptr) offset, size))
00746             {
00747               warning (_("Couldn't read StackGhost cookie in core file."));
00748               return -1;
00749             }
00750 
00751           return size;
00752         }
00753       return -1;
00754 
00755     case TARGET_OBJECT_LIBRARIES:
00756       if (core_gdbarch
00757           && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
00758         {
00759           if (writebuf)
00760             return -1;
00761           return
00762             gdbarch_core_xfer_shared_libraries (core_gdbarch,
00763                                                 readbuf, offset, len);
00764         }
00765       /* FALL THROUGH */
00766 
00767     case TARGET_OBJECT_LIBRARIES_AIX:
00768       if (core_gdbarch
00769           && gdbarch_core_xfer_shared_libraries_aix_p (core_gdbarch))
00770         {
00771           if (writebuf)
00772             return -1;
00773           return
00774             gdbarch_core_xfer_shared_libraries_aix (core_gdbarch,
00775                                                     readbuf, offset, len);
00776         }
00777       /* FALL THROUGH */
00778 
00779     case TARGET_OBJECT_SPU:
00780       if (readbuf && annex)
00781         {
00782           /* When the SPU contexts are stored in a core file, BFD
00783              represents this with a fake section called
00784              "SPU/<annex>".  */
00785 
00786           struct bfd_section *section;
00787           bfd_size_type size;
00788           char sectionstr[100];
00789 
00790           xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
00791 
00792           section = bfd_get_section_by_name (core_bfd, sectionstr);
00793           if (section == NULL)
00794             return -1;
00795 
00796           size = bfd_section_size (core_bfd, section);
00797           if (offset >= size)
00798             return 0;
00799           size -= offset;
00800           if (size > len)
00801             size = len;
00802           if (size > 0
00803               && !bfd_get_section_contents (core_bfd, section, readbuf,
00804                                             (file_ptr) offset, size))
00805             {
00806               warning (_("Couldn't read SPU section in core file."));
00807               return -1;
00808             }
00809 
00810           return size;
00811         }
00812       else if (readbuf)
00813         {
00814           /* NULL annex requests list of all present spuids.  */
00815           struct spuid_list list;
00816 
00817           list.buf = readbuf;
00818           list.offset = offset;
00819           list.len = len;
00820           list.pos = 0;
00821           list.written = 0;
00822           bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
00823           return list.written;
00824         }
00825       return -1;
00826 
00827     case TARGET_OBJECT_SIGNAL_INFO:
00828       if (readbuf)
00829         return get_core_siginfo (core_bfd, readbuf, offset, len);
00830       return -1;
00831 
00832     default:
00833       if (ops->beneath != NULL)
00834         return ops->beneath->to_xfer_partial (ops->beneath, object,
00835                                               annex, readbuf,
00836                                               writebuf, offset, len);
00837       return -1;
00838     }
00839 }
00840 
00841 
00842 /* If mourn is being called in all the right places, this could be say
00843    `gdb internal error' (since generic_mourn calls
00844    breakpoint_init_inferior).  */
00845 
00846 static int
00847 ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
00848 {
00849   return 0;
00850 }
00851 
00852 
00853 /* Okay, let's be honest: threads gleaned from a core file aren't
00854    exactly lively, are they?  On the other hand, if we don't claim
00855    that each & every one is alive, then we don't get any of them
00856    to appear in an "info thread" command, which is quite a useful
00857    behaviour.
00858  */
00859 static int
00860 core_thread_alive (struct target_ops *ops, ptid_t ptid)
00861 {
00862   return 1;
00863 }
00864 
00865 /* Ask the current architecture what it knows about this core file.
00866    That will be used, in turn, to pick a better architecture.  This
00867    wrapper could be avoided if targets got a chance to specialize
00868    core_ops.  */
00869 
00870 static const struct target_desc *
00871 core_read_description (struct target_ops *target)
00872 {
00873   if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
00874     return gdbarch_core_read_description (core_gdbarch, 
00875                                           target, core_bfd);
00876 
00877   return NULL;
00878 }
00879 
00880 static char *
00881 core_pid_to_str (struct target_ops *ops, ptid_t ptid)
00882 {
00883   static char buf[64];
00884   struct inferior *inf;
00885   int pid;
00886 
00887   /* The preferred way is to have a gdbarch/OS specific
00888      implementation.  */
00889   if (core_gdbarch
00890       && gdbarch_core_pid_to_str_p (core_gdbarch))
00891     return gdbarch_core_pid_to_str (core_gdbarch, ptid);
00892 
00893   /* Otherwise, if we don't have one, we'll just fallback to
00894      "process", with normal_pid_to_str.  */
00895 
00896   /* Try the LWPID field first.  */
00897   pid = ptid_get_lwp (ptid);
00898   if (pid != 0)
00899     return normal_pid_to_str (pid_to_ptid (pid));
00900 
00901   /* Otherwise, this isn't a "threaded" core -- use the PID field, but
00902      only if it isn't a fake PID.  */
00903   inf = find_inferior_pid (ptid_get_pid (ptid));
00904   if (inf != NULL && !inf->fake_pid_p)
00905     return normal_pid_to_str (ptid);
00906 
00907   /* No luck.  We simply don't have a valid PID to print.  */
00908   xsnprintf (buf, sizeof buf, "<main task>");
00909   return buf;
00910 }
00911 
00912 static int
00913 core_has_memory (struct target_ops *ops)
00914 {
00915   return (core_bfd != NULL);
00916 }
00917 
00918 static int
00919 core_has_stack (struct target_ops *ops)
00920 {
00921   return (core_bfd != NULL);
00922 }
00923 
00924 static int
00925 core_has_registers (struct target_ops *ops)
00926 {
00927   return (core_bfd != NULL);
00928 }
00929 
00930 /* Implement the to_info_proc method.  */
00931 
00932 static void
00933 core_info_proc (struct target_ops *ops, char *args, enum info_proc_what request)
00934 {
00935   struct gdbarch *gdbarch = get_current_arch ();
00936 
00937   /* Since this is the core file target, call the 'core_info_proc'
00938      method on gdbarch, not 'info_proc'.  */
00939   if (gdbarch_core_info_proc_p (gdbarch))
00940     gdbarch_core_info_proc (gdbarch, args, request);
00941 }
00942 
00943 /* Fill in core_ops with its defined operations and properties.  */
00944 
00945 static void
00946 init_core_ops (void)
00947 {
00948   core_ops.to_shortname = "core";
00949   core_ops.to_longname = "Local core dump file";
00950   core_ops.to_doc =
00951     "Use a core file as a target.  Specify the filename of the core file.";
00952   core_ops.to_open = core_open;
00953   core_ops.to_close = core_close;
00954   core_ops.to_attach = find_default_attach;
00955   core_ops.to_detach = core_detach;
00956   core_ops.to_fetch_registers = get_core_registers;
00957   core_ops.to_xfer_partial = core_xfer_partial;
00958   core_ops.to_files_info = core_files_info;
00959   core_ops.to_insert_breakpoint = ignore;
00960   core_ops.to_remove_breakpoint = ignore;
00961   core_ops.to_create_inferior = find_default_create_inferior;
00962   core_ops.to_thread_alive = core_thread_alive;
00963   core_ops.to_read_description = core_read_description;
00964   core_ops.to_pid_to_str = core_pid_to_str;
00965   core_ops.to_stratum = process_stratum;
00966   core_ops.to_has_memory = core_has_memory;
00967   core_ops.to_has_stack = core_has_stack;
00968   core_ops.to_has_registers = core_has_registers;
00969   core_ops.to_info_proc = core_info_proc;
00970   core_ops.to_magic = OPS_MAGIC;
00971 
00972   if (core_target)
00973     internal_error (__FILE__, __LINE__, 
00974                     _("init_core_ops: core target already exists (\"%s\")."),
00975                     core_target->to_longname);
00976   core_target = &core_ops;
00977 }
00978 
00979 void
00980 _initialize_corelow (void)
00981 {
00982   init_core_ops ();
00983 
00984   add_target_with_completer (&core_ops, filename_completer);
00985 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines