* gencode.c (build_instruction) [MUL]: Cast operands to word64, to
[deliverable/binutils-gdb.git] / gdb / corefile.c
CommitLineData
8afd6ca5 1/* Core dump and executable file functions above target vector, for GDB.
ba47c66a
PS
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
dd3b648e
RP
4
5This file is part of GDB.
6
99a7de40 7This program is free software; you can redistribute it and/or modify
dd3b648e 8it under the terms of the GNU General Public License as published by
99a7de40
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
dd3b648e 11
99a7de40 12This program is distributed in the hope that it will be useful,
dd3b648e
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
99a7de40 18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
dd3b648e 20
d747e0af 21#include "defs.h"
2b576293 22#include "gdb_string.h"
dd3b648e
RP
23#include <errno.h>
24#include <signal.h>
bdbd5f50 25#include <fcntl.h>
dd3b648e
RP
26#include "frame.h" /* required by inferior.h */
27#include "inferior.h"
28#include "symtab.h"
29#include "command.h"
df0f0dcc 30#include "gdbcmd.h"
dd3b648e
RP
31#include "bfd.h"
32#include "target.h"
33#include "gdbcore.h"
5d0734a7 34#include "dis-asm.h"
100f92e2 35#include "language.h"
dd3b648e 36
dd3b648e
RP
37extern char registers[];
38
f9fedc48 39/* Local function declarations. */
dd3b648e 40
f9fedc48
MA
41static 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
51typedef void (*hook_type) PARAMS ((char *));
52
53hook_type exec_file_display_hook; /* the original hook */
54static hook_type *exec_file_extra_hooks; /* array of additional hooks */
55static int exec_file_hook_count = 0; /* size of array */
dd3b648e 56
dd3b648e
RP
57/* Binary file diddling handle for the core file. */
58
59bfd *core_bfd = NULL;
60
dd3b648e 61\f
dd3b648e
RP
62/* Backward compatability with old way of specifying core files. */
63
64void
65core_file_command (filename, from_tty)
66 char *filename;
67 int from_tty;
68{
df9b3bfc 69 struct target_ops *t;
327f7197 70
3f2e006b 71 dont_repeat (); /* Either way, seems bogus. */
8afd6ca5 72
df9b3bfc
RP
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);
dd3b648e 79 else
327f7197 80 error ("GDB can't read core files on this machine.");
dd3b648e
RP
81}
82
83\f
f9fedc48
MA
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
87static void
88call_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
dd3b648e
RP
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
100void
101specify_exec_file_hook (hook)
7ed0f002 102 void (*hook) PARAMS ((char *));
dd3b648e 103{
f9fedc48
MA
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;
dd3b648e
RP
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
137void
138close_exec_file ()
139{
28444bf3 140#if 0 /* FIXME */
dd3b648e
RP
141 if (exec_bfd)
142 bfd_tempclose (exec_bfd);
143#endif
144}
145
146void
147reopen_exec_file ()
148{
28444bf3 149#if 0 /* FIXME */
dd3b648e
RP
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,
c561ca5d 156 print a warning if they don't go together. */
dd3b648e
RP
157
158void
159validate_files ()
160{
161 if (exec_bfd && core_bfd)
162 {
bdbd5f50 163 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
c8094777 164 warning ("core file may not match specified executable file.");
dd3b648e 165 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
c8094777 166 warning ("exec file is newer than core file.");
dd3b648e
RP
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
174char *
175get_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\
182Use the \"file\" or \"exec-file\" command.");
183 return NULL;
184}
185
dd3b648e 186\f
7ed0f002
JG
187/* Report a memory error with error(). */
188
dd3b648e
RP
189void
190memory_error (status, memaddr)
191 int status;
192 CORE_ADDR memaddr;
193{
dd3b648e
RP
194 if (status == EIO)
195 {
196 /* Actually, address between memaddr and memaddr + len
197 was out of bounds. */
a0cf4681 198 error_begin ();
e16b9023 199 printf_filtered ("Cannot access memory at address ");
d24c0599 200 print_address_numeric (memaddr, 1, gdb_stdout);
e16b9023 201 printf_filtered (".\n");
a0cf4681 202 return_to_top_level (RETURN_ERROR);
dd3b648e
RP
203 }
204 else
205 {
a0cf4681 206 error_begin ();
e16b9023 207 printf_filtered ("Error accessing memory address ");
d24c0599 208 print_address_numeric (memaddr, 1, gdb_stdout);
e16b9023 209 printf_filtered (": %s.\n",
a0cf4681
JK
210 safe_strerror (status));
211 return_to_top_level (RETURN_ERROR);
dd3b648e
RP
212 }
213}
214
215/* Same as target_read_memory, but report an error if can't read. */
216void
217read_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
720b3aed 228/* Like target_read_memory, but slightly different parameters. */
bf097a0b 229
5d0734a7 230int
a6cead71 231dis_asm_read_memory (memaddr, myaddr, len, info)
5d0734a7
JK
232 bfd_vma memaddr;
233 bfd_byte *myaddr;
234 int len;
a6cead71 235 disassemble_info *info;
5d0734a7 236{
34b70237 237 return target_read_memory (memaddr, (char *) myaddr, len);
5d0734a7
JK
238}
239
240/* Like memory_error with slightly different parameters. */
241void
242dis_asm_memory_error (status, memaddr, info)
243 int status;
244 bfd_vma memaddr;
245 disassemble_info *info;
246{
247 memory_error (status, memaddr);
248}
249
720b3aed
JK
250/* Like print_address with slightly different parameters. */
251void
252dis_asm_print_address (addr, info)
253 bfd_vma addr;
254 struct disassemble_info *info;
255{
256 print_address (addr, info->stream);
257}
258
dd3b648e
RP
259/* Same as target_write_memory, but report an error if can't write. */
260void
261write_memory (memaddr, myaddr, len)
262 CORE_ADDR memaddr;
263 char *myaddr;
264 int len;
265{
266 int status;
267
268 status = target_write_memory (memaddr, myaddr, len);
269 if (status != 0)
270 memory_error (status, memaddr);
271}
272
273/* Read an integer from debugged memory, given address and number of bytes. */
274
34df79fc 275LONGEST
dd3b648e
RP
276read_memory_integer (memaddr, len)
277 CORE_ADDR memaddr;
278 int len;
279{
58e49e21 280 char buf[sizeof (LONGEST)];
dd3b648e 281
34df79fc
JK
282 read_memory (memaddr, buf, len);
283 return extract_signed_integer (buf, len);
dd3b648e 284}
86a5593e 285
119dfbb7 286ULONGEST
86a5593e
SC
287read_memory_unsigned_integer (memaddr, len)
288 CORE_ADDR memaddr;
289 int len;
290{
119dfbb7 291 char buf[sizeof (ULONGEST)];
86a5593e 292
34df79fc
JK
293 read_memory (memaddr, buf, len);
294 return extract_unsigned_integer (buf, len);
86a5593e 295}
dd3b648e 296\f
63dcc380
JK
297#if 0
298/* Enable after 4.12. It is not tested. */
299
300/* Search code. Targets can just make this their search function, or
301 if the protocol has a less general search function, they can call this
302 in the cases it can't handle. */
303void
304generic_search (len, data, mask, startaddr, increment, lorange, hirange
305 addr_found, data_found)
306 int len;
307 char *data;
308 char *mask;
309 CORE_ADDR startaddr;
310 int increment;
311 CORE_ADDR lorange;
312 CORE_ADDR hirange;
313 CORE_ADDR *addr_found;
314 char *data_found;
315{
316 int i;
317 CORE_ADDR curaddr = startaddr;
318
319 while (curaddr >= lorange && curaddr < hirange)
320 {
321 read_memory (curaddr, data_found, len);
322 for (i = 0; i < len; ++i)
323 if ((data_found[i] & mask[i]) != data[i])
324 goto try_again;
325 /* It matches. */
326 *addr_found = curaddr;
327 return;
328
329 try_again:
330 curaddr += increment;
331 }
332 *addr_found = (CORE_ADDR)0;
333 return;
334}
335#endif /* 0 */
336\f
0685d95f
JK
337/* The current default bfd target. Points to storage allocated for
338 gnutarget_string. */
339char *gnutarget;
340
341/* Same thing, except it is "auto" not NULL for the default case. */
342static char *gnutarget_string;
343
344static void set_gnutarget_command
345 PARAMS ((char *, int, struct cmd_list_element *));
346
347static void
348set_gnutarget_command (ignore, from_tty, c)
349 char *ignore;
350 int from_tty;
351 struct cmd_list_element *c;
352{
353 if (STREQ (gnutarget_string, "auto"))
354 gnutarget = NULL;
355 else
356 gnutarget = gnutarget_string;
357}
358
359/* Set the gnutarget. */
360void
361set_gnutarget (newtarget)
362 char *newtarget;
363{
364 if (gnutarget_string != NULL)
365 free (gnutarget_string);
366 gnutarget_string = savestring (newtarget, strlen (newtarget));
367 set_gnutarget_command (NULL, 0, NULL);
368}
369
dd3b648e
RP
370void
371_initialize_core()
372{
df0f0dcc
JK
373 struct cmd_list_element *c;
374 c = add_cmd ("core-file", class_files, core_file_command,
375 "Use FILE as core dump for examining memory and registers.\n\
dd3b648e 376No arg means have no core file. This command has been superseded by the\n\
df0f0dcc
JK
377`target core' and `detach' commands.", &cmdlist);
378 c->completer = filename_completer;
0685d95f
JK
379
380 c = add_set_cmd ("gnutarget", class_files, var_string_noescape,
381 (char *) &gnutarget_string,
382 "Set the current BFD target.\n\
383Use `set gnutarget auto' to specify automatic detection.",
384 &setlist);
385 c->function.sfunc = set_gnutarget_command;
386 add_show_from_set (c, &showlist);
387
388 if (getenv ("GNUTARGET"))
389 set_gnutarget (getenv ("GNUTARGET"));
390 else
391 set_gnutarget ("auto");
dd3b648e 392}
This page took 0.27497 seconds and 4 git commands to generate.