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