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