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