corefile.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Core dump and executable file functions above target vector, for GDB.
  2. Copyright (C) 1986-2022 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 "observable.h"
  28. #include "cli/cli-utils.h"
  29. #include "gdbarch.h"
  30. /* You can have any number of hooks for `exec_file_command' command to
  31. call. If there's only one hook, it is set in exec_file_display
  32. hook. If there are two or more hooks, they are set in
  33. exec_file_extra_hooks[], and deprecated_exec_file_display_hook is
  34. set to a function that calls all of them. This extra complexity is
  35. needed to preserve compatibility with old code that assumed that
  36. only one hook could be set, and which called
  37. deprecated_exec_file_display_hook directly. */
  38. typedef void (*hook_type) (const char *);
  39. hook_type deprecated_exec_file_display_hook; /* The original hook. */
  40. static hook_type *exec_file_extra_hooks; /* Array of additional
  41. hooks. */
  42. static int exec_file_hook_count = 0; /* Size of array. */
  43. /* If there are two or more functions that wish to hook into
  44. exec_file_command, this function will call all of the hook
  45. functions. */
  46. static void
  47. call_extra_exec_file_hooks (const char *filename)
  48. {
  49. int i;
  50. for (i = 0; i < exec_file_hook_count; i++)
  51. (*exec_file_extra_hooks[i]) (filename);
  52. }
  53. /* Call this to specify the hook for exec_file_command to call back.
  54. This is called from the x-window display code. */
  55. void
  56. specify_exec_file_hook (void (*hook) (const char *))
  57. {
  58. hook_type *new_array;
  59. if (deprecated_exec_file_display_hook != NULL)
  60. {
  61. /* There's already a hook installed. Arrange to have both it
  62. and the subsequent hooks called. */
  63. if (exec_file_hook_count == 0)
  64. {
  65. /* If this is the first extra hook, initialize the hook
  66. array. */
  67. exec_file_extra_hooks = XNEW (hook_type);
  68. exec_file_extra_hooks[0] = deprecated_exec_file_display_hook;
  69. deprecated_exec_file_display_hook = call_extra_exec_file_hooks;
  70. exec_file_hook_count = 1;
  71. }
  72. /* Grow the hook array by one and add the new hook to the end.
  73. Yes, it's inefficient to grow it by one each time but since
  74. this is hardly ever called it's not a big deal. */
  75. exec_file_hook_count++;
  76. new_array = (hook_type *)
  77. xrealloc (exec_file_extra_hooks,
  78. exec_file_hook_count * sizeof (hook_type));
  79. exec_file_extra_hooks = new_array;
  80. exec_file_extra_hooks[exec_file_hook_count - 1] = hook;
  81. }
  82. else
  83. deprecated_exec_file_display_hook = hook;
  84. }
  85. void
  86. reopen_exec_file (void)
  87. {
  88. int res;
  89. struct stat st;
  90. /* Don't do anything if there isn't an exec file. */
  91. if (current_program_space->exec_bfd () == NULL)
  92. return;
  93. /* If the timestamp of the exec file has changed, reopen it. */
  94. std::string filename = bfd_get_filename (current_program_space->exec_bfd ());
  95. res = stat (filename.c_str (), &st);
  96. if (res == 0
  97. && current_program_space->ebfd_mtime
  98. && current_program_space->ebfd_mtime != st.st_mtime)
  99. exec_file_attach (filename.c_str (), 0);
  100. else
  101. /* If we accessed the file since last opening it, close it now;
  102. this stops GDB from holding the executable open after it
  103. exits. */
  104. bfd_cache_close_all ();
  105. }
  106. /* If we have both a core file and an exec file,
  107. print a warning if they don't go together. */
  108. void
  109. validate_files (void)
  110. {
  111. if (current_program_space->exec_bfd () && core_bfd)
  112. {
  113. if (!core_file_matches_executable_p (core_bfd,
  114. current_program_space->exec_bfd ()))
  115. warning (_("core file may not match specified executable file."));
  116. else if (bfd_get_mtime (current_program_space->exec_bfd ())
  117. > bfd_get_mtime (core_bfd))
  118. warning (_("exec file is newer than core file."));
  119. }
  120. }
  121. /* See gdbsupport/common-inferior.h. */
  122. const char *
  123. get_exec_file (int err)
  124. {
  125. if (current_program_space->exec_filename != nullptr)
  126. return current_program_space->exec_filename.get ();
  127. if (!err)
  128. return NULL;
  129. error (_("No executable file specified.\n\
  130. Use the \"file\" or \"exec-file\" command."));
  131. }
  132. std::string
  133. memory_error_message (enum target_xfer_status err,
  134. struct gdbarch *gdbarch, CORE_ADDR memaddr)
  135. {
  136. switch (err)
  137. {
  138. case TARGET_XFER_E_IO:
  139. /* Actually, address between memaddr and memaddr + len was out of
  140. bounds. */
  141. return string_printf (_("Cannot access memory at address %s"),
  142. paddress (gdbarch, memaddr));
  143. case TARGET_XFER_UNAVAILABLE:
  144. return string_printf (_("Memory at address %s unavailable."),
  145. paddress (gdbarch, memaddr));
  146. default:
  147. internal_error (__FILE__, __LINE__,
  148. "unhandled target_xfer_status: %s (%s)",
  149. target_xfer_status_to_string (err),
  150. plongest (err));
  151. }
  152. }
  153. /* Report a memory error by throwing a suitable exception. */
  154. void
  155. memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
  156. {
  157. enum errors exception = GDB_NO_ERROR;
  158. /* Build error string. */
  159. std::string str = memory_error_message (err, target_gdbarch (), memaddr);
  160. /* Choose the right error to throw. */
  161. switch (err)
  162. {
  163. case TARGET_XFER_E_IO:
  164. exception = MEMORY_ERROR;
  165. break;
  166. case TARGET_XFER_UNAVAILABLE:
  167. exception = NOT_AVAILABLE_ERROR;
  168. break;
  169. }
  170. /* Throw it. */
  171. throw_error (exception, ("%s"), str.c_str ());
  172. }
  173. /* Helper function. */
  174. static void
  175. read_memory_object (enum target_object object, CORE_ADDR memaddr,
  176. gdb_byte *myaddr, ssize_t len)
  177. {
  178. ULONGEST xfered = 0;
  179. while (xfered < len)
  180. {
  181. enum target_xfer_status status;
  182. ULONGEST xfered_len;
  183. status = target_xfer_partial (current_inferior ()->top_target (), object,
  184. NULL, myaddr + xfered, NULL,
  185. memaddr + xfered, len - xfered,
  186. &xfered_len);
  187. if (status != TARGET_XFER_OK)
  188. memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
  189. memaddr + xfered);
  190. xfered += xfered_len;
  191. QUIT;
  192. }
  193. }
  194. /* Same as target_read_memory, but report an error if can't read. */
  195. void
  196. read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  197. {
  198. read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len);
  199. }
  200. /* Same as target_read_stack, but report an error if can't read. */
  201. void
  202. read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  203. {
  204. read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len);
  205. }
  206. /* Same as target_read_code, but report an error if can't read. */
  207. void
  208. read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  209. {
  210. read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len);
  211. }
  212. /* Read memory at MEMADDR of length LEN and put the contents in
  213. RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
  214. if successful. */
  215. int
  216. safe_read_memory_integer (CORE_ADDR memaddr, int len,
  217. enum bfd_endian byte_order,
  218. LONGEST *return_value)
  219. {
  220. gdb_byte buf[sizeof (LONGEST)];
  221. if (target_read_memory (memaddr, buf, len))
  222. return 0;
  223. *return_value = extract_signed_integer (buf, len, byte_order);
  224. return 1;
  225. }
  226. /* Read memory at MEMADDR of length LEN and put the contents in
  227. RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
  228. if successful. */
  229. int
  230. safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
  231. enum bfd_endian byte_order,
  232. ULONGEST *return_value)
  233. {
  234. gdb_byte buf[sizeof (ULONGEST)];
  235. if (target_read_memory (memaddr, buf, len))
  236. return 0;
  237. *return_value = extract_unsigned_integer (buf, len, byte_order);
  238. return 1;
  239. }
  240. LONGEST
  241. read_memory_integer (CORE_ADDR memaddr, int len,
  242. enum bfd_endian byte_order)
  243. {
  244. gdb_byte buf[sizeof (LONGEST)];
  245. read_memory (memaddr, buf, len);
  246. return extract_signed_integer (buf, len, byte_order);
  247. }
  248. ULONGEST
  249. read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
  250. enum bfd_endian byte_order)
  251. {
  252. gdb_byte buf[sizeof (ULONGEST)];
  253. read_memory (memaddr, buf, len);
  254. return extract_unsigned_integer (buf, len, byte_order);
  255. }
  256. LONGEST
  257. read_code_integer (CORE_ADDR memaddr, int len,
  258. enum bfd_endian byte_order)
  259. {
  260. gdb_byte buf[sizeof (LONGEST)];
  261. read_code (memaddr, buf, len);
  262. return extract_signed_integer (buf, len, byte_order);
  263. }
  264. ULONGEST
  265. read_code_unsigned_integer (CORE_ADDR memaddr, int len,
  266. enum bfd_endian byte_order)
  267. {
  268. gdb_byte buf[sizeof (ULONGEST)];
  269. read_code (memaddr, buf, len);
  270. return extract_unsigned_integer (buf, len, byte_order);
  271. }
  272. CORE_ADDR
  273. read_memory_typed_address (CORE_ADDR addr, struct type *type)
  274. {
  275. gdb_byte *buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
  276. read_memory (addr, buf, TYPE_LENGTH (type));
  277. return extract_typed_address (buf, type);
  278. }
  279. /* See gdbcore.h. */
  280. void
  281. write_memory (CORE_ADDR memaddr,
  282. const bfd_byte *myaddr, ssize_t len)
  283. {
  284. int status;
  285. status = target_write_memory (memaddr, myaddr, len);
  286. if (status != 0)
  287. memory_error (TARGET_XFER_E_IO, memaddr);
  288. }
  289. /* Same as write_memory, but notify 'memory_changed' observers. */
  290. void
  291. write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
  292. ssize_t len)
  293. {
  294. write_memory (memaddr, myaddr, len);
  295. gdb::observers::memory_changed.notify (current_inferior (), memaddr, len, myaddr);
  296. }
  297. /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
  298. integer. */
  299. void
  300. write_memory_unsigned_integer (CORE_ADDR addr, int len,
  301. enum bfd_endian byte_order,
  302. ULONGEST value)
  303. {
  304. gdb_byte *buf = (gdb_byte *) alloca (len);
  305. store_unsigned_integer (buf, len, byte_order, value);
  306. write_memory (addr, buf, len);
  307. }
  308. /* Store VALUE at ADDR in the inferior as a LEN-byte signed
  309. integer. */
  310. void
  311. write_memory_signed_integer (CORE_ADDR addr, int len,
  312. enum bfd_endian byte_order,
  313. LONGEST value)
  314. {
  315. gdb_byte *buf = (gdb_byte *) alloca (len);
  316. store_signed_integer (buf, len, byte_order, value);
  317. write_memory (addr, buf, len);
  318. }
  319. /* The current default bfd target. Points to storage allocated for
  320. gnutarget_string. */
  321. const char *gnutarget;
  322. /* Same thing, except it is "auto" not NULL for the default case. */
  323. static std::string gnutarget_string;
  324. static void
  325. show_gnutarget_string (struct ui_file *file, int from_tty,
  326. struct cmd_list_element *c,
  327. const char *value)
  328. {
  329. gdb_printf (file,
  330. _("The current BFD target is \"%s\".\n"), value);
  331. }
  332. static void
  333. set_gnutarget_command (const char *ignore, int from_tty,
  334. struct cmd_list_element *c)
  335. {
  336. const char *gend = gnutarget_string.c_str () + gnutarget_string.size ();
  337. gend = remove_trailing_whitespace (gnutarget_string.c_str (), gend);
  338. gnutarget_string
  339. = gnutarget_string.substr (0, gend - gnutarget_string.data ());
  340. if (gnutarget_string == "auto")
  341. gnutarget = NULL;
  342. else
  343. gnutarget = gnutarget_string.c_str ();
  344. }
  345. /* A completion function for "set gnutarget". */
  346. static void
  347. complete_set_gnutarget (struct cmd_list_element *cmd,
  348. completion_tracker &tracker,
  349. const char *text, const char *word)
  350. {
  351. static const char **bfd_targets;
  352. if (bfd_targets == NULL)
  353. {
  354. int last;
  355. bfd_targets = bfd_target_list ();
  356. for (last = 0; bfd_targets[last] != NULL; ++last)
  357. ;
  358. bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2);
  359. bfd_targets[last] = "auto";
  360. bfd_targets[last + 1] = NULL;
  361. }
  362. complete_on_enum (tracker, bfd_targets, text, word);
  363. }
  364. /* Set the gnutarget. */
  365. void
  366. set_gnutarget (const char *newtarget)
  367. {
  368. gnutarget_string = newtarget;
  369. set_gnutarget_command (NULL, 0, NULL);
  370. }
  371. void _initialize_core ();
  372. void
  373. _initialize_core ()
  374. {
  375. cmd_list_element *core_file_cmd
  376. = add_cmd ("core-file", class_files, core_file_command, _("\
  377. Use FILE as core dump for examining memory and registers.\n\
  378. Usage: core-file FILE\n\
  379. No arg means have no core file. This command has been superseded by the\n\
  380. `target core' and `detach' commands."), &cmdlist);
  381. set_cmd_completer (core_file_cmd, filename_completer);
  382. set_show_commands set_show_gnutarget
  383. = add_setshow_string_noescape_cmd ("gnutarget", class_files,
  384. &gnutarget_string, _("\
  385. Set the current BFD target."), _("\
  386. Show the current BFD target."), _("\
  387. Use `set gnutarget auto' to specify automatic detection."),
  388. set_gnutarget_command,
  389. show_gnutarget_string,
  390. &setlist, &showlist);
  391. set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget);
  392. add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist);
  393. if (getenv ("GNUTARGET"))
  394. set_gnutarget (getenv ("GNUTARGET"));
  395. else
  396. set_gnutarget ("auto");
  397. }