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