]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Core dump and executable file functions below target vector, for GDB. |
4646aa9d | 2 | |
e2882c85 | 3 | Copyright (C) 1986-2018 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
0e24ac5d | 21 | #include "arch-utils.h" |
c906108c SS |
22 | #include <signal.h> |
23 | #include <fcntl.h> | |
fc24370e MS |
24 | #ifdef HAVE_SYS_FILE_H |
25 | #include <sys/file.h> /* needed for F_OK and friends */ | |
26 | #endif | |
c5aa993b | 27 | #include "frame.h" /* required by inferior.h */ |
c906108c | 28 | #include "inferior.h" |
45741a9c | 29 | #include "infrun.h" |
c906108c SS |
30 | #include "symtab.h" |
31 | #include "command.h" | |
32 | #include "bfd.h" | |
33 | #include "target.h" | |
34 | #include "gdbcore.h" | |
35 | #include "gdbthread.h" | |
4e052eda | 36 | #include "regcache.h" |
0e24ac5d | 37 | #include "regset.h" |
990f9fe3 | 38 | #include "symfile.h" |
4646aa9d | 39 | #include "exec.h" |
dbda9972 | 40 | #include "readline/readline.h" |
a77053c2 | 41 | #include "solib.h" |
f90c07ac | 42 | #include "filenames.h" |
6c95b8df | 43 | #include "progspace.h" |
516ba659 | 44 | #include "objfiles.h" |
cbb099e8 | 45 | #include "gdb_bfd.h" |
9852c492 | 46 | #include "completer.h" |
614c279d | 47 | #include "filestuff.h" |
8e860359 | 48 | |
ee28ca0f AC |
49 | #ifndef O_LARGEFILE |
50 | #define O_LARGEFILE 0 | |
51 | #endif | |
52 | ||
f6ac5f3d PA |
53 | /* The core file target. */ |
54 | ||
d9f719f1 PA |
55 | static const target_info core_target_info = { |
56 | "core", | |
57 | N_("Local core dump file"), | |
58 | N_("Use a core file as a target. Specify the filename of the core file.") | |
59 | }; | |
60 | ||
f6ac5f3d PA |
61 | class core_target final : public target_ops |
62 | { | |
63 | public: | |
64 | core_target () | |
65 | { to_stratum = process_stratum; } | |
66 | ||
d9f719f1 PA |
67 | const target_info &info () const override |
68 | { return core_target_info; } | |
f6ac5f3d | 69 | |
f6ac5f3d PA |
70 | void close () override; |
71 | void detach (inferior *, int) override; | |
72 | void fetch_registers (struct regcache *, int) override; | |
73 | ||
74 | enum target_xfer_status xfer_partial (enum target_object object, | |
75 | const char *annex, | |
76 | gdb_byte *readbuf, | |
77 | const gdb_byte *writebuf, | |
78 | ULONGEST offset, ULONGEST len, | |
79 | ULONGEST *xfered_len) override; | |
80 | void files_info () override; | |
81 | ||
57810aa7 | 82 | bool thread_alive (ptid_t ptid) override; |
f6ac5f3d PA |
83 | const struct target_desc *read_description () override; |
84 | ||
85 | const char *pid_to_str (ptid_t) override; | |
86 | ||
87 | const char *thread_name (struct thread_info *) override; | |
88 | ||
57810aa7 PA |
89 | bool has_memory () override; |
90 | bool has_stack () override; | |
91 | bool has_registers () override; | |
f6ac5f3d PA |
92 | bool info_proc (const char *, enum info_proc_what) override; |
93 | }; | |
94 | ||
95 | /* See gdbcore.h. */ | |
96 | struct target_ops *the_core_target; | |
97 | ||
00e32a35 AC |
98 | /* List of all available core_fns. On gdb startup, each core file |
99 | register reader calls deprecated_add_core_fns() to register | |
100 | information on each core format it is prepared to read. */ | |
c906108c SS |
101 | |
102 | static struct core_fns *core_file_fns = NULL; | |
103 | ||
aff410f1 MS |
104 | /* The core_fns for a core file handler that is prepared to read the |
105 | core file currently open on core_bfd. */ | |
2acceee2 JM |
106 | |
107 | static struct core_fns *core_vec = NULL; | |
108 | ||
0e24ac5d MK |
109 | /* FIXME: kettenis/20031023: Eventually this variable should |
110 | disappear. */ | |
111 | ||
6a3bfc5c | 112 | static struct gdbarch *core_gdbarch = NULL; |
0e24ac5d | 113 | |
07b82ea5 PA |
114 | /* Per-core data. Currently, only the section table. Note that these |
115 | target sections are *not* mapped in the current address spaces' set | |
116 | of target sections --- those should come only from pure executable | |
117 | or shared library bfds. The core bfd sections are an | |
118 | implementation detail of the core target, just like ptrace is for | |
119 | unix child targets. */ | |
120 | static struct target_section_table *core_data; | |
121 | ||
a14ed312 | 122 | static struct core_fns *sniff_core_bfd (bfd *); |
2acceee2 | 123 | |
020cc13c | 124 | static int gdb_check_format (bfd *); |
2acceee2 | 125 | |
74b7792f AC |
126 | static void core_close_cleanup (void *ignore); |
127 | ||
4efb68b1 | 128 | static void add_to_thread_list (bfd *, asection *, void *); |
c906108c | 129 | |
f6ac5f3d | 130 | static core_target core_ops; |
c906108c | 131 | |
7f9f62ba PA |
132 | /* An arbitrary identifier for the core inferior. */ |
133 | #define CORELOW_PID 1 | |
134 | ||
aff410f1 MS |
135 | /* Link a new core_fns into the global core_file_fns list. Called on |
136 | gdb startup by the _initialize routine in each core file register | |
b021a221 | 137 | reader, to register information about each format the reader is |
aff410f1 | 138 | prepared to handle. */ |
c906108c SS |
139 | |
140 | void | |
00e32a35 | 141 | deprecated_add_core_fns (struct core_fns *cf) |
c906108c | 142 | { |
c5aa993b | 143 | cf->next = core_file_fns; |
c906108c SS |
144 | core_file_fns = cf; |
145 | } | |
146 | ||
2acceee2 JM |
147 | /* The default function that core file handlers can use to examine a |
148 | core file BFD and decide whether or not to accept the job of | |
aff410f1 | 149 | reading the core file. */ |
2acceee2 JM |
150 | |
151 | int | |
fba45db2 | 152 | default_core_sniffer (struct core_fns *our_fns, bfd *abfd) |
2acceee2 JM |
153 | { |
154 | int result; | |
155 | ||
156 | result = (bfd_get_flavour (abfd) == our_fns -> core_flavour); | |
157 | return (result); | |
158 | } | |
159 | ||
160 | /* Walk through the list of core functions to find a set that can | |
06b9f45f | 161 | handle the core file open on ABFD. Returns pointer to set that is |
aff410f1 | 162 | selected. */ |
2acceee2 JM |
163 | |
164 | static struct core_fns * | |
fba45db2 | 165 | sniff_core_bfd (bfd *abfd) |
2acceee2 JM |
166 | { |
167 | struct core_fns *cf; | |
168 | struct core_fns *yummy = NULL; | |
45eba0ab | 169 | int matches = 0; |
2acceee2 | 170 | |
aff410f1 MS |
171 | /* Don't sniff if we have support for register sets in |
172 | CORE_GDBARCH. */ | |
29082443 | 173 | if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch)) |
0e24ac5d MK |
174 | return NULL; |
175 | ||
2acceee2 JM |
176 | for (cf = core_file_fns; cf != NULL; cf = cf->next) |
177 | { | |
178 | if (cf->core_sniffer (cf, abfd)) | |
179 | { | |
180 | yummy = cf; | |
181 | matches++; | |
182 | } | |
183 | } | |
184 | if (matches > 1) | |
185 | { | |
8a3fe4f8 | 186 | warning (_("\"%s\": ambiguous core format, %d handlers match"), |
2acceee2 JM |
187 | bfd_get_filename (abfd), matches); |
188 | } | |
189 | else if (matches == 0) | |
06b9f45f JK |
190 | error (_("\"%s\": no core file handler recognizes format"), |
191 | bfd_get_filename (abfd)); | |
192 | ||
2acceee2 JM |
193 | return (yummy); |
194 | } | |
195 | ||
196 | /* The default is to reject every core file format we see. Either | |
197 | BFD has to recognize it, or we have to provide a function in the | |
aff410f1 | 198 | core file handler that recognizes it. */ |
2acceee2 JM |
199 | |
200 | int | |
fba45db2 | 201 | default_check_format (bfd *abfd) |
2acceee2 JM |
202 | { |
203 | return (0); | |
204 | } | |
205 | ||
aff410f1 | 206 | /* Attempt to recognize core file formats that BFD rejects. */ |
2acceee2 | 207 | |
020cc13c | 208 | static int |
fba45db2 | 209 | gdb_check_format (bfd *abfd) |
2acceee2 JM |
210 | { |
211 | struct core_fns *cf; | |
212 | ||
213 | for (cf = core_file_fns; cf != NULL; cf = cf->next) | |
214 | { | |
215 | if (cf->check_format (abfd)) | |
216 | { | |
81a9a963 | 217 | return (1); |
2acceee2 JM |
218 | } |
219 | } | |
81a9a963 | 220 | return (0); |
2acceee2 | 221 | } |
c906108c | 222 | |
aff410f1 MS |
223 | /* Discard all vestiges of any previous core file and mark data and |
224 | stack spaces as empty. */ | |
c906108c | 225 | |
c906108c | 226 | static void |
f6ac5f3d | 227 | core_close () |
c906108c | 228 | { |
c906108c SS |
229 | if (core_bfd) |
230 | { | |
959b8724 | 231 | int pid = ptid_get_pid (inferior_ptid); |
aff410f1 MS |
232 | inferior_ptid = null_ptid; /* Avoid confusion from thread |
233 | stuff. */ | |
06b9f45f JK |
234 | if (pid != 0) |
235 | exit_inferior_silent (pid); | |
c906108c | 236 | |
aff410f1 MS |
237 | /* Clear out solib state while the bfd is still open. See |
238 | comments in clear_solib in solib.c. */ | |
a77053c2 | 239 | clear_solib (); |
7a292a7a | 240 | |
06b9f45f JK |
241 | if (core_data) |
242 | { | |
243 | xfree (core_data->sections); | |
244 | xfree (core_data); | |
245 | core_data = NULL; | |
246 | } | |
07b82ea5 | 247 | |
cbb099e8 | 248 | gdb_bfd_unref (core_bfd); |
c906108c | 249 | core_bfd = NULL; |
c906108c | 250 | } |
2acceee2 | 251 | core_vec = NULL; |
0e24ac5d | 252 | core_gdbarch = NULL; |
c906108c SS |
253 | } |
254 | ||
74b7792f AC |
255 | static void |
256 | core_close_cleanup (void *ignore) | |
257 | { | |
f6ac5f3d PA |
258 | core_close (); |
259 | } | |
260 | ||
261 | void | |
262 | core_target::close () | |
263 | { | |
264 | core_close (); | |
74b7792f AC |
265 | } |
266 | ||
aff410f1 MS |
267 | /* Look for sections whose names start with `.reg/' so that we can |
268 | extract the list of threads in a core file. */ | |
c906108c SS |
269 | |
270 | static void | |
4efb68b1 | 271 | add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg) |
c906108c | 272 | { |
0de3b513 | 273 | ptid_t ptid; |
3cdd9356 PA |
274 | int core_tid; |
275 | int pid, lwpid; | |
c906108c | 276 | asection *reg_sect = (asection *) reg_sect_arg; |
88f38a04 PA |
277 | int fake_pid_p = 0; |
278 | struct inferior *inf; | |
c906108c | 279 | |
61012eef | 280 | if (!startswith (bfd_section_name (abfd, asect), ".reg/")) |
c906108c SS |
281 | return; |
282 | ||
3cdd9356 | 283 | core_tid = atoi (bfd_section_name (abfd, asect) + 5); |
c906108c | 284 | |
261b8d08 PA |
285 | pid = bfd_core_file_pid (core_bfd); |
286 | if (pid == 0) | |
3cdd9356 | 287 | { |
88f38a04 | 288 | fake_pid_p = 1; |
3cdd9356 | 289 | pid = CORELOW_PID; |
3cdd9356 | 290 | } |
0de3b513 | 291 | |
261b8d08 PA |
292 | lwpid = core_tid; |
293 | ||
88f38a04 PA |
294 | inf = current_inferior (); |
295 | if (inf->pid == 0) | |
296 | { | |
297 | inferior_appeared (inf, pid); | |
298 | inf->fake_pid_p = fake_pid_p; | |
299 | } | |
3cdd9356 PA |
300 | |
301 | ptid = ptid_build (pid, lwpid, 0); | |
302 | ||
303 | add_thread (ptid); | |
c906108c SS |
304 | |
305 | /* Warning, Will Robinson, looking at BFD private data! */ | |
306 | ||
307 | if (reg_sect != NULL | |
aff410f1 MS |
308 | && asect->filepos == reg_sect->filepos) /* Did we find .reg? */ |
309 | inferior_ptid = ptid; /* Yes, make it current. */ | |
c906108c SS |
310 | } |
311 | ||
d9f719f1 | 312 | /* See gdbcore.h. */ |
c906108c | 313 | |
f6ac5f3d | 314 | void |
d9f719f1 | 315 | core_target_open (const char *arg, int from_tty) |
c906108c SS |
316 | { |
317 | const char *p; | |
318 | int siggy; | |
319 | struct cleanup *old_chain; | |
c906108c | 320 | int scratch_chan; |
ee28ca0f | 321 | int flags; |
c906108c SS |
322 | |
323 | target_preopen (from_tty); | |
014f9477 | 324 | if (!arg) |
c906108c | 325 | { |
8a3fe4f8 | 326 | if (core_bfd) |
3e43a32a MS |
327 | error (_("No core file specified. (Use `detach' " |
328 | "to stop debugging a core file.)")); | |
8a3fe4f8 AC |
329 | else |
330 | error (_("No core file specified.")); | |
c906108c SS |
331 | } |
332 | ||
ee0c3293 TT |
333 | gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg)); |
334 | if (!IS_ABSOLUTE_PATH (filename.get ())) | |
335 | filename.reset (concat (current_directory, "/", | |
336 | filename.get (), (char *) NULL)); | |
c906108c | 337 | |
ee28ca0f AC |
338 | flags = O_BINARY | O_LARGEFILE; |
339 | if (write_files) | |
340 | flags |= O_RDWR; | |
341 | else | |
342 | flags |= O_RDONLY; | |
ee0c3293 | 343 | scratch_chan = gdb_open_cloexec (filename.get (), flags, 0); |
c906108c | 344 | if (scratch_chan < 0) |
ee0c3293 | 345 | perror_with_name (filename.get ()); |
c906108c | 346 | |
ee0c3293 | 347 | gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget, |
192b62ce TT |
348 | write_files ? FOPEN_RUB : FOPEN_RB, |
349 | scratch_chan)); | |
c906108c | 350 | if (temp_bfd == NULL) |
ee0c3293 | 351 | perror_with_name (filename.get ()); |
c906108c | 352 | |
192b62ce TT |
353 | if (!bfd_check_format (temp_bfd.get (), bfd_core) |
354 | && !gdb_check_format (temp_bfd.get ())) | |
c906108c SS |
355 | { |
356 | /* Do it after the err msg */ | |
aff410f1 MS |
357 | /* FIXME: should be checking for errors from bfd_close (for one |
358 | thing, on error it does not free all the storage associated | |
359 | with the bfd). */ | |
8a3fe4f8 | 360 | error (_("\"%s\" is not a core dump: %s"), |
ee0c3293 | 361 | filename.get (), bfd_errmsg (bfd_get_error ())); |
c906108c SS |
362 | } |
363 | ||
aff410f1 MS |
364 | /* Looks semi-reasonable. Toss the old core file and work on the |
365 | new. */ | |
c906108c | 366 | |
c906108c | 367 | unpush_target (&core_ops); |
192b62ce | 368 | core_bfd = temp_bfd.release (); |
74b7792f | 369 | old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/); |
c906108c | 370 | |
0e24ac5d MK |
371 | core_gdbarch = gdbarch_from_bfd (core_bfd); |
372 | ||
2acceee2 JM |
373 | /* Find a suitable core file handler to munch on core_bfd */ |
374 | core_vec = sniff_core_bfd (core_bfd); | |
375 | ||
c906108c SS |
376 | validate_files (); |
377 | ||
41bf6aca | 378 | core_data = XCNEW (struct target_section_table); |
07b82ea5 | 379 | |
c906108c | 380 | /* Find the data section */ |
07b82ea5 | 381 | if (build_section_table (core_bfd, |
aff410f1 MS |
382 | &core_data->sections, |
383 | &core_data->sections_end)) | |
8a3fe4f8 | 384 | error (_("\"%s\": Can't find sections: %s"), |
c906108c SS |
385 | bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ())); |
386 | ||
2f1b5984 MK |
387 | /* If we have no exec file, try to set the architecture from the |
388 | core file. We don't do this unconditionally since an exec file | |
389 | typically contains more information that helps us determine the | |
390 | architecture than a core file. */ | |
391 | if (!exec_bfd) | |
392 | set_gdbarch_from_file (core_bfd); | |
cbda0a99 | 393 | |
87ab71f0 | 394 | push_target (&core_ops); |
c906108c SS |
395 | discard_cleanups (old_chain); |
396 | ||
0de3b513 PA |
397 | /* Do this before acknowledging the inferior, so if |
398 | post_create_inferior throws (can happen easilly if you're loading | |
399 | a core file with the wrong exec), we aren't left with threads | |
400 | from the previous inferior. */ | |
401 | init_thread_list (); | |
402 | ||
3cdd9356 | 403 | inferior_ptid = null_ptid; |
0de3b513 | 404 | |
739fc47a PA |
405 | /* Need to flush the register cache (and the frame cache) from a |
406 | previous debug session. If inferior_ptid ends up the same as the | |
407 | last debug session --- e.g., b foo; run; gcore core1; step; gcore | |
408 | core2; core core1; core core2 --- then there's potential for | |
409 | get_current_regcache to return the cached regcache of the | |
410 | previous session, and the frame cache being stale. */ | |
411 | registers_changed (); | |
412 | ||
0de3b513 PA |
413 | /* Build up thread list from BFD sections, and possibly set the |
414 | current thread to the .reg/NN section matching the .reg | |
aff410f1 | 415 | section. */ |
0de3b513 PA |
416 | bfd_map_over_sections (core_bfd, add_to_thread_list, |
417 | bfd_get_section_by_name (core_bfd, ".reg")); | |
418 | ||
3cdd9356 PA |
419 | if (ptid_equal (inferior_ptid, null_ptid)) |
420 | { | |
421 | /* Either we found no .reg/NN section, and hence we have a | |
422 | non-threaded core (single-threaded, from gdb's perspective), | |
423 | or for some reason add_to_thread_list couldn't determine | |
424 | which was the "main" thread. The latter case shouldn't | |
425 | usually happen, but we're dealing with input here, which can | |
426 | always be broken in different ways. */ | |
427 | struct thread_info *thread = first_thread_of_process (-1); | |
c5504eaf | 428 | |
3cdd9356 PA |
429 | if (thread == NULL) |
430 | { | |
c45ceae0 | 431 | inferior_appeared (current_inferior (), CORELOW_PID); |
3cdd9356 PA |
432 | inferior_ptid = pid_to_ptid (CORELOW_PID); |
433 | add_thread_silent (inferior_ptid); | |
434 | } | |
435 | else | |
436 | switch_to_thread (thread->ptid); | |
437 | } | |
438 | ||
959b8724 PA |
439 | post_create_inferior (&core_ops, from_tty); |
440 | ||
0de3b513 PA |
441 | /* Now go through the target stack looking for threads since there |
442 | may be a thread_stratum target loaded on top of target core by | |
443 | now. The layer above should claim threads found in the BFD | |
444 | sections. */ | |
492d29ea | 445 | TRY |
8e7b59a5 | 446 | { |
e8032dde | 447 | target_update_thread_list (); |
8e7b59a5 KS |
448 | } |
449 | ||
492d29ea PA |
450 | CATCH (except, RETURN_MASK_ERROR) |
451 | { | |
452 | exception_print (gdb_stderr, except); | |
453 | } | |
454 | END_CATCH | |
0de3b513 | 455 | |
c906108c SS |
456 | p = bfd_core_file_failing_command (core_bfd); |
457 | if (p) | |
a3f17187 | 458 | printf_filtered (_("Core was generated by `%s'.\n"), p); |
c906108c | 459 | |
0c557179 SDJ |
460 | /* Clearing any previous state of convenience variables. */ |
461 | clear_exit_convenience_vars (); | |
462 | ||
c906108c SS |
463 | siggy = bfd_core_file_failing_signal (core_bfd); |
464 | if (siggy > 0) | |
423ec54c | 465 | { |
22203bbf | 466 | /* If we don't have a CORE_GDBARCH to work with, assume a native |
1f8cf220 PA |
467 | core (map gdb_signal from host signals). If we do have |
468 | CORE_GDBARCH to work with, but no gdb_signal_from_target | |
469 | implementation for that gdbarch, as a fallback measure, | |
470 | assume the host signal mapping. It'll be correct for native | |
471 | cores, but most likely incorrect for cross-cores. */ | |
2ea28649 | 472 | enum gdb_signal sig = (core_gdbarch != NULL |
1f8cf220 PA |
473 | && gdbarch_gdb_signal_from_target_p (core_gdbarch) |
474 | ? gdbarch_gdb_signal_from_target (core_gdbarch, | |
475 | siggy) | |
476 | : gdb_signal_from_host (siggy)); | |
423ec54c | 477 | |
2d503272 PM |
478 | printf_filtered (_("Program terminated with signal %s, %s.\n"), |
479 | gdb_signal_to_name (sig), gdb_signal_to_string (sig)); | |
0c557179 SDJ |
480 | |
481 | /* Set the value of the internal variable $_exitsignal, | |
482 | which holds the signal uncaught by the inferior. */ | |
483 | set_internalvar_integer (lookup_internalvar ("_exitsignal"), | |
484 | siggy); | |
423ec54c | 485 | } |
c906108c | 486 | |
87ab71f0 PA |
487 | /* Fetch all registers from core file. */ |
488 | target_fetch_registers (get_current_regcache (), -1); | |
c906108c | 489 | |
87ab71f0 PA |
490 | /* Now, set up the frame cache, and print the top of stack. */ |
491 | reinit_frame_cache (); | |
08d72866 | 492 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); |
f0e8c4c5 JK |
493 | |
494 | /* Current thread should be NUM 1 but the user does not know that. | |
495 | If a program is single threaded gdb in general does not mention | |
496 | anything about threads. That is why the test is >= 2. */ | |
497 | if (thread_count () >= 2) | |
498 | { | |
492d29ea | 499 | TRY |
f0e8c4c5 JK |
500 | { |
501 | thread_command (NULL, from_tty); | |
502 | } | |
492d29ea PA |
503 | CATCH (except, RETURN_MASK_ERROR) |
504 | { | |
505 | exception_print (gdb_stderr, except); | |
506 | } | |
507 | END_CATCH | |
f0e8c4c5 | 508 | } |
c906108c SS |
509 | } |
510 | ||
f6ac5f3d PA |
511 | void |
512 | core_target::detach (inferior *inf, int from_tty) | |
c906108c | 513 | { |
f6ac5f3d | 514 | unpush_target (this); |
c906108c SS |
515 | reinit_frame_cache (); |
516 | if (from_tty) | |
a3f17187 | 517 | printf_filtered (_("No core file now.\n")); |
c906108c SS |
518 | } |
519 | ||
de57eccd JM |
520 | /* Try to retrieve registers from a section in core_bfd, and supply |
521 | them to core_vec->core_read_registers, as the register set numbered | |
522 | WHICH. | |
523 | ||
11a33714 SM |
524 | If ptid's lwp member is zero, do the single-threaded |
525 | thing: look for a section named NAME. If ptid's lwp | |
0de3b513 PA |
526 | member is non-zero, do the multi-threaded thing: look for a section |
527 | named "NAME/LWP", where LWP is the shortest ASCII decimal | |
11a33714 | 528 | representation of ptid's lwp member. |
de57eccd JM |
529 | |
530 | HUMAN_NAME is a human-readable name for the kind of registers the | |
531 | NAME section contains, for use in error messages. | |
532 | ||
533 | If REQUIRED is non-zero, print an error if the core file doesn't | |
aff410f1 MS |
534 | have a section by the appropriate name. Otherwise, just do |
535 | nothing. */ | |
de57eccd JM |
536 | |
537 | static void | |
9eefc95f | 538 | get_core_register_section (struct regcache *regcache, |
8f0435f7 | 539 | const struct regset *regset, |
1b1818e4 | 540 | const char *name, |
8f0435f7 | 541 | int min_size, |
de57eccd | 542 | int which, |
1b1818e4 | 543 | const char *human_name, |
de57eccd JM |
544 | int required) |
545 | { | |
7be0c536 | 546 | struct bfd_section *section; |
de57eccd JM |
547 | bfd_size_type size; |
548 | char *contents; | |
874a1c8c AT |
549 | bool variable_size_section = (regset != NULL |
550 | && regset->flags & REGSET_VARIABLE_SIZE); | |
de57eccd | 551 | |
3c3ae77e | 552 | thread_section_name section_name (name, regcache->ptid ()); |
de57eccd | 553 | |
3c3ae77e | 554 | section = bfd_get_section_by_name (core_bfd, section_name.c_str ()); |
de57eccd JM |
555 | if (! section) |
556 | { | |
557 | if (required) | |
aff410f1 MS |
558 | warning (_("Couldn't find %s registers in core file."), |
559 | human_name); | |
de57eccd JM |
560 | return; |
561 | } | |
562 | ||
563 | size = bfd_section_size (core_bfd, section); | |
8f0435f7 AA |
564 | if (size < min_size) |
565 | { | |
3c3ae77e PA |
566 | warning (_("Section `%s' in core file too small."), |
567 | section_name.c_str ()); | |
8f0435f7 AA |
568 | return; |
569 | } | |
874a1c8c | 570 | if (size != min_size && !variable_size_section) |
f962539a AA |
571 | { |
572 | warning (_("Unexpected size of section `%s' in core file."), | |
3c3ae77e | 573 | section_name.c_str ()); |
f962539a | 574 | } |
8f0435f7 | 575 | |
224c3ddb | 576 | contents = (char *) alloca (size); |
de57eccd JM |
577 | if (! bfd_get_section_contents (core_bfd, section, contents, |
578 | (file_ptr) 0, size)) | |
579 | { | |
8a3fe4f8 | 580 | warning (_("Couldn't read %s registers from `%s' section in core file."), |
3c3ae77e | 581 | human_name, section_name.c_str ()); |
de57eccd JM |
582 | return; |
583 | } | |
584 | ||
8f0435f7 AA |
585 | if (regset != NULL) |
586 | { | |
9eefc95f | 587 | regset->supply_regset (regset, regcache, -1, contents, size); |
0e24ac5d MK |
588 | return; |
589 | } | |
590 | ||
591 | gdb_assert (core_vec); | |
9eefc95f | 592 | core_vec->core_read_registers (regcache, contents, size, which, |
de57eccd JM |
593 | ((CORE_ADDR) |
594 | bfd_section_vma (core_bfd, section))); | |
595 | } | |
596 | ||
5aa82d05 AA |
597 | /* Callback for get_core_registers that handles a single core file |
598 | register note section. */ | |
599 | ||
600 | static void | |
601 | get_core_registers_cb (const char *sect_name, int size, | |
8f0435f7 | 602 | const struct regset *regset, |
5aa82d05 AA |
603 | const char *human_name, void *cb_data) |
604 | { | |
605 | struct regcache *regcache = (struct regcache *) cb_data; | |
8f0435f7 | 606 | int required = 0; |
5aa82d05 AA |
607 | |
608 | if (strcmp (sect_name, ".reg") == 0) | |
8f0435f7 AA |
609 | { |
610 | required = 1; | |
611 | if (human_name == NULL) | |
612 | human_name = "general-purpose"; | |
613 | } | |
5aa82d05 | 614 | else if (strcmp (sect_name, ".reg2") == 0) |
8f0435f7 AA |
615 | { |
616 | if (human_name == NULL) | |
617 | human_name = "floating-point"; | |
618 | } | |
619 | ||
620 | /* The 'which' parameter is only used when no regset is provided. | |
621 | Thus we just set it to -1. */ | |
622 | get_core_register_section (regcache, regset, sect_name, | |
623 | size, -1, human_name, required); | |
5aa82d05 | 624 | } |
de57eccd | 625 | |
c906108c SS |
626 | /* Get the registers out of a core file. This is the machine- |
627 | independent part. Fetch_core_registers is the machine-dependent | |
aff410f1 MS |
628 | part, typically implemented in the xm-file for each |
629 | architecture. */ | |
c906108c SS |
630 | |
631 | /* We just get all the registers, so we don't use regno. */ | |
632 | ||
f6ac5f3d PA |
633 | void |
634 | core_target::fetch_registers (struct regcache *regcache, int regno) | |
c906108c | 635 | { |
9c5ea4d9 | 636 | int i; |
5aa82d05 | 637 | struct gdbarch *gdbarch; |
c906108c | 638 | |
29082443 | 639 | if (!(core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch)) |
0e24ac5d | 640 | && (core_vec == NULL || core_vec->core_read_registers == NULL)) |
c906108c SS |
641 | { |
642 | fprintf_filtered (gdb_stderr, | |
c5aa993b | 643 | "Can't fetch registers from this type of core file\n"); |
c906108c SS |
644 | return; |
645 | } | |
646 | ||
ac7936df | 647 | gdbarch = regcache->arch (); |
5aa82d05 AA |
648 | if (gdbarch_iterate_over_regset_sections_p (gdbarch)) |
649 | gdbarch_iterate_over_regset_sections (gdbarch, | |
650 | get_core_registers_cb, | |
651 | (void *) regcache, NULL); | |
1b1818e4 UW |
652 | else |
653 | { | |
8f0435f7 AA |
654 | get_core_register_section (regcache, NULL, |
655 | ".reg", 0, 0, "general-purpose", 1); | |
656 | get_core_register_section (regcache, NULL, | |
657 | ".reg2", 0, 2, "floating-point", 0); | |
1b1818e4 | 658 | } |
c906108c | 659 | |
ee99023e | 660 | /* Mark all registers not found in the core as unavailable. */ |
ac7936df | 661 | for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++) |
ee99023e | 662 | if (regcache_register_status (regcache, i) == REG_UNKNOWN) |
9c5ea4d9 | 663 | regcache_raw_supply (regcache, i, NULL); |
c906108c SS |
664 | } |
665 | ||
f6ac5f3d PA |
666 | void |
667 | core_target::files_info () | |
c906108c | 668 | { |
07b82ea5 | 669 | print_section_info (core_data, core_bfd); |
c906108c | 670 | } |
e2544d02 | 671 | \f |
efcbbd14 UW |
672 | struct spuid_list |
673 | { | |
674 | gdb_byte *buf; | |
675 | ULONGEST offset; | |
676 | LONGEST len; | |
677 | ULONGEST pos; | |
678 | ULONGEST written; | |
679 | }; | |
680 | ||
681 | static void | |
682 | add_to_spuid_list (bfd *abfd, asection *asect, void *list_p) | |
683 | { | |
9a3c8263 | 684 | struct spuid_list *list = (struct spuid_list *) list_p; |
efcbbd14 | 685 | enum bfd_endian byte_order |
aff410f1 | 686 | = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE; |
efcbbd14 UW |
687 | int fd, pos = 0; |
688 | ||
689 | sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos); | |
690 | if (pos == 0) | |
691 | return; | |
692 | ||
693 | if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len) | |
694 | { | |
695 | store_unsigned_integer (list->buf + list->pos - list->offset, | |
696 | 4, byte_order, fd); | |
697 | list->written += 4; | |
698 | } | |
699 | list->pos += 4; | |
700 | } | |
701 | ||
f6ac5f3d PA |
702 | enum target_xfer_status |
703 | core_target::xfer_partial (enum target_object object, const char *annex, | |
704 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
705 | ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) | |
e2544d02 RM |
706 | { |
707 | switch (object) | |
708 | { | |
709 | case TARGET_OBJECT_MEMORY: | |
07b82ea5 | 710 | return section_table_xfer_memory_partial (readbuf, writebuf, |
9b409511 | 711 | offset, len, xfered_len, |
07b82ea5 PA |
712 | core_data->sections, |
713 | core_data->sections_end, | |
714 | NULL); | |
e2544d02 RM |
715 | |
716 | case TARGET_OBJECT_AUXV: | |
717 | if (readbuf) | |
718 | { | |
719 | /* When the aux vector is stored in core file, BFD | |
720 | represents this with a fake section called ".auxv". */ | |
721 | ||
c4c5b7ba | 722 | struct bfd_section *section; |
e2544d02 | 723 | bfd_size_type size; |
e2544d02 RM |
724 | |
725 | section = bfd_get_section_by_name (core_bfd, ".auxv"); | |
726 | if (section == NULL) | |
2ed4b548 | 727 | return TARGET_XFER_E_IO; |
e2544d02 RM |
728 | |
729 | size = bfd_section_size (core_bfd, section); | |
730 | if (offset >= size) | |
9b409511 | 731 | return TARGET_XFER_EOF; |
e2544d02 RM |
732 | size -= offset; |
733 | if (size > len) | |
734 | size = len; | |
9b409511 YQ |
735 | |
736 | if (size == 0) | |
737 | return TARGET_XFER_EOF; | |
738 | if (!bfd_get_section_contents (core_bfd, section, readbuf, | |
739 | (file_ptr) offset, size)) | |
e2544d02 | 740 | { |
8a3fe4f8 | 741 | warning (_("Couldn't read NT_AUXV note in core file.")); |
2ed4b548 | 742 | return TARGET_XFER_E_IO; |
e2544d02 RM |
743 | } |
744 | ||
9b409511 YQ |
745 | *xfered_len = (ULONGEST) size; |
746 | return TARGET_XFER_OK; | |
e2544d02 | 747 | } |
2ed4b548 | 748 | return TARGET_XFER_E_IO; |
e2544d02 | 749 | |
403e1656 MK |
750 | case TARGET_OBJECT_WCOOKIE: |
751 | if (readbuf) | |
752 | { | |
753 | /* When the StackGhost cookie is stored in core file, BFD | |
aff410f1 MS |
754 | represents this with a fake section called |
755 | ".wcookie". */ | |
403e1656 MK |
756 | |
757 | struct bfd_section *section; | |
758 | bfd_size_type size; | |
403e1656 MK |
759 | |
760 | section = bfd_get_section_by_name (core_bfd, ".wcookie"); | |
761 | if (section == NULL) | |
2ed4b548 | 762 | return TARGET_XFER_E_IO; |
403e1656 MK |
763 | |
764 | size = bfd_section_size (core_bfd, section); | |
765 | if (offset >= size) | |
96c4f946 | 766 | return TARGET_XFER_EOF; |
403e1656 MK |
767 | size -= offset; |
768 | if (size > len) | |
769 | size = len; | |
9b409511 YQ |
770 | |
771 | if (size == 0) | |
772 | return TARGET_XFER_EOF; | |
773 | if (!bfd_get_section_contents (core_bfd, section, readbuf, | |
774 | (file_ptr) offset, size)) | |
403e1656 | 775 | { |
8a3fe4f8 | 776 | warning (_("Couldn't read StackGhost cookie in core file.")); |
2ed4b548 | 777 | return TARGET_XFER_E_IO; |
403e1656 MK |
778 | } |
779 | ||
9b409511 YQ |
780 | *xfered_len = (ULONGEST) size; |
781 | return TARGET_XFER_OK; | |
782 | ||
403e1656 | 783 | } |
2ed4b548 | 784 | return TARGET_XFER_E_IO; |
403e1656 | 785 | |
de584861 PA |
786 | case TARGET_OBJECT_LIBRARIES: |
787 | if (core_gdbarch | |
788 | && gdbarch_core_xfer_shared_libraries_p (core_gdbarch)) | |
789 | { | |
790 | if (writebuf) | |
2ed4b548 | 791 | return TARGET_XFER_E_IO; |
9b409511 YQ |
792 | else |
793 | { | |
794 | *xfered_len = gdbarch_core_xfer_shared_libraries (core_gdbarch, | |
795 | readbuf, | |
796 | offset, len); | |
797 | ||
798 | if (*xfered_len == 0) | |
799 | return TARGET_XFER_EOF; | |
800 | else | |
801 | return TARGET_XFER_OK; | |
802 | } | |
de584861 PA |
803 | } |
804 | /* FALL THROUGH */ | |
805 | ||
356a5233 JB |
806 | case TARGET_OBJECT_LIBRARIES_AIX: |
807 | if (core_gdbarch | |
808 | && gdbarch_core_xfer_shared_libraries_aix_p (core_gdbarch)) | |
809 | { | |
810 | if (writebuf) | |
2ed4b548 | 811 | return TARGET_XFER_E_IO; |
9b409511 YQ |
812 | else |
813 | { | |
814 | *xfered_len | |
815 | = gdbarch_core_xfer_shared_libraries_aix (core_gdbarch, | |
816 | readbuf, offset, | |
817 | len); | |
818 | ||
819 | if (*xfered_len == 0) | |
820 | return TARGET_XFER_EOF; | |
821 | else | |
822 | return TARGET_XFER_OK; | |
823 | } | |
356a5233 JB |
824 | } |
825 | /* FALL THROUGH */ | |
826 | ||
efcbbd14 UW |
827 | case TARGET_OBJECT_SPU: |
828 | if (readbuf && annex) | |
829 | { | |
830 | /* When the SPU contexts are stored in a core file, BFD | |
aff410f1 MS |
831 | represents this with a fake section called |
832 | "SPU/<annex>". */ | |
efcbbd14 UW |
833 | |
834 | struct bfd_section *section; | |
835 | bfd_size_type size; | |
efcbbd14 | 836 | char sectionstr[100]; |
c5504eaf | 837 | |
efcbbd14 UW |
838 | xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex); |
839 | ||
840 | section = bfd_get_section_by_name (core_bfd, sectionstr); | |
841 | if (section == NULL) | |
2ed4b548 | 842 | return TARGET_XFER_E_IO; |
efcbbd14 UW |
843 | |
844 | size = bfd_section_size (core_bfd, section); | |
845 | if (offset >= size) | |
9b409511 | 846 | return TARGET_XFER_EOF; |
efcbbd14 UW |
847 | size -= offset; |
848 | if (size > len) | |
849 | size = len; | |
9b409511 YQ |
850 | |
851 | if (size == 0) | |
852 | return TARGET_XFER_EOF; | |
853 | if (!bfd_get_section_contents (core_bfd, section, readbuf, | |
854 | (file_ptr) offset, size)) | |
efcbbd14 UW |
855 | { |
856 | warning (_("Couldn't read SPU section in core file.")); | |
2ed4b548 | 857 | return TARGET_XFER_E_IO; |
efcbbd14 UW |
858 | } |
859 | ||
9b409511 YQ |
860 | *xfered_len = (ULONGEST) size; |
861 | return TARGET_XFER_OK; | |
efcbbd14 UW |
862 | } |
863 | else if (readbuf) | |
864 | { | |
865 | /* NULL annex requests list of all present spuids. */ | |
866 | struct spuid_list list; | |
c5504eaf | 867 | |
efcbbd14 UW |
868 | list.buf = readbuf; |
869 | list.offset = offset; | |
870 | list.len = len; | |
871 | list.pos = 0; | |
872 | list.written = 0; | |
873 | bfd_map_over_sections (core_bfd, add_to_spuid_list, &list); | |
9b409511 YQ |
874 | |
875 | if (list.written == 0) | |
876 | return TARGET_XFER_EOF; | |
877 | else | |
878 | { | |
879 | *xfered_len = (ULONGEST) list.written; | |
880 | return TARGET_XFER_OK; | |
881 | } | |
efcbbd14 | 882 | } |
2ed4b548 | 883 | return TARGET_XFER_E_IO; |
efcbbd14 | 884 | |
9015683b TT |
885 | case TARGET_OBJECT_SIGNAL_INFO: |
886 | if (readbuf) | |
9b409511 | 887 | { |
382b69bb JB |
888 | if (core_gdbarch |
889 | && gdbarch_core_xfer_siginfo_p (core_gdbarch)) | |
9b409511 | 890 | { |
382b69bb JB |
891 | LONGEST l = gdbarch_core_xfer_siginfo (core_gdbarch, readbuf, |
892 | offset, len); | |
893 | ||
894 | if (l >= 0) | |
895 | { | |
896 | *xfered_len = l; | |
897 | if (l == 0) | |
898 | return TARGET_XFER_EOF; | |
899 | else | |
900 | return TARGET_XFER_OK; | |
901 | } | |
9b409511 YQ |
902 | } |
903 | } | |
2ed4b548 | 904 | return TARGET_XFER_E_IO; |
9015683b | 905 | |
e2544d02 | 906 | default: |
f6ac5f3d PA |
907 | return this->beneath->xfer_partial (object, annex, readbuf, |
908 | writebuf, offset, len, | |
909 | xfered_len); | |
e2544d02 RM |
910 | } |
911 | } | |
912 | ||
c906108c | 913 | \f |
c906108c SS |
914 | |
915 | /* Okay, let's be honest: threads gleaned from a core file aren't | |
916 | exactly lively, are they? On the other hand, if we don't claim | |
917 | that each & every one is alive, then we don't get any of them | |
918 | to appear in an "info thread" command, which is quite a useful | |
919 | behaviour. | |
c5aa993b | 920 | */ |
57810aa7 | 921 | bool |
f6ac5f3d | 922 | core_target::thread_alive (ptid_t ptid) |
c906108c | 923 | { |
57810aa7 | 924 | return true; |
c906108c SS |
925 | } |
926 | ||
4eb0ad19 DJ |
927 | /* Ask the current architecture what it knows about this core file. |
928 | That will be used, in turn, to pick a better architecture. This | |
929 | wrapper could be avoided if targets got a chance to specialize | |
930 | core_ops. */ | |
931 | ||
f6ac5f3d PA |
932 | const struct target_desc * |
933 | core_target::read_description () | |
4eb0ad19 | 934 | { |
a78c2d62 | 935 | if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch)) |
2117c711 TT |
936 | { |
937 | const struct target_desc *result; | |
938 | ||
f6ac5f3d | 939 | result = gdbarch_core_read_description (core_gdbarch, this, core_bfd); |
2117c711 TT |
940 | if (result != NULL) |
941 | return result; | |
942 | } | |
4eb0ad19 | 943 | |
f6ac5f3d | 944 | return this->beneath->read_description (); |
4eb0ad19 DJ |
945 | } |
946 | ||
f6ac5f3d PA |
947 | const char * |
948 | core_target::pid_to_str (ptid_t ptid) | |
0de3b513 PA |
949 | { |
950 | static char buf[64]; | |
88f38a04 | 951 | struct inferior *inf; |
a5ee0f0c | 952 | int pid; |
0de3b513 | 953 | |
a5ee0f0c PA |
954 | /* The preferred way is to have a gdbarch/OS specific |
955 | implementation. */ | |
28439f5e PA |
956 | if (core_gdbarch |
957 | && gdbarch_core_pid_to_str_p (core_gdbarch)) | |
a5ee0f0c | 958 | return gdbarch_core_pid_to_str (core_gdbarch, ptid); |
c5504eaf | 959 | |
a5ee0f0c PA |
960 | /* Otherwise, if we don't have one, we'll just fallback to |
961 | "process", with normal_pid_to_str. */ | |
28439f5e | 962 | |
a5ee0f0c PA |
963 | /* Try the LWPID field first. */ |
964 | pid = ptid_get_lwp (ptid); | |
965 | if (pid != 0) | |
966 | return normal_pid_to_str (pid_to_ptid (pid)); | |
967 | ||
968 | /* Otherwise, this isn't a "threaded" core -- use the PID field, but | |
969 | only if it isn't a fake PID. */ | |
c9657e70 | 970 | inf = find_inferior_ptid (ptid); |
88f38a04 | 971 | if (inf != NULL && !inf->fake_pid_p) |
a5ee0f0c | 972 | return normal_pid_to_str (ptid); |
0de3b513 | 973 | |
a5ee0f0c PA |
974 | /* No luck. We simply don't have a valid PID to print. */ |
975 | xsnprintf (buf, sizeof buf, "<main task>"); | |
0de3b513 PA |
976 | return buf; |
977 | } | |
978 | ||
f6ac5f3d PA |
979 | const char * |
980 | core_target::thread_name (struct thread_info *thr) | |
4dfc5dbc JB |
981 | { |
982 | if (core_gdbarch | |
983 | && gdbarch_core_thread_name_p (core_gdbarch)) | |
984 | return gdbarch_core_thread_name (core_gdbarch, thr); | |
985 | return NULL; | |
986 | } | |
987 | ||
57810aa7 | 988 | bool |
f6ac5f3d | 989 | core_target::has_memory () |
c35b1492 PA |
990 | { |
991 | return (core_bfd != NULL); | |
992 | } | |
993 | ||
57810aa7 | 994 | bool |
f6ac5f3d | 995 | core_target::has_stack () |
c35b1492 PA |
996 | { |
997 | return (core_bfd != NULL); | |
998 | } | |
999 | ||
57810aa7 | 1000 | bool |
f6ac5f3d | 1001 | core_target::has_registers () |
c35b1492 PA |
1002 | { |
1003 | return (core_bfd != NULL); | |
1004 | } | |
1005 | ||
451b7c33 TT |
1006 | /* Implement the to_info_proc method. */ |
1007 | ||
f6ac5f3d PA |
1008 | bool |
1009 | core_target::info_proc (const char *args, enum info_proc_what request) | |
451b7c33 TT |
1010 | { |
1011 | struct gdbarch *gdbarch = get_current_arch (); | |
1012 | ||
1013 | /* Since this is the core file target, call the 'core_info_proc' | |
1014 | method on gdbarch, not 'info_proc'. */ | |
1015 | if (gdbarch_core_info_proc_p (gdbarch)) | |
1016 | gdbarch_core_info_proc (gdbarch, args, request); | |
c906108c | 1017 | |
f6ac5f3d | 1018 | return true; |
c906108c SS |
1019 | } |
1020 | ||
c906108c | 1021 | void |
fba45db2 | 1022 | _initialize_corelow (void) |
c906108c | 1023 | { |
f6ac5f3d PA |
1024 | if (the_core_target != NULL) |
1025 | internal_error (__FILE__, __LINE__, | |
1026 | _("core target already exists (\"%s\")."), | |
1027 | the_core_target->longname ()); | |
1028 | the_core_target = &core_ops; | |
c906108c | 1029 | |
d9f719f1 | 1030 | add_target (core_target_info, core_target_open, filename_completer); |
c906108c | 1031 | } |