Patches for eCos: Dump literal pool on section change. Mark thumb function
[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 #include "gdb_stat.h"
37 #include "symfile.h"
38 #include "objfiles.h"
39
40 /* Local function declarations.  */
41
42 extern void _initialize_core PARAMS ((void));
43 static void call_extra_exec_file_hooks PARAMS ((char *filename));
44
45 /* You can have any number of hooks for `exec_file_command' command to call.
46    If there's only one hook, it is set in exec_file_display hook.
47    If there are two or more hooks, they are set in exec_file_extra_hooks[],
48    and exec_file_display_hook is set to a function that calls all of them.
49    This extra complexity is needed to preserve compatibility with
50    old code that assumed that only one hook could be set, and which called
51    exec_file_display_hook directly.  */
52
53 typedef void (*hook_type) PARAMS ((char *));
54
55 hook_type exec_file_display_hook;               /* the original hook */
56 static hook_type *exec_file_extra_hooks;        /* array of additional 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 \f
64 /* Backward compatability with old way of specifying core files.  */
65
66 void
67 core_file_command (filename, from_tty)
68      char *filename;
69      int from_tty;
70 {
71   struct target_ops *t;
72
73   dont_repeat ();                       /* Either way, seems bogus. */
74
75   t = find_core_target ();
76   if (t != NULL)
77     if (!filename)
78       (t->to_detach) (filename, from_tty);
79     else
80       {
81         /* Yes, we were given the path of a core file.  Do we already
82            have a symbol file?  If not, can we determine it from the
83            core file?  If we can, do so.
84            */
85 #ifdef HPUXHPPA
86         if (symfile_objfile == NULL)
87           {
88             char *  symfile;
89             symfile = t->to_core_file_to_sym_file (filename);
90             if (symfile)
91               {
92                 char *  symfile_copy = strdup (symfile);
93
94                 make_cleanup (free, symfile_copy);
95                 symbol_file_command (symfile_copy, from_tty);
96               }
97             else
98               warning ("Unknown symbols for '%s'; use the 'symbol-file' command.", filename);
99           }
100 #endif
101         (t->to_open) (filename, from_tty);
102       }
103   else
104     error ("GDB can't read core files on this machine.");
105 }
106
107 \f
108 /* If there are two or more functions that wish to hook into exec_file_command,
109  * this function will call all of the hook functions. */
110
111 static void
112 call_extra_exec_file_hooks (filename)
113      char *filename;
114 {
115   int i;
116
117   for (i = 0; i < exec_file_hook_count; i++)
118     (*exec_file_extra_hooks[i])(filename);
119 }
120
121 /* Call this to specify the hook for exec_file_command to call back.
122    This is called from the x-window display code.  */
123
124 void
125 specify_exec_file_hook (hook)
126      void (*hook) PARAMS ((char *));
127 {
128   hook_type *new_array;
129
130   if (exec_file_display_hook != NULL)
131     {
132       /* There's already a hook installed.  Arrange to have both it
133        * and the subsequent hooks called. */
134       if (exec_file_hook_count == 0)
135         {
136           /* If this is the first extra hook, initialize the hook array. */
137           exec_file_extra_hooks = (hook_type *) xmalloc (sizeof(hook_type));
138           exec_file_extra_hooks[0] = exec_file_display_hook;
139           exec_file_display_hook = call_extra_exec_file_hooks;
140           exec_file_hook_count = 1;
141         }
142
143       /* Grow the hook array by one and add the new hook to the end.
144          Yes, it's inefficient to grow it by one each time but since
145          this is hardly ever called it's not a big deal.  */
146       exec_file_hook_count++;
147       new_array =
148         (hook_type *) xrealloc (exec_file_extra_hooks,
149                                 exec_file_hook_count * sizeof(hook_type));
150       exec_file_extra_hooks = new_array;
151       exec_file_extra_hooks[exec_file_hook_count - 1] = hook;
152     }
153   else
154     exec_file_display_hook = hook;
155 }
156
157 /* The exec file must be closed before running an inferior.
158    If it is needed again after the inferior dies, it must
159    be reopened.  */
160
161 void
162 close_exec_file ()
163 {
164 #if 0 /* FIXME */
165   if (exec_bfd)
166     bfd_tempclose (exec_bfd);
167 #endif
168 }
169
170 void
171 reopen_exec_file ()
172 {
173 #if 0 /* FIXME */
174   if (exec_bfd)
175     bfd_reopen (exec_bfd);
176 #else
177   char *filename;
178   int res;
179   struct stat st;
180   long mtime;
181
182   /* Don't do anything if the current target isn't exec. */
183   if (exec_bfd == NULL || strcmp (target_shortname, "exec") != 0)
184     return;
185  
186   /* If the timestamp of the exec file has changed, reopen it. */
187   filename = strdup (bfd_get_filename (exec_bfd));
188   make_cleanup (free, filename);
189   mtime = bfd_get_mtime(exec_bfd);
190   res = stat (filename, &st);
191
192   if (mtime && mtime != st.st_mtime)
193     exec_file_command (filename, 0);
194 #endif
195 }
196 \f
197 /* If we have both a core file and an exec file,
198    print a warning if they don't go together.  */
199
200 void
201 validate_files ()
202 {
203   if (exec_bfd && core_bfd)
204     {
205       if (!core_file_matches_executable_p (core_bfd, exec_bfd))
206         warning ("core file may not match specified executable file.");
207       else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
208         warning ("exec file is newer than core file.");
209     }
210 }
211
212 /* Return the name of the executable file as a string.
213    ERR nonzero means get error if there is none specified;
214    otherwise return 0 in that case.  */
215
216 char *
217 get_exec_file (err)
218      int err;
219 {
220   if (exec_bfd) return bfd_get_filename(exec_bfd);
221   if (!err)     return NULL;
222
223   error ("No executable file specified.\n\
224 Use the \"file\" or \"exec-file\" command.");
225   return NULL;
226 }
227
228 \f
229 /* Report a memory error with error().  */
230
231 void
232 memory_error (status, memaddr)
233      int status;
234      CORE_ADDR memaddr;
235 {
236   if (status == EIO)
237     {
238       /* Actually, address between memaddr and memaddr + len
239          was out of bounds. */
240       error_begin ();
241       printf_filtered ("Cannot access memory at address ");
242       print_address_numeric (memaddr, 1, gdb_stdout);
243       printf_filtered (".\n");
244       return_to_top_level (RETURN_ERROR);
245     }
246   else
247     {
248       error_begin ();
249       printf_filtered ("Error accessing memory address ");
250       print_address_numeric (memaddr, 1, gdb_stdout);
251       printf_filtered (": %s.\n",
252                          safe_strerror (status));
253       return_to_top_level (RETURN_ERROR);
254     }
255 }
256
257 /* Same as target_read_memory, but report an error if can't read.  */
258 void
259 read_memory (memaddr, myaddr, len)
260      CORE_ADDR memaddr;
261      char *myaddr;
262      int len;
263 {
264   int status;
265   status = target_read_memory (memaddr, myaddr, len);
266   if (status != 0)
267     memory_error (status, memaddr);
268 }
269
270 void
271 read_memory_section (memaddr, myaddr, len, bfd_section)
272      CORE_ADDR memaddr;
273      char *myaddr;
274      int len;
275      asection *bfd_section;
276 {
277   int status;
278   status = target_read_memory_section (memaddr, myaddr, len, bfd_section);
279   if (status != 0)
280     memory_error (status, memaddr);
281 }
282
283 /* Like target_read_memory, but slightly different parameters.  */
284
285 int
286 dis_asm_read_memory (memaddr, myaddr, len, info)
287      bfd_vma memaddr;
288      bfd_byte *myaddr;
289      int len;
290      disassemble_info *info;
291 {
292   return target_read_memory (memaddr, (char *) myaddr, len);
293 }
294
295 /* Like memory_error with slightly different parameters.  */
296 void
297 dis_asm_memory_error (status, memaddr, info)
298      int status;
299      bfd_vma memaddr;
300      disassemble_info *info;
301 {
302   memory_error (status, memaddr);
303 }
304
305 /* Like print_address with slightly different parameters.  */
306 void
307 dis_asm_print_address (addr, info)
308      bfd_vma addr;
309      struct disassemble_info *info;
310 {
311   print_address (addr, info->stream);
312 }
313
314 /* Same as target_write_memory, but report an error if can't write.  */
315 void
316 write_memory (memaddr, myaddr, len)
317      CORE_ADDR memaddr;
318      char *myaddr;
319      int len;
320 {
321   int status;
322
323   status = target_write_memory (memaddr, myaddr, len);
324   if (status != 0)
325     memory_error (status, memaddr);
326 }
327
328 /* Read an integer from debugged memory, given address and number of bytes.  */
329
330 LONGEST
331 read_memory_integer (memaddr, len)
332      CORE_ADDR memaddr;
333      int len;
334 {
335   char buf[sizeof (LONGEST)];
336
337   read_memory (memaddr, buf, len);
338   return extract_signed_integer (buf, len);
339 }
340
341 ULONGEST
342 read_memory_unsigned_integer (memaddr, len)
343      CORE_ADDR memaddr;
344      int len;
345 {
346   char buf[sizeof (ULONGEST)];
347
348   read_memory (memaddr, buf, len);
349   return extract_unsigned_integer (buf, len);
350 }
351
352 void
353 read_memory_string (memaddr, buffer, max_len)
354      CORE_ADDR memaddr;
355      char * buffer;
356      int max_len;
357 {
358   register char * cp;
359   register int i;
360   int cnt;
361
362   cp = buffer;
363   while (1)
364     {
365       if (cp - buffer >= max_len)
366         {
367           buffer[max_len - 1] = '\0';
368           break;
369         }
370       cnt = max_len - (cp - buffer);
371       if (cnt > 8)
372         cnt = 8;
373       read_memory (memaddr + (int) (cp - buffer), cp, cnt);
374       for (i = 0; i < cnt && *cp; i++, cp++)
375         ; /* null body */
376
377       if (i < cnt && !*cp)
378         break;
379     }
380 }
381
382 \f
383 #if 0
384 /* Enable after 4.12.  It is not tested.  */
385
386 /* Search code.  Targets can just make this their search function, or
387    if the protocol has a less general search function, they can call this
388    in the cases it can't handle.  */
389 void
390 generic_search (len, data, mask, startaddr, increment, lorange, hirange
391                 addr_found, data_found)
392      int len;
393      char *data;
394      char *mask;
395      CORE_ADDR startaddr;
396      int increment;
397      CORE_ADDR lorange;
398      CORE_ADDR hirange;
399      CORE_ADDR *addr_found;
400      char *data_found;
401 {
402   int i;
403   CORE_ADDR curaddr = startaddr;
404
405   while (curaddr >= lorange && curaddr < hirange)
406     {
407       read_memory (curaddr, data_found, len);
408       for (i = 0; i < len; ++i)
409         if ((data_found[i] & mask[i]) != data[i])
410           goto try_again;
411       /* It matches.  */
412       *addr_found = curaddr;
413       return;
414
415     try_again:
416       curaddr += increment;
417     }
418   *addr_found = (CORE_ADDR)0;
419   return;
420 }
421 #endif /* 0 */
422 \f
423 /* The current default bfd target.  Points to storage allocated for
424    gnutarget_string.  */
425 char *gnutarget;
426
427 /* Same thing, except it is "auto" not NULL for the default case.  */
428 static char *gnutarget_string;
429
430 static void set_gnutarget_command
431   PARAMS ((char *, int, struct cmd_list_element *));
432
433 static void
434 set_gnutarget_command (ignore, from_tty, c)
435      char *ignore;
436      int from_tty;
437      struct cmd_list_element *c;
438 {
439   if (STREQ (gnutarget_string, "auto"))
440     gnutarget = NULL;
441   else
442     gnutarget = gnutarget_string;
443 }
444
445 /* Set the gnutarget.  */
446 void
447 set_gnutarget (newtarget)
448      char *newtarget;
449 {
450   if (gnutarget_string != NULL)
451     free (gnutarget_string);
452   gnutarget_string = savestring (newtarget, strlen (newtarget));
453   set_gnutarget_command (NULL, 0, NULL);
454 }
455
456 void
457 _initialize_core()
458 {
459   struct cmd_list_element *c;
460   c = add_cmd ("core-file", class_files, core_file_command,
461                "Use FILE as core dump for examining memory and registers.\n\
462 No arg means have no core file.  This command has been superseded by the\n\
463 `target core' and `detach' commands.", &cmdlist);
464   c->completer = filename_completer;
465
466   c = add_set_cmd ("gnutarget", class_files, var_string_noescape,
467                   (char *) &gnutarget_string,
468                   "Set the current BFD target.\n\
469 Use `set gnutarget auto' to specify automatic detection.",
470                   &setlist);
471   c->function.sfunc = set_gnutarget_command;
472   add_show_from_set (c, &showlist);
473
474   if (getenv ("GNUTARGET"))
475     set_gnutarget (getenv ("GNUTARGET"));
476   else
477     set_gnutarget ("auto");
478 }