check makefile.in for arc, rce stuff
[deliverable/binutils-gdb.git] / gdb / core.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., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include <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 /* Hook for `exec_file_command' command to call. */
40
41 void (*exec_file_display_hook) PARAMS ((char *)) = NULL;
42
43 /* Binary file diddling handle for the core file. */
44
45 bfd *core_bfd = NULL;
46
47 \f
48 /* Backward compatability with old way of specifying core files. */
49
50 void
51 core_file_command (filename, from_tty)
52 char *filename;
53 int from_tty;
54 {
55 struct target_ops *t;
56
57 dont_repeat (); /* Either way, seems bogus. */
58
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);
65 else
66 error ("GDB can't read core files on this machine.");
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
73 void
74 specify_exec_file_hook (hook)
75 void (*hook) PARAMS ((char *));
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
84 void
85 close_exec_file ()
86 {
87 #ifdef FIXME
88 if (exec_bfd)
89 bfd_tempclose (exec_bfd);
90 #endif
91 }
92
93 void
94 reopen_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,
103 print a warning if they don't go together. */
104
105 void
106 validate_files ()
107 {
108 if (exec_bfd && core_bfd)
109 {
110 if (!core_file_matches_executable_p (core_bfd, exec_bfd))
111 warning ("core file may not match specified executable file.");
112 else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
113 warning ("exec file is newer than core file.");
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
121 char *
122 get_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\
129 Use the \"file\" or \"exec-file\" command.");
130 return NULL;
131 }
132
133 \f
134 /* Report a memory error with error(). */
135
136 void
137 memory_error (status, memaddr)
138 int status;
139 CORE_ADDR memaddr;
140 {
141 if (status == EIO)
142 {
143 /* Actually, address between memaddr and memaddr + len
144 was out of bounds. */
145 error_begin ();
146 printf_filtered ("Cannot access memory at address ");
147 print_address_numeric (memaddr, 1, gdb_stdout);
148 printf_filtered (".\n");
149 return_to_top_level (RETURN_ERROR);
150 }
151 else
152 {
153 error_begin ();
154 printf_filtered ("Error accessing memory address ");
155 print_address_numeric (memaddr, 1, gdb_stdout);
156 printf_filtered (": %s.\n",
157 safe_strerror (status));
158 return_to_top_level (RETURN_ERROR);
159 }
160 }
161
162 /* Same as target_read_memory, but report an error if can't read. */
163 void
164 read_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
175 /* Like target_read_memory, but slightly different parameters. */
176
177 int
178 dis_asm_read_memory (memaddr, myaddr, len, info)
179 bfd_vma memaddr;
180 bfd_byte *myaddr;
181 int len;
182 disassemble_info *info;
183 {
184 if (dis_asm_read_memory_hook)
185 return dis_asm_read_memory_hook (memaddr, myaddr, len, info);
186
187 return target_read_memory (memaddr, (char *) myaddr, len);
188 }
189
190 /* Like memory_error with slightly different parameters. */
191 void
192 dis_asm_memory_error (status, memaddr, info)
193 int status;
194 bfd_vma memaddr;
195 disassemble_info *info;
196 {
197 memory_error (status, memaddr);
198 }
199
200 /* Like print_address with slightly different parameters. */
201 void
202 dis_asm_print_address (addr, info)
203 bfd_vma addr;
204 struct disassemble_info *info;
205 {
206 print_address (addr, info->stream);
207 }
208
209 /* Same as target_write_memory, but report an error if can't write. */
210 void
211 write_memory (memaddr, myaddr, len)
212 CORE_ADDR memaddr;
213 char *myaddr;
214 int len;
215 {
216 int status;
217
218 status = target_write_memory (memaddr, myaddr, len);
219 if (status != 0)
220 memory_error (status, memaddr);
221 }
222
223 /* Read an integer from debugged memory, given address and number of bytes. */
224
225 LONGEST
226 read_memory_integer (memaddr, len)
227 CORE_ADDR memaddr;
228 int len;
229 {
230 char buf[sizeof (LONGEST)];
231
232 read_memory (memaddr, buf, len);
233 return extract_signed_integer (buf, len);
234 }
235
236 unsigned LONGEST
237 read_memory_unsigned_integer (memaddr, len)
238 CORE_ADDR memaddr;
239 int len;
240 {
241 char buf[sizeof (unsigned LONGEST)];
242
243 read_memory (memaddr, buf, len);
244 return extract_unsigned_integer (buf, len);
245 }
246 \f
247 #if 0
248 /* Enable after 4.12. It is not tested. */
249
250 /* Search code. Targets can just make this their search function, or
251 if the protocol has a less general search function, they can call this
252 in the cases it can't handle. */
253 void
254 generic_search (len, data, mask, startaddr, increment, lorange, hirange
255 addr_found, data_found)
256 int len;
257 char *data;
258 char *mask;
259 CORE_ADDR startaddr;
260 int increment;
261 CORE_ADDR lorange;
262 CORE_ADDR hirange;
263 CORE_ADDR *addr_found;
264 char *data_found;
265 {
266 int i;
267 CORE_ADDR curaddr = startaddr;
268
269 while (curaddr >= lorange && curaddr < hirange)
270 {
271 read_memory (curaddr, data_found, len);
272 for (i = 0; i < len; ++i)
273 if ((data_found[i] & mask[i]) != data[i])
274 goto try_again;
275 /* It matches. */
276 *addr_found = curaddr;
277 return;
278
279 try_again:
280 curaddr += increment;
281 }
282 *addr_found = (CORE_ADDR)0;
283 return;
284 }
285 #endif /* 0 */
286 \f
287 /* The current default bfd target. Points to storage allocated for
288 gnutarget_string. */
289 char *gnutarget;
290
291 /* Same thing, except it is "auto" not NULL for the default case. */
292 static char *gnutarget_string;
293
294 static void set_gnutarget_command
295 PARAMS ((char *, int, struct cmd_list_element *));
296
297 static void
298 set_gnutarget_command (ignore, from_tty, c)
299 char *ignore;
300 int from_tty;
301 struct cmd_list_element *c;
302 {
303 if (STREQ (gnutarget_string, "auto"))
304 gnutarget = NULL;
305 else
306 gnutarget = gnutarget_string;
307 }
308
309 /* Set the gnutarget. */
310 void
311 set_gnutarget (newtarget)
312 char *newtarget;
313 {
314 if (gnutarget_string != NULL)
315 free (gnutarget_string);
316 gnutarget_string = savestring (newtarget, strlen (newtarget));
317 set_gnutarget_command (NULL, 0, NULL);
318 }
319
320 void
321 _initialize_core()
322 {
323 struct cmd_list_element *c;
324 c = add_cmd ("core-file", class_files, core_file_command,
325 "Use FILE as core dump for examining memory and registers.\n\
326 No arg means have no core file. This command has been superseded by the\n\
327 `target core' and `detach' commands.", &cmdlist);
328 c->completer = filename_completer;
329
330 c = add_set_cmd ("gnutarget", class_files, var_string_noescape,
331 (char *) &gnutarget_string,
332 "Set the current BFD target.\n\
333 Use `set gnutarget auto' to specify automatic detection.",
334 &setlist);
335 c->function.sfunc = set_gnutarget_command;
336 add_show_from_set (c, &showlist);
337
338 if (getenv ("GNUTARGET"))
339 set_gnutarget (getenv ("GNUTARGET"));
340 else
341 set_gnutarget ("auto");
342 }
This page took 0.041617 seconds and 4 git commands to generate.