gdb/corefile.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Core dump and executable file functions above 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 <signal.h>
  16. #include <fcntl.h>
  17. #include "inferior.h"
  18. #include "symtab.h"
  19. #include "command.h"
  20. #include "gdbcmd.h"
  21. #include "bfd.h"
  22. #include "target.h"
  23. #include "gdbcore.h"
  24. #include "dis-asm.h"
  25. #include <sys/stat.h>
  26. #include "completer.h"
  27. #include "observer.h"
  28. #include "cli/cli-utils.h"

  29. /* Local function declarations.  */

  30. extern void _initialize_core (void);

  31. /* You can have any number of hooks for `exec_file_command' command to
  32.    call.  If there's only one hook, it is set in exec_file_display
  33.    hook.  If there are two or more hooks, they are set in
  34.    exec_file_extra_hooks[], and deprecated_exec_file_display_hook is
  35.    set to a function that calls all of them.  This extra complexity is
  36.    needed to preserve compatibility with old code that assumed that
  37.    only one hook could be set, and which called
  38.    deprecated_exec_file_display_hook directly.  */

  39. typedef void (*hook_type) (const char *);

  40. hook_type deprecated_exec_file_display_hook;        /* The original hook.  */
  41. static hook_type *exec_file_extra_hooks;        /* Array of additional
  42.                                                    hooks.  */
  43. static int exec_file_hook_count = 0;                /* Size of array.  */

  44. /* Binary file diddling handle for the core file.  */

  45. bfd *core_bfd = NULL;

  46. /* corelow.c target.  It is never NULL after GDB initialization.  */

  47. struct target_ops *core_target;


  48. /* Backward compatability with old way of specifying core files.  */

  49. void
  50. core_file_command (char *filename, int from_tty)
  51. {
  52.   dont_repeat ();                /* Either way, seems bogus.  */

  53.   gdb_assert (core_target != NULL);

  54.   if (!filename)
  55.     (core_target->to_detach) (core_target, filename, from_tty);
  56.   else
  57.     (core_target->to_open) (filename, from_tty);
  58. }


  59. /* If there are two or more functions that wish to hook into
  60.    exec_file_command, this function will call all of the hook
  61.    functions.  */

  62. static void
  63. call_extra_exec_file_hooks (const char *filename)
  64. {
  65.   int i;

  66.   for (i = 0; i < exec_file_hook_count; i++)
  67.     (*exec_file_extra_hooks[i]) (filename);
  68. }

  69. /* Call this to specify the hook for exec_file_command to call back.
  70.    This is called from the x-window display code.  */

  71. void
  72. specify_exec_file_hook (void (*hook) (const char *))
  73. {
  74.   hook_type *new_array;

  75.   if (deprecated_exec_file_display_hook != NULL)
  76.     {
  77.       /* There's already a hook installed.  Arrange to have both it
  78.          and the subsequent hooks called.  */
  79.       if (exec_file_hook_count == 0)
  80.         {
  81.           /* If this is the first extra hook, initialize the hook
  82.              array.  */
  83.           exec_file_extra_hooks = (hook_type *)
  84.             xmalloc (sizeof (hook_type));
  85.           exec_file_extra_hooks[0] = deprecated_exec_file_display_hook;
  86.           deprecated_exec_file_display_hook = call_extra_exec_file_hooks;
  87.           exec_file_hook_count = 1;
  88.         }

  89.       /* Grow the hook array by one and add the new hook to the end.
  90.          Yes, it's inefficient to grow it by one each time but since
  91.          this is hardly ever called it's not a big deal.  */
  92.       exec_file_hook_count++;
  93.       new_array = (hook_type *)
  94.         xrealloc (exec_file_extra_hooks,
  95.                   exec_file_hook_count * sizeof (hook_type));
  96.       exec_file_extra_hooks = new_array;
  97.       exec_file_extra_hooks[exec_file_hook_count - 1] = hook;
  98.     }
  99.   else
  100.     deprecated_exec_file_display_hook = hook;
  101. }

  102. void
  103. reopen_exec_file (void)
  104. {
  105.   char *filename;
  106.   int res;
  107.   struct stat st;
  108.   struct cleanup *cleanups;

  109.   /* Don't do anything if there isn't an exec file.  */
  110.   if (exec_bfd == NULL)
  111.     return;

  112.   /* If the timestamp of the exec file has changed, reopen it.  */
  113.   filename = xstrdup (bfd_get_filename (exec_bfd));
  114.   cleanups = make_cleanup (xfree, filename);
  115.   res = stat (filename, &st);

  116.   if (exec_bfd_mtime && exec_bfd_mtime != st.st_mtime)
  117.     exec_file_attach (filename, 0);
  118.   else
  119.     /* If we accessed the file since last opening it, close it now;
  120.        this stops GDB from holding the executable open after it
  121.        exits.  */
  122.     bfd_cache_close_all ();

  123.   do_cleanups (cleanups);
  124. }

  125. /* If we have both a core file and an exec file,
  126.    print a warning if they don't go together.  */

  127. void
  128. validate_files (void)
  129. {
  130.   if (exec_bfd && core_bfd)
  131.     {
  132.       if (!core_file_matches_executable_p (core_bfd, exec_bfd))
  133.         warning (_("core file may not match specified executable file."));
  134.       else if (bfd_get_mtime (exec_bfd) > bfd_get_mtime (core_bfd))
  135.         warning (_("exec file is newer than core file."));
  136.     }
  137. }

  138. /* Return the name of the executable file as a string.
  139.    ERR nonzero means get error if there is none specified;
  140.    otherwise return 0 in that case.  */

  141. char *
  142. get_exec_file (int err)
  143. {
  144.   if (exec_filename)
  145.     return exec_filename;
  146.   if (!err)
  147.     return NULL;

  148.   error (_("No executable file specified.\n\
  149. Use the \"file\" or \"exec-file\" command."));
  150.   return NULL;
  151. }


  152. char *
  153. memory_error_message (enum target_xfer_status err,
  154.                       struct gdbarch *gdbarch, CORE_ADDR memaddr)
  155. {
  156.   switch (err)
  157.     {
  158.     case TARGET_XFER_E_IO:
  159.       /* Actually, address between memaddr and memaddr + len was out of
  160.          bounds.  */
  161.       return xstrprintf (_("Cannot access memory at address %s"),
  162.                          paddress (gdbarch, memaddr));
  163.     case TARGET_XFER_UNAVAILABLE:
  164.       return xstrprintf (_("Memory at address %s unavailable."),
  165.                          paddress (gdbarch, memaddr));
  166.     default:
  167.       internal_error (__FILE__, __LINE__,
  168.                       "unhandled target_xfer_status: %s (%s)",
  169.                       target_xfer_status_to_string (err),
  170.                       plongest (err));
  171.     }
  172. }

  173. /* Report a memory error by throwing a suitable exception.  */

  174. void
  175. memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
  176. {
  177.   char *str;
  178.   enum errors exception = GDB_NO_ERROR;

  179.   /* Build error string.  */
  180.   str = memory_error_message (err, target_gdbarch (), memaddr);
  181.   make_cleanup (xfree, str);

  182.   /* Choose the right error to throw.  */
  183.   switch (err)
  184.     {
  185.     case TARGET_XFER_E_IO:
  186.       exception = MEMORY_ERROR;
  187.       break;
  188.     case TARGET_XFER_UNAVAILABLE:
  189.       exception = NOT_AVAILABLE_ERROR;
  190.       break;
  191.     }

  192.   /* Throw it.  */
  193.   throw_error (exception, ("%s"), str);
  194. }

  195. /* Same as target_read_memory, but report an error if can't read.  */

  196. void
  197. read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  198. {
  199.   ULONGEST xfered = 0;

  200.   while (xfered < len)
  201.     {
  202.       enum target_xfer_status status;
  203.       ULONGEST xfered_len;

  204.       status = target_xfer_partial (current_target.beneath,
  205.                                     TARGET_OBJECT_MEMORY, NULL,
  206.                                     myaddr + xfered, NULL,
  207.                                     memaddr + xfered, len - xfered,
  208.                                     &xfered_len);

  209.       if (status != TARGET_XFER_OK)
  210.         memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
  211.                       memaddr + xfered);

  212.       xfered += xfered_len;
  213.       QUIT;
  214.     }
  215. }

  216. /* Same as target_read_stack, but report an error if can't read.  */

  217. void
  218. read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  219. {
  220.   int status;

  221.   status = target_read_stack (memaddr, myaddr, len);
  222.   if (status != 0)
  223.     memory_error (status, memaddr);
  224. }

  225. /* Same as target_read_code, but report an error if can't read.  */

  226. void
  227. read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  228. {
  229.   int status;

  230.   status = target_read_code (memaddr, myaddr, len);
  231.   if (status != 0)
  232.     memory_error (status, memaddr);
  233. }

  234. /* Read memory at MEMADDR of length LEN and put the contents in
  235.    RETURN_VALUE.  Return 0 if MEMADDR couldn't be read and non-zero
  236.    if successful.  */

  237. int
  238. safe_read_memory_integer (CORE_ADDR memaddr, int len,
  239.                           enum bfd_endian byte_order,
  240.                           LONGEST *return_value)
  241. {
  242.   gdb_byte buf[sizeof (LONGEST)];

  243.   if (target_read_memory (memaddr, buf, len))
  244.     return 0;

  245.   *return_value = extract_signed_integer (buf, len, byte_order);
  246.   return 1;
  247. }

  248. LONGEST
  249. read_memory_integer (CORE_ADDR memaddr, int len,
  250.                      enum bfd_endian byte_order)
  251. {
  252.   gdb_byte buf[sizeof (LONGEST)];

  253.   read_memory (memaddr, buf, len);
  254.   return extract_signed_integer (buf, len, byte_order);
  255. }

  256. ULONGEST
  257. read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
  258.                               enum bfd_endian byte_order)
  259. {
  260.   gdb_byte buf[sizeof (ULONGEST)];

  261.   read_memory (memaddr, buf, len);
  262.   return extract_unsigned_integer (buf, len, byte_order);
  263. }

  264. LONGEST
  265. read_code_integer (CORE_ADDR memaddr, int len,
  266.                    enum bfd_endian byte_order)
  267. {
  268.   gdb_byte buf[sizeof (LONGEST)];

  269.   read_code (memaddr, buf, len);
  270.   return extract_signed_integer (buf, len, byte_order);
  271. }

  272. ULONGEST
  273. read_code_unsigned_integer (CORE_ADDR memaddr, int len,
  274.                             enum bfd_endian byte_order)
  275. {
  276.   gdb_byte buf[sizeof (ULONGEST)];

  277.   read_code (memaddr, buf, len);
  278.   return extract_unsigned_integer (buf, len, byte_order);
  279. }

  280. void
  281. read_memory_string (CORE_ADDR memaddr, char *buffer, int max_len)
  282. {
  283.   char *cp;
  284.   int i;
  285.   int cnt;

  286.   cp = buffer;
  287.   while (1)
  288.     {
  289.       if (cp - buffer >= max_len)
  290.         {
  291.           buffer[max_len - 1] = '\0';
  292.           break;
  293.         }
  294.       cnt = max_len - (cp - buffer);
  295.       if (cnt > 8)
  296.         cnt = 8;
  297.       read_memory (memaddr + (int) (cp - buffer), (gdb_byte *) cp, cnt);
  298.       for (i = 0; i < cnt && *cp; i++, cp++)
  299.         ;                        /* null body */

  300.       if (i < cnt && !*cp)
  301.         break;
  302.     }
  303. }

  304. CORE_ADDR
  305. read_memory_typed_address (CORE_ADDR addr, struct type *type)
  306. {
  307.   gdb_byte *buf = alloca (TYPE_LENGTH (type));

  308.   read_memory (addr, buf, TYPE_LENGTH (type));
  309.   return extract_typed_address (buf, type);
  310. }

  311. /* Same as target_write_memory, but report an error if can't
  312.    write.  */
  313. void
  314. write_memory (CORE_ADDR memaddr,
  315.               const bfd_byte *myaddr, ssize_t len)
  316. {
  317.   int status;

  318.   status = target_write_memory (memaddr, myaddr, len);
  319.   if (status != 0)
  320.     memory_error (status, memaddr);
  321. }

  322. /* Same as write_memory, but notify 'memory_changed' observers.  */

  323. void
  324. write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
  325.                                 ssize_t len)
  326. {
  327.   write_memory (memaddr, myaddr, len);
  328.   observer_notify_memory_changed (current_inferior (), memaddr, len, myaddr);
  329. }

  330. /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
  331.    integer.  */
  332. void
  333. write_memory_unsigned_integer (CORE_ADDR addr, int len,
  334.                                enum bfd_endian byte_order,
  335.                                ULONGEST value)
  336. {
  337.   gdb_byte *buf = alloca (len);

  338.   store_unsigned_integer (buf, len, byte_order, value);
  339.   write_memory (addr, buf, len);
  340. }

  341. /* Store VALUE at ADDR in the inferior as a LEN-byte signed
  342.    integer.  */
  343. void
  344. write_memory_signed_integer (CORE_ADDR addr, int len,
  345.                              enum bfd_endian byte_order,
  346.                              LONGEST value)
  347. {
  348.   gdb_byte *buf = alloca (len);

  349.   store_signed_integer (buf, len, byte_order, value);
  350.   write_memory (addr, buf, len);
  351. }

  352. /* The current default bfd target.  Points to storage allocated for
  353.    gnutarget_string.  */
  354. char *gnutarget;

  355. /* Same thing, except it is "auto" not NULL for the default case.  */
  356. static char *gnutarget_string;
  357. static void
  358. show_gnutarget_string (struct ui_file *file, int from_tty,
  359.                        struct cmd_list_element *c,
  360.                        const char *value)
  361. {
  362.   fprintf_filtered (file,
  363.                     _("The current BFD target is \"%s\".\n"), value);
  364. }

  365. static void set_gnutarget_command (char *, int,
  366.                                    struct cmd_list_element *);

  367. static void
  368. set_gnutarget_command (char *ignore, int from_tty,
  369.                        struct cmd_list_element *c)
  370. {
  371.   char *gend = gnutarget_string + strlen (gnutarget_string);

  372.   gend = remove_trailing_whitespace (gnutarget_string, gend);
  373.   *gend = '\0';

  374.   if (strcmp (gnutarget_string, "auto") == 0)
  375.     gnutarget = NULL;
  376.   else
  377.     gnutarget = gnutarget_string;
  378. }

  379. /* A completion function for "set gnutarget".  */

  380. static VEC (char_ptr) *
  381. complete_set_gnutarget (struct cmd_list_element *cmd,
  382.                         const char *text, const char *word)
  383. {
  384.   static const char **bfd_targets;

  385.   if (bfd_targets == NULL)
  386.     {
  387.       int last;

  388.       bfd_targets = bfd_target_list ();
  389.       for (last = 0; bfd_targets[last] != NULL; ++last)
  390.         ;

  391.       bfd_targets = xrealloc (bfd_targets, (last + 2) * sizeof (const char **));
  392.       bfd_targets[last] = "auto";
  393.       bfd_targets[last + 1] = NULL;
  394.     }

  395.   return complete_on_enum (bfd_targets, text, word);
  396. }

  397. /* Set the gnutarget.  */
  398. void
  399. set_gnutarget (char *newtarget)
  400. {
  401.   if (gnutarget_string != NULL)
  402.     xfree (gnutarget_string);
  403.   gnutarget_string = xstrdup (newtarget);
  404.   set_gnutarget_command (NULL, 0, NULL);
  405. }

  406. void
  407. _initialize_core (void)
  408. {
  409.   struct cmd_list_element *c;

  410.   c = add_cmd ("core-file", class_files, core_file_command, _("\
  411. Use FILE as core dump for examining memory and registers.\n\
  412. No arg means have no core file.  This command has been superseded by the\n\
  413. `target core' and `detach' commands."), &cmdlist);
  414.   set_cmd_completer (c, filename_completer);


  415.   c = add_setshow_string_noescape_cmd ("gnutarget", class_files,
  416.                                        &gnutarget_string, _("\
  417. Set the current BFD target."), _("\
  418. Show the current BFD target."), _("\
  419. Use `set gnutarget auto' to specify automatic detection."),
  420.                                        set_gnutarget_command,
  421.                                        show_gnutarget_string,
  422.                                        &setlist, &showlist);
  423.   set_cmd_completer (c, complete_set_gnutarget);

  424.   add_alias_cmd ("g", "gnutarget", class_files, 1, &setlist);

  425.   if (getenv ("GNUTARGET"))
  426.     set_gnutarget (getenv ("GNUTARGET"));
  427.   else
  428.     set_gnutarget ("auto");
  429. }