Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Core dump and executable file functions above target vector, for GDB. |
1bac305b | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1996, 1997, 1998, |
7b6bb8da | 4 | 1999, 2000, 2001, 2003, 2006, 2007, 2008, 2009, 2010, 2011 |
0fb0cc75 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "gdb_string.h" | |
24 | #include <errno.h> | |
25 | #include <signal.h> | |
26 | #include <fcntl.h> | |
c906108c SS |
27 | #include "inferior.h" |
28 | #include "symtab.h" | |
29 | #include "command.h" | |
30 | #include "gdbcmd.h" | |
31 | #include "bfd.h" | |
32 | #include "target.h" | |
33 | #include "gdbcore.h" | |
34 | #include "dis-asm.h" | |
c906108c | 35 | #include "gdb_stat.h" |
d75b5104 | 36 | #include "completer.h" |
60250e8b | 37 | #include "exceptions.h" |
c906108c | 38 | |
c906108c SS |
39 | /* Local function declarations. */ |
40 | ||
a14ed312 KB |
41 | extern void _initialize_core (void); |
42 | static void call_extra_exec_file_hooks (char *filename); | |
c906108c | 43 | |
9a4105ab AC |
44 | /* You can have any number of hooks for `exec_file_command' command to |
45 | call. If there's only one hook, it is set in exec_file_display | |
46 | hook. If there are two or more hooks, they are set in | |
47 | exec_file_extra_hooks[], and deprecated_exec_file_display_hook is | |
48 | set to a function that calls all of them. This extra complexity is | |
49 | needed to preserve compatibility with old code that assumed that | |
50 | only one hook could be set, and which called | |
51 | deprecated_exec_file_display_hook directly. */ | |
c906108c | 52 | |
507f3c78 | 53 | typedef void (*hook_type) (char *); |
c906108c | 54 | |
aff410f1 MS |
55 | hook_type deprecated_exec_file_display_hook; /* The original hook. */ |
56 | static hook_type *exec_file_extra_hooks; /* Array of additional | |
57 | hooks. */ | |
58 | static int exec_file_hook_count = 0; /* Size of array. */ | |
c906108c SS |
59 | |
60 | /* Binary file diddling handle for the core file. */ | |
61 | ||
62 | bfd *core_bfd = NULL; | |
c0edd9ed JK |
63 | |
64 | /* corelow.c target (if included for this gdb target). */ | |
65 | ||
66 | struct target_ops *core_target; | |
c906108c | 67 | \f |
c5aa993b | 68 | |
c906108c SS |
69 | /* Backward compatability with old way of specifying core files. */ |
70 | ||
71 | void | |
fba45db2 | 72 | core_file_command (char *filename, int from_tty) |
c906108c | 73 | { |
aff410f1 | 74 | dont_repeat (); /* Either way, seems bogus. */ |
c906108c | 75 | |
c0edd9ed | 76 | if (core_target == NULL) |
8a3fe4f8 | 77 | error (_("GDB can't read core files on this machine.")); |
46c6cdcf C |
78 | |
79 | if (!filename) | |
c0edd9ed | 80 | (core_target->to_detach) (core_target, filename, from_tty); |
46c6cdcf | 81 | else |
c0edd9ed | 82 | (core_target->to_open) (filename, from_tty); |
c906108c | 83 | } |
c906108c | 84 | \f |
c5aa993b | 85 | |
de6854b5 MS |
86 | /* If there are two or more functions that wish to hook into |
87 | exec_file_command, this function will call all of the hook | |
88 | functions. */ | |
c906108c SS |
89 | |
90 | static void | |
fba45db2 | 91 | call_extra_exec_file_hooks (char *filename) |
c906108c SS |
92 | { |
93 | int i; | |
94 | ||
95 | for (i = 0; i < exec_file_hook_count; i++) | |
c5aa993b | 96 | (*exec_file_extra_hooks[i]) (filename); |
c906108c SS |
97 | } |
98 | ||
99 | /* Call this to specify the hook for exec_file_command to call back. | |
100 | This is called from the x-window display code. */ | |
101 | ||
102 | void | |
dbb41be1 | 103 | specify_exec_file_hook (void (*hook) (char *)) |
c906108c SS |
104 | { |
105 | hook_type *new_array; | |
106 | ||
9a4105ab | 107 | if (deprecated_exec_file_display_hook != NULL) |
c906108c SS |
108 | { |
109 | /* There's already a hook installed. Arrange to have both it | |
aff410f1 | 110 | and the subsequent hooks called. */ |
c906108c SS |
111 | if (exec_file_hook_count == 0) |
112 | { | |
aff410f1 MS |
113 | /* If this is the first extra hook, initialize the hook |
114 | array. */ | |
115 | exec_file_extra_hooks = (hook_type *) | |
116 | xmalloc (sizeof (hook_type)); | |
9a4105ab AC |
117 | exec_file_extra_hooks[0] = deprecated_exec_file_display_hook; |
118 | deprecated_exec_file_display_hook = call_extra_exec_file_hooks; | |
c906108c SS |
119 | exec_file_hook_count = 1; |
120 | } | |
121 | ||
122 | /* Grow the hook array by one and add the new hook to the end. | |
123 | Yes, it's inefficient to grow it by one each time but since | |
124 | this is hardly ever called it's not a big deal. */ | |
125 | exec_file_hook_count++; | |
aff410f1 MS |
126 | new_array = (hook_type *) |
127 | xrealloc (exec_file_extra_hooks, | |
128 | exec_file_hook_count * sizeof (hook_type)); | |
c906108c SS |
129 | exec_file_extra_hooks = new_array; |
130 | exec_file_extra_hooks[exec_file_hook_count - 1] = hook; | |
131 | } | |
132 | else | |
9a4105ab | 133 | deprecated_exec_file_display_hook = hook; |
c906108c SS |
134 | } |
135 | ||
136 | /* The exec file must be closed before running an inferior. | |
137 | If it is needed again after the inferior dies, it must | |
138 | be reopened. */ | |
139 | ||
140 | void | |
fba45db2 | 141 | close_exec_file (void) |
c906108c | 142 | { |
c5aa993b | 143 | #if 0 /* FIXME */ |
c906108c SS |
144 | if (exec_bfd) |
145 | bfd_tempclose (exec_bfd); | |
146 | #endif | |
147 | } | |
148 | ||
149 | void | |
fba45db2 | 150 | reopen_exec_file (void) |
c906108c | 151 | { |
c5aa993b | 152 | #if 0 /* FIXME */ |
c906108c SS |
153 | if (exec_bfd) |
154 | bfd_reopen (exec_bfd); | |
155 | #else | |
156 | char *filename; | |
157 | int res; | |
158 | struct stat st; | |
f7545552 | 159 | struct cleanup *cleanups; |
c906108c | 160 | |
aff410f1 | 161 | /* Don't do anything if there isn't an exec file. */ |
4c42eaff | 162 | if (exec_bfd == NULL) |
c906108c | 163 | return; |
c5aa993b | 164 | |
aff410f1 | 165 | /* If the timestamp of the exec file has changed, reopen it. */ |
c2d11a7d | 166 | filename = xstrdup (bfd_get_filename (exec_bfd)); |
f7545552 | 167 | cleanups = make_cleanup (xfree, filename); |
c906108c SS |
168 | res = stat (filename, &st); |
169 | ||
c04ea773 | 170 | if (exec_bfd_mtime && exec_bfd_mtime != st.st_mtime) |
4c42eaff | 171 | exec_file_attach (filename, 0); |
939643d7 DJ |
172 | else |
173 | /* If we accessed the file since last opening it, close it now; | |
174 | this stops GDB from holding the executable open after it | |
175 | exits. */ | |
176 | bfd_cache_close_all (); | |
f7545552 TT |
177 | |
178 | do_cleanups (cleanups); | |
c906108c SS |
179 | #endif |
180 | } | |
181 | \f | |
182 | /* If we have both a core file and an exec file, | |
183 | print a warning if they don't go together. */ | |
184 | ||
185 | void | |
fba45db2 | 186 | validate_files (void) |
c906108c SS |
187 | { |
188 | if (exec_bfd && core_bfd) | |
189 | { | |
190 | if (!core_file_matches_executable_p (core_bfd, exec_bfd)) | |
8a3fe4f8 | 191 | warning (_("core file may not match specified executable file.")); |
c5aa993b | 192 | else if (bfd_get_mtime (exec_bfd) > bfd_get_mtime (core_bfd)) |
8a3fe4f8 | 193 | warning (_("exec file is newer than core file.")); |
c906108c SS |
194 | } |
195 | } | |
196 | ||
197 | /* Return the name of the executable file as a string. | |
198 | ERR nonzero means get error if there is none specified; | |
199 | otherwise return 0 in that case. */ | |
200 | ||
201 | char * | |
fba45db2 | 202 | get_exec_file (int err) |
c906108c | 203 | { |
c5aa993b JM |
204 | if (exec_bfd) |
205 | return bfd_get_filename (exec_bfd); | |
206 | if (!err) | |
207 | return NULL; | |
c906108c | 208 | |
8a3fe4f8 AC |
209 | error (_("No executable file specified.\n\ |
210 | Use the \"file\" or \"exec-file\" command.")); | |
c906108c SS |
211 | return NULL; |
212 | } | |
c906108c | 213 | \f |
c5aa993b | 214 | |
ccc57cf9 | 215 | /* Report a memory error by throwing a MEMORY_ERROR error. */ |
c906108c SS |
216 | |
217 | void | |
fba45db2 | 218 | memory_error (int status, CORE_ADDR memaddr) |
c906108c SS |
219 | { |
220 | if (status == EIO) | |
ccc57cf9 PA |
221 | /* Actually, address between memaddr and memaddr + len was out of |
222 | bounds. */ | |
223 | throw_error (MEMORY_ERROR, | |
224 | _("Cannot access memory at address %s"), | |
5af949e3 | 225 | paddress (target_gdbarch, memaddr)); |
c906108c | 226 | else |
ccc57cf9 PA |
227 | throw_error (MEMORY_ERROR, |
228 | _("Error accessing memory address %s: %s."), | |
5af949e3 | 229 | paddress (target_gdbarch, memaddr), |
ccc57cf9 | 230 | safe_strerror (status)); |
c906108c SS |
231 | } |
232 | ||
233 | /* Same as target_read_memory, but report an error if can't read. */ | |
4e5d721f | 234 | |
c906108c | 235 | void |
10c42a71 | 236 | read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len) |
c906108c SS |
237 | { |
238 | int status; | |
c5504eaf | 239 | |
c906108c SS |
240 | status = target_read_memory (memaddr, myaddr, len); |
241 | if (status != 0) | |
242 | memory_error (status, memaddr); | |
243 | } | |
244 | ||
4e5d721f DE |
245 | /* Same as target_read_stack, but report an error if can't read. */ |
246 | ||
247 | void | |
248 | read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len) | |
249 | { | |
250 | int status; | |
c5504eaf | 251 | |
4e5d721f DE |
252 | status = target_read_stack (memaddr, myaddr, len); |
253 | if (status != 0) | |
254 | memory_error (status, memaddr); | |
255 | } | |
256 | ||
ee8ff470 KB |
257 | /* Argument / return result struct for use with |
258 | do_captured_read_memory_integer(). MEMADDR and LEN are filled in | |
259 | by gdb_read_memory_integer(). RESULT is the contents that were | |
260 | successfully read from MEMADDR of length LEN. */ | |
c906108c | 261 | |
16a0f3e7 EZ |
262 | struct captured_read_memory_integer_arguments |
263 | { | |
264 | CORE_ADDR memaddr; | |
265 | int len; | |
e17a4113 | 266 | enum bfd_endian byte_order; |
16a0f3e7 EZ |
267 | LONGEST result; |
268 | }; | |
269 | ||
ee8ff470 | 270 | /* Helper function for gdb_read_memory_integer(). DATA must be a |
aff410f1 | 271 | pointer to a captured_read_memory_integer_arguments struct. |
ee8ff470 KB |
272 | Return 1 if successful. Note that the catch_errors() interface |
273 | will return 0 if an error occurred while reading memory. This | |
274 | choice of return code is so that we can distinguish between | |
275 | success and failure. */ | |
276 | ||
16a0f3e7 EZ |
277 | static int |
278 | do_captured_read_memory_integer (void *data) | |
279 | { | |
aff410f1 MS |
280 | struct captured_read_memory_integer_arguments *args |
281 | = (struct captured_read_memory_integer_arguments*) data; | |
16a0f3e7 EZ |
282 | CORE_ADDR memaddr = args->memaddr; |
283 | int len = args->len; | |
e17a4113 | 284 | enum bfd_endian byte_order = args->byte_order; |
16a0f3e7 | 285 | |
e17a4113 | 286 | args->result = read_memory_integer (memaddr, len, byte_order); |
16a0f3e7 | 287 | |
ee8ff470 | 288 | return 1; |
16a0f3e7 EZ |
289 | } |
290 | ||
ee8ff470 KB |
291 | /* Read memory at MEMADDR of length LEN and put the contents in |
292 | RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero | |
293 | if successful. */ | |
294 | ||
16a0f3e7 | 295 | int |
c5504eaf MS |
296 | safe_read_memory_integer (CORE_ADDR memaddr, int len, |
297 | enum bfd_endian byte_order, | |
e17a4113 | 298 | LONGEST *return_value) |
16a0f3e7 EZ |
299 | { |
300 | int status; | |
301 | struct captured_read_memory_integer_arguments args; | |
c5504eaf | 302 | |
16a0f3e7 EZ |
303 | args.memaddr = memaddr; |
304 | args.len = len; | |
e17a4113 | 305 | args.byte_order = byte_order; |
16a0f3e7 EZ |
306 | |
307 | status = catch_errors (do_captured_read_memory_integer, &args, | |
aff410f1 | 308 | "", RETURN_MASK_ALL); |
ee8ff470 | 309 | if (status) |
16a0f3e7 EZ |
310 | *return_value = args.result; |
311 | ||
312 | return status; | |
313 | } | |
314 | ||
c906108c | 315 | LONGEST |
aff410f1 MS |
316 | read_memory_integer (CORE_ADDR memaddr, int len, |
317 | enum bfd_endian byte_order) | |
c906108c | 318 | { |
dfb65433 | 319 | gdb_byte buf[sizeof (LONGEST)]; |
c906108c SS |
320 | |
321 | read_memory (memaddr, buf, len); | |
e17a4113 | 322 | return extract_signed_integer (buf, len, byte_order); |
c906108c SS |
323 | } |
324 | ||
325 | ULONGEST | |
aff410f1 MS |
326 | read_memory_unsigned_integer (CORE_ADDR memaddr, int len, |
327 | enum bfd_endian byte_order) | |
c906108c | 328 | { |
dfb65433 | 329 | gdb_byte buf[sizeof (ULONGEST)]; |
c906108c SS |
330 | |
331 | read_memory (memaddr, buf, len); | |
e17a4113 | 332 | return extract_unsigned_integer (buf, len, byte_order); |
c906108c SS |
333 | } |
334 | ||
335 | void | |
fba45db2 | 336 | read_memory_string (CORE_ADDR memaddr, char *buffer, int max_len) |
c906108c | 337 | { |
52f0bd74 AC |
338 | char *cp; |
339 | int i; | |
c906108c SS |
340 | int cnt; |
341 | ||
342 | cp = buffer; | |
343 | while (1) | |
344 | { | |
345 | if (cp - buffer >= max_len) | |
c5aa993b JM |
346 | { |
347 | buffer[max_len - 1] = '\0'; | |
348 | break; | |
349 | } | |
c906108c SS |
350 | cnt = max_len - (cp - buffer); |
351 | if (cnt > 8) | |
352 | cnt = 8; | |
353 | read_memory (memaddr + (int) (cp - buffer), cp, cnt); | |
354 | for (i = 0; i < cnt && *cp; i++, cp++) | |
c5aa993b | 355 | ; /* null body */ |
c906108c SS |
356 | |
357 | if (i < cnt && !*cp) | |
c5aa993b | 358 | break; |
c906108c SS |
359 | } |
360 | } | |
c26e4683 | 361 | |
0d540cdf KD |
362 | CORE_ADDR |
363 | read_memory_typed_address (CORE_ADDR addr, struct type *type) | |
364 | { | |
dfb65433 | 365 | gdb_byte *buf = alloca (TYPE_LENGTH (type)); |
c5504eaf | 366 | |
0d540cdf KD |
367 | read_memory (addr, buf, TYPE_LENGTH (type)); |
368 | return extract_typed_address (buf, type); | |
369 | } | |
370 | ||
aff410f1 MS |
371 | /* Same as target_write_memory, but report an error if can't |
372 | write. */ | |
c26e4683 | 373 | void |
aff410f1 MS |
374 | write_memory (CORE_ADDR memaddr, |
375 | const bfd_byte *myaddr, int len) | |
c26e4683 JB |
376 | { |
377 | int status; | |
c5504eaf | 378 | |
00630ca8 | 379 | status = target_write_memory (memaddr, myaddr, len); |
c26e4683 JB |
380 | if (status != 0) |
381 | memory_error (status, memaddr); | |
382 | } | |
383 | ||
aff410f1 MS |
384 | /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned |
385 | integer. */ | |
c26e4683 | 386 | void |
c5504eaf MS |
387 | write_memory_unsigned_integer (CORE_ADDR addr, int len, |
388 | enum bfd_endian byte_order, | |
e17a4113 | 389 | ULONGEST value) |
c26e4683 | 390 | { |
dfb65433 | 391 | gdb_byte *buf = alloca (len); |
c5504eaf | 392 | |
e17a4113 | 393 | store_unsigned_integer (buf, len, byte_order, value); |
c26e4683 JB |
394 | write_memory (addr, buf, len); |
395 | } | |
396 | ||
aff410f1 MS |
397 | /* Store VALUE at ADDR in the inferior as a LEN-byte signed |
398 | integer. */ | |
c26e4683 | 399 | void |
c5504eaf MS |
400 | write_memory_signed_integer (CORE_ADDR addr, int len, |
401 | enum bfd_endian byte_order, | |
e17a4113 | 402 | LONGEST value) |
c26e4683 | 403 | { |
dfb65433 | 404 | gdb_byte *buf = alloca (len); |
c5504eaf | 405 | |
e17a4113 | 406 | store_signed_integer (buf, len, byte_order, value); |
c26e4683 JB |
407 | write_memory (addr, buf, len); |
408 | } | |
c906108c SS |
409 | \f |
410 | /* The current default bfd target. Points to storage allocated for | |
411 | gnutarget_string. */ | |
412 | char *gnutarget; | |
413 | ||
414 | /* Same thing, except it is "auto" not NULL for the default case. */ | |
415 | static char *gnutarget_string; | |
920d2a44 AC |
416 | static void |
417 | show_gnutarget_string (struct ui_file *file, int from_tty, | |
aff410f1 MS |
418 | struct cmd_list_element *c, |
419 | const char *value) | |
920d2a44 | 420 | { |
aff410f1 MS |
421 | fprintf_filtered (file, |
422 | _("The current BFD target is \"%s\".\n"), value); | |
920d2a44 | 423 | } |
c906108c | 424 | |
aff410f1 MS |
425 | static void set_gnutarget_command (char *, int, |
426 | struct cmd_list_element *); | |
c906108c SS |
427 | |
428 | static void | |
aff410f1 MS |
429 | set_gnutarget_command (char *ignore, int from_tty, |
430 | struct cmd_list_element *c) | |
c906108c | 431 | { |
bde58177 | 432 | if (strcmp (gnutarget_string, "auto") == 0) |
c906108c SS |
433 | gnutarget = NULL; |
434 | else | |
435 | gnutarget = gnutarget_string; | |
436 | } | |
437 | ||
438 | /* Set the gnutarget. */ | |
439 | void | |
fba45db2 | 440 | set_gnutarget (char *newtarget) |
c906108c SS |
441 | { |
442 | if (gnutarget_string != NULL) | |
b8c9b27d | 443 | xfree (gnutarget_string); |
1b36a34b | 444 | gnutarget_string = xstrdup (newtarget); |
c906108c SS |
445 | set_gnutarget_command (NULL, 0, NULL); |
446 | } | |
447 | ||
448 | void | |
fba45db2 | 449 | _initialize_core (void) |
c906108c SS |
450 | { |
451 | struct cmd_list_element *c; | |
c5504eaf | 452 | |
1a966eab AC |
453 | c = add_cmd ("core-file", class_files, core_file_command, _("\ |
454 | Use FILE as core dump for examining memory and registers.\n\ | |
c906108c | 455 | No arg means have no core file. This command has been superseded by the\n\ |
1a966eab | 456 | `target core' and `detach' commands."), &cmdlist); |
5ba2abeb | 457 | set_cmd_completer (c, filename_completer); |
c906108c | 458 | |
26c41df3 AC |
459 | |
460 | add_setshow_string_noescape_cmd ("gnutarget", class_files, | |
f1648876 | 461 | &gnutarget_string, _("\ |
26c41df3 AC |
462 | Set the current BFD target."), _("\ |
463 | Show the current BFD target."), _("\ | |
464 | Use `set gnutarget auto' to specify automatic detection."), | |
465 | set_gnutarget_command, | |
920d2a44 | 466 | show_gnutarget_string, |
26c41df3 | 467 | &setlist, &showlist); |
c906108c SS |
468 | |
469 | if (getenv ("GNUTARGET")) | |
470 | set_gnutarget (getenv ("GNUTARGET")); | |
471 | else | |
472 | set_gnutarget ("auto"); | |
473 | } |