* complaints.c: New file, code moved from utils.c.
[deliverable/binutils-gdb.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include <errno.h>
22 #include <signal.h>
23 #include <fcntl.h>
24 #include "frame.h" /* required by inferior.h */
25 #include "inferior.h"
26 #include "symtab.h"
27 #include "command.h"
28 #include "bfd.h"
29 #include "target.h"
30 #include "gdbcore.h"
31
32 static void
33 core_files_info PARAMS ((struct target_ops *));
34
35 #ifdef SOLIB_ADD
36 static int
37 solib_add_stub PARAMS ((char *));
38 #endif
39
40 static void
41 core_close PARAMS ((int));
42
43 static void
44 get_core_registers PARAMS ((int));
45
46 /* Discard all vestiges of any previous core file
47 and mark data and stack spaces as empty. */
48
49 /* ARGSUSED */
50 static void
51 core_close (quitting)
52 int quitting;
53 {
54 if (core_bfd) {
55 free (bfd_get_filename (core_bfd));
56 bfd_close (core_bfd);
57 core_bfd = NULL;
58 #ifdef CLEAR_SOLIB
59 CLEAR_SOLIB ();
60 #endif
61 if (core_ops.to_sections) {
62 free ((PTR)core_ops.to_sections);
63 core_ops.to_sections = NULL;
64 core_ops.to_sections_end = NULL;
65 }
66 }
67 }
68
69 #ifdef SOLIB_ADD
70 /* Stub function for catch_errors around shared library hacking. */
71
72 static int
73 solib_add_stub (from_tty)
74 char *from_tty;
75 {
76 SOLIB_ADD (NULL, (int)from_tty, &core_ops);
77 return 0;
78 }
79 #endif /* SOLIB_ADD */
80
81 /* This routine opens and sets up the core file bfd */
82
83 void
84 core_open (filename, from_tty)
85 char *filename;
86 int from_tty;
87 {
88 const char *p;
89 int siggy;
90 struct cleanup *old_chain;
91 char *temp;
92 bfd *temp_bfd;
93 int ontop;
94 int scratch_chan;
95
96 target_preopen (from_tty);
97 if (!filename)
98 {
99 error (core_bfd?
100 "No core file specified. (Use `detach' to stop debugging a core file.)"
101 : "No core file specified.");
102 }
103
104 filename = tilde_expand (filename);
105 if (filename[0] != '/') {
106 temp = concat (current_directory, "/", filename, NULL);
107 free (filename);
108 filename = temp;
109 }
110
111 old_chain = make_cleanup (free, filename);
112
113 scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
114 if (scratch_chan < 0)
115 perror_with_name (filename);
116
117 temp_bfd = bfd_fdopenr (filename, NULL, scratch_chan);
118 if (temp_bfd == NULL)
119 {
120 perror_with_name (filename);
121 }
122
123 if (!bfd_check_format (temp_bfd, bfd_core))
124 {
125 /* Do it after the err msg */
126 make_cleanup (bfd_close, temp_bfd);
127 error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
128 }
129
130 /* Looks semi-reasonable. Toss the old core file and work on the new. */
131
132 discard_cleanups (old_chain); /* Don't free filename any more */
133 unpush_target (&core_ops);
134 core_bfd = temp_bfd;
135 old_chain = make_cleanup (core_close, core_bfd);
136
137 validate_files ();
138
139 /* Find the data section */
140 if (build_section_table (core_bfd, &core_ops.to_sections,
141 &core_ops.to_sections_end))
142 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
143 bfd_errmsg (bfd_error));
144
145 ontop = !push_target (&core_ops);
146 discard_cleanups (old_chain);
147
148 p = bfd_core_file_failing_command (core_bfd);
149 if (p)
150 printf_filtered ("Core was generated by `%s'.\n", p);
151
152 siggy = bfd_core_file_failing_signal (core_bfd);
153 if (siggy > 0)
154 printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
155 safe_strsignal (siggy));
156
157 if (ontop) {
158 /* Fetch all registers from core file */
159 target_fetch_registers (-1);
160
161 /* Add symbols and section mappings for any shared libraries */
162 #ifdef SOLIB_ADD
163 catch_errors (solib_add_stub, (char *)from_tty, (char *)0);
164 #endif
165
166 /* Now, set up the frame cache, and print the top of stack */
167 set_current_frame (create_new_frame (read_register (FP_REGNUM),
168 read_pc ()));
169 select_frame (get_current_frame (), 0);
170 print_stack_frame (selected_frame, selected_frame_level, 1);
171 } else {
172 warning (
173 "you won't be able to access this core file until you terminate\n\
174 your %s; do ``info files''", current_target->to_longname);
175 }
176 }
177
178 void
179 core_detach (args, from_tty)
180 char *args;
181 int from_tty;
182 {
183 if (args)
184 error ("Too many arguments");
185 unpush_target (&core_ops);
186 if (from_tty)
187 printf_filtered ("No core file now.\n");
188 }
189
190 /* Get the registers out of a core file. This is the machine-
191 independent part. Fetch_core_registers is the machine-dependent
192 part, typically implemented in the xm-file for each architecture. */
193
194 /* We just get all the registers, so we don't use regno. */
195 /* ARGSUSED */
196 static void
197 get_core_registers (regno)
198 int regno;
199 {
200 sec_ptr reg_sec;
201 unsigned size;
202 char *the_regs;
203
204 reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
205 if (!reg_sec) goto cant;
206 size = bfd_section_size (core_bfd, reg_sec);
207 the_regs = alloca (size);
208 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
209 {
210 fetch_core_registers (the_regs, size, 0,
211 (unsigned) bfd_section_vma (abfd,reg_sec));
212 }
213 else
214 {
215 cant:
216 fprintf_filtered (stderr, "Couldn't fetch registers from core file: %s\n",
217 bfd_errmsg (bfd_error));
218 }
219
220 /* Now do it again for the float registers, if they exist. */
221 reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
222 if (reg_sec) {
223 size = bfd_section_size (core_bfd, reg_sec);
224 the_regs = alloca (size);
225 if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
226 size))
227 {
228 fetch_core_registers (the_regs, size, 2,
229 (unsigned) bfd_section_vma (abfd,reg_sec));
230 }
231 else
232 {
233 fprintf_filtered (stderr, "Couldn't fetch register set 2 from core file: %s\n",
234 bfd_errmsg (bfd_error));
235 }
236 }
237 registers_fetched();
238 }
239
240 static void
241 core_files_info (t)
242 struct target_ops *t;
243 {
244 print_section_info (t, core_bfd);
245 }
246 \f
247 struct target_ops core_ops = {
248 "core", "Local core dump file",
249 "Use a core file as a target. Specify the filename of the core file.",
250 core_open, core_close,
251 find_default_attach, core_detach, 0, 0, /* resume, wait */
252 get_core_registers,
253 0, 0, /* store_regs, prepare_to_store */
254 xfer_memory, core_files_info,
255 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
256 0, 0, 0, 0, 0, /* terminal stuff */
257 0, 0, 0, /* kill, load, lookup sym */
258 find_default_create_inferior, 0, /* mourn_inferior */
259 0, /* can_run */
260 0, /* notice_signals */
261 core_stratum, 0, /* next */
262 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
263 0, 0, /* section pointers */
264 OPS_MAGIC, /* Always the last thing */
265 };
266
267 void
268 _initialize_corelow()
269 {
270 add_target (&core_ops);
271 }
This page took 0.044062 seconds and 4 git commands to generate.