* aout64.h (BMAGIC): Define.
[deliverable/binutils-gdb.git] / gdb / core.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
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
dd3b648e 20
d747e0af 21#include "defs.h"
ba47c66a 22#include <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
39/* Hook for `exec_file_command' command to call. */
40
7ed0f002 41void (*exec_file_display_hook) PARAMS ((char *)) = NULL;
dd3b648e 42
dd3b648e
RP
43/* Binary file diddling handle for the core file. */
44
45bfd *core_bfd = NULL;
46
dd3b648e 47\f
dd3b648e
RP
48/* Backward compatability with old way of specifying core files. */
49
50void
51core_file_command (filename, from_tty)
52 char *filename;
53 int from_tty;
54{
df9b3bfc 55 struct target_ops *t;
327f7197 56
3f2e006b 57 dont_repeat (); /* Either way, seems bogus. */
8afd6ca5 58
df9b3bfc
RP
59 t = find_core_target ();
60 if (t != NULL)
61 if (!filename)
62 (t->to_detach) (filename, from_tty);
63 else
64 (t->to_open) (filename, from_tty);
dd3b648e 65 else
327f7197 66 error ("GDB can't read core files on this machine.");
dd3b648e
RP
67}
68
69\f
70/* Call this to specify the hook for exec_file_command to call back.
71 This is called from the x-window display code. */
72
73void
74specify_exec_file_hook (hook)
7ed0f002 75 void (*hook) PARAMS ((char *));
dd3b648e
RP
76{
77 exec_file_display_hook = hook;
78}
79
80/* The exec file must be closed before running an inferior.
81 If it is needed again after the inferior dies, it must
82 be reopened. */
83
84void
85close_exec_file ()
86{
87#ifdef FIXME
88 if (exec_bfd)
89 bfd_tempclose (exec_bfd);
90#endif
91}
92
93void
94reopen_exec_file ()
95{
96#ifdef FIXME
97 if (exec_bfd)
98 bfd_reopen (exec_bfd);
99#endif
100}
101\f
102/* If we have both a core file and an exec file,
c561ca5d 103 print a warning if they don't go together. */
dd3b648e
RP
104
105void
106validate_files ()
107{
108 if (exec_bfd && core_bfd)
109 {
bdbd5f50 110 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
c8094777 111 warning ("core file may not match specified executable file.");
dd3b648e 112 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
c8094777 113 warning ("exec file is newer than core file.");
dd3b648e
RP
114 }
115}
116
117/* Return the name of the executable file as a string.
118 ERR nonzero means get error if there is none specified;
119 otherwise return 0 in that case. */
120
121char *
122get_exec_file (err)
123 int err;
124{
125 if (exec_bfd) return bfd_get_filename(exec_bfd);
126 if (!err) return NULL;
127
128 error ("No executable file specified.\n\
129Use the \"file\" or \"exec-file\" command.");
130 return NULL;
131}
132
dd3b648e 133\f
7ed0f002
JG
134/* Report a memory error with error(). */
135
dd3b648e
RP
136void
137memory_error (status, memaddr)
138 int status;
139 CORE_ADDR memaddr;
140{
dd3b648e
RP
141 if (status == EIO)
142 {
143 /* Actually, address between memaddr and memaddr + len
144 was out of bounds. */
a0cf4681 145 error_begin ();
e16b9023 146 printf_filtered ("Cannot access memory at address ");
d24c0599 147 print_address_numeric (memaddr, 1, gdb_stdout);
e16b9023 148 printf_filtered (".\n");
a0cf4681 149 return_to_top_level (RETURN_ERROR);
dd3b648e
RP
150 }
151 else
152 {
a0cf4681 153 error_begin ();
e16b9023 154 printf_filtered ("Error accessing memory address ");
d24c0599 155 print_address_numeric (memaddr, 1, gdb_stdout);
e16b9023 156 printf_filtered (": %s.\n",
a0cf4681
JK
157 safe_strerror (status));
158 return_to_top_level (RETURN_ERROR);
dd3b648e
RP
159 }
160}
161
162/* Same as target_read_memory, but report an error if can't read. */
163void
164read_memory (memaddr, myaddr, len)
165 CORE_ADDR memaddr;
166 char *myaddr;
167 int len;
168{
169 int status;
170 status = target_read_memory (memaddr, myaddr, len);
171 if (status != 0)
172 memory_error (status, memaddr);
173}
174
720b3aed 175/* Like target_read_memory, but slightly different parameters. */
bf097a0b 176
5d0734a7 177int
a6cead71 178dis_asm_read_memory (memaddr, myaddr, len, info)
5d0734a7
JK
179 bfd_vma memaddr;
180 bfd_byte *myaddr;
181 int len;
a6cead71 182 disassemble_info *info;
5d0734a7 183{
34b70237 184 return target_read_memory (memaddr, (char *) myaddr, len);
5d0734a7
JK
185}
186
187/* Like memory_error with slightly different parameters. */
188void
189dis_asm_memory_error (status, memaddr, info)
190 int status;
191 bfd_vma memaddr;
192 disassemble_info *info;
193{
194 memory_error (status, memaddr);
195}
196
720b3aed
JK
197/* Like print_address with slightly different parameters. */
198void
199dis_asm_print_address (addr, info)
200 bfd_vma addr;
201 struct disassemble_info *info;
202{
203 print_address (addr, info->stream);
204}
205
dd3b648e
RP
206/* Same as target_write_memory, but report an error if can't write. */
207void
208write_memory (memaddr, myaddr, len)
209 CORE_ADDR memaddr;
210 char *myaddr;
211 int len;
212{
213 int status;
214
215 status = target_write_memory (memaddr, myaddr, len);
216 if (status != 0)
217 memory_error (status, memaddr);
218}
219
220/* Read an integer from debugged memory, given address and number of bytes. */
221
34df79fc 222LONGEST
dd3b648e
RP
223read_memory_integer (memaddr, len)
224 CORE_ADDR memaddr;
225 int len;
226{
58e49e21 227 char buf[sizeof (LONGEST)];
dd3b648e 228
34df79fc
JK
229 read_memory (memaddr, buf, len);
230 return extract_signed_integer (buf, len);
dd3b648e 231}
86a5593e 232
34df79fc 233unsigned LONGEST
86a5593e
SC
234read_memory_unsigned_integer (memaddr, len)
235 CORE_ADDR memaddr;
236 int len;
237{
58e49e21 238 char buf[sizeof (unsigned LONGEST)];
86a5593e 239
34df79fc
JK
240 read_memory (memaddr, buf, len);
241 return extract_unsigned_integer (buf, len);
86a5593e 242}
dd3b648e 243\f
63dcc380
JK
244#if 0
245/* Enable after 4.12. It is not tested. */
246
247/* Search code. Targets can just make this their search function, or
248 if the protocol has a less general search function, they can call this
249 in the cases it can't handle. */
250void
251generic_search (len, data, mask, startaddr, increment, lorange, hirange
252 addr_found, data_found)
253 int len;
254 char *data;
255 char *mask;
256 CORE_ADDR startaddr;
257 int increment;
258 CORE_ADDR lorange;
259 CORE_ADDR hirange;
260 CORE_ADDR *addr_found;
261 char *data_found;
262{
263 int i;
264 CORE_ADDR curaddr = startaddr;
265
266 while (curaddr >= lorange && curaddr < hirange)
267 {
268 read_memory (curaddr, data_found, len);
269 for (i = 0; i < len; ++i)
270 if ((data_found[i] & mask[i]) != data[i])
271 goto try_again;
272 /* It matches. */
273 *addr_found = curaddr;
274 return;
275
276 try_again:
277 curaddr += increment;
278 }
279 *addr_found = (CORE_ADDR)0;
280 return;
281}
282#endif /* 0 */
283\f
0685d95f
JK
284/* The current default bfd target. Points to storage allocated for
285 gnutarget_string. */
286char *gnutarget;
287
288/* Same thing, except it is "auto" not NULL for the default case. */
289static char *gnutarget_string;
290
291static void set_gnutarget_command
292 PARAMS ((char *, int, struct cmd_list_element *));
293
294static void
295set_gnutarget_command (ignore, from_tty, c)
296 char *ignore;
297 int from_tty;
298 struct cmd_list_element *c;
299{
300 if (STREQ (gnutarget_string, "auto"))
301 gnutarget = NULL;
302 else
303 gnutarget = gnutarget_string;
304}
305
306/* Set the gnutarget. */
307void
308set_gnutarget (newtarget)
309 char *newtarget;
310{
311 if (gnutarget_string != NULL)
312 free (gnutarget_string);
313 gnutarget_string = savestring (newtarget, strlen (newtarget));
314 set_gnutarget_command (NULL, 0, NULL);
315}
316
dd3b648e
RP
317void
318_initialize_core()
319{
df0f0dcc
JK
320 struct cmd_list_element *c;
321 c = add_cmd ("core-file", class_files, core_file_command,
322 "Use FILE as core dump for examining memory and registers.\n\
dd3b648e 323No arg means have no core file. This command has been superseded by the\n\
df0f0dcc
JK
324`target core' and `detach' commands.", &cmdlist);
325 c->completer = filename_completer;
0685d95f
JK
326
327 c = add_set_cmd ("gnutarget", class_files, var_string_noescape,
328 (char *) &gnutarget_string,
329 "Set the current BFD target.\n\
330Use `set gnutarget auto' to specify automatic detection.",
331 &setlist);
332 c->function.sfunc = set_gnutarget_command;
333 add_show_from_set (c, &showlist);
334
335 if (getenv ("GNUTARGET"))
336 set_gnutarget (getenv ("GNUTARGET"));
337 else
338 set_gnutarget ("auto");
dd3b648e 339}
This page took 0.192765 seconds and 4 git commands to generate.