sim: unify hardware settings
[deliverable/binutils-gdb.git] / gdb / corelow.c
CommitLineData
c906108c 1/* Core dump and executable file functions below target vector, for GDB.
4646aa9d 2
3666a048 3 Copyright (C) 1986-2021 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 39#include "solib.h"
db082f59 40#include "solist.h"
4de283e4
TT
41#include "filenames.h"
42#include "progspace.h"
43#include "objfiles.h"
44#include "gdb_bfd.h"
45#include "completer.h"
268a13a5 46#include "gdbsupport/filestuff.h"
aa2d5a42 47#include "build-id.h"
ff8577f6 48#include "gdbsupport/pathstuff.h"
db082f59 49#include <unordered_map>
973695d6 50#include <unordered_set>
09c2f5d4 51#include "gdbcmd.h"
95ce627a 52#include "xml-tdesc.h"
8e860359 53
ee28ca0f
AC
54#ifndef O_LARGEFILE
55#define O_LARGEFILE 0
56#endif
57
f6ac5f3d
PA
58/* The core file target. */
59
d9f719f1
PA
60static const target_info core_target_info = {
61 "core",
62 N_("Local core dump file"),
590042fc
PW
63 N_("Use a core file as a target.\n\
64Specify the filename of the core file.")
d9f719f1
PA
65};
66
3b3dac9b 67class core_target final : public process_stratum_target
f6ac5f3d
PA
68{
69public:
15244507 70 core_target ();
f6ac5f3d 71
d9f719f1
PA
72 const target_info &info () const override
73 { return core_target_info; }
f6ac5f3d 74
f6ac5f3d
PA
75 void close () override;
76 void detach (inferior *, int) override;
77 void fetch_registers (struct regcache *, int) override;
78
79 enum target_xfer_status xfer_partial (enum target_object object,
80 const char *annex,
81 gdb_byte *readbuf,
82 const gdb_byte *writebuf,
83 ULONGEST offset, ULONGEST len,
84 ULONGEST *xfered_len) override;
85 void files_info () override;
86
57810aa7 87 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
88 const struct target_desc *read_description () override;
89
a068643d 90 std::string pid_to_str (ptid_t) override;
f6ac5f3d
PA
91
92 const char *thread_name (struct thread_info *) override;
93
2735d421 94 bool has_all_memory () override { return true; }
57810aa7
PA
95 bool has_memory () override;
96 bool has_stack () override;
97 bool has_registers () override;
5018ce90 98 bool has_execution (inferior *inf) override { return false; }
f3d11a9a 99
f6ac5f3d 100 bool info_proc (const char *, enum info_proc_what) override;
f6ac5f3d 101
15244507
PA
102 /* A few helpers. */
103
104 /* Getter, see variable definition. */
105 struct gdbarch *core_gdbarch ()
106 {
107 return m_core_gdbarch;
108 }
109
110 /* See definition. */
111 void get_core_register_section (struct regcache *regcache,
112 const struct regset *regset,
113 const char *name,
dbd534fe 114 int section_min_size,
15244507
PA
115 const char *human_name,
116 bool required);
117
09c2f5d4
KB
118 /* See definition. */
119 void info_proc_mappings (struct gdbarch *gdbarch);
120
15244507
PA
121private: /* per-core data */
122
123 /* The core's section table. Note that these target sections are
124 *not* mapped in the current address spaces' set of target
125 sections --- those should come only from pure executable or
126 shared library bfds. The core bfd sections are an implementation
127 detail of the core target, just like ptrace is for unix child
128 targets. */
bb2a6777 129 target_section_table m_core_section_table;
15244507 130
db082f59
KB
131 /* File-backed address space mappings: some core files include
132 information about memory mapped files. */
bb2a6777 133 target_section_table m_core_file_mappings;
db082f59 134
973695d6
KB
135 /* Unavailable mappings. These correspond to pathnames which either
136 weren't found or could not be opened. Knowing these addresses can
137 still be useful. */
138 std::vector<mem_range> m_core_unavailable_mappings;
139
db082f59
KB
140 /* Build m_core_file_mappings. Called from the constructor. */
141 void build_file_mappings ();
142
973695d6
KB
143 /* Helper method for xfer_partial. */
144 enum target_xfer_status xfer_memory_via_mappings (gdb_byte *readbuf,
145 const gdb_byte *writebuf,
146 ULONGEST offset,
147 ULONGEST len,
148 ULONGEST *xfered_len);
149
15244507
PA
150 /* FIXME: kettenis/20031023: Eventually this field should
151 disappear. */
152 struct gdbarch *m_core_gdbarch = NULL;
153};
c906108c 154
15244507
PA
155core_target::core_target ()
156{
15244507 157 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
2acceee2 158
6ba0a321
CB
159 if (!m_core_gdbarch
160 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
161 error (_("\"%s\": Core file format not supported"),
162 bfd_get_filename (core_bfd));
2acceee2 163
15244507 164 /* Find the data section */
2d128614 165 m_core_section_table = build_section_table (core_bfd);
db082f59
KB
166
167 build_file_mappings ();
15244507 168}
0e24ac5d 169
db082f59
KB
170/* Construct the target_section_table for file-backed mappings if
171 they exist.
172
173 For each unique path in the note, we'll open a BFD with a bfd
174 target of "binary". This is an unstructured bfd target upon which
175 we'll impose a structure from the mappings in the architecture-specific
176 mappings note. A BFD section is allocated and initialized for each
177 file-backed mapping.
178
179 We take care to not share already open bfds with other parts of
180 GDB; in particular, we don't want to add new sections to existing
181 BFDs. We do, however, ensure that the BFDs that we allocate here
182 will go away (be deallocated) when the core target is detached. */
183
184void
185core_target::build_file_mappings ()
186{
187 std::unordered_map<std::string, struct bfd *> bfd_map;
973695d6 188 std::unordered_set<std::string> unavailable_paths;
db082f59
KB
189
190 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
191 read_core_file_mappings method. */
192 gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
193
194 /* After determining the number of mappings, read_core_file_mappings
bb2a6777
TT
195 will invoke this lambda. */
196 [&] (ULONGEST)
db082f59 197 {
db082f59
KB
198 },
199
200 /* read_core_file_mappings will invoke this lambda for each mapping
201 that it finds. */
202 [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
70125a45 203 const char *filename)
db082f59
KB
204 {
205 /* Architecture-specific read_core_mapping methods are expected to
206 weed out non-file-backed mappings. */
207 gdb_assert (filename != nullptr);
208
209 struct bfd *bfd = bfd_map[filename];
210 if (bfd == nullptr)
211 {
212 /* Use exec_file_find() to do sysroot expansion. It'll
213 also strip the potential sysroot "target:" prefix. If
214 there is no sysroot, an equivalent (possibly more
215 canonical) pathname will be provided. */
216 gdb::unique_xmalloc_ptr<char> expanded_fname
217 = exec_file_find (filename, NULL);
218 if (expanded_fname == nullptr)
219 {
973695d6
KB
220 m_core_unavailable_mappings.emplace_back (start, end - start);
221 /* Print just one warning per path. */
222 if (unavailable_paths.insert (filename).second)
223 warning (_("Can't open file %s during file-backed mapping "
224 "note processing"),
225 filename);
db082f59
KB
226 return;
227 }
228
229 bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (),
dda83cd7 230 "binary");
db082f59
KB
231
232 if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
233 {
973695d6 234 m_core_unavailable_mappings.emplace_back (start, end - start);
db082f59
KB
235 /* If we get here, there's a good chance that it's due to
236 an internal error. We issue a warning instead of an
237 internal error because of the possibility that the
238 file was removed in between checking for its
239 existence during the expansion in exec_file_find()
240 and the calls to bfd_openr() / bfd_check_format().
241 Output both the path from the core file note along
242 with its expansion to make debugging this problem
243 easier. */
244 warning (_("Can't open file %s which was expanded to %s "
245 "during file-backed mapping note processing"),
246 filename, expanded_fname.get ());
247 if (bfd != nullptr)
248 bfd_close (bfd);
249 return;
250 }
251 /* Ensure that the bfd will be closed when core_bfd is closed.
252 This can be checked before/after a core file detach via
253 "maint info bfds". */
254 gdb_bfd_record_inclusion (core_bfd, bfd);
255 }
256
257 /* Make new BFD section. All sections have the same name,
258 which is permitted by bfd_make_section_anyway(). */
259 asection *sec = bfd_make_section_anyway (bfd, "load");
260 if (sec == nullptr)
261 error (_("Can't make section"));
262 sec->filepos = file_ofs;
263 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
264 bfd_set_section_size (sec, end - start);
265 bfd_set_section_vma (sec, start);
266 bfd_set_section_lma (sec, start);
267 bfd_set_section_alignment (sec, 2);
268
269 /* Set target_section fields. */
6be2a9ab 270 m_core_file_mappings.emplace_back (start, end, sec);
db082f59 271 });
973695d6
KB
272
273 normalize_mem_ranges (&m_core_unavailable_mappings);
15244507 274}
0e24ac5d 275
7f9f62ba
PA
276/* An arbitrary identifier for the core inferior. */
277#define CORELOW_PID 1
278
15244507 279/* Close the core target. */
c906108c 280
15244507
PA
281void
282core_target::close ()
c906108c 283{
c906108c
SS
284 if (core_bfd)
285 {
60db1b85
PA
286 switch_to_no_thread (); /* Avoid confusion from thread
287 stuff. */
00431a78 288 exit_inferior_silent (current_inferior ());
c906108c 289
aff410f1 290 /* Clear out solib state while the bfd is still open. See
dda83cd7 291 comments in clear_solib in solib.c. */
a77053c2 292 clear_solib ();
7a292a7a 293
06333fea 294 current_program_space->cbfd.reset (nullptr);
c906108c 295 }
c906108c 296
15244507
PA
297 /* Core targets are heap-allocated (see core_target_open), so here
298 we delete ourselves. */
299 delete this;
74b7792f
AC
300}
301
aff410f1
MS
302/* Look for sections whose names start with `.reg/' so that we can
303 extract the list of threads in a core file. */
c906108c
SS
304
305static void
a190fabb 306add_to_thread_list (asection *asect, asection *reg_sect)
c906108c 307{
3cdd9356
PA
308 int core_tid;
309 int pid, lwpid;
9ab8741a 310 bool fake_pid_p = false;
88f38a04 311 struct inferior *inf;
c906108c 312
fd361982 313 if (!startswith (bfd_section_name (asect), ".reg/"))
c906108c
SS
314 return;
315
fd361982 316 core_tid = atoi (bfd_section_name (asect) + 5);
c906108c 317
261b8d08
PA
318 pid = bfd_core_file_pid (core_bfd);
319 if (pid == 0)
3cdd9356 320 {
9ab8741a 321 fake_pid_p = true;
3cdd9356 322 pid = CORELOW_PID;
3cdd9356 323 }
0de3b513 324
261b8d08
PA
325 lwpid = core_tid;
326
88f38a04
PA
327 inf = current_inferior ();
328 if (inf->pid == 0)
329 {
330 inferior_appeared (inf, pid);
331 inf->fake_pid_p = fake_pid_p;
332 }
3cdd9356 333
60db1b85 334 ptid_t ptid (pid, lwpid);
3cdd9356 335
60db1b85 336 thread_info *thr = add_thread (inf->process_target (), ptid);
c906108c
SS
337
338/* Warning, Will Robinson, looking at BFD private data! */
339
340 if (reg_sect != NULL
aff410f1 341 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
60db1b85 342 switch_to_thread (thr); /* Yes, make it current. */
c906108c
SS
343}
344
451953fa
PA
345/* Issue a message saying we have no core to debug, if FROM_TTY. */
346
347static void
348maybe_say_no_core_file_now (int from_tty)
349{
350 if (from_tty)
351 printf_filtered (_("No core file now.\n"));
352}
353
30baf67b 354/* Backward compatibility with old way of specifying core files. */
451953fa
PA
355
356void
357core_file_command (const char *filename, int from_tty)
358{
359 dont_repeat (); /* Either way, seems bogus. */
360
361 if (filename == NULL)
362 {
363 if (core_bfd != NULL)
364 {
365 target_detach (current_inferior (), from_tty);
366 gdb_assert (core_bfd == NULL);
367 }
368 else
369 maybe_say_no_core_file_now (from_tty);
370 }
371 else
372 core_target_open (filename, from_tty);
373}
374
aa2d5a42
KS
375/* Locate (and load) an executable file (and symbols) given the core file
376 BFD ABFD. */
377
378static void
379locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
380{
381 const bfd_build_id *build_id = build_id_bfd_get (abfd);
382 if (build_id == nullptr)
383 return;
384
385 gdb_bfd_ref_ptr execbfd
386 = build_id_to_exec_bfd (build_id->size, build_id->data);
387
388 if (execbfd != nullptr)
389 {
390 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
391 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
392 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
393 }
394}
395
d9f719f1 396/* See gdbcore.h. */
c906108c 397
f6ac5f3d 398void
d9f719f1 399core_target_open (const char *arg, int from_tty)
c906108c
SS
400{
401 const char *p;
402 int siggy;
c906108c 403 int scratch_chan;
ee28ca0f 404 int flags;
c906108c
SS
405
406 target_preopen (from_tty);
014f9477 407 if (!arg)
c906108c 408 {
8a3fe4f8 409 if (core_bfd)
3e43a32a
MS
410 error (_("No core file specified. (Use `detach' "
411 "to stop debugging a core file.)"));
8a3fe4f8
AC
412 else
413 error (_("No core file specified."));
c906108c
SS
414 }
415
ee0c3293
TT
416 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
417 if (!IS_ABSOLUTE_PATH (filename.get ()))
ff8577f6 418 filename = gdb_abspath (filename.get ());
c906108c 419
ee28ca0f
AC
420 flags = O_BINARY | O_LARGEFILE;
421 if (write_files)
422 flags |= O_RDWR;
423 else
424 flags |= O_RDONLY;
ee0c3293 425 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
c906108c 426 if (scratch_chan < 0)
ee0c3293 427 perror_with_name (filename.get ());
c906108c 428
ee0c3293 429 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
192b62ce
TT
430 write_files ? FOPEN_RUB : FOPEN_RB,
431 scratch_chan));
c906108c 432 if (temp_bfd == NULL)
ee0c3293 433 perror_with_name (filename.get ());
c906108c 434
6ba0a321 435 if (!bfd_check_format (temp_bfd.get (), bfd_core))
c906108c
SS
436 {
437 /* Do it after the err msg */
aff410f1 438 /* FIXME: should be checking for errors from bfd_close (for one
dda83cd7
SM
439 thing, on error it does not free all the storage associated
440 with the bfd). */
8a3fe4f8 441 error (_("\"%s\" is not a core dump: %s"),
ee0c3293 442 filename.get (), bfd_errmsg (bfd_get_error ()));
c906108c
SS
443 }
444
06333fea 445 current_program_space->cbfd = std::move (temp_bfd);
c906108c 446
15244507 447 core_target *target = new core_target ();
0e24ac5d 448
15244507
PA
449 /* Own the target until it is successfully pushed. */
450 target_ops_up target_holder (target);
2acceee2 451
c906108c
SS
452 validate_files ();
453
2f1b5984
MK
454 /* If we have no exec file, try to set the architecture from the
455 core file. We don't do this unconditionally since an exec file
456 typically contains more information that helps us determine the
457 architecture than a core file. */
7e10abd1 458 if (!current_program_space->exec_bfd ())
2f1b5984 459 set_gdbarch_from_file (core_bfd);
cbda0a99 460
02980c56 461 current_inferior ()->push_target (std::move (target_holder));
c906108c 462
60db1b85 463 switch_to_no_thread ();
0de3b513 464
739fc47a
PA
465 /* Need to flush the register cache (and the frame cache) from a
466 previous debug session. If inferior_ptid ends up the same as the
467 last debug session --- e.g., b foo; run; gcore core1; step; gcore
468 core2; core core1; core core2 --- then there's potential for
469 get_current_regcache to return the cached regcache of the
470 previous session, and the frame cache being stale. */
471 registers_changed ();
472
0de3b513
PA
473 /* Build up thread list from BFD sections, and possibly set the
474 current thread to the .reg/NN section matching the .reg
aff410f1 475 section. */
a190fabb
TT
476 asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
477 for (asection *sect : gdb_bfd_sections (core_bfd))
478 add_to_thread_list (sect, reg_sect);
0de3b513 479
d7e15655 480 if (inferior_ptid == null_ptid)
3cdd9356
PA
481 {
482 /* Either we found no .reg/NN section, and hence we have a
483 non-threaded core (single-threaded, from gdb's perspective),
484 or for some reason add_to_thread_list couldn't determine
485 which was the "main" thread. The latter case shouldn't
486 usually happen, but we're dealing with input here, which can
487 always be broken in different ways. */
00431a78 488 thread_info *thread = first_thread_of_inferior (current_inferior ());
c5504eaf 489
3cdd9356
PA
490 if (thread == NULL)
491 {
c45ceae0 492 inferior_appeared (current_inferior (), CORELOW_PID);
60db1b85 493 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
3cdd9356 494 }
60db1b85
PA
495
496 switch_to_thread (thread);
3cdd9356
PA
497 }
498
7e10abd1 499 if (current_program_space->exec_bfd () == nullptr)
aa2d5a42
KS
500 locate_exec_from_corefile_build_id (core_bfd, from_tty);
501
a7aba266 502 post_create_inferior (from_tty);
959b8724 503
0de3b513
PA
504 /* Now go through the target stack looking for threads since there
505 may be a thread_stratum target loaded on top of target core by
506 now. The layer above should claim threads found in the BFD
507 sections. */
a70b8144 508 try
8e7b59a5 509 {
e8032dde 510 target_update_thread_list ();
8e7b59a5
KS
511 }
512
230d2906 513 catch (const gdb_exception_error &except)
492d29ea
PA
514 {
515 exception_print (gdb_stderr, except);
516 }
0de3b513 517
c906108c
SS
518 p = bfd_core_file_failing_command (core_bfd);
519 if (p)
a3f17187 520 printf_filtered (_("Core was generated by `%s'.\n"), p);
c906108c 521
0c557179
SDJ
522 /* Clearing any previous state of convenience variables. */
523 clear_exit_convenience_vars ();
524
c906108c
SS
525 siggy = bfd_core_file_failing_signal (core_bfd);
526 if (siggy > 0)
423ec54c 527 {
15244507
PA
528 gdbarch *core_gdbarch = target->core_gdbarch ();
529
22203bbf 530 /* If we don't have a CORE_GDBARCH to work with, assume a native
1f8cf220
PA
531 core (map gdb_signal from host signals). If we do have
532 CORE_GDBARCH to work with, but no gdb_signal_from_target
533 implementation for that gdbarch, as a fallback measure,
534 assume the host signal mapping. It'll be correct for native
535 cores, but most likely incorrect for cross-cores. */
2ea28649 536 enum gdb_signal sig = (core_gdbarch != NULL
1f8cf220
PA
537 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
538 ? gdbarch_gdb_signal_from_target (core_gdbarch,
539 siggy)
540 : gdb_signal_from_host (siggy));
423ec54c 541
ad97bfc5 542 printf_filtered (_("Program terminated with signal %s, %s"),
2d503272 543 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
ad97bfc5
JB
544 if (gdbarch_report_signal_info_p (core_gdbarch))
545 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
546 printf_filtered (_(".\n"));
0c557179
SDJ
547
548 /* Set the value of the internal variable $_exitsignal,
549 which holds the signal uncaught by the inferior. */
550 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
551 siggy);
423ec54c 552 }
c906108c 553
87ab71f0
PA
554 /* Fetch all registers from core file. */
555 target_fetch_registers (get_current_regcache (), -1);
c906108c 556
87ab71f0
PA
557 /* Now, set up the frame cache, and print the top of stack. */
558 reinit_frame_cache ();
08d72866 559 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
f0e8c4c5
JK
560
561 /* Current thread should be NUM 1 but the user does not know that.
562 If a program is single threaded gdb in general does not mention
563 anything about threads. That is why the test is >= 2. */
5b6d1e4f 564 if (thread_count (target) >= 2)
f0e8c4c5 565 {
a70b8144 566 try
f0e8c4c5
JK
567 {
568 thread_command (NULL, from_tty);
569 }
230d2906 570 catch (const gdb_exception_error &except)
492d29ea
PA
571 {
572 exception_print (gdb_stderr, except);
573 }
f0e8c4c5 574 }
c906108c
SS
575}
576
f6ac5f3d
PA
577void
578core_target::detach (inferior *inf, int from_tty)
c906108c 579{
15244507
PA
580 /* Note that 'this' is dangling after this call. unpush_target
581 closes the target, and our close implementation deletes
582 'this'. */
fadf6add 583 inf->unpush_target (this);
15244507 584
66452beb
PW
585 /* Clear the register cache and the frame cache. */
586 registers_changed ();
c906108c 587 reinit_frame_cache ();
451953fa 588 maybe_say_no_core_file_now (from_tty);
c906108c
SS
589}
590
de57eccd 591/* Try to retrieve registers from a section in core_bfd, and supply
6ba0a321 592 them to REGSET.
de57eccd 593
11a33714
SM
594 If ptid's lwp member is zero, do the single-threaded
595 thing: look for a section named NAME. If ptid's lwp
0de3b513
PA
596 member is non-zero, do the multi-threaded thing: look for a section
597 named "NAME/LWP", where LWP is the shortest ASCII decimal
11a33714 598 representation of ptid's lwp member.
de57eccd
JM
599
600 HUMAN_NAME is a human-readable name for the kind of registers the
601 NAME section contains, for use in error messages.
602
15244507
PA
603 If REQUIRED is true, print an error if the core file doesn't have a
604 section by the appropriate name. Otherwise, just do nothing. */
de57eccd 605
15244507
PA
606void
607core_target::get_core_register_section (struct regcache *regcache,
608 const struct regset *regset,
609 const char *name,
dbd534fe 610 int section_min_size,
15244507
PA
611 const char *human_name,
612 bool required)
de57eccd 613{
6ba0a321
CB
614 gdb_assert (regset != nullptr);
615
7be0c536 616 struct bfd_section *section;
de57eccd 617 bfd_size_type size;
6ba0a321 618 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
de57eccd 619
3c3ae77e 620 thread_section_name section_name (name, regcache->ptid ());
de57eccd 621
3c3ae77e 622 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
de57eccd
JM
623 if (! section)
624 {
625 if (required)
aff410f1
MS
626 warning (_("Couldn't find %s registers in core file."),
627 human_name);
de57eccd
JM
628 return;
629 }
630
fd361982 631 size = bfd_section_size (section);
dbd534fe 632 if (size < section_min_size)
8f0435f7 633 {
3c3ae77e
PA
634 warning (_("Section `%s' in core file too small."),
635 section_name.c_str ());
8f0435f7
AA
636 return;
637 }
dbd534fe 638 if (size != section_min_size && !variable_size_section)
f962539a
AA
639 {
640 warning (_("Unexpected size of section `%s' in core file."),
3c3ae77e 641 section_name.c_str ());
f962539a 642 }
8f0435f7 643
0cac9354 644 gdb::byte_vector contents (size);
d8b2f9e3
SM
645 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
646 (file_ptr) 0, size))
de57eccd 647 {
8a3fe4f8 648 warning (_("Couldn't read %s registers from `%s' section in core file."),
3c3ae77e 649 human_name, section_name.c_str ());
de57eccd
JM
650 return;
651 }
652
6ba0a321 653 regset->supply_regset (regset, regcache, -1, contents.data (), size);
de57eccd
JM
654}
655
15244507
PA
656/* Data passed to gdbarch_iterate_over_regset_sections's callback. */
657struct get_core_registers_cb_data
658{
659 core_target *target;
660 struct regcache *regcache;
661};
662
5aa82d05
AA
663/* Callback for get_core_registers that handles a single core file
664 register note section. */
665
666static void
a616bb94 667get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
8f0435f7 668 const struct regset *regset,
5aa82d05
AA
669 const char *human_name, void *cb_data)
670{
6ba0a321
CB
671 gdb_assert (regset != nullptr);
672
15244507
PA
673 auto *data = (get_core_registers_cb_data *) cb_data;
674 bool required = false;
6ba0a321 675 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
a616bb94
AH
676
677 if (!variable_size_section)
678 gdb_assert (supply_size == collect_size);
5aa82d05
AA
679
680 if (strcmp (sect_name, ".reg") == 0)
8f0435f7 681 {
15244507 682 required = true;
8f0435f7
AA
683 if (human_name == NULL)
684 human_name = "general-purpose";
685 }
5aa82d05 686 else if (strcmp (sect_name, ".reg2") == 0)
8f0435f7
AA
687 {
688 if (human_name == NULL)
689 human_name = "floating-point";
690 }
691
15244507 692 data->target->get_core_register_section (data->regcache, regset, sect_name,
6ba0a321 693 supply_size, human_name, required);
5aa82d05 694}
de57eccd 695
c906108c
SS
696/* Get the registers out of a core file. This is the machine-
697 independent part. Fetch_core_registers is the machine-dependent
aff410f1
MS
698 part, typically implemented in the xm-file for each
699 architecture. */
c906108c
SS
700
701/* We just get all the registers, so we don't use regno. */
702
f6ac5f3d
PA
703void
704core_target::fetch_registers (struct regcache *regcache, int regno)
c906108c 705{
15244507 706 if (!(m_core_gdbarch != nullptr
6ba0a321 707 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
c906108c
SS
708 {
709 fprintf_filtered (gdb_stderr,
c5aa993b 710 "Can't fetch registers from this type of core file\n");
c906108c
SS
711 return;
712 }
713
6ba0a321
CB
714 struct gdbarch *gdbarch = regcache->arch ();
715 get_core_registers_cb_data data = { this, regcache };
716 gdbarch_iterate_over_regset_sections (gdbarch,
717 get_core_registers_cb,
718 (void *) &data, NULL);
c906108c 719
ee99023e 720 /* Mark all registers not found in the core as unavailable. */
6ba0a321 721 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
0ec9f114 722 if (regcache->get_register_status (i) == REG_UNKNOWN)
73e1c03f 723 regcache->raw_supply (i, NULL);
c906108c
SS
724}
725
f6ac5f3d
PA
726void
727core_target::files_info ()
c906108c 728{
15244507 729 print_section_info (&m_core_section_table, core_bfd);
c906108c 730}
e2544d02 731\f
973695d6
KB
732/* Helper method for core_target::xfer_partial. */
733
734enum target_xfer_status
735core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
736 const gdb_byte *writebuf,
737 ULONGEST offset, ULONGEST len,
738 ULONGEST *xfered_len)
739{
740 enum target_xfer_status xfer_status;
741
742 xfer_status = (section_table_xfer_memory_partial
743 (readbuf, writebuf,
744 offset, len, xfered_len,
bb2a6777 745 m_core_file_mappings));
973695d6
KB
746
747 if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
748 return xfer_status;
749
750 /* There are instances - e.g. when debugging within a docker
751 container using the AUFS storage driver - where the pathnames
752 obtained from the note section are incorrect. Despite the path
753 being wrong, just knowing the start and end addresses of the
754 mappings is still useful; we can attempt an access of the file
755 stratum constrained to the address ranges corresponding to the
756 unavailable mappings. */
757
758 ULONGEST memaddr = offset;
759 ULONGEST memend = offset + len;
760
761 for (const auto &mr : m_core_unavailable_mappings)
762 {
763 if (address_in_mem_range (memaddr, &mr))
dda83cd7 764 {
973695d6
KB
765 if (!address_in_mem_range (memend, &mr))
766 len = mr.start + mr.length - memaddr;
767
768 xfer_status = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY,
769 NULL,
770 readbuf,
771 writebuf,
772 offset,
773 len,
774 xfered_len);
775 break;
776 }
777 }
778
779 return xfer_status;
780}
781
f6ac5f3d
PA
782enum target_xfer_status
783core_target::xfer_partial (enum target_object object, const char *annex,
784 gdb_byte *readbuf, const gdb_byte *writebuf,
785 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
e2544d02
RM
786{
787 switch (object)
788 {
789 case TARGET_OBJECT_MEMORY:
2735d421
KB
790 {
791 enum target_xfer_status xfer_status;
792
793 /* Try accessing memory contents from core file data,
794 restricting consideration to those sections for which
795 the BFD section flag SEC_HAS_CONTENTS is set. */
796 auto has_contents_cb = [] (const struct target_section *s)
797 {
798 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
799 };
800 xfer_status = section_table_xfer_memory_partial
801 (readbuf, writebuf,
802 offset, len, xfered_len,
bb2a6777 803 m_core_section_table,
2735d421
KB
804 has_contents_cb);
805 if (xfer_status == TARGET_XFER_OK)
806 return TARGET_XFER_OK;
807
db082f59
KB
808 /* Check file backed mappings. If they're available, use
809 core file provided mappings (e.g. from .note.linuxcore.file
810 or the like) as this should provide a more accurate
811 result. If not, check the stratum beneath us, which should
1bd57575
LM
812 be the file stratum.
813
814 We also check unavailable mappings due to Docker/AUFS driver
815 issues. */
816 if (!m_core_file_mappings.empty ()
817 || !m_core_unavailable_mappings.empty ())
818 {
819 xfer_status = xfer_memory_via_mappings (readbuf, writebuf, offset,
820 len, xfered_len);
821 }
db082f59
KB
822 else
823 xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
824 writebuf, offset, len,
825 xfered_len);
2735d421
KB
826 if (xfer_status == TARGET_XFER_OK)
827 return TARGET_XFER_OK;
e2544d02 828
2735d421
KB
829 /* Finally, attempt to access data in core file sections with
830 no contents. These will typically read as all zero. */
831 auto no_contents_cb = [&] (const struct target_section *s)
832 {
833 return !has_contents_cb (s);
834 };
835 xfer_status = section_table_xfer_memory_partial
836 (readbuf, writebuf,
837 offset, len, xfered_len,
bb2a6777 838 m_core_section_table,
2735d421
KB
839 no_contents_cb);
840
841 return xfer_status;
842 }
e2544d02
RM
843 case TARGET_OBJECT_AUXV:
844 if (readbuf)
845 {
846 /* When the aux vector is stored in core file, BFD
847 represents this with a fake section called ".auxv". */
848
c4c5b7ba 849 struct bfd_section *section;
e2544d02 850 bfd_size_type size;
e2544d02
RM
851
852 section = bfd_get_section_by_name (core_bfd, ".auxv");
853 if (section == NULL)
2ed4b548 854 return TARGET_XFER_E_IO;
e2544d02 855
fd361982 856 size = bfd_section_size (section);
e2544d02 857 if (offset >= size)
9b409511 858 return TARGET_XFER_EOF;
e2544d02
RM
859 size -= offset;
860 if (size > len)
861 size = len;
9b409511
YQ
862
863 if (size == 0)
864 return TARGET_XFER_EOF;
865 if (!bfd_get_section_contents (core_bfd, section, readbuf,
866 (file_ptr) offset, size))
e2544d02 867 {
8a3fe4f8 868 warning (_("Couldn't read NT_AUXV note in core file."));
2ed4b548 869 return TARGET_XFER_E_IO;
e2544d02
RM
870 }
871
9b409511
YQ
872 *xfered_len = (ULONGEST) size;
873 return TARGET_XFER_OK;
e2544d02 874 }
2ed4b548 875 return TARGET_XFER_E_IO;
e2544d02 876
403e1656
MK
877 case TARGET_OBJECT_WCOOKIE:
878 if (readbuf)
879 {
880 /* When the StackGhost cookie is stored in core file, BFD
aff410f1
MS
881 represents this with a fake section called
882 ".wcookie". */
403e1656
MK
883
884 struct bfd_section *section;
885 bfd_size_type size;
403e1656
MK
886
887 section = bfd_get_section_by_name (core_bfd, ".wcookie");
888 if (section == NULL)
2ed4b548 889 return TARGET_XFER_E_IO;
403e1656 890
fd361982 891 size = bfd_section_size (section);
403e1656 892 if (offset >= size)
96c4f946 893 return TARGET_XFER_EOF;
403e1656
MK
894 size -= offset;
895 if (size > len)
896 size = len;
9b409511
YQ
897
898 if (size == 0)
899 return TARGET_XFER_EOF;
900 if (!bfd_get_section_contents (core_bfd, section, readbuf,
901 (file_ptr) offset, size))
403e1656 902 {
8a3fe4f8 903 warning (_("Couldn't read StackGhost cookie in core file."));
2ed4b548 904 return TARGET_XFER_E_IO;
403e1656
MK
905 }
906
9b409511
YQ
907 *xfered_len = (ULONGEST) size;
908 return TARGET_XFER_OK;
909
403e1656 910 }
2ed4b548 911 return TARGET_XFER_E_IO;
403e1656 912
de584861 913 case TARGET_OBJECT_LIBRARIES:
15244507
PA
914 if (m_core_gdbarch != nullptr
915 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
de584861
PA
916 {
917 if (writebuf)
2ed4b548 918 return TARGET_XFER_E_IO;
9b409511
YQ
919 else
920 {
15244507 921 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
9b409511
YQ
922 readbuf,
923 offset, len);
924
925 if (*xfered_len == 0)
926 return TARGET_XFER_EOF;
927 else
928 return TARGET_XFER_OK;
929 }
de584861
PA
930 }
931 /* FALL THROUGH */
932
356a5233 933 case TARGET_OBJECT_LIBRARIES_AIX:
15244507
PA
934 if (m_core_gdbarch != nullptr
935 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
356a5233
JB
936 {
937 if (writebuf)
2ed4b548 938 return TARGET_XFER_E_IO;
9b409511
YQ
939 else
940 {
941 *xfered_len
15244507 942 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
9b409511
YQ
943 readbuf, offset,
944 len);
945
946 if (*xfered_len == 0)
947 return TARGET_XFER_EOF;
948 else
949 return TARGET_XFER_OK;
950 }
356a5233
JB
951 }
952 /* FALL THROUGH */
953
9015683b
TT
954 case TARGET_OBJECT_SIGNAL_INFO:
955 if (readbuf)
9b409511 956 {
15244507
PA
957 if (m_core_gdbarch != nullptr
958 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
9b409511 959 {
15244507 960 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
382b69bb
JB
961 offset, len);
962
963 if (l >= 0)
964 {
965 *xfered_len = l;
966 if (l == 0)
967 return TARGET_XFER_EOF;
968 else
969 return TARGET_XFER_OK;
970 }
9b409511
YQ
971 }
972 }
2ed4b548 973 return TARGET_XFER_E_IO;
9015683b 974
e2544d02 975 default:
b6a8c27b
PA
976 return this->beneath ()->xfer_partial (object, annex, readbuf,
977 writebuf, offset, len,
978 xfered_len);
e2544d02
RM
979 }
980}
981
c906108c 982\f
c906108c
SS
983
984/* Okay, let's be honest: threads gleaned from a core file aren't
985 exactly lively, are they? On the other hand, if we don't claim
986 that each & every one is alive, then we don't get any of them
987 to appear in an "info thread" command, which is quite a useful
988 behaviour.
c5aa993b 989 */
57810aa7 990bool
f6ac5f3d 991core_target::thread_alive (ptid_t ptid)
c906108c 992{
57810aa7 993 return true;
c906108c
SS
994}
995
4eb0ad19
DJ
996/* Ask the current architecture what it knows about this core file.
997 That will be used, in turn, to pick a better architecture. This
998 wrapper could be avoided if targets got a chance to specialize
15244507 999 core_target. */
4eb0ad19 1000
f6ac5f3d
PA
1001const struct target_desc *
1002core_target::read_description ()
4eb0ad19 1003{
95ce627a
AB
1004 /* If the core file contains a target description note then we will use
1005 that in preference to anything else. */
1006 bfd_size_type tdesc_note_size = 0;
1007 struct bfd_section *tdesc_note_section
1008 = bfd_get_section_by_name (core_bfd, ".gdb-tdesc");
1009 if (tdesc_note_section != nullptr)
1010 tdesc_note_size = bfd_section_size (tdesc_note_section);
1011 if (tdesc_note_size > 0)
1012 {
1013 gdb::char_vector contents (tdesc_note_size + 1);
1014 if (bfd_get_section_contents (core_bfd, tdesc_note_section,
1015 contents.data (), (file_ptr) 0,
1016 tdesc_note_size))
1017 {
1018 /* Ensure we have a null terminator. */
1019 contents[tdesc_note_size] = '\0';
1020 const struct target_desc *result
1021 = string_read_description_xml (contents.data ());
1022 if (result != nullptr)
1023 return result;
1024 }
1025 }
1026
15244507 1027 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
2117c711
TT
1028 {
1029 const struct target_desc *result;
1030
15244507 1031 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
2117c711
TT
1032 if (result != NULL)
1033 return result;
1034 }
4eb0ad19 1035
b6a8c27b 1036 return this->beneath ()->read_description ();
4eb0ad19
DJ
1037}
1038
a068643d 1039std::string
f6ac5f3d 1040core_target::pid_to_str (ptid_t ptid)
0de3b513 1041{
88f38a04 1042 struct inferior *inf;
a5ee0f0c 1043 int pid;
0de3b513 1044
a5ee0f0c
PA
1045 /* The preferred way is to have a gdbarch/OS specific
1046 implementation. */
15244507
PA
1047 if (m_core_gdbarch != nullptr
1048 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1049 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
c5504eaf 1050
a5ee0f0c
PA
1051 /* Otherwise, if we don't have one, we'll just fallback to
1052 "process", with normal_pid_to_str. */
28439f5e 1053
a5ee0f0c 1054 /* Try the LWPID field first. */
e38504b3 1055 pid = ptid.lwp ();
a5ee0f0c 1056 if (pid != 0)
f2907e49 1057 return normal_pid_to_str (ptid_t (pid));
a5ee0f0c
PA
1058
1059 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1060 only if it isn't a fake PID. */
5b6d1e4f 1061 inf = find_inferior_ptid (this, ptid);
88f38a04 1062 if (inf != NULL && !inf->fake_pid_p)
a5ee0f0c 1063 return normal_pid_to_str (ptid);
0de3b513 1064
a5ee0f0c 1065 /* No luck. We simply don't have a valid PID to print. */
a068643d 1066 return "<main task>";
0de3b513
PA
1067}
1068
f6ac5f3d
PA
1069const char *
1070core_target::thread_name (struct thread_info *thr)
4dfc5dbc 1071{
15244507
PA
1072 if (m_core_gdbarch != nullptr
1073 && gdbarch_core_thread_name_p (m_core_gdbarch))
1074 return gdbarch_core_thread_name (m_core_gdbarch, thr);
4dfc5dbc
JB
1075 return NULL;
1076}
1077
57810aa7 1078bool
f6ac5f3d 1079core_target::has_memory ()
c35b1492
PA
1080{
1081 return (core_bfd != NULL);
1082}
1083
57810aa7 1084bool
f6ac5f3d 1085core_target::has_stack ()
c35b1492
PA
1086{
1087 return (core_bfd != NULL);
1088}
1089
57810aa7 1090bool
f6ac5f3d 1091core_target::has_registers ()
c35b1492
PA
1092{
1093 return (core_bfd != NULL);
1094}
1095
451b7c33
TT
1096/* Implement the to_info_proc method. */
1097
f6ac5f3d
PA
1098bool
1099core_target::info_proc (const char *args, enum info_proc_what request)
451b7c33
TT
1100{
1101 struct gdbarch *gdbarch = get_current_arch ();
1102
1103 /* Since this is the core file target, call the 'core_info_proc'
1104 method on gdbarch, not 'info_proc'. */
1105 if (gdbarch_core_info_proc_p (gdbarch))
1106 gdbarch_core_info_proc (gdbarch, args, request);
c906108c 1107
f6ac5f3d 1108 return true;
c906108c
SS
1109}
1110
09c2f5d4
KB
1111/* Get a pointer to the current core target. If not connected to a
1112 core target, return NULL. */
1113
1114static core_target *
1115get_current_core_target ()
1116{
1117 target_ops *proc_target = current_inferior ()->process_target ();
1118 return dynamic_cast<core_target *> (proc_target);
1119}
1120
1121/* Display file backed mappings from core file. */
1122
1123void
1124core_target::info_proc_mappings (struct gdbarch *gdbarch)
1125{
d7a78e5c 1126 if (!m_core_file_mappings.empty ())
09c2f5d4
KB
1127 {
1128 printf_filtered (_("Mapped address spaces:\n\n"));
1129 if (gdbarch_addr_bit (gdbarch) == 32)
1130 {
1131 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1132 "Start Addr",
1133 " End Addr",
1134 " Size", " Offset", "objfile");
1135 }
1136 else
1137 {
1138 printf_filtered (" %18s %18s %10s %10s %s\n",
1139 "Start Addr",
1140 " End Addr",
1141 " Size", " Offset", "objfile");
1142 }
1143 }
1144
d7a78e5c 1145 for (const target_section &tsp : m_core_file_mappings)
09c2f5d4 1146 {
bb2a6777
TT
1147 ULONGEST start = tsp.addr;
1148 ULONGEST end = tsp.endaddr;
1149 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1150 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
09c2f5d4
KB
1151
1152 if (gdbarch_addr_bit (gdbarch) == 32)
1153 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1154 paddress (gdbarch, start),
1155 paddress (gdbarch, end),
1156 hex_string (end - start),
1157 hex_string (file_ofs),
1158 filename);
1159 else
1160 printf_filtered (" %18s %18s %10s %10s %s\n",
1161 paddress (gdbarch, start),
1162 paddress (gdbarch, end),
1163 hex_string (end - start),
1164 hex_string (file_ofs),
1165 filename);
1166 }
1167}
1168
1169/* Implement "maintenance print core-file-backed-mappings" command.
1170
1171 If mappings are loaded, the results should be similar to the
1172 mappings shown by "info proc mappings". This command is mainly a
1173 debugging tool for GDB developers to make sure that the expected
1174 mappings are present after loading a core file. For Linux, the
1175 output provided by this command will be very similar (if not
1176 identical) to that provided by "info proc mappings". This is not
1177 necessarily the case for other OSes which might provide
1178 more/different information in the "info proc mappings" output. */
1179
1180static void
1181maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
1182{
1183 core_target *targ = get_current_core_target ();
1184 if (targ != nullptr)
1185 targ->info_proc_mappings (targ->core_gdbarch ());
1186}
1187
6c265988 1188void _initialize_corelow ();
c906108c 1189void
6c265988 1190_initialize_corelow ()
c906108c 1191{
d9f719f1 1192 add_target (core_target_info, core_target_open, filename_completer);
09c2f5d4 1193 add_cmd ("core-file-backed-mappings", class_maintenance,
dda83cd7 1194 maintenance_print_core_file_backed_mappings,
513487e1 1195 _("Print core file's file-backed mappings."),
09c2f5d4 1196 &maintenanceprintlist);
c906108c 1197}
This page took 1.626809 seconds and 4 git commands to generate.