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