This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / corefile.c
1 /* Core dump and executable file functions above target vector, for GDB.
2    Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
3    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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include <errno.h>
24 #include <signal.h>
25 #include <fcntl.h>
26 #include "frame.h"  /* required by inferior.h */
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"
35 #include "language.h"
36
37 extern char registers[];
38
39 /* Local function declarations.  */
40
41 static void call_extra_exec_file_hooks PARAMS ((char *filename));
42
43 /* You can have any number of hooks for `exec_file_command' command to call.
44    If there's only one hook, it is set in exec_file_display hook.
45    If there are two or more hooks, they are set in exec_file_extra_hooks[],
46    and exec_file_display_hook is set to a function that calls all of them.
47    This extra complexity is needed to preserve compatibility with
48    old code that assumed that only one hook could be set, and which called
49    exec_file_display_hook directly.  */
50
51 typedef void (*hook_type) PARAMS ((char *));
52
53 hook_type exec_file_display_hook;               /* the original hook */
54 static hook_type *exec_file_extra_hooks;        /* array of additional hooks */
55 static int exec_file_hook_count = 0;            /* size of array */
56
57 /* Binary file diddling handle for the core file.  */
58
59 bfd *core_bfd = NULL;
60
61 \f
62 /* Backward compatability with old way of specifying core files.  */
63
64 void
65 core_file_command (filename, from_tty)
66      char *filename;
67      int from_tty;
68 {
69   struct target_ops *t;
70
71   dont_repeat ();                       /* Either way, seems bogus. */
72
73   t = find_core_target ();
74   if (t != NULL)
75     if (!filename)
76       (t->to_detach) (filename, from_tty);
77     else
78       (t->to_open) (filename, from_tty);
79   else
80     error ("GDB can't read core files on this machine.");
81 }
82
83 \f
84 /* If there are two or more functions that wish to hook into exec_file_command,
85  * this function will call all of the hook functions. */
86
87 static void
88 call_extra_exec_file_hooks (filename)
89      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 (hook)
102      void (*hook) PARAMS ((char *));
103 {
104   hook_type *new_array;
105
106   if (exec_file_display_hook != NULL)
107     {
108       /* There's already a hook installed.  Arrange to have both it
109        * and the subsequent hooks called. */
110       if (exec_file_hook_count == 0)
111         {
112           /* If this is the first extra hook, initialize the hook array. */
113           exec_file_extra_hooks = (hook_type *) xmalloc (sizeof(hook_type));
114           exec_file_extra_hooks[0] = exec_file_display_hook;
115           exec_file_display_hook = call_extra_exec_file_hooks;
116           exec_file_hook_count = 1;
117         }
118
119       /* Grow the hook array by one and add the new hook to the end.
120          Yes, it's inefficient to grow it by one each time but since
121          this is hardly ever called it's not a big deal.  */
122       exec_file_hook_count++;
123       new_array =
124         (hook_type *) xrealloc (exec_file_extra_hooks,
125                                 exec_file_hook_count * sizeof(hook_type));
126       exec_file_extra_hooks = new_array;
127       exec_file_extra_hooks[exec_file_hook_count - 1] = hook;
128     }
129   else
130     exec_file_display_hook = hook;
131 }
132
133 /* The exec file must be closed before running an inferior.
134    If it is needed again after the inferior dies, it must
135    be reopened.  */
136
137 void
138 close_exec_file ()
139 {
140 #if 0 /* FIXME */
141   if (exec_bfd)
142     bfd_tempclose (exec_bfd);
143 #endif
144 }
145
146 void
147 reopen_exec_file ()
148 {
149 #if 0 /* FIXME */
150   if (exec_bfd)
151     bfd_reopen (exec_bfd);
152 #endif
153 }
154 \f
155 /* If we have both a core file and an exec file,
156    print a warning if they don't go together.  */
157
158 void
159 validate_files ()
160 {
161   if (exec_bfd && core_bfd)
162     {
163       if (!core_file_matches_executable_p (core_bfd, exec_bfd))
164         warning ("core file may not match specified executable file.");
165       else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
166         warning ("exec file is newer than core file.");
167     }
168 }
169
170 /* Return the name of the executable file as a string.
171    ERR nonzero means get error if there is none specified;
172    otherwise return 0 in that case.  */
173
174 char *
175 get_exec_file (err)
176      int err;
177 {
178   if (exec_bfd) return bfd_get_filename(exec_bfd);
179   if (!err)     return NULL;
180
181   error ("No executable file specified.\n\
182 Use the \"file\" or \"exec-file\" command.");
183   return NULL;
184 }
185
186 \f
187 /* Report a memory error with error().  */
188
189 void
190 memory_error (status, memaddr)
191      int status;
192      CORE_ADDR memaddr;
193 {
194   if (status == EIO)
195     {
196       /* Actually, address between memaddr and memaddr + len
197          was out of bounds. */
198       error_begin ();
199       printf_filtered ("Cannot access memory at address ");
200       print_address_numeric (memaddr, 1, gdb_stdout);
201       printf_filtered (".\n");
202       return_to_top_level (RETURN_ERROR);
203     }
204   else
205     {
206       error_begin ();
207       printf_filtered ("Error accessing memory address ");
208       print_address_numeric (memaddr, 1, gdb_stdout);
209       printf_filtered (": %s.\n",
210                          safe_strerror (status));
211       return_to_top_level (RETURN_ERROR);
212     }
213 }
214
215 /* Same as target_read_memory, but report an error if can't read.  */
216 void
217 read_memory (memaddr, myaddr, len)
218      CORE_ADDR memaddr;
219      char *myaddr;
220      int len;
221 {
222   int status;
223   status = target_read_memory (memaddr, myaddr, len);
224   if (status != 0)
225     memory_error (status, memaddr);
226 }
227
228 void
229 read_memory_section (memaddr, myaddr, len, bfd_section)
230      CORE_ADDR memaddr;
231      char *myaddr;
232      int len;
233      asection *bfd_section;
234 {
235   int status;
236   status = target_read_memory_section (memaddr, myaddr, len, bfd_section);
237   if (status != 0)
238     memory_error (status, memaddr);
239 }
240
241 /* Like target_read_memory, but slightly different parameters.  */
242
243 int
244 dis_asm_read_memory (memaddr, myaddr, len, info)
245      bfd_vma memaddr;
246      bfd_byte *myaddr;
247      int len;
248      disassemble_info *info;
249 {
250   return target_read_memory (memaddr, (char *) myaddr, len);
251 }
252
253 /* Like memory_error with slightly different parameters.  */
254 void
255 dis_asm_memory_error (status, memaddr, info)
256      int status;
257      bfd_vma memaddr;
258      disassemble_info *info;
259 {
260   memory_error (status, memaddr);
261 }
262
263 /* Like print_address with slightly different parameters.  */
264 void
265 dis_asm_print_address (addr, info)
266      bfd_vma addr;
267      struct disassemble_info *info;
268 {
269   print_address (addr, info->stream);
270 }
271
272 /* Same as target_write_memory, but report an error if can't write.  */
273 void
274 write_memory (memaddr, myaddr, len)
275      CORE_ADDR memaddr;
276      char *myaddr;
277      int len;
278 {
279   int status;
280
281   status = target_write_memory (memaddr, myaddr, len);
282   if (status != 0)
283     memory_error (status, memaddr);
284 }
285
286 /* Read an integer from debugged memory, given address and number of bytes.  */
287
288 LONGEST
289 read_memory_integer (memaddr, len)
290      CORE_ADDR memaddr;
291      int len;
292 {
293   char buf[sizeof (LONGEST)];
294
295   read_memory (memaddr, buf, len);
296   return extract_signed_integer (buf, len);
297 }
298
299 ULONGEST
300 read_memory_unsigned_integer (memaddr, len)
301      CORE_ADDR memaddr;
302      int len;
303 {
304   char buf[sizeof (ULONGEST)];
305
306   read_memory (memaddr, buf, len);
307   return extract_unsigned_integer (buf, len);
308 }
309 \f
310 #if 0
311 /* Enable after 4.12.  It is not tested.  */
312
313 /* Search code.  Targets can just make this their search function, or
314    if the protocol has a less general search function, they can call this
315    in the cases it can't handle.  */
316 void
317 generic_search (len, data, mask, startaddr, increment, lorange, hirange
318                 addr_found, data_found)
319      int len;
320      char *data;
321      char *mask;
322      CORE_ADDR startaddr;
323      int increment;
324      CORE_ADDR lorange;
325      CORE_ADDR hirange;
326      CORE_ADDR *addr_found;
327      char *data_found;
328 {
329   int i;
330   CORE_ADDR curaddr = startaddr;
331
332   while (curaddr >= lorange && curaddr < hirange)
333     {
334       read_memory (curaddr, data_found, len);
335       for (i = 0; i < len; ++i)
336         if ((data_found[i] & mask[i]) != data[i])
337           goto try_again;
338       /* It matches.  */
339       *addr_found = curaddr;
340       return;
341
342     try_again:
343       curaddr += increment;
344     }
345   *addr_found = (CORE_ADDR)0;
346   return;
347 }
348 #endif /* 0 */
349 \f
350 /* The current default bfd target.  Points to storage allocated for
351    gnutarget_string.  */
352 char *gnutarget;
353
354 /* Same thing, except it is "auto" not NULL for the default case.  */
355 static char *gnutarget_string;
356
357 static void set_gnutarget_command
358   PARAMS ((char *, int, struct cmd_list_element *));
359
360 static void
361 set_gnutarget_command (ignore, from_tty, c)
362      char *ignore;
363      int from_tty;
364      struct cmd_list_element *c;
365 {
366   if (STREQ (gnutarget_string, "auto"))
367     gnutarget = NULL;
368   else
369     gnutarget = gnutarget_string;
370 }
371
372 /* Set the gnutarget.  */
373 void
374 set_gnutarget (newtarget)
375      char *newtarget;
376 {
377   if (gnutarget_string != NULL)
378     free (gnutarget_string);
379   gnutarget_string = savestring (newtarget, strlen (newtarget));
380   set_gnutarget_command (NULL, 0, NULL);
381 }
382
383 void
384 _initialize_core()
385 {
386   struct cmd_list_element *c;
387   c = add_cmd ("core-file", class_files, core_file_command,
388                "Use FILE as core dump for examining memory and registers.\n\
389 No arg means have no core file.  This command has been superseded by the\n\
390 `target core' and `detach' commands.", &cmdlist);
391   c->completer = filename_completer;
392
393   c = add_set_cmd ("gnutarget", class_files, var_string_noescape,
394                   (char *) &gnutarget_string,
395                   "Set the current BFD target.\n\
396 Use `set gnutarget auto' to specify automatic detection.",
397                   &setlist);
398   c->function.sfunc = set_gnutarget_command;
399   add_show_from_set (c, &showlist);
400
401   if (getenv ("GNUTARGET"))
402     set_gnutarget (getenv ("GNUTARGET"));
403   else
404     set_gnutarget ("auto");
405 }