Fix native follow-exec-mode "new"
[deliverable/binutils-gdb.git] / gdb / corefile.c
CommitLineData
c906108c 1/* Core dump and executable file functions above target vector, for GDB.
1bac305b 2
32d0add0 3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
c906108c
SS
21#include <signal.h>
22#include <fcntl.h>
c906108c
SS
23#include "inferior.h"
24#include "symtab.h"
25#include "command.h"
26#include "gdbcmd.h"
27#include "bfd.h"
28#include "target.h"
29#include "gdbcore.h"
30#include "dis-asm.h"
53ce3c39 31#include <sys/stat.h>
d75b5104 32#include "completer.h"
972daa01 33#include "observer.h"
44478ab3 34#include "cli/cli-utils.h"
c906108c 35
c906108c
SS
36/* Local function declarations. */
37
a14ed312 38extern void _initialize_core (void);
c906108c 39
9a4105ab
AC
40/* You can have any number of hooks for `exec_file_command' command to
41 call. If there's only one hook, it is set in exec_file_display
42 hook. If there are two or more hooks, they are set in
43 exec_file_extra_hooks[], and deprecated_exec_file_display_hook is
44 set to a function that calls all of them. This extra complexity is
45 needed to preserve compatibility with old code that assumed that
46 only one hook could be set, and which called
47 deprecated_exec_file_display_hook directly. */
c906108c 48
5f08566b 49typedef void (*hook_type) (const char *);
c906108c 50
aff410f1
MS
51hook_type deprecated_exec_file_display_hook; /* The original hook. */
52static hook_type *exec_file_extra_hooks; /* Array of additional
53 hooks. */
54static int exec_file_hook_count = 0; /* Size of array. */
c906108c
SS
55
56/* Binary file diddling handle for the core file. */
57
58bfd *core_bfd = NULL;
c0edd9ed 59
06b9f45f 60/* corelow.c target. It is never NULL after GDB initialization. */
c0edd9ed
JK
61
62struct target_ops *core_target;
c906108c 63\f
c5aa993b 64
c906108c
SS
65/* Backward compatability with old way of specifying core files. */
66
67void
fba45db2 68core_file_command (char *filename, int from_tty)
c906108c 69{
aff410f1 70 dont_repeat (); /* Either way, seems bogus. */
c906108c 71
06b9f45f 72 gdb_assert (core_target != NULL);
46c6cdcf
C
73
74 if (!filename)
c0edd9ed 75 (core_target->to_detach) (core_target, filename, from_tty);
46c6cdcf 76 else
c0edd9ed 77 (core_target->to_open) (filename, from_tty);
c906108c 78}
c906108c 79\f
c5aa993b 80
de6854b5
MS
81/* If there are two or more functions that wish to hook into
82 exec_file_command, this function will call all of the hook
83 functions. */
c906108c
SS
84
85static void
5f08566b 86call_extra_exec_file_hooks (const char *filename)
c906108c
SS
87{
88 int i;
89
90 for (i = 0; i < exec_file_hook_count; i++)
c5aa993b 91 (*exec_file_extra_hooks[i]) (filename);
c906108c
SS
92}
93
94/* Call this to specify the hook for exec_file_command to call back.
95 This is called from the x-window display code. */
96
97void
5f08566b 98specify_exec_file_hook (void (*hook) (const char *))
c906108c
SS
99{
100 hook_type *new_array;
101
9a4105ab 102 if (deprecated_exec_file_display_hook != NULL)
c906108c
SS
103 {
104 /* There's already a hook installed. Arrange to have both it
aff410f1 105 and the subsequent hooks called. */
c906108c
SS
106 if (exec_file_hook_count == 0)
107 {
aff410f1
MS
108 /* If this is the first extra hook, initialize the hook
109 array. */
110 exec_file_extra_hooks = (hook_type *)
111 xmalloc (sizeof (hook_type));
9a4105ab
AC
112 exec_file_extra_hooks[0] = deprecated_exec_file_display_hook;
113 deprecated_exec_file_display_hook = call_extra_exec_file_hooks;
c906108c
SS
114 exec_file_hook_count = 1;
115 }
116
117 /* Grow the hook array by one and add the new hook to the end.
118 Yes, it's inefficient to grow it by one each time but since
119 this is hardly ever called it's not a big deal. */
120 exec_file_hook_count++;
aff410f1
MS
121 new_array = (hook_type *)
122 xrealloc (exec_file_extra_hooks,
123 exec_file_hook_count * sizeof (hook_type));
c906108c
SS
124 exec_file_extra_hooks = new_array;
125 exec_file_extra_hooks[exec_file_hook_count - 1] = hook;
126 }
127 else
9a4105ab 128 deprecated_exec_file_display_hook = hook;
c906108c
SS
129}
130
c906108c 131void
fba45db2 132reopen_exec_file (void)
c906108c 133{
c906108c
SS
134 char *filename;
135 int res;
136 struct stat st;
f7545552 137 struct cleanup *cleanups;
c906108c 138
aff410f1 139 /* Don't do anything if there isn't an exec file. */
4c42eaff 140 if (exec_bfd == NULL)
c906108c 141 return;
c5aa993b 142
aff410f1 143 /* If the timestamp of the exec file has changed, reopen it. */
c2d11a7d 144 filename = xstrdup (bfd_get_filename (exec_bfd));
f7545552 145 cleanups = make_cleanup (xfree, filename);
c906108c
SS
146 res = stat (filename, &st);
147
c04ea773 148 if (exec_bfd_mtime && exec_bfd_mtime != st.st_mtime)
4c42eaff 149 exec_file_attach (filename, 0);
939643d7
DJ
150 else
151 /* If we accessed the file since last opening it, close it now;
152 this stops GDB from holding the executable open after it
153 exits. */
154 bfd_cache_close_all ();
f7545552
TT
155
156 do_cleanups (cleanups);
c906108c
SS
157}
158\f
159/* If we have both a core file and an exec file,
160 print a warning if they don't go together. */
161
162void
fba45db2 163validate_files (void)
c906108c
SS
164{
165 if (exec_bfd && core_bfd)
166 {
167 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
8a3fe4f8 168 warning (_("core file may not match specified executable file."));
c5aa993b 169 else if (bfd_get_mtime (exec_bfd) > bfd_get_mtime (core_bfd))
8a3fe4f8 170 warning (_("exec file is newer than core file."));
c906108c
SS
171 }
172}
173
174/* Return the name of the executable file as a string.
175 ERR nonzero means get error if there is none specified;
176 otherwise return 0 in that case. */
177
178char *
fba45db2 179get_exec_file (int err)
c906108c 180{
1f0c4988
JK
181 if (exec_filename)
182 return exec_filename;
c5aa993b
JM
183 if (!err)
184 return NULL;
c906108c 185
8a3fe4f8
AC
186 error (_("No executable file specified.\n\
187Use the \"file\" or \"exec-file\" command."));
c906108c
SS
188 return NULL;
189}
c906108c 190\f
c5aa993b 191
578d3588 192char *
9b409511 193memory_error_message (enum target_xfer_status err,
578d3588 194 struct gdbarch *gdbarch, CORE_ADDR memaddr)
6be7b56e
PA
195{
196 switch (err)
197 {
198 case TARGET_XFER_E_IO:
199 /* Actually, address between memaddr and memaddr + len was out of
200 bounds. */
578d3588
PA
201 return xstrprintf (_("Cannot access memory at address %s"),
202 paddress (gdbarch, memaddr));
bc113b4e 203 case TARGET_XFER_UNAVAILABLE:
578d3588
PA
204 return xstrprintf (_("Memory at address %s unavailable."),
205 paddress (gdbarch, memaddr));
6be7b56e
PA
206 default:
207 internal_error (__FILE__, __LINE__,
9b409511
YQ
208 "unhandled target_xfer_status: %s (%s)",
209 target_xfer_status_to_string (err),
6be7b56e
PA
210 plongest (err));
211 }
212}
213
578d3588 214/* Report a memory error by throwing a suitable exception. */
c906108c
SS
215
216void
9b409511 217memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
c906108c 218{
578d3588 219 char *str;
8635b3bf 220 enum errors exception = GDB_NO_ERROR;
578d3588
PA
221
222 /* Build error string. */
223 str = memory_error_message (err, target_gdbarch (), memaddr);
224 make_cleanup (xfree, str);
225
226 /* Choose the right error to throw. */
227 switch (err)
228 {
229 case TARGET_XFER_E_IO:
8635b3bf 230 exception = MEMORY_ERROR;
578d3588 231 break;
bc113b4e 232 case TARGET_XFER_UNAVAILABLE:
8635b3bf 233 exception = NOT_AVAILABLE_ERROR;
578d3588
PA
234 break;
235 }
236
237 /* Throw it. */
8635b3bf 238 throw_error (exception, ("%s"), str);
c906108c
SS
239}
240
241/* Same as target_read_memory, but report an error if can't read. */
4e5d721f 242
c906108c 243void
45aa4659 244read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
c906108c 245{
9b409511 246 ULONGEST xfered = 0;
c5504eaf 247
6be7b56e
PA
248 while (xfered < len)
249 {
9b409511
YQ
250 enum target_xfer_status status;
251 ULONGEST xfered_len;
6be7b56e 252
9b409511
YQ
253 status = target_xfer_partial (current_target.beneath,
254 TARGET_OBJECT_MEMORY, NULL,
255 myaddr + xfered, NULL,
256 memaddr + xfered, len - xfered,
257 &xfered_len);
258
5c328c05
YQ
259 if (status != TARGET_XFER_OK)
260 memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
261 memaddr + xfered);
9b409511 262
9b409511 263 xfered += xfered_len;
6be7b56e
PA
264 QUIT;
265 }
c906108c
SS
266}
267
4e5d721f
DE
268/* Same as target_read_stack, but report an error if can't read. */
269
270void
45aa4659 271read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
4e5d721f
DE
272{
273 int status;
c5504eaf 274
4e5d721f
DE
275 status = target_read_stack (memaddr, myaddr, len);
276 if (status != 0)
277 memory_error (status, memaddr);
278}
279
0865b04a
YQ
280/* Same as target_read_code, but report an error if can't read. */
281
282void
283read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
284{
285 int status;
286
287 status = target_read_code (memaddr, myaddr, len);
288 if (status != 0)
289 memory_error (status, memaddr);
290}
291
ee8ff470
KB
292/* Read memory at MEMADDR of length LEN and put the contents in
293 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
294 if successful. */
295
16a0f3e7 296int
c5504eaf
MS
297safe_read_memory_integer (CORE_ADDR memaddr, int len,
298 enum bfd_endian byte_order,
e17a4113 299 LONGEST *return_value)
16a0f3e7 300{
5e43d467 301 gdb_byte buf[sizeof (LONGEST)];
16a0f3e7 302
5e43d467
UW
303 if (target_read_memory (memaddr, buf, len))
304 return 0;
16a0f3e7 305
5e43d467
UW
306 *return_value = extract_signed_integer (buf, len, byte_order);
307 return 1;
16a0f3e7
EZ
308}
309
c906108c 310LONGEST
aff410f1
MS
311read_memory_integer (CORE_ADDR memaddr, int len,
312 enum bfd_endian byte_order)
c906108c 313{
dfb65433 314 gdb_byte buf[sizeof (LONGEST)];
c906108c
SS
315
316 read_memory (memaddr, buf, len);
e17a4113 317 return extract_signed_integer (buf, len, byte_order);
c906108c
SS
318}
319
320ULONGEST
aff410f1
MS
321read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
322 enum bfd_endian byte_order)
c906108c 323{
dfb65433 324 gdb_byte buf[sizeof (ULONGEST)];
c906108c
SS
325
326 read_memory (memaddr, buf, len);
e17a4113 327 return extract_unsigned_integer (buf, len, byte_order);
c906108c
SS
328}
329
0865b04a
YQ
330LONGEST
331read_code_integer (CORE_ADDR memaddr, int len,
332 enum bfd_endian byte_order)
333{
334 gdb_byte buf[sizeof (LONGEST)];
335
336 read_code (memaddr, buf, len);
337 return extract_signed_integer (buf, len, byte_order);
338}
339
340ULONGEST
341read_code_unsigned_integer (CORE_ADDR memaddr, int len,
342 enum bfd_endian byte_order)
343{
344 gdb_byte buf[sizeof (ULONGEST)];
345
346 read_code (memaddr, buf, len);
347 return extract_unsigned_integer (buf, len, byte_order);
348}
349
c906108c 350void
fba45db2 351read_memory_string (CORE_ADDR memaddr, char *buffer, int max_len)
c906108c 352{
52f0bd74
AC
353 char *cp;
354 int i;
c906108c
SS
355 int cnt;
356
357 cp = buffer;
358 while (1)
359 {
360 if (cp - buffer >= max_len)
c5aa993b
JM
361 {
362 buffer[max_len - 1] = '\0';
363 break;
364 }
c906108c
SS
365 cnt = max_len - (cp - buffer);
366 if (cnt > 8)
367 cnt = 8;
c8af03a2 368 read_memory (memaddr + (int) (cp - buffer), (gdb_byte *) cp, cnt);
c906108c 369 for (i = 0; i < cnt && *cp; i++, cp++)
c5aa993b 370 ; /* null body */
c906108c
SS
371
372 if (i < cnt && !*cp)
c5aa993b 373 break;
c906108c
SS
374 }
375}
c26e4683 376
0d540cdf
KD
377CORE_ADDR
378read_memory_typed_address (CORE_ADDR addr, struct type *type)
379{
dfb65433 380 gdb_byte *buf = alloca (TYPE_LENGTH (type));
c5504eaf 381
0d540cdf
KD
382 read_memory (addr, buf, TYPE_LENGTH (type));
383 return extract_typed_address (buf, type);
384}
385
cb6f16cf
SM
386/* See gdbcore.h. */
387
c26e4683 388void
aff410f1 389write_memory (CORE_ADDR memaddr,
45aa4659 390 const bfd_byte *myaddr, ssize_t len)
c26e4683
JB
391{
392 int status;
c5504eaf 393
00630ca8 394 status = target_write_memory (memaddr, myaddr, len);
c26e4683
JB
395 if (status != 0)
396 memory_error (status, memaddr);
397}
398
972daa01
YQ
399/* Same as write_memory, but notify 'memory_changed' observers. */
400
401void
402write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
403 ssize_t len)
404{
405 write_memory (memaddr, myaddr, len);
8de0566d 406 observer_notify_memory_changed (current_inferior (), memaddr, len, myaddr);
972daa01
YQ
407}
408
aff410f1
MS
409/* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
410 integer. */
c26e4683 411void
c5504eaf
MS
412write_memory_unsigned_integer (CORE_ADDR addr, int len,
413 enum bfd_endian byte_order,
e17a4113 414 ULONGEST value)
c26e4683 415{
dfb65433 416 gdb_byte *buf = alloca (len);
c5504eaf 417
e17a4113 418 store_unsigned_integer (buf, len, byte_order, value);
c26e4683
JB
419 write_memory (addr, buf, len);
420}
421
aff410f1
MS
422/* Store VALUE at ADDR in the inferior as a LEN-byte signed
423 integer. */
c26e4683 424void
c5504eaf
MS
425write_memory_signed_integer (CORE_ADDR addr, int len,
426 enum bfd_endian byte_order,
e17a4113 427 LONGEST value)
c26e4683 428{
dfb65433 429 gdb_byte *buf = alloca (len);
c5504eaf 430
e17a4113 431 store_signed_integer (buf, len, byte_order, value);
c26e4683
JB
432 write_memory (addr, buf, len);
433}
c906108c
SS
434\f
435/* The current default bfd target. Points to storage allocated for
436 gnutarget_string. */
437char *gnutarget;
438
439/* Same thing, except it is "auto" not NULL for the default case. */
440static char *gnutarget_string;
920d2a44
AC
441static void
442show_gnutarget_string (struct ui_file *file, int from_tty,
aff410f1
MS
443 struct cmd_list_element *c,
444 const char *value)
920d2a44 445{
aff410f1
MS
446 fprintf_filtered (file,
447 _("The current BFD target is \"%s\".\n"), value);
920d2a44 448}
c906108c 449
aff410f1
MS
450static void set_gnutarget_command (char *, int,
451 struct cmd_list_element *);
c906108c
SS
452
453static void
aff410f1
MS
454set_gnutarget_command (char *ignore, int from_tty,
455 struct cmd_list_element *c)
c906108c 456{
44478ab3
TT
457 char *gend = gnutarget_string + strlen (gnutarget_string);
458
459 gend = remove_trailing_whitespace (gnutarget_string, gend);
460 *gend = '\0';
461
bde58177 462 if (strcmp (gnutarget_string, "auto") == 0)
c906108c
SS
463 gnutarget = NULL;
464 else
465 gnutarget = gnutarget_string;
466}
467
44478ab3
TT
468/* A completion function for "set gnutarget". */
469
470static VEC (char_ptr) *
6f937416
PA
471complete_set_gnutarget (struct cmd_list_element *cmd,
472 const char *text, const char *word)
44478ab3
TT
473{
474 static const char **bfd_targets;
475
476 if (bfd_targets == NULL)
477 {
478 int last;
479
480 bfd_targets = bfd_target_list ();
481 for (last = 0; bfd_targets[last] != NULL; ++last)
482 ;
483
484 bfd_targets = xrealloc (bfd_targets, (last + 2) * sizeof (const char **));
485 bfd_targets[last] = "auto";
486 bfd_targets[last + 1] = NULL;
487 }
488
489 return complete_on_enum (bfd_targets, text, word);
490}
491
c906108c
SS
492/* Set the gnutarget. */
493void
fba45db2 494set_gnutarget (char *newtarget)
c906108c
SS
495{
496 if (gnutarget_string != NULL)
b8c9b27d 497 xfree (gnutarget_string);
1b36a34b 498 gnutarget_string = xstrdup (newtarget);
c906108c
SS
499 set_gnutarget_command (NULL, 0, NULL);
500}
501
502void
fba45db2 503_initialize_core (void)
c906108c
SS
504{
505 struct cmd_list_element *c;
c5504eaf 506
1a966eab
AC
507 c = add_cmd ("core-file", class_files, core_file_command, _("\
508Use FILE as core dump for examining memory and registers.\n\
c906108c 509No arg means have no core file. This command has been superseded by the\n\
1a966eab 510`target core' and `detach' commands."), &cmdlist);
5ba2abeb 511 set_cmd_completer (c, filename_completer);
c906108c 512
26c41df3 513
44478ab3
TT
514 c = add_setshow_string_noescape_cmd ("gnutarget", class_files,
515 &gnutarget_string, _("\
26c41df3
AC
516Set the current BFD target."), _("\
517Show the current BFD target."), _("\
518Use `set gnutarget auto' to specify automatic detection."),
44478ab3
TT
519 set_gnutarget_command,
520 show_gnutarget_string,
521 &setlist, &showlist);
522 set_cmd_completer (c, complete_set_gnutarget);
523
7e20dfcd 524 add_alias_cmd ("g", "gnutarget", class_files, 1, &setlist);
c906108c
SS
525
526 if (getenv ("GNUTARGET"))
527 set_gnutarget (getenv ("GNUTARGET"));
528 else
529 set_gnutarget ("auto");
530}
This page took 1.222859 seconds and 4 git commands to generate.