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