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