GDB (API)
/home/stan/gdb/src/gdb/corefile.c
Go to the documentation of this file.
00001 /* Core dump and executable file functions above 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 "gdb_string.h"
00022 #include <errno.h>
00023 #include <signal.h>
00024 #include <fcntl.h>
00025 #include "inferior.h"
00026 #include "symtab.h"
00027 #include "command.h"
00028 #include "gdbcmd.h"
00029 #include "bfd.h"
00030 #include "target.h"
00031 #include "gdbcore.h"
00032 #include "dis-asm.h"
00033 #include "gdb_stat.h"
00034 #include "completer.h"
00035 #include "exceptions.h"
00036 #include "observer.h"
00037 #include "cli/cli-utils.h"
00038 
00039 /* Local function declarations.  */
00040 
00041 extern void _initialize_core (void);
00042 static void call_extra_exec_file_hooks (char *filename);
00043 
00044 /* You can have any number of hooks for `exec_file_command' command to
00045    call.  If there's only one hook, it is set in exec_file_display
00046    hook.  If there are two or more hooks, they are set in
00047    exec_file_extra_hooks[], and deprecated_exec_file_display_hook is
00048    set to a function that calls all of them.  This extra complexity is
00049    needed to preserve compatibility with old code that assumed that
00050    only one hook could be set, and which called
00051    deprecated_exec_file_display_hook directly.  */
00052 
00053 typedef void (*hook_type) (char *);
00054 
00055 hook_type deprecated_exec_file_display_hook;    /* The original hook.  */
00056 static hook_type *exec_file_extra_hooks;        /* Array of additional
00057                                                    hooks.  */
00058 static int exec_file_hook_count = 0;            /* Size of array.  */
00059 
00060 /* Binary file diddling handle for the core file.  */
00061 
00062 bfd *core_bfd = NULL;
00063 
00064 /* corelow.c target.  It is never NULL after GDB initialization.  */
00065 
00066 struct target_ops *core_target;
00067 
00068 
00069 /* Backward compatability with old way of specifying core files.  */
00070 
00071 void
00072 core_file_command (char *filename, int from_tty)
00073 {
00074   dont_repeat ();               /* Either way, seems bogus.  */
00075 
00076   gdb_assert (core_target != NULL);
00077 
00078   if (!filename)
00079     (core_target->to_detach) (core_target, filename, from_tty);
00080   else
00081     (core_target->to_open) (filename, from_tty);
00082 }
00083 
00084 
00085 /* If there are two or more functions that wish to hook into
00086    exec_file_command, this function will call all of the hook
00087    functions.  */
00088 
00089 static void
00090 call_extra_exec_file_hooks (char *filename)
00091 {
00092   int i;
00093 
00094   for (i = 0; i < exec_file_hook_count; i++)
00095     (*exec_file_extra_hooks[i]) (filename);
00096 }
00097 
00098 /* Call this to specify the hook for exec_file_command to call back.
00099    This is called from the x-window display code.  */
00100 
00101 void
00102 specify_exec_file_hook (void (*hook) (char *))
00103 {
00104   hook_type *new_array;
00105 
00106   if (deprecated_exec_file_display_hook != NULL)
00107     {
00108       /* There's already a hook installed.  Arrange to have both it
00109          and the subsequent hooks called.  */
00110       if (exec_file_hook_count == 0)
00111         {
00112           /* If this is the first extra hook, initialize the hook
00113              array.  */
00114           exec_file_extra_hooks = (hook_type *)
00115             xmalloc (sizeof (hook_type));
00116           exec_file_extra_hooks[0] = deprecated_exec_file_display_hook;
00117           deprecated_exec_file_display_hook = call_extra_exec_file_hooks;
00118           exec_file_hook_count = 1;
00119         }
00120 
00121       /* Grow the hook array by one and add the new hook to the end.
00122          Yes, it's inefficient to grow it by one each time but since
00123          this is hardly ever called it's not a big deal.  */
00124       exec_file_hook_count++;
00125       new_array = (hook_type *)
00126         xrealloc (exec_file_extra_hooks,
00127                   exec_file_hook_count * sizeof (hook_type));
00128       exec_file_extra_hooks = new_array;
00129       exec_file_extra_hooks[exec_file_hook_count - 1] = hook;
00130     }
00131   else
00132     deprecated_exec_file_display_hook = hook;
00133 }
00134 
00135 void
00136 reopen_exec_file (void)
00137 {
00138   char *filename;
00139   int res;
00140   struct stat st;
00141   struct cleanup *cleanups;
00142 
00143   /* Don't do anything if there isn't an exec file.  */
00144   if (exec_bfd == NULL)
00145     return;
00146 
00147   /* If the timestamp of the exec file has changed, reopen it.  */
00148   filename = xstrdup (bfd_get_filename (exec_bfd));
00149   cleanups = make_cleanup (xfree, filename);
00150   res = stat (filename, &st);
00151 
00152   if (exec_bfd_mtime && exec_bfd_mtime != st.st_mtime)
00153     exec_file_attach (filename, 0);
00154   else
00155     /* If we accessed the file since last opening it, close it now;
00156        this stops GDB from holding the executable open after it
00157        exits.  */
00158     bfd_cache_close_all ();
00159 
00160   do_cleanups (cleanups);
00161 }
00162 
00163 /* If we have both a core file and an exec file,
00164    print a warning if they don't go together.  */
00165 
00166 void
00167 validate_files (void)
00168 {
00169   if (exec_bfd && core_bfd)
00170     {
00171       if (!core_file_matches_executable_p (core_bfd, exec_bfd))
00172         warning (_("core file may not match specified executable file."));
00173       else if (bfd_get_mtime (exec_bfd) > bfd_get_mtime (core_bfd))
00174         warning (_("exec file is newer than core file."));
00175     }
00176 }
00177 
00178 /* Return the name of the executable file as a string.
00179    ERR nonzero means get error if there is none specified;
00180    otherwise return 0 in that case.  */
00181 
00182 char *
00183 get_exec_file (int err)
00184 {
00185   if (exec_filename)
00186     return exec_filename;
00187   if (!err)
00188     return NULL;
00189 
00190   error (_("No executable file specified.\n\
00191 Use the \"file\" or \"exec-file\" command."));
00192   return NULL;
00193 }
00194 
00195 
00196 char *
00197 memory_error_message (enum target_xfer_error err,
00198                       struct gdbarch *gdbarch, CORE_ADDR memaddr)
00199 {
00200   switch (err)
00201     {
00202     case TARGET_XFER_E_IO:
00203       /* Actually, address between memaddr and memaddr + len was out of
00204          bounds.  */
00205       return xstrprintf (_("Cannot access memory at address %s"),
00206                          paddress (gdbarch, memaddr));
00207     case TARGET_XFER_E_UNAVAILABLE:
00208       return xstrprintf (_("Memory at address %s unavailable."),
00209                          paddress (gdbarch, memaddr));
00210     default:
00211       internal_error (__FILE__, __LINE__,
00212                       "unhandled target_xfer_error: %s (%s)",
00213                       target_xfer_error_to_string (err),
00214                       plongest (err));
00215     }
00216 }
00217 
00218 /* Report a memory error by throwing a suitable exception.  */
00219 
00220 void
00221 memory_error (enum target_xfer_error err, CORE_ADDR memaddr)
00222 {
00223   char *str;
00224 
00225   /* Build error string.  */
00226   str = memory_error_message (err, target_gdbarch (), memaddr);
00227   make_cleanup (xfree, str);
00228 
00229   /* Choose the right error to throw.  */
00230   switch (err)
00231     {
00232     case TARGET_XFER_E_IO:
00233       err = MEMORY_ERROR;
00234       break;
00235     case TARGET_XFER_E_UNAVAILABLE:
00236       err = NOT_AVAILABLE_ERROR;
00237       break;
00238     }
00239 
00240   /* Throw it.  */
00241   throw_error (err, ("%s"), str);
00242 }
00243 
00244 /* Same as target_read_memory, but report an error if can't read.  */
00245 
00246 void
00247 read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
00248 {
00249   LONGEST xfered = 0;
00250 
00251   while (xfered < len)
00252     {
00253       LONGEST xfer = target_xfer_partial (current_target.beneath,
00254                                           TARGET_OBJECT_MEMORY, NULL,
00255                                           myaddr + xfered, NULL,
00256                                           memaddr + xfered, len - xfered);
00257 
00258       if (xfer == 0)
00259         memory_error (TARGET_XFER_E_IO, memaddr + xfered);
00260       if (xfer < 0)
00261         memory_error (xfer, memaddr + xfered);
00262       xfered += xfer;
00263       QUIT;
00264     }
00265 }
00266 
00267 /* Same as target_read_stack, but report an error if can't read.  */
00268 
00269 void
00270 read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
00271 {
00272   int status;
00273 
00274   status = target_read_stack (memaddr, myaddr, len);
00275   if (status != 0)
00276     memory_error (status, memaddr);
00277 }
00278 
00279 /* Argument / return result struct for use with
00280    do_captured_read_memory_integer().  MEMADDR and LEN are filled in
00281    by gdb_read_memory_integer().  RESULT is the contents that were
00282    successfully read from MEMADDR of length LEN.  */
00283 
00284 struct captured_read_memory_integer_arguments
00285 {
00286   CORE_ADDR memaddr;
00287   int len;
00288   enum bfd_endian byte_order;
00289   LONGEST result;
00290 };
00291 
00292 /* Helper function for gdb_read_memory_integer().  DATA must be a
00293    pointer to a captured_read_memory_integer_arguments struct.
00294    Return 1 if successful.  Note that the catch_errors() interface
00295    will return 0 if an error occurred while reading memory.  This
00296    choice of return code is so that we can distinguish between
00297    success and failure.  */
00298 
00299 static int
00300 do_captured_read_memory_integer (void *data)
00301 {
00302   struct captured_read_memory_integer_arguments *args
00303     = (struct captured_read_memory_integer_arguments*) data;
00304   CORE_ADDR memaddr = args->memaddr;
00305   int len = args->len;
00306   enum bfd_endian byte_order = args->byte_order;
00307 
00308   args->result = read_memory_integer (memaddr, len, byte_order);
00309 
00310   return 1;
00311 }
00312 
00313 /* Read memory at MEMADDR of length LEN and put the contents in
00314    RETURN_VALUE.  Return 0 if MEMADDR couldn't be read and non-zero
00315    if successful.  */
00316 
00317 int
00318 safe_read_memory_integer (CORE_ADDR memaddr, int len, 
00319                           enum bfd_endian byte_order,
00320                           LONGEST *return_value)
00321 {
00322   int status;
00323   struct captured_read_memory_integer_arguments args;
00324 
00325   args.memaddr = memaddr;
00326   args.len = len;
00327   args.byte_order = byte_order;
00328 
00329   status = catch_errors (do_captured_read_memory_integer, &args,
00330                          "", RETURN_MASK_ALL);
00331   if (status)
00332     *return_value = args.result;
00333 
00334   return status;
00335 }
00336 
00337 LONGEST
00338 read_memory_integer (CORE_ADDR memaddr, int len,
00339                      enum bfd_endian byte_order)
00340 {
00341   gdb_byte buf[sizeof (LONGEST)];
00342 
00343   read_memory (memaddr, buf, len);
00344   return extract_signed_integer (buf, len, byte_order);
00345 }
00346 
00347 ULONGEST
00348 read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
00349                               enum bfd_endian byte_order)
00350 {
00351   gdb_byte buf[sizeof (ULONGEST)];
00352 
00353   read_memory (memaddr, buf, len);
00354   return extract_unsigned_integer (buf, len, byte_order);
00355 }
00356 
00357 void
00358 read_memory_string (CORE_ADDR memaddr, char *buffer, int max_len)
00359 {
00360   char *cp;
00361   int i;
00362   int cnt;
00363 
00364   cp = buffer;
00365   while (1)
00366     {
00367       if (cp - buffer >= max_len)
00368         {
00369           buffer[max_len - 1] = '\0';
00370           break;
00371         }
00372       cnt = max_len - (cp - buffer);
00373       if (cnt > 8)
00374         cnt = 8;
00375       read_memory (memaddr + (int) (cp - buffer), (gdb_byte *) cp, cnt);
00376       for (i = 0; i < cnt && *cp; i++, cp++)
00377         ;                       /* null body */
00378 
00379       if (i < cnt && !*cp)
00380         break;
00381     }
00382 }
00383 
00384 CORE_ADDR
00385 read_memory_typed_address (CORE_ADDR addr, struct type *type)
00386 {
00387   gdb_byte *buf = alloca (TYPE_LENGTH (type));
00388 
00389   read_memory (addr, buf, TYPE_LENGTH (type));
00390   return extract_typed_address (buf, type);
00391 }
00392 
00393 /* Same as target_write_memory, but report an error if can't
00394    write.  */
00395 void
00396 write_memory (CORE_ADDR memaddr, 
00397               const bfd_byte *myaddr, ssize_t len)
00398 {
00399   int status;
00400 
00401   status = target_write_memory (memaddr, myaddr, len);
00402   if (status != 0)
00403     memory_error (status, memaddr);
00404 }
00405 
00406 /* Same as write_memory, but notify 'memory_changed' observers.  */
00407 
00408 void
00409 write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
00410                                 ssize_t len)
00411 {
00412   write_memory (memaddr, myaddr, len);
00413   observer_notify_memory_changed (current_inferior (), memaddr, len, myaddr);
00414 }
00415 
00416 /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
00417    integer.  */
00418 void
00419 write_memory_unsigned_integer (CORE_ADDR addr, int len, 
00420                                enum bfd_endian byte_order,
00421                                ULONGEST value)
00422 {
00423   gdb_byte *buf = alloca (len);
00424 
00425   store_unsigned_integer (buf, len, byte_order, value);
00426   write_memory (addr, buf, len);
00427 }
00428 
00429 /* Store VALUE at ADDR in the inferior as a LEN-byte signed
00430    integer.  */
00431 void
00432 write_memory_signed_integer (CORE_ADDR addr, int len, 
00433                              enum bfd_endian byte_order,
00434                              LONGEST value)
00435 {
00436   gdb_byte *buf = alloca (len);
00437 
00438   store_signed_integer (buf, len, byte_order, value);
00439   write_memory (addr, buf, len);
00440 }
00441 
00442 /* The current default bfd target.  Points to storage allocated for
00443    gnutarget_string.  */
00444 char *gnutarget;
00445 
00446 /* Same thing, except it is "auto" not NULL for the default case.  */
00447 static char *gnutarget_string;
00448 static void
00449 show_gnutarget_string (struct ui_file *file, int from_tty,
00450                        struct cmd_list_element *c,
00451                        const char *value)
00452 {
00453   fprintf_filtered (file,
00454                     _("The current BFD target is \"%s\".\n"), value);
00455 }
00456 
00457 static void set_gnutarget_command (char *, int,
00458                                    struct cmd_list_element *);
00459 
00460 static void
00461 set_gnutarget_command (char *ignore, int from_tty,
00462                        struct cmd_list_element *c)
00463 {
00464   char *gend = gnutarget_string + strlen (gnutarget_string);
00465 
00466   gend = remove_trailing_whitespace (gnutarget_string, gend);
00467   *gend = '\0';
00468 
00469   if (strcmp (gnutarget_string, "auto") == 0)
00470     gnutarget = NULL;
00471   else
00472     gnutarget = gnutarget_string;
00473 }
00474 
00475 /* A completion function for "set gnutarget".  */
00476 
00477 static VEC (char_ptr) *
00478 complete_set_gnutarget (struct cmd_list_element *cmd,
00479                         const char *text, const char *word)
00480 {
00481   static const char **bfd_targets;
00482 
00483   if (bfd_targets == NULL)
00484     {
00485       int last;
00486 
00487       bfd_targets = bfd_target_list ();
00488       for (last = 0; bfd_targets[last] != NULL; ++last)
00489         ;
00490 
00491       bfd_targets = xrealloc (bfd_targets, (last + 2) * sizeof (const char **));
00492       bfd_targets[last] = "auto";
00493       bfd_targets[last + 1] = NULL;
00494     }
00495 
00496   return complete_on_enum (bfd_targets, text, word);
00497 }
00498 
00499 /* Set the gnutarget.  */
00500 void
00501 set_gnutarget (char *newtarget)
00502 {
00503   if (gnutarget_string != NULL)
00504     xfree (gnutarget_string);
00505   gnutarget_string = xstrdup (newtarget);
00506   set_gnutarget_command (NULL, 0, NULL);
00507 }
00508 
00509 void
00510 _initialize_core (void)
00511 {
00512   struct cmd_list_element *c;
00513 
00514   c = add_cmd ("core-file", class_files, core_file_command, _("\
00515 Use FILE as core dump for examining memory and registers.\n\
00516 No arg means have no core file.  This command has been superseded by the\n\
00517 `target core' and `detach' commands."), &cmdlist);
00518   set_cmd_completer (c, filename_completer);
00519 
00520   
00521   c = add_setshow_string_noescape_cmd ("gnutarget", class_files,
00522                                        &gnutarget_string, _("\
00523 Set the current BFD target."), _("\
00524 Show the current BFD target."), _("\
00525 Use `set gnutarget auto' to specify automatic detection."),
00526                                        set_gnutarget_command,
00527                                        show_gnutarget_string,
00528                                        &setlist, &showlist);
00529   set_cmd_completer (c, complete_set_gnutarget);
00530 
00531   add_alias_cmd ("g", "gnutarget", class_files, 1, &setlist);
00532 
00533   if (getenv ("GNUTARGET"))
00534     set_gnutarget (getenv ("GNUTARGET"));
00535   else
00536     set_gnutarget ("auto");
00537 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines