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