Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Core dump and executable file functions below target vector, for GDB. |
4646aa9d | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
7b6bb8da JB |
4 | 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, |
5 | 2011 Free Software Foundation, Inc. | |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
0e24ac5d | 23 | #include "arch-utils.h" |
c906108c SS |
24 | #include "gdb_string.h" |
25 | #include <errno.h> | |
26 | #include <signal.h> | |
27 | #include <fcntl.h> | |
fc24370e MS |
28 | #ifdef HAVE_SYS_FILE_H |
29 | #include <sys/file.h> /* needed for F_OK and friends */ | |
30 | #endif | |
c5aa993b | 31 | #include "frame.h" /* required by inferior.h */ |
c906108c SS |
32 | #include "inferior.h" |
33 | #include "symtab.h" | |
34 | #include "command.h" | |
35 | #include "bfd.h" | |
36 | #include "target.h" | |
37 | #include "gdbcore.h" | |
38 | #include "gdbthread.h" | |
4e052eda | 39 | #include "regcache.h" |
0e24ac5d | 40 | #include "regset.h" |
990f9fe3 | 41 | #include "symfile.h" |
4646aa9d | 42 | #include "exec.h" |
dbda9972 | 43 | #include "readline/readline.h" |
0e24ac5d | 44 | #include "gdb_assert.h" |
60250e8b | 45 | #include "exceptions.h" |
a77053c2 | 46 | #include "solib.h" |
f90c07ac | 47 | #include "filenames.h" |
6c95b8df | 48 | #include "progspace.h" |
516ba659 | 49 | #include "objfiles.h" |
0e24ac5d | 50 | |
8e860359 | 51 | |
ee28ca0f AC |
52 | #ifndef O_LARGEFILE |
53 | #define O_LARGEFILE 0 | |
54 | #endif | |
55 | ||
00e32a35 AC |
56 | /* List of all available core_fns. On gdb startup, each core file |
57 | register reader calls deprecated_add_core_fns() to register | |
58 | information on each core format it is prepared to read. */ | |
c906108c SS |
59 | |
60 | static struct core_fns *core_file_fns = NULL; | |
61 | ||
aff410f1 MS |
62 | /* The core_fns for a core file handler that is prepared to read the |
63 | core file currently open on core_bfd. */ | |
2acceee2 JM |
64 | |
65 | static struct core_fns *core_vec = NULL; | |
66 | ||
0e24ac5d MK |
67 | /* FIXME: kettenis/20031023: Eventually this variable should |
68 | disappear. */ | |
69 | ||
70 | struct gdbarch *core_gdbarch = NULL; | |
71 | ||
07b82ea5 PA |
72 | /* Per-core data. Currently, only the section table. Note that these |
73 | target sections are *not* mapped in the current address spaces' set | |
74 | of target sections --- those should come only from pure executable | |
75 | or shared library bfds. The core bfd sections are an | |
76 | implementation detail of the core target, just like ptrace is for | |
77 | unix child targets. */ | |
78 | static struct target_section_table *core_data; | |
79 | ||
3cdd9356 PA |
80 | /* True if we needed to fake the pid of the loaded core inferior. */ |
81 | static int core_has_fake_pid = 0; | |
82 | ||
a14ed312 | 83 | static void core_files_info (struct target_ops *); |
c906108c | 84 | |
a14ed312 | 85 | static struct core_fns *sniff_core_bfd (bfd *); |
2acceee2 | 86 | |
020cc13c | 87 | static int gdb_check_format (bfd *); |
2acceee2 | 88 | |
a14ed312 | 89 | static void core_open (char *, int); |
c906108c | 90 | |
136d6dae | 91 | static void core_detach (struct target_ops *ops, char *, int); |
c906108c | 92 | |
a14ed312 | 93 | static void core_close (int); |
c906108c | 94 | |
74b7792f AC |
95 | static void core_close_cleanup (void *ignore); |
96 | ||
4efb68b1 | 97 | static void add_to_thread_list (bfd *, asection *, void *); |
c906108c | 98 | |
a14ed312 | 99 | static void init_core_ops (void); |
c906108c | 100 | |
a14ed312 | 101 | void _initialize_corelow (void); |
c906108c | 102 | |
c0edd9ed | 103 | static struct target_ops core_ops; |
c906108c | 104 | |
7f9f62ba PA |
105 | /* An arbitrary identifier for the core inferior. */ |
106 | #define CORELOW_PID 1 | |
107 | ||
aff410f1 MS |
108 | /* Link a new core_fns into the global core_file_fns list. Called on |
109 | gdb startup by the _initialize routine in each core file register | |
b021a221 | 110 | reader, to register information about each format the reader is |
aff410f1 | 111 | prepared to handle. */ |
c906108c SS |
112 | |
113 | void | |
00e32a35 | 114 | deprecated_add_core_fns (struct core_fns *cf) |
c906108c | 115 | { |
c5aa993b | 116 | cf->next = core_file_fns; |
c906108c SS |
117 | core_file_fns = cf; |
118 | } | |
119 | ||
2acceee2 JM |
120 | /* The default function that core file handlers can use to examine a |
121 | core file BFD and decide whether or not to accept the job of | |
aff410f1 | 122 | reading the core file. */ |
2acceee2 JM |
123 | |
124 | int | |
fba45db2 | 125 | default_core_sniffer (struct core_fns *our_fns, bfd *abfd) |
2acceee2 JM |
126 | { |
127 | int result; | |
128 | ||
129 | result = (bfd_get_flavour (abfd) == our_fns -> core_flavour); | |
130 | return (result); | |
131 | } | |
132 | ||
133 | /* Walk through the list of core functions to find a set that can | |
134 | handle the core file open on ABFD. Default to the first one in the | |
ee923081 | 135 | list if nothing matches. Returns pointer to set that is |
aff410f1 | 136 | selected. */ |
2acceee2 JM |
137 | |
138 | static struct core_fns * | |
fba45db2 | 139 | sniff_core_bfd (bfd *abfd) |
2acceee2 JM |
140 | { |
141 | struct core_fns *cf; | |
142 | struct core_fns *yummy = NULL; | |
143 | int matches = 0;; | |
144 | ||
aff410f1 MS |
145 | /* Don't sniff if we have support for register sets in |
146 | CORE_GDBARCH. */ | |
0e24ac5d MK |
147 | if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch)) |
148 | return NULL; | |
149 | ||
2acceee2 JM |
150 | for (cf = core_file_fns; cf != NULL; cf = cf->next) |
151 | { | |
152 | if (cf->core_sniffer (cf, abfd)) | |
153 | { | |
154 | yummy = cf; | |
155 | matches++; | |
156 | } | |
157 | } | |
158 | if (matches > 1) | |
159 | { | |
8a3fe4f8 | 160 | warning (_("\"%s\": ambiguous core format, %d handlers match"), |
2acceee2 JM |
161 | bfd_get_filename (abfd), matches); |
162 | } | |
163 | else if (matches == 0) | |
164 | { | |
3e43a32a MS |
165 | warning (_("\"%s\": no core file handler " |
166 | "recognizes format, using default"), | |
2acceee2 JM |
167 | bfd_get_filename (abfd)); |
168 | } | |
169 | if (yummy == NULL) | |
170 | { | |
171 | yummy = core_file_fns; | |
172 | } | |
173 | return (yummy); | |
174 | } | |
175 | ||
176 | /* The default is to reject every core file format we see. Either | |
177 | BFD has to recognize it, or we have to provide a function in the | |
aff410f1 | 178 | core file handler that recognizes it. */ |
2acceee2 JM |
179 | |
180 | int | |
fba45db2 | 181 | default_check_format (bfd *abfd) |
2acceee2 JM |
182 | { |
183 | return (0); | |
184 | } | |
185 | ||
aff410f1 | 186 | /* Attempt to recognize core file formats that BFD rejects. */ |
2acceee2 | 187 | |
020cc13c | 188 | static int |
fba45db2 | 189 | gdb_check_format (bfd *abfd) |
2acceee2 JM |
190 | { |
191 | struct core_fns *cf; | |
192 | ||
193 | for (cf = core_file_fns; cf != NULL; cf = cf->next) | |
194 | { | |
195 | if (cf->check_format (abfd)) | |
196 | { | |
81a9a963 | 197 | return (1); |
2acceee2 JM |
198 | } |
199 | } | |
81a9a963 | 200 | return (0); |
2acceee2 | 201 | } |
c906108c | 202 | |
aff410f1 MS |
203 | /* Discard all vestiges of any previous core file and mark data and |
204 | stack spaces as empty. */ | |
c906108c | 205 | |
c906108c | 206 | static void |
fba45db2 | 207 | core_close (int quitting) |
c906108c SS |
208 | { |
209 | char *name; | |
210 | ||
211 | if (core_bfd) | |
212 | { | |
959b8724 | 213 | int pid = ptid_get_pid (inferior_ptid); |
aff410f1 MS |
214 | inferior_ptid = null_ptid; /* Avoid confusion from thread |
215 | stuff. */ | |
6c95b8df | 216 | exit_inferior_silent (pid); |
c906108c | 217 | |
aff410f1 MS |
218 | /* Clear out solib state while the bfd is still open. See |
219 | comments in clear_solib in solib.c. */ | |
a77053c2 | 220 | clear_solib (); |
7a292a7a | 221 | |
07b82ea5 PA |
222 | xfree (core_data->sections); |
223 | xfree (core_data); | |
224 | core_data = NULL; | |
3cdd9356 | 225 | core_has_fake_pid = 0; |
07b82ea5 | 226 | |
c906108c | 227 | name = bfd_get_filename (core_bfd); |
516ba659 | 228 | gdb_bfd_close_or_warn (core_bfd); |
b8c9b27d | 229 | xfree (name); |
c906108c | 230 | core_bfd = NULL; |
c906108c | 231 | } |
2acceee2 | 232 | core_vec = NULL; |
0e24ac5d | 233 | core_gdbarch = NULL; |
c906108c SS |
234 | } |
235 | ||
74b7792f AC |
236 | static void |
237 | core_close_cleanup (void *ignore) | |
238 | { | |
239 | core_close (0/*ignored*/); | |
240 | } | |
241 | ||
aff410f1 MS |
242 | /* Look for sections whose names start with `.reg/' so that we can |
243 | extract the list of threads in a core file. */ | |
c906108c SS |
244 | |
245 | static void | |
4efb68b1 | 246 | add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg) |
c906108c | 247 | { |
0de3b513 | 248 | ptid_t ptid; |
3cdd9356 PA |
249 | int core_tid; |
250 | int pid, lwpid; | |
c906108c SS |
251 | asection *reg_sect = (asection *) reg_sect_arg; |
252 | ||
253 | if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) | |
254 | return; | |
255 | ||
3cdd9356 | 256 | core_tid = atoi (bfd_section_name (abfd, asect) + 5); |
c906108c | 257 | |
261b8d08 PA |
258 | pid = bfd_core_file_pid (core_bfd); |
259 | if (pid == 0) | |
3cdd9356 PA |
260 | { |
261 | core_has_fake_pid = 1; | |
262 | pid = CORELOW_PID; | |
3cdd9356 | 263 | } |
0de3b513 | 264 | |
261b8d08 PA |
265 | lwpid = core_tid; |
266 | ||
6c95b8df PA |
267 | if (current_inferior ()->pid == 0) |
268 | inferior_appeared (current_inferior (), pid); | |
3cdd9356 PA |
269 | |
270 | ptid = ptid_build (pid, lwpid, 0); | |
271 | ||
272 | add_thread (ptid); | |
c906108c SS |
273 | |
274 | /* Warning, Will Robinson, looking at BFD private data! */ | |
275 | ||
276 | if (reg_sect != NULL | |
aff410f1 MS |
277 | && asect->filepos == reg_sect->filepos) /* Did we find .reg? */ |
278 | inferior_ptid = ptid; /* Yes, make it current. */ | |
c906108c SS |
279 | } |
280 | ||
281 | /* This routine opens and sets up the core file bfd. */ | |
282 | ||
283 | static void | |
fba45db2 | 284 | core_open (char *filename, int from_tty) |
c906108c SS |
285 | { |
286 | const char *p; | |
287 | int siggy; | |
288 | struct cleanup *old_chain; | |
289 | char *temp; | |
290 | bfd *temp_bfd; | |
c906108c | 291 | int scratch_chan; |
ee28ca0f | 292 | int flags; |
c906108c SS |
293 | |
294 | target_preopen (from_tty); | |
295 | if (!filename) | |
296 | { | |
8a3fe4f8 | 297 | if (core_bfd) |
3e43a32a MS |
298 | error (_("No core file specified. (Use `detach' " |
299 | "to stop debugging a core file.)")); | |
8a3fe4f8 AC |
300 | else |
301 | error (_("No core file specified.")); | |
c906108c SS |
302 | } |
303 | ||
304 | filename = tilde_expand (filename); | |
aff410f1 | 305 | if (!IS_ABSOLUTE_PATH (filename)) |
c906108c | 306 | { |
aff410f1 MS |
307 | temp = concat (current_directory, "/", |
308 | filename, (char *) NULL); | |
b8c9b27d | 309 | xfree (filename); |
c906108c SS |
310 | filename = temp; |
311 | } | |
312 | ||
b8c9b27d | 313 | old_chain = make_cleanup (xfree, filename); |
c906108c | 314 | |
ee28ca0f AC |
315 | flags = O_BINARY | O_LARGEFILE; |
316 | if (write_files) | |
317 | flags |= O_RDWR; | |
318 | else | |
319 | flags |= O_RDONLY; | |
320 | scratch_chan = open (filename, flags, 0); | |
c906108c SS |
321 | if (scratch_chan < 0) |
322 | perror_with_name (filename); | |
323 | ||
9f76c2cd MM |
324 | temp_bfd = bfd_fopen (filename, gnutarget, |
325 | write_files ? FOPEN_RUB : FOPEN_RB, | |
326 | scratch_chan); | |
c906108c SS |
327 | if (temp_bfd == NULL) |
328 | perror_with_name (filename); | |
329 | ||
5aafa1cc PM |
330 | if (!bfd_check_format (temp_bfd, bfd_core) |
331 | && !gdb_check_format (temp_bfd)) | |
c906108c SS |
332 | { |
333 | /* Do it after the err msg */ | |
aff410f1 MS |
334 | /* FIXME: should be checking for errors from bfd_close (for one |
335 | thing, on error it does not free all the storage associated | |
336 | with the bfd). */ | |
5c65bbb6 | 337 | make_cleanup_bfd_close (temp_bfd); |
8a3fe4f8 | 338 | error (_("\"%s\" is not a core dump: %s"), |
c906108c SS |
339 | filename, bfd_errmsg (bfd_get_error ())); |
340 | } | |
341 | ||
aff410f1 MS |
342 | /* Looks semi-reasonable. Toss the old core file and work on the |
343 | new. */ | |
c906108c | 344 | |
c5aa993b | 345 | discard_cleanups (old_chain); /* Don't free filename any more */ |
c906108c SS |
346 | unpush_target (&core_ops); |
347 | core_bfd = temp_bfd; | |
74b7792f | 348 | old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/); |
c906108c | 349 | |
0e24ac5d MK |
350 | /* FIXME: kettenis/20031023: This is very dangerous. The |
351 | CORE_GDBARCH that results from this call may very well be | |
352 | different from CURRENT_GDBARCH. However, its methods may only | |
353 | work if it is selected as the current architecture, because they | |
354 | rely on swapped data (see gdbarch.c). We should get rid of that | |
355 | swapped data. */ | |
356 | core_gdbarch = gdbarch_from_bfd (core_bfd); | |
357 | ||
2acceee2 JM |
358 | /* Find a suitable core file handler to munch on core_bfd */ |
359 | core_vec = sniff_core_bfd (core_bfd); | |
360 | ||
c906108c SS |
361 | validate_files (); |
362 | ||
07b82ea5 PA |
363 | core_data = XZALLOC (struct target_section_table); |
364 | ||
c906108c | 365 | /* Find the data section */ |
07b82ea5 | 366 | if (build_section_table (core_bfd, |
aff410f1 MS |
367 | &core_data->sections, |
368 | &core_data->sections_end)) | |
8a3fe4f8 | 369 | error (_("\"%s\": Can't find sections: %s"), |
c906108c SS |
370 | bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ())); |
371 | ||
2f1b5984 MK |
372 | /* If we have no exec file, try to set the architecture from the |
373 | core file. We don't do this unconditionally since an exec file | |
374 | typically contains more information that helps us determine the | |
375 | architecture than a core file. */ | |
376 | if (!exec_bfd) | |
377 | set_gdbarch_from_file (core_bfd); | |
cbda0a99 | 378 | |
87ab71f0 | 379 | push_target (&core_ops); |
c906108c SS |
380 | discard_cleanups (old_chain); |
381 | ||
0de3b513 PA |
382 | /* Do this before acknowledging the inferior, so if |
383 | post_create_inferior throws (can happen easilly if you're loading | |
384 | a core file with the wrong exec), we aren't left with threads | |
385 | from the previous inferior. */ | |
386 | init_thread_list (); | |
387 | ||
3cdd9356 PA |
388 | inferior_ptid = null_ptid; |
389 | core_has_fake_pid = 0; | |
0de3b513 | 390 | |
739fc47a PA |
391 | /* Need to flush the register cache (and the frame cache) from a |
392 | previous debug session. If inferior_ptid ends up the same as the | |
393 | last debug session --- e.g., b foo; run; gcore core1; step; gcore | |
394 | core2; core core1; core core2 --- then there's potential for | |
395 | get_current_regcache to return the cached regcache of the | |
396 | previous session, and the frame cache being stale. */ | |
397 | registers_changed (); | |
398 | ||
0de3b513 PA |
399 | /* Build up thread list from BFD sections, and possibly set the |
400 | current thread to the .reg/NN section matching the .reg | |
aff410f1 | 401 | section. */ |
0de3b513 PA |
402 | bfd_map_over_sections (core_bfd, add_to_thread_list, |
403 | bfd_get_section_by_name (core_bfd, ".reg")); | |
404 | ||
3cdd9356 PA |
405 | if (ptid_equal (inferior_ptid, null_ptid)) |
406 | { | |
407 | /* Either we found no .reg/NN section, and hence we have a | |
408 | non-threaded core (single-threaded, from gdb's perspective), | |
409 | or for some reason add_to_thread_list couldn't determine | |
410 | which was the "main" thread. The latter case shouldn't | |
411 | usually happen, but we're dealing with input here, which can | |
412 | always be broken in different ways. */ | |
413 | struct thread_info *thread = first_thread_of_process (-1); | |
c5504eaf | 414 | |
3cdd9356 PA |
415 | if (thread == NULL) |
416 | { | |
c45ceae0 | 417 | inferior_appeared (current_inferior (), CORELOW_PID); |
3cdd9356 PA |
418 | inferior_ptid = pid_to_ptid (CORELOW_PID); |
419 | add_thread_silent (inferior_ptid); | |
420 | } | |
421 | else | |
422 | switch_to_thread (thread->ptid); | |
423 | } | |
424 | ||
959b8724 PA |
425 | post_create_inferior (&core_ops, from_tty); |
426 | ||
0de3b513 PA |
427 | /* Now go through the target stack looking for threads since there |
428 | may be a thread_stratum target loaded on top of target core by | |
429 | now. The layer above should claim threads found in the BFD | |
430 | sections. */ | |
431 | target_find_new_threads (); | |
432 | ||
c906108c SS |
433 | p = bfd_core_file_failing_command (core_bfd); |
434 | if (p) | |
a3f17187 | 435 | printf_filtered (_("Core was generated by `%s'.\n"), p); |
c906108c SS |
436 | |
437 | siggy = bfd_core_file_failing_signal (core_bfd); | |
438 | if (siggy > 0) | |
423ec54c | 439 | { |
aff410f1 MS |
440 | /* NOTE: target_signal_from_host() converts a target signal |
441 | value into gdb's internal signal value. Unfortunately gdb's | |
442 | internal value is called ``target_signal'' and this function | |
443 | got the name ..._from_host(). */ | |
423ec54c | 444 | enum target_signal sig = (core_gdbarch != NULL |
aff410f1 MS |
445 | ? gdbarch_target_signal_from_host (core_gdbarch, |
446 | siggy) | |
423ec54c JK |
447 | : target_signal_from_host (siggy)); |
448 | ||
aff410f1 MS |
449 | printf_filtered (_("Program terminated with signal %d, %s.\n"), |
450 | siggy, target_signal_to_string (sig)); | |
423ec54c | 451 | } |
c906108c | 452 | |
87ab71f0 PA |
453 | /* Fetch all registers from core file. */ |
454 | target_fetch_registers (get_current_regcache (), -1); | |
c906108c | 455 | |
87ab71f0 PA |
456 | /* Now, set up the frame cache, and print the top of stack. */ |
457 | reinit_frame_cache (); | |
458 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); | |
c906108c SS |
459 | } |
460 | ||
461 | static void | |
136d6dae | 462 | core_detach (struct target_ops *ops, char *args, int from_tty) |
c906108c SS |
463 | { |
464 | if (args) | |
8a3fe4f8 | 465 | error (_("Too many arguments")); |
136d6dae | 466 | unpush_target (ops); |
c906108c SS |
467 | reinit_frame_cache (); |
468 | if (from_tty) | |
a3f17187 | 469 | printf_filtered (_("No core file now.\n")); |
c906108c SS |
470 | } |
471 | ||
07b82ea5 PA |
472 | #ifdef DEPRECATED_IBM6000_TARGET |
473 | ||
474 | /* Resize the core memory's section table, by NUM_ADDED. Returns a | |
475 | pointer into the first new slot. This will not be necessary when | |
476 | the rs6000 target is converted to use the standard solib | |
477 | framework. */ | |
478 | ||
479 | struct target_section * | |
480 | deprecated_core_resize_section_table (int num_added) | |
481 | { | |
482 | int old_count; | |
483 | ||
484 | old_count = resize_section_table (core_data, num_added); | |
485 | return core_data->sections + old_count; | |
486 | } | |
487 | ||
488 | #endif | |
de57eccd JM |
489 | |
490 | /* Try to retrieve registers from a section in core_bfd, and supply | |
491 | them to core_vec->core_read_registers, as the register set numbered | |
492 | WHICH. | |
493 | ||
0de3b513 PA |
494 | If inferior_ptid's lwp member is zero, do the single-threaded |
495 | thing: look for a section named NAME. If inferior_ptid's lwp | |
496 | member is non-zero, do the multi-threaded thing: look for a section | |
497 | named "NAME/LWP", where LWP is the shortest ASCII decimal | |
498 | representation of inferior_ptid's lwp member. | |
de57eccd JM |
499 | |
500 | HUMAN_NAME is a human-readable name for the kind of registers the | |
501 | NAME section contains, for use in error messages. | |
502 | ||
503 | If REQUIRED is non-zero, print an error if the core file doesn't | |
aff410f1 MS |
504 | have a section by the appropriate name. Otherwise, just do |
505 | nothing. */ | |
de57eccd JM |
506 | |
507 | static void | |
9eefc95f | 508 | get_core_register_section (struct regcache *regcache, |
1b1818e4 | 509 | const char *name, |
de57eccd | 510 | int which, |
1b1818e4 | 511 | const char *human_name, |
de57eccd JM |
512 | int required) |
513 | { | |
3ecda457 | 514 | static char *section_name = NULL; |
7be0c536 | 515 | struct bfd_section *section; |
de57eccd JM |
516 | bfd_size_type size; |
517 | char *contents; | |
518 | ||
3ecda457 | 519 | xfree (section_name); |
959b8724 | 520 | |
261b8d08 | 521 | if (ptid_get_lwp (inferior_ptid)) |
aff410f1 MS |
522 | section_name = xstrprintf ("%s/%ld", name, |
523 | ptid_get_lwp (inferior_ptid)); | |
de57eccd | 524 | else |
3ecda457 | 525 | section_name = xstrdup (name); |
de57eccd JM |
526 | |
527 | section = bfd_get_section_by_name (core_bfd, section_name); | |
528 | if (! section) | |
529 | { | |
530 | if (required) | |
aff410f1 MS |
531 | warning (_("Couldn't find %s registers in core file."), |
532 | human_name); | |
de57eccd JM |
533 | return; |
534 | } | |
535 | ||
536 | size = bfd_section_size (core_bfd, section); | |
537 | contents = alloca (size); | |
538 | if (! bfd_get_section_contents (core_bfd, section, contents, | |
539 | (file_ptr) 0, size)) | |
540 | { | |
8a3fe4f8 | 541 | warning (_("Couldn't read %s registers from `%s' section in core file."), |
de57eccd JM |
542 | human_name, name); |
543 | return; | |
544 | } | |
545 | ||
0e24ac5d MK |
546 | if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch)) |
547 | { | |
548 | const struct regset *regset; | |
549 | ||
aff410f1 MS |
550 | regset = gdbarch_regset_from_core_section (core_gdbarch, |
551 | name, size); | |
0e24ac5d MK |
552 | if (regset == NULL) |
553 | { | |
554 | if (required) | |
8a3fe4f8 | 555 | warning (_("Couldn't recognize %s registers in core file."), |
0e24ac5d MK |
556 | human_name); |
557 | return; | |
558 | } | |
559 | ||
9eefc95f | 560 | regset->supply_regset (regset, regcache, -1, contents, size); |
0e24ac5d MK |
561 | return; |
562 | } | |
563 | ||
564 | gdb_assert (core_vec); | |
9eefc95f | 565 | core_vec->core_read_registers (regcache, contents, size, which, |
de57eccd JM |
566 | ((CORE_ADDR) |
567 | bfd_section_vma (core_bfd, section))); | |
568 | } | |
569 | ||
570 | ||
c906108c SS |
571 | /* Get the registers out of a core file. This is the machine- |
572 | independent part. Fetch_core_registers is the machine-dependent | |
aff410f1 MS |
573 | part, typically implemented in the xm-file for each |
574 | architecture. */ | |
c906108c SS |
575 | |
576 | /* We just get all the registers, so we don't use regno. */ | |
577 | ||
c906108c | 578 | static void |
28439f5e PA |
579 | get_core_registers (struct target_ops *ops, |
580 | struct regcache *regcache, int regno) | |
c906108c | 581 | { |
1b1818e4 | 582 | struct core_regset_section *sect_list; |
9c5ea4d9 | 583 | int i; |
c906108c | 584 | |
0e24ac5d MK |
585 | if (!(core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch)) |
586 | && (core_vec == NULL || core_vec->core_read_registers == NULL)) | |
c906108c SS |
587 | { |
588 | fprintf_filtered (gdb_stderr, | |
c5aa993b | 589 | "Can't fetch registers from this type of core file\n"); |
c906108c SS |
590 | return; |
591 | } | |
592 | ||
1b1818e4 UW |
593 | sect_list = gdbarch_core_regset_sections (get_regcache_arch (regcache)); |
594 | if (sect_list) | |
595 | while (sect_list->sect_name != NULL) | |
596 | { | |
597 | if (strcmp (sect_list->sect_name, ".reg") == 0) | |
598 | get_core_register_section (regcache, sect_list->sect_name, | |
599 | 0, sect_list->human_name, 1); | |
600 | else if (strcmp (sect_list->sect_name, ".reg2") == 0) | |
601 | get_core_register_section (regcache, sect_list->sect_name, | |
602 | 2, sect_list->human_name, 0); | |
603 | else | |
604 | get_core_register_section (regcache, sect_list->sect_name, | |
605 | 3, sect_list->human_name, 0); | |
606 | ||
607 | sect_list++; | |
608 | } | |
609 | ||
610 | else | |
611 | { | |
612 | get_core_register_section (regcache, | |
613 | ".reg", 0, "general-purpose", 1); | |
614 | get_core_register_section (regcache, | |
615 | ".reg2", 2, "floating-point", 0); | |
616 | } | |
c906108c | 617 | |
ee99023e | 618 | /* Mark all registers not found in the core as unavailable. */ |
13b8769f | 619 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
ee99023e | 620 | if (regcache_register_status (regcache, i) == REG_UNKNOWN) |
9c5ea4d9 | 621 | regcache_raw_supply (regcache, i, NULL); |
c906108c SS |
622 | } |
623 | ||
c906108c | 624 | static void |
fba45db2 | 625 | core_files_info (struct target_ops *t) |
c906108c | 626 | { |
07b82ea5 | 627 | print_section_info (core_data, core_bfd); |
c906108c | 628 | } |
e2544d02 | 629 | \f |
efcbbd14 UW |
630 | struct spuid_list |
631 | { | |
632 | gdb_byte *buf; | |
633 | ULONGEST offset; | |
634 | LONGEST len; | |
635 | ULONGEST pos; | |
636 | ULONGEST written; | |
637 | }; | |
638 | ||
639 | static void | |
640 | add_to_spuid_list (bfd *abfd, asection *asect, void *list_p) | |
641 | { | |
642 | struct spuid_list *list = list_p; | |
643 | enum bfd_endian byte_order | |
aff410f1 | 644 | = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE; |
efcbbd14 UW |
645 | int fd, pos = 0; |
646 | ||
647 | sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos); | |
648 | if (pos == 0) | |
649 | return; | |
650 | ||
651 | if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len) | |
652 | { | |
653 | store_unsigned_integer (list->buf + list->pos - list->offset, | |
654 | 4, byte_order, fd); | |
655 | list->written += 4; | |
656 | } | |
657 | list->pos += 4; | |
658 | } | |
659 | ||
e2544d02 RM |
660 | static LONGEST |
661 | core_xfer_partial (struct target_ops *ops, enum target_object object, | |
961cb7b5 | 662 | const char *annex, gdb_byte *readbuf, |
aff410f1 MS |
663 | const gdb_byte *writebuf, ULONGEST offset, |
664 | LONGEST len) | |
e2544d02 RM |
665 | { |
666 | switch (object) | |
667 | { | |
668 | case TARGET_OBJECT_MEMORY: | |
07b82ea5 PA |
669 | return section_table_xfer_memory_partial (readbuf, writebuf, |
670 | offset, len, | |
671 | core_data->sections, | |
672 | core_data->sections_end, | |
673 | NULL); | |
e2544d02 RM |
674 | |
675 | case TARGET_OBJECT_AUXV: | |
676 | if (readbuf) | |
677 | { | |
678 | /* When the aux vector is stored in core file, BFD | |
679 | represents this with a fake section called ".auxv". */ | |
680 | ||
c4c5b7ba | 681 | struct bfd_section *section; |
e2544d02 | 682 | bfd_size_type size; |
e2544d02 RM |
683 | |
684 | section = bfd_get_section_by_name (core_bfd, ".auxv"); | |
685 | if (section == NULL) | |
686 | return -1; | |
687 | ||
688 | size = bfd_section_size (core_bfd, section); | |
689 | if (offset >= size) | |
690 | return 0; | |
691 | size -= offset; | |
692 | if (size > len) | |
693 | size = len; | |
403e1656 MK |
694 | if (size > 0 |
695 | && !bfd_get_section_contents (core_bfd, section, readbuf, | |
696 | (file_ptr) offset, size)) | |
e2544d02 | 697 | { |
8a3fe4f8 | 698 | warning (_("Couldn't read NT_AUXV note in core file.")); |
e2544d02 RM |
699 | return -1; |
700 | } | |
701 | ||
702 | return size; | |
703 | } | |
704 | return -1; | |
705 | ||
403e1656 MK |
706 | case TARGET_OBJECT_WCOOKIE: |
707 | if (readbuf) | |
708 | { | |
709 | /* When the StackGhost cookie is stored in core file, BFD | |
aff410f1 MS |
710 | represents this with a fake section called |
711 | ".wcookie". */ | |
403e1656 MK |
712 | |
713 | struct bfd_section *section; | |
714 | bfd_size_type size; | |
403e1656 MK |
715 | |
716 | section = bfd_get_section_by_name (core_bfd, ".wcookie"); | |
717 | if (section == NULL) | |
718 | return -1; | |
719 | ||
720 | size = bfd_section_size (core_bfd, section); | |
721 | if (offset >= size) | |
722 | return 0; | |
723 | size -= offset; | |
724 | if (size > len) | |
725 | size = len; | |
726 | if (size > 0 | |
727 | && !bfd_get_section_contents (core_bfd, section, readbuf, | |
728 | (file_ptr) offset, size)) | |
729 | { | |
8a3fe4f8 | 730 | warning (_("Couldn't read StackGhost cookie in core file.")); |
403e1656 MK |
731 | return -1; |
732 | } | |
733 | ||
734 | return size; | |
735 | } | |
736 | return -1; | |
737 | ||
de584861 PA |
738 | case TARGET_OBJECT_LIBRARIES: |
739 | if (core_gdbarch | |
740 | && gdbarch_core_xfer_shared_libraries_p (core_gdbarch)) | |
741 | { | |
742 | if (writebuf) | |
743 | return -1; | |
744 | return | |
745 | gdbarch_core_xfer_shared_libraries (core_gdbarch, | |
746 | readbuf, offset, len); | |
747 | } | |
748 | /* FALL THROUGH */ | |
749 | ||
efcbbd14 UW |
750 | case TARGET_OBJECT_SPU: |
751 | if (readbuf && annex) | |
752 | { | |
753 | /* When the SPU contexts are stored in a core file, BFD | |
aff410f1 MS |
754 | represents this with a fake section called |
755 | "SPU/<annex>". */ | |
efcbbd14 UW |
756 | |
757 | struct bfd_section *section; | |
758 | bfd_size_type size; | |
efcbbd14 | 759 | char sectionstr[100]; |
c5504eaf | 760 | |
efcbbd14 UW |
761 | xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex); |
762 | ||
763 | section = bfd_get_section_by_name (core_bfd, sectionstr); | |
764 | if (section == NULL) | |
765 | return -1; | |
766 | ||
767 | size = bfd_section_size (core_bfd, section); | |
768 | if (offset >= size) | |
769 | return 0; | |
770 | size -= offset; | |
771 | if (size > len) | |
772 | size = len; | |
773 | if (size > 0 | |
774 | && !bfd_get_section_contents (core_bfd, section, readbuf, | |
775 | (file_ptr) offset, size)) | |
776 | { | |
777 | warning (_("Couldn't read SPU section in core file.")); | |
778 | return -1; | |
779 | } | |
780 | ||
781 | return size; | |
782 | } | |
783 | else if (readbuf) | |
784 | { | |
785 | /* NULL annex requests list of all present spuids. */ | |
786 | struct spuid_list list; | |
c5504eaf | 787 | |
efcbbd14 UW |
788 | list.buf = readbuf; |
789 | list.offset = offset; | |
790 | list.len = len; | |
791 | list.pos = 0; | |
792 | list.written = 0; | |
793 | bfd_map_over_sections (core_bfd, add_to_spuid_list, &list); | |
794 | return list.written; | |
795 | } | |
796 | return -1; | |
797 | ||
e2544d02 RM |
798 | default: |
799 | if (ops->beneath != NULL) | |
aff410f1 MS |
800 | return ops->beneath->to_xfer_partial (ops->beneath, object, |
801 | annex, readbuf, | |
802 | writebuf, offset, len); | |
e2544d02 RM |
803 | return -1; |
804 | } | |
805 | } | |
806 | ||
c906108c SS |
807 | \f |
808 | /* If mourn is being called in all the right places, this could be say | |
aff410f1 MS |
809 | `gdb internal error' (since generic_mourn calls |
810 | breakpoint_init_inferior). */ | |
c906108c SS |
811 | |
812 | static int | |
a6d9a66e | 813 | ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt) |
c906108c SS |
814 | { |
815 | return 0; | |
816 | } | |
817 | ||
818 | ||
819 | /* Okay, let's be honest: threads gleaned from a core file aren't | |
820 | exactly lively, are they? On the other hand, if we don't claim | |
821 | that each & every one is alive, then we don't get any of them | |
822 | to appear in an "info thread" command, which is quite a useful | |
823 | behaviour. | |
c5aa993b | 824 | */ |
c906108c | 825 | static int |
28439f5e | 826 | core_thread_alive (struct target_ops *ops, ptid_t ptid) |
c906108c SS |
827 | { |
828 | return 1; | |
829 | } | |
830 | ||
4eb0ad19 DJ |
831 | /* Ask the current architecture what it knows about this core file. |
832 | That will be used, in turn, to pick a better architecture. This | |
833 | wrapper could be avoided if targets got a chance to specialize | |
834 | core_ops. */ | |
835 | ||
836 | static const struct target_desc * | |
837 | core_read_description (struct target_ops *target) | |
838 | { | |
a78c2d62 | 839 | if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch)) |
aff410f1 MS |
840 | return gdbarch_core_read_description (core_gdbarch, |
841 | target, core_bfd); | |
4eb0ad19 DJ |
842 | |
843 | return NULL; | |
844 | } | |
845 | ||
0de3b513 | 846 | static char * |
117de6a9 | 847 | core_pid_to_str (struct target_ops *ops, ptid_t ptid) |
0de3b513 PA |
848 | { |
849 | static char buf[64]; | |
a5ee0f0c | 850 | int pid; |
0de3b513 | 851 | |
a5ee0f0c PA |
852 | /* The preferred way is to have a gdbarch/OS specific |
853 | implementation. */ | |
28439f5e PA |
854 | if (core_gdbarch |
855 | && gdbarch_core_pid_to_str_p (core_gdbarch)) | |
a5ee0f0c | 856 | return gdbarch_core_pid_to_str (core_gdbarch, ptid); |
c5504eaf | 857 | |
a5ee0f0c PA |
858 | /* Otherwise, if we don't have one, we'll just fallback to |
859 | "process", with normal_pid_to_str. */ | |
28439f5e | 860 | |
a5ee0f0c PA |
861 | /* Try the LWPID field first. */ |
862 | pid = ptid_get_lwp (ptid); | |
863 | if (pid != 0) | |
864 | return normal_pid_to_str (pid_to_ptid (pid)); | |
865 | ||
866 | /* Otherwise, this isn't a "threaded" core -- use the PID field, but | |
867 | only if it isn't a fake PID. */ | |
868 | if (!core_has_fake_pid) | |
869 | return normal_pid_to_str (ptid); | |
0de3b513 | 870 | |
a5ee0f0c PA |
871 | /* No luck. We simply don't have a valid PID to print. */ |
872 | xsnprintf (buf, sizeof buf, "<main task>"); | |
0de3b513 PA |
873 | return buf; |
874 | } | |
875 | ||
c35b1492 PA |
876 | static int |
877 | core_has_memory (struct target_ops *ops) | |
878 | { | |
879 | return (core_bfd != NULL); | |
880 | } | |
881 | ||
882 | static int | |
883 | core_has_stack (struct target_ops *ops) | |
884 | { | |
885 | return (core_bfd != NULL); | |
886 | } | |
887 | ||
888 | static int | |
889 | core_has_registers (struct target_ops *ops) | |
890 | { | |
891 | return (core_bfd != NULL); | |
892 | } | |
893 | ||
c906108c SS |
894 | /* Fill in core_ops with its defined operations and properties. */ |
895 | ||
896 | static void | |
fba45db2 | 897 | init_core_ops (void) |
c906108c SS |
898 | { |
899 | core_ops.to_shortname = "core"; | |
900 | core_ops.to_longname = "Local core dump file"; | |
901 | core_ops.to_doc = | |
902 | "Use a core file as a target. Specify the filename of the core file."; | |
903 | core_ops.to_open = core_open; | |
904 | core_ops.to_close = core_close; | |
905 | core_ops.to_attach = find_default_attach; | |
c906108c | 906 | core_ops.to_detach = core_detach; |
c906108c | 907 | core_ops.to_fetch_registers = get_core_registers; |
e2544d02 | 908 | core_ops.to_xfer_partial = core_xfer_partial; |
c906108c SS |
909 | core_ops.to_files_info = core_files_info; |
910 | core_ops.to_insert_breakpoint = ignore; | |
911 | core_ops.to_remove_breakpoint = ignore; | |
912 | core_ops.to_create_inferior = find_default_create_inferior; | |
28439f5e | 913 | core_ops.to_thread_alive = core_thread_alive; |
4eb0ad19 | 914 | core_ops.to_read_description = core_read_description; |
0de3b513 | 915 | core_ops.to_pid_to_str = core_pid_to_str; |
c0edd9ed | 916 | core_ops.to_stratum = process_stratum; |
c35b1492 PA |
917 | core_ops.to_has_memory = core_has_memory; |
918 | core_ops.to_has_stack = core_has_stack; | |
919 | core_ops.to_has_registers = core_has_registers; | |
c5aa993b | 920 | core_ops.to_magic = OPS_MAGIC; |
c0edd9ed JK |
921 | |
922 | if (core_target) | |
923 | internal_error (__FILE__, __LINE__, | |
924 | _("init_core_ops: core target already exists (\"%s\")."), | |
925 | core_target->to_longname); | |
926 | core_target = &core_ops; | |
c906108c SS |
927 | } |
928 | ||
c906108c | 929 | void |
fba45db2 | 930 | _initialize_corelow (void) |
c906108c SS |
931 | { |
932 | init_core_ops (); | |
933 | ||
28439f5e | 934 | add_target (&core_ops); |
c906108c | 935 | } |