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