]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/corelow.c
New Romanian translation for ld
[thirdparty/binutils-gdb.git] / gdb / corelow.c
CommitLineData
c906108c 1/* Core dump and executable file functions below target vector, for GDB.
4646aa9d 2
213516ef 3 Copyright (C) 1986-2023 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>
ef0f16cc 24#include "frame.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"
b91f93a0 49#include "gdbsupport/scoped_fd.h"
c689d1fe 50#include "gdbsupport/x86-xstate.h"
b91f93a0 51#include "debuginfod-support.h"
db082f59 52#include <unordered_map>
973695d6 53#include <unordered_set>
09c2f5d4 54#include "gdbcmd.h"
95ce627a 55#include "xml-tdesc.h"
68cffbbd 56#include "memtag.h"
8e860359 57
ee28ca0f
AC
58#ifndef O_LARGEFILE
59#define O_LARGEFILE 0
60#endif
61
f6ac5f3d
PA
62/* The core file target. */
63
d9f719f1
PA
64static const target_info core_target_info = {
65 "core",
66 N_("Local core dump file"),
590042fc
PW
67 N_("Use a core file as a target.\n\
68Specify the filename of the core file.")
d9f719f1
PA
69};
70
3b3dac9b 71class core_target final : public process_stratum_target
f6ac5f3d
PA
72{
73public:
15244507 74 core_target ();
f6ac5f3d 75
d9f719f1
PA
76 const target_info &info () const override
77 { return core_target_info; }
f6ac5f3d 78
f6ac5f3d
PA
79 void close () override;
80 void detach (inferior *, int) override;
81 void fetch_registers (struct regcache *, int) override;
82
83 enum target_xfer_status xfer_partial (enum target_object object,
84 const char *annex,
85 gdb_byte *readbuf,
86 const gdb_byte *writebuf,
87 ULONGEST offset, ULONGEST len,
88 ULONGEST *xfered_len) override;
89 void files_info () override;
90
57810aa7 91 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
92 const struct target_desc *read_description () override;
93
a068643d 94 std::string pid_to_str (ptid_t) override;
f6ac5f3d
PA
95
96 const char *thread_name (struct thread_info *) override;
97
2735d421 98 bool has_all_memory () override { return true; }
57810aa7
PA
99 bool has_memory () override;
100 bool has_stack () override;
101 bool has_registers () override;
5018ce90 102 bool has_execution (inferior *inf) override { return false; }
f3d11a9a 103
f6ac5f3d 104 bool info_proc (const char *, enum info_proc_what) override;
f6ac5f3d 105
68cffbbd
LM
106 bool supports_memory_tagging () override;
107
108 /* Core file implementation of fetch_memtags. Fetch the memory tags from
109 core file notes. */
110 bool fetch_memtags (CORE_ADDR address, size_t len,
111 gdb::byte_vector &tags, int type) override;
112
c689d1fe
JB
113 x86_xsave_layout fetch_x86_xsave_layout () override;
114
15244507
PA
115 /* A few helpers. */
116
117 /* Getter, see variable definition. */
118 struct gdbarch *core_gdbarch ()
119 {
120 return m_core_gdbarch;
121 }
122
123 /* See definition. */
124 void get_core_register_section (struct regcache *regcache,
125 const struct regset *regset,
126 const char *name,
dbd534fe 127 int section_min_size,
15244507
PA
128 const char *human_name,
129 bool required);
130
09c2f5d4
KB
131 /* See definition. */
132 void info_proc_mappings (struct gdbarch *gdbarch);
133
15244507
PA
134private: /* per-core data */
135
5d067f3d
PA
136 /* Get rid of the core inferior. */
137 void clear_core ();
138
15244507
PA
139 /* The core's section table. Note that these target sections are
140 *not* mapped in the current address spaces' set of target
141 sections --- those should come only from pure executable or
142 shared library bfds. The core bfd sections are an implementation
143 detail of the core target, just like ptrace is for unix child
144 targets. */
25b5a04e 145 std::vector<target_section> m_core_section_table;
15244507 146
db082f59
KB
147 /* File-backed address space mappings: some core files include
148 information about memory mapped files. */
25b5a04e 149 std::vector<target_section> m_core_file_mappings;
db082f59 150
973695d6
KB
151 /* Unavailable mappings. These correspond to pathnames which either
152 weren't found or could not be opened. Knowing these addresses can
153 still be useful. */
154 std::vector<mem_range> m_core_unavailable_mappings;
155
db082f59
KB
156 /* Build m_core_file_mappings. Called from the constructor. */
157 void build_file_mappings ();
158
973695d6
KB
159 /* Helper method for xfer_partial. */
160 enum target_xfer_status xfer_memory_via_mappings (gdb_byte *readbuf,
161 const gdb_byte *writebuf,
162 ULONGEST offset,
163 ULONGEST len,
164 ULONGEST *xfered_len);
165
15244507
PA
166 /* FIXME: kettenis/20031023: Eventually this field should
167 disappear. */
168 struct gdbarch *m_core_gdbarch = NULL;
169};
c906108c 170
15244507
PA
171core_target::core_target ()
172{
134df964
LM
173 /* Find a first arch based on the BFD. We need the initial gdbarch so
174 we can setup the hooks to find a target description. */
15244507 175 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
2acceee2 176
134df964
LM
177 /* If the arch is able to read a target description from the core, it
178 could yield a more specific gdbarch. */
179 const struct target_desc *tdesc = read_description ();
180
181 if (tdesc != nullptr)
182 {
183 struct gdbarch_info info;
134df964
LM
184 info.abfd = core_bfd;
185 info.target_desc = tdesc;
186 m_core_gdbarch = gdbarch_find_by_info (info);
187 }
188
6ba0a321
CB
189 if (!m_core_gdbarch
190 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
191 error (_("\"%s\": Core file format not supported"),
192 bfd_get_filename (core_bfd));
2acceee2 193
15244507 194 /* Find the data section */
2d128614 195 m_core_section_table = build_section_table (core_bfd);
db082f59
KB
196
197 build_file_mappings ();
15244507 198}
0e24ac5d 199
25b5a04e 200/* Construct the table for file-backed mappings if they exist.
db082f59
KB
201
202 For each unique path in the note, we'll open a BFD with a bfd
203 target of "binary". This is an unstructured bfd target upon which
204 we'll impose a structure from the mappings in the architecture-specific
205 mappings note. A BFD section is allocated and initialized for each
206 file-backed mapping.
207
208 We take care to not share already open bfds with other parts of
209 GDB; in particular, we don't want to add new sections to existing
210 BFDs. We do, however, ensure that the BFDs that we allocate here
211 will go away (be deallocated) when the core target is detached. */
212
213void
214core_target::build_file_mappings ()
215{
216 std::unordered_map<std::string, struct bfd *> bfd_map;
973695d6 217 std::unordered_set<std::string> unavailable_paths;
db082f59
KB
218
219 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
220 read_core_file_mappings method. */
221 gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
222
223 /* After determining the number of mappings, read_core_file_mappings
bb2a6777
TT
224 will invoke this lambda. */
225 [&] (ULONGEST)
db082f59 226 {
db082f59
KB
227 },
228
229 /* read_core_file_mappings will invoke this lambda for each mapping
230 that it finds. */
231 [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
aa95b2d4 232 const char *filename, const bfd_build_id *build_id)
db082f59
KB
233 {
234 /* Architecture-specific read_core_mapping methods are expected to
235 weed out non-file-backed mappings. */
236 gdb_assert (filename != nullptr);
237
ea70f941
LS
238 if (unavailable_paths.find (filename) != unavailable_paths.end ())
239 {
240 /* We have already seen some mapping for FILENAME but failed to
241 find/open the file. There is no point in trying the same
242 thing again so just record that the range [start, end) is
243 unavailable. */
244 m_core_unavailable_mappings.emplace_back (start, end - start);
245 return;
246 }
247
db082f59
KB
248 struct bfd *bfd = bfd_map[filename];
249 if (bfd == nullptr)
250 {
251 /* Use exec_file_find() to do sysroot expansion. It'll
252 also strip the potential sysroot "target:" prefix. If
253 there is no sysroot, an equivalent (possibly more
254 canonical) pathname will be provided. */
255 gdb::unique_xmalloc_ptr<char> expanded_fname
256 = exec_file_find (filename, NULL);
b91f93a0
AM
257
258 if (expanded_fname == nullptr && build_id != nullptr)
259 debuginfod_exec_query (build_id->data, build_id->size,
260 filename, &expanded_fname);
261
db082f59
KB
262 if (expanded_fname == nullptr)
263 {
973695d6 264 m_core_unavailable_mappings.emplace_back (start, end - start);
ea70f941
LS
265 unavailable_paths.insert (filename);
266 warning (_("Can't open file %s during file-backed mapping "
267 "note processing"),
268 filename);
db082f59
KB
269 return;
270 }
271
9b3a1001 272 bfd = bfd_openr (expanded_fname.get (), "binary");
db082f59
KB
273
274 if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
275 {
973695d6 276 m_core_unavailable_mappings.emplace_back (start, end - start);
ea70f941
LS
277 unavailable_paths.insert (filename);
278 warning (_("Can't open file %s which was expanded to %s "
279 "during file-backed mapping note processing"),
280 filename, expanded_fname.get ());
0ad504dd 281
db082f59
KB
282 if (bfd != nullptr)
283 bfd_close (bfd);
284 return;
285 }
286 /* Ensure that the bfd will be closed when core_bfd is closed.
287 This can be checked before/after a core file detach via
288 "maint info bfds". */
289 gdb_bfd_record_inclusion (core_bfd, bfd);
9b3a1001 290 bfd_map[filename] = bfd;
db082f59
KB
291 }
292
293 /* Make new BFD section. All sections have the same name,
294 which is permitted by bfd_make_section_anyway(). */
295 asection *sec = bfd_make_section_anyway (bfd, "load");
296 if (sec == nullptr)
297 error (_("Can't make section"));
298 sec->filepos = file_ofs;
299 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
300 bfd_set_section_size (sec, end - start);
301 bfd_set_section_vma (sec, start);
302 bfd_set_section_lma (sec, start);
303 bfd_set_section_alignment (sec, 2);
304
305 /* Set target_section fields. */
6be2a9ab 306 m_core_file_mappings.emplace_back (start, end, sec);
39f53acb
AM
307
308 /* If this is a bfd of a shared library, record its soname
309 and build id. */
310 if (build_id != nullptr)
311 {
312 gdb::unique_xmalloc_ptr<char> soname
313 = gdb_bfd_read_elf_soname (bfd->filename);
314 if (soname != nullptr)
315 set_cbfd_soname_build_id (current_program_space->cbfd,
316 soname.get (), build_id);
317 }
db082f59 318 });
973695d6
KB
319
320 normalize_mem_ranges (&m_core_unavailable_mappings);
15244507 321}
0e24ac5d 322
7f9f62ba
PA
323/* An arbitrary identifier for the core inferior. */
324#define CORELOW_PID 1
325
15244507 326void
5d067f3d 327core_target::clear_core ()
c906108c 328{
c906108c
SS
329 if (core_bfd)
330 {
60db1b85
PA
331 switch_to_no_thread (); /* Avoid confusion from thread
332 stuff. */
9324bfea 333 exit_inferior (current_inferior ());
c906108c 334
aff410f1 335 /* Clear out solib state while the bfd is still open. See
dda83cd7 336 comments in clear_solib in solib.c. */
a77053c2 337 clear_solib ();
7a292a7a 338
06333fea 339 current_program_space->cbfd.reset (nullptr);
c906108c 340 }
5d067f3d
PA
341}
342
343/* Close the core target. */
344
345void
346core_target::close ()
347{
348 clear_core ();
c906108c 349
15244507
PA
350 /* Core targets are heap-allocated (see core_target_open), so here
351 we delete ourselves. */
352 delete this;
74b7792f
AC
353}
354
aff410f1
MS
355/* Look for sections whose names start with `.reg/' so that we can
356 extract the list of threads in a core file. */
c906108c 357
22ed0412
AB
358/* If ASECT is a section whose name begins with '.reg/' then extract the
359 lwpid after the '/' and create a new thread in INF.
360
361 If REG_SECT is not nullptr, and the both ASECT and REG_SECT point at the
362 same position in the parent bfd object then switch to the newly created
363 thread, otherwise, the selected thread is left unchanged. */
364
c906108c 365static void
22ed0412 366add_to_thread_list (asection *asect, asection *reg_sect, inferior *inf)
c906108c 367{
fd361982 368 if (!startswith (bfd_section_name (asect), ".reg/"))
c906108c
SS
369 return;
370
22ed0412
AB
371 int lwpid = atoi (bfd_section_name (asect) + 5);
372 ptid_t ptid (inf->pid, lwpid);
60db1b85 373 thread_info *thr = add_thread (inf->process_target (), ptid);
c906108c 374
22ed0412 375 /* Warning, Will Robinson, looking at BFD private data! */
c906108c
SS
376
377 if (reg_sect != NULL
aff410f1 378 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
60db1b85 379 switch_to_thread (thr); /* Yes, make it current. */
c906108c
SS
380}
381
451953fa
PA
382/* Issue a message saying we have no core to debug, if FROM_TTY. */
383
384static void
385maybe_say_no_core_file_now (int from_tty)
386{
387 if (from_tty)
6cb06a8c 388 gdb_printf (_("No core file now.\n"));
451953fa
PA
389}
390
30baf67b 391/* Backward compatibility with old way of specifying core files. */
451953fa
PA
392
393void
394core_file_command (const char *filename, int from_tty)
395{
396 dont_repeat (); /* Either way, seems bogus. */
397
398 if (filename == NULL)
399 {
400 if (core_bfd != NULL)
401 {
402 target_detach (current_inferior (), from_tty);
403 gdb_assert (core_bfd == NULL);
404 }
405 else
406 maybe_say_no_core_file_now (from_tty);
407 }
408 else
409 core_target_open (filename, from_tty);
410}
411
08796916
AB
412/* A vmcore file is a core file created by the Linux kernel at the point of
413 a crash. Each thread in the core file represents a real CPU core, and
414 the lwpid for each thread is the pid of the process that was running on
415 that core at the moment of the crash.
416
417 However, not every CPU core will have been running a process, some cores
418 will be idle. For these idle cores the CPU writes an lwpid of 0. And
419 of course, multiple cores might be idle, so there could be multiple
420 threads with an lwpid of 0.
421
422 The problem is GDB doesn't really like threads with an lwpid of 0; GDB
423 presents such a thread as a process rather than a thread. And GDB
424 certainly doesn't like multiple threads having the same lwpid, each time
425 a new thread is seen with the same lwpid the earlier thread (with the
426 same lwpid) will be deleted.
427
428 This function addresses both of these problems by assigning a fake lwpid
429 to any thread with an lwpid of 0.
430
431 GDB finds the lwpid information by looking at the bfd section names
432 which include the lwpid, e.g. .reg/NN where NN is the lwpid. This
433 function looks though all the section names looking for sections named
434 .reg/NN. If any sections are found where NN == 0, then we assign a new
435 unique value of NN. Then, in a second pass, any sections ending /0 are
436 assigned their new number.
437
438 Remember, a core file may contain multiple register sections for
439 different register sets, but the sets are always grouped by thread, so
440 we can figure out which registers should be assigned the same new
441 lwpid. For example, consider a core file containing:
442
443 .reg/0, .reg2/0, .reg/0, .reg2/0
444
445 This represents two threads, each thread contains a .reg and .reg2
446 register set. The .reg represents the start of each thread. After
447 renaming the sections will now look like this:
448
449 .reg/1, .reg2/1, .reg/2, .reg2/2
450
451 After calling this function the rest of the core file handling code can
452 treat this core file just like any other core file. */
453
454static void
455rename_vmcore_idle_reg_sections (bfd *abfd, inferior *inf)
456{
457 /* Map from the bfd section to its lwpid (the /NN number). */
458 std::vector<std::pair<asection *, int>> sections_and_lwpids;
459
460 /* The set of all /NN numbers found. Needed so we can easily find unused
461 numbers in the case that we need to rename some sections. */
462 std::unordered_set<int> all_lwpids;
463
464 /* A count of how many sections called .reg/0 we have found. */
465 unsigned zero_lwpid_count = 0;
466
467 /* Look for all the .reg sections. Record the section object and the
468 lwpid which is extracted from the section name. Spot if any have an
469 lwpid of zero. */
470 for (asection *sect : gdb_bfd_sections (core_bfd))
471 {
472 if (startswith (bfd_section_name (sect), ".reg/"))
473 {
474 int lwpid = atoi (bfd_section_name (sect) + 5);
475 sections_and_lwpids.emplace_back (sect, lwpid);
476 all_lwpids.insert (lwpid);
477 if (lwpid == 0)
478 zero_lwpid_count++;
479 }
480 }
481
482 /* If every ".reg/NN" section has a non-zero lwpid then we don't need to
483 do any renaming. */
484 if (zero_lwpid_count == 0)
485 return;
486
487 /* Assign a new number to any .reg sections with an lwpid of 0. */
488 int new_lwpid = 1;
489 for (auto &sect_and_lwpid : sections_and_lwpids)
490 if (sect_and_lwpid.second == 0)
491 {
492 while (all_lwpids.find (new_lwpid) != all_lwpids.end ())
493 new_lwpid++;
494 sect_and_lwpid.second = new_lwpid;
495 new_lwpid++;
496 }
497
498 /* Now update the names of any sections with an lwpid of 0. This is
499 more than just the .reg sections we originally found. */
500 std::string replacement_lwpid_str;
501 auto iter = sections_and_lwpids.begin ();
502 int replacement_lwpid = 0;
503 for (asection *sect : gdb_bfd_sections (core_bfd))
504 {
505 if (iter != sections_and_lwpids.end () && sect == iter->first)
506 {
507 gdb_assert (startswith (bfd_section_name (sect), ".reg/"));
508
509 int lwpid = atoi (bfd_section_name (sect) + 5);
510 if (lwpid == iter->second)
511 {
512 /* This section was not given a new number. */
513 gdb_assert (lwpid != 0);
514 replacement_lwpid = 0;
515 }
516 else
517 {
518 replacement_lwpid = iter->second;
519 ptid_t ptid (inf->pid, replacement_lwpid);
520 if (!replacement_lwpid_str.empty ())
521 replacement_lwpid_str += ", ";
522 replacement_lwpid_str += target_pid_to_str (ptid);
523 }
524
525 iter++;
526 }
527
528 if (replacement_lwpid != 0)
529 {
530 const char *name = bfd_section_name (sect);
531 size_t len = strlen (name);
532
533 if (strncmp (name + len - 2, "/0", 2) == 0)
534 {
535 /* This section needs a new name. */
536 std::string name_str
537 = string_printf ("%.*s/%d",
538 static_cast<int> (len - 2),
539 name, replacement_lwpid);
540 char *name_buf
541 = static_cast<char *> (bfd_alloc (abfd, name_str.size () + 1));
542 if (name_buf == nullptr)
543 error (_("failed to allocate space for section name '%s'"),
544 name_str.c_str ());
545 memcpy (name_buf, name_str.c_str(), name_str.size () + 1);
546 bfd_rename_section (sect, name_buf);
547 }
548 }
549 }
550
551 if (zero_lwpid_count == 1)
552 warning (_("found thread with pid 0, assigned replacement Target Id: %s"),
553 replacement_lwpid_str.c_str ());
554 else
555 warning (_("found threads with pid 0, assigned replacement Target Ids: %s"),
556 replacement_lwpid_str.c_str ());
557}
558
aa2d5a42
KS
559/* Locate (and load) an executable file (and symbols) given the core file
560 BFD ABFD. */
561
562static void
563locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
564{
565 const bfd_build_id *build_id = build_id_bfd_get (abfd);
566 if (build_id == nullptr)
567 return;
568
569 gdb_bfd_ref_ptr execbfd
570 = build_id_to_exec_bfd (build_id->size, build_id->data);
571
b91f93a0
AM
572 if (execbfd == nullptr)
573 {
574 /* Attempt to query debuginfod for the executable. */
575 gdb::unique_xmalloc_ptr<char> execpath;
576 scoped_fd fd = debuginfod_exec_query (build_id->data, build_id->size,
577 abfd->filename, &execpath);
578
579 if (fd.get () >= 0)
580 {
581 execbfd = gdb_bfd_open (execpath.get (), gnutarget);
582
583 if (execbfd == nullptr)
584 warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"),
585 execpath.get (),
586 gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
587 else if (!build_id_verify (execbfd.get (), build_id->size,
588 build_id->data))
589 execbfd.reset (nullptr);
590 }
591 }
592
aa2d5a42
KS
593 if (execbfd != nullptr)
594 {
595 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
596 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
597 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
598 }
599}
600
d9f719f1 601/* See gdbcore.h. */
c906108c 602
f6ac5f3d 603void
d9f719f1 604core_target_open (const char *arg, int from_tty)
c906108c
SS
605{
606 const char *p;
607 int siggy;
c906108c 608 int scratch_chan;
ee28ca0f 609 int flags;
c906108c
SS
610
611 target_preopen (from_tty);
014f9477 612 if (!arg)
c906108c 613 {
8a3fe4f8 614 if (core_bfd)
3e43a32a
MS
615 error (_("No core file specified. (Use `detach' "
616 "to stop debugging a core file.)"));
8a3fe4f8
AC
617 else
618 error (_("No core file specified."));
c906108c
SS
619 }
620
ee0c3293 621 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
f947f967
TV
622 if (strlen (filename.get ()) != 0
623 && !IS_ABSOLUTE_PATH (filename.get ()))
7ab2607f 624 filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ());
c906108c 625
ee28ca0f
AC
626 flags = O_BINARY | O_LARGEFILE;
627 if (write_files)
628 flags |= O_RDWR;
629 else
630 flags |= O_RDONLY;
13084383 631 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0).release ();
c906108c 632 if (scratch_chan < 0)
ee0c3293 633 perror_with_name (filename.get ());
c906108c 634
ee0c3293 635 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
192b62ce
TT
636 write_files ? FOPEN_RUB : FOPEN_RB,
637 scratch_chan));
c906108c 638 if (temp_bfd == NULL)
ee0c3293 639 perror_with_name (filename.get ());
c906108c 640
6ba0a321 641 if (!bfd_check_format (temp_bfd.get (), bfd_core))
c906108c
SS
642 {
643 /* Do it after the err msg */
aff410f1 644 /* FIXME: should be checking for errors from bfd_close (for one
dda83cd7
SM
645 thing, on error it does not free all the storage associated
646 with the bfd). */
8a3fe4f8 647 error (_("\"%s\" is not a core dump: %s"),
ee0c3293 648 filename.get (), bfd_errmsg (bfd_get_error ()));
c906108c
SS
649 }
650
06333fea 651 current_program_space->cbfd = std::move (temp_bfd);
c906108c 652
15244507 653 core_target *target = new core_target ();
0e24ac5d 654
15244507
PA
655 /* Own the target until it is successfully pushed. */
656 target_ops_up target_holder (target);
2acceee2 657
c906108c
SS
658 validate_files ();
659
2f1b5984
MK
660 /* If we have no exec file, try to set the architecture from the
661 core file. We don't do this unconditionally since an exec file
662 typically contains more information that helps us determine the
663 architecture than a core file. */
7e10abd1 664 if (!current_program_space->exec_bfd ())
2f1b5984 665 set_gdbarch_from_file (core_bfd);
cbda0a99 666
02980c56 667 current_inferior ()->push_target (std::move (target_holder));
c906108c 668
60db1b85 669 switch_to_no_thread ();
0de3b513 670
739fc47a
PA
671 /* Need to flush the register cache (and the frame cache) from a
672 previous debug session. If inferior_ptid ends up the same as the
673 last debug session --- e.g., b foo; run; gcore core1; step; gcore
674 core2; core core1; core core2 --- then there's potential for
675 get_current_regcache to return the cached regcache of the
676 previous session, and the frame cache being stale. */
677 registers_changed ();
678
22ed0412
AB
679 /* Find (or fake) the pid for the process in this core file, and
680 initialise the current inferior with that pid. */
681 bool fake_pid_p = false;
682 int pid = bfd_core_file_pid (core_bfd);
683 if (pid == 0)
684 {
685 fake_pid_p = true;
686 pid = CORELOW_PID;
687 }
688
689 inferior *inf = current_inferior ();
690 gdb_assert (inf->pid == 0);
691 inferior_appeared (inf, pid);
692 inf->fake_pid_p = fake_pid_p;
693
08796916
AB
694 /* Rename any .reg/0 sections, giving them each a fake lwpid. */
695 rename_vmcore_idle_reg_sections (core_bfd, inf);
696
0de3b513
PA
697 /* Build up thread list from BFD sections, and possibly set the
698 current thread to the .reg/NN section matching the .reg
aff410f1 699 section. */
a190fabb
TT
700 asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
701 for (asection *sect : gdb_bfd_sections (core_bfd))
22ed0412 702 add_to_thread_list (sect, reg_sect, inf);
0de3b513 703
d7e15655 704 if (inferior_ptid == null_ptid)
3cdd9356
PA
705 {
706 /* Either we found no .reg/NN section, and hence we have a
707 non-threaded core (single-threaded, from gdb's perspective),
708 or for some reason add_to_thread_list couldn't determine
709 which was the "main" thread. The latter case shouldn't
710 usually happen, but we're dealing with input here, which can
711 always be broken in different ways. */
22ed0412 712 thread_info *thread = first_thread_of_inferior (inf);
c5504eaf 713
3cdd9356 714 if (thread == NULL)
22ed0412 715 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
60db1b85
PA
716
717 switch_to_thread (thread);
3cdd9356
PA
718 }
719
7e10abd1 720 if (current_program_space->exec_bfd () == nullptr)
aa2d5a42
KS
721 locate_exec_from_corefile_build_id (core_bfd, from_tty);
722
a7aba266 723 post_create_inferior (from_tty);
959b8724 724
0de3b513
PA
725 /* Now go through the target stack looking for threads since there
726 may be a thread_stratum target loaded on top of target core by
727 now. The layer above should claim threads found in the BFD
728 sections. */
a70b8144 729 try
8e7b59a5 730 {
e8032dde 731 target_update_thread_list ();
8e7b59a5
KS
732 }
733
230d2906 734 catch (const gdb_exception_error &except)
492d29ea
PA
735 {
736 exception_print (gdb_stderr, except);
737 }
0de3b513 738
c906108c
SS
739 p = bfd_core_file_failing_command (core_bfd);
740 if (p)
6cb06a8c 741 gdb_printf (_("Core was generated by `%s'.\n"), p);
c906108c 742
0c557179
SDJ
743 /* Clearing any previous state of convenience variables. */
744 clear_exit_convenience_vars ();
745
c906108c
SS
746 siggy = bfd_core_file_failing_signal (core_bfd);
747 if (siggy > 0)
423ec54c 748 {
15244507
PA
749 gdbarch *core_gdbarch = target->core_gdbarch ();
750
22203bbf 751 /* If we don't have a CORE_GDBARCH to work with, assume a native
1f8cf220
PA
752 core (map gdb_signal from host signals). If we do have
753 CORE_GDBARCH to work with, but no gdb_signal_from_target
754 implementation for that gdbarch, as a fallback measure,
755 assume the host signal mapping. It'll be correct for native
756 cores, but most likely incorrect for cross-cores. */
2ea28649 757 enum gdb_signal sig = (core_gdbarch != NULL
1f8cf220
PA
758 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
759 ? gdbarch_gdb_signal_from_target (core_gdbarch,
760 siggy)
761 : gdb_signal_from_host (siggy));
423ec54c 762
6cb06a8c
TT
763 gdb_printf (_("Program terminated with signal %s, %s"),
764 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
ad97bfc5
JB
765 if (gdbarch_report_signal_info_p (core_gdbarch))
766 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
6cb06a8c 767 gdb_printf (_(".\n"));
0c557179
SDJ
768
769 /* Set the value of the internal variable $_exitsignal,
770 which holds the signal uncaught by the inferior. */
771 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
772 siggy);
423ec54c 773 }
c906108c 774
87ab71f0 775 /* Fetch all registers from core file. */
9c742269 776 target_fetch_registers (get_thread_regcache (inferior_thread ()), -1);
c906108c 777
87ab71f0
PA
778 /* Now, set up the frame cache, and print the top of stack. */
779 reinit_frame_cache ();
08d72866 780 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
f0e8c4c5
JK
781
782 /* Current thread should be NUM 1 but the user does not know that.
783 If a program is single threaded gdb in general does not mention
784 anything about threads. That is why the test is >= 2. */
5b6d1e4f 785 if (thread_count (target) >= 2)
f0e8c4c5 786 {
a70b8144 787 try
f0e8c4c5
JK
788 {
789 thread_command (NULL, from_tty);
790 }
230d2906 791 catch (const gdb_exception_error &except)
492d29ea
PA
792 {
793 exception_print (gdb_stderr, except);
794 }
f0e8c4c5 795 }
c906108c
SS
796}
797
f6ac5f3d
PA
798void
799core_target::detach (inferior *inf, int from_tty)
c906108c 800{
5d067f3d
PA
801 /* Get rid of the core. Don't rely on core_target::close doing it,
802 because target_detach may be called with core_target's refcount > 1,
803 meaning core_target::close may not be called yet by the
804 unpush_target call below. */
805 clear_core ();
806
807 /* Note that 'this' may be dangling after this call. unpush_target
808 closes the target if the refcount reaches 0, and our close
809 implementation deletes 'this'. */
fadf6add 810 inf->unpush_target (this);
15244507 811
66452beb
PW
812 /* Clear the register cache and the frame cache. */
813 registers_changed ();
c906108c 814 reinit_frame_cache ();
451953fa 815 maybe_say_no_core_file_now (from_tty);
c906108c
SS
816}
817
de57eccd 818/* Try to retrieve registers from a section in core_bfd, and supply
6ba0a321 819 them to REGSET.
de57eccd 820
11a33714
SM
821 If ptid's lwp member is zero, do the single-threaded
822 thing: look for a section named NAME. If ptid's lwp
0de3b513
PA
823 member is non-zero, do the multi-threaded thing: look for a section
824 named "NAME/LWP", where LWP is the shortest ASCII decimal
11a33714 825 representation of ptid's lwp member.
de57eccd
JM
826
827 HUMAN_NAME is a human-readable name for the kind of registers the
828 NAME section contains, for use in error messages.
829
15244507
PA
830 If REQUIRED is true, print an error if the core file doesn't have a
831 section by the appropriate name. Otherwise, just do nothing. */
de57eccd 832
15244507
PA
833void
834core_target::get_core_register_section (struct regcache *regcache,
835 const struct regset *regset,
836 const char *name,
dbd534fe 837 int section_min_size,
15244507
PA
838 const char *human_name,
839 bool required)
de57eccd 840{
6ba0a321
CB
841 gdb_assert (regset != nullptr);
842
7be0c536 843 struct bfd_section *section;
de57eccd 844 bfd_size_type size;
6ba0a321 845 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
de57eccd 846
3c3ae77e 847 thread_section_name section_name (name, regcache->ptid ());
de57eccd 848
3c3ae77e 849 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
de57eccd
JM
850 if (! section)
851 {
852 if (required)
aff410f1
MS
853 warning (_("Couldn't find %s registers in core file."),
854 human_name);
de57eccd
JM
855 return;
856 }
857
fd361982 858 size = bfd_section_size (section);
dbd534fe 859 if (size < section_min_size)
8f0435f7 860 {
3c3ae77e
PA
861 warning (_("Section `%s' in core file too small."),
862 section_name.c_str ());
8f0435f7
AA
863 return;
864 }
dbd534fe 865 if (size != section_min_size && !variable_size_section)
f962539a
AA
866 {
867 warning (_("Unexpected size of section `%s' in core file."),
3c3ae77e 868 section_name.c_str ());
f962539a 869 }
8f0435f7 870
0cac9354 871 gdb::byte_vector contents (size);
d8b2f9e3
SM
872 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
873 (file_ptr) 0, size))
de57eccd 874 {
8a3fe4f8 875 warning (_("Couldn't read %s registers from `%s' section in core file."),
3c3ae77e 876 human_name, section_name.c_str ());
de57eccd
JM
877 return;
878 }
879
6ba0a321 880 regset->supply_regset (regset, regcache, -1, contents.data (), size);
de57eccd
JM
881}
882
15244507
PA
883/* Data passed to gdbarch_iterate_over_regset_sections's callback. */
884struct get_core_registers_cb_data
885{
886 core_target *target;
887 struct regcache *regcache;
888};
889
5aa82d05
AA
890/* Callback for get_core_registers that handles a single core file
891 register note section. */
892
893static void
a616bb94 894get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
8f0435f7 895 const struct regset *regset,
5aa82d05
AA
896 const char *human_name, void *cb_data)
897{
6ba0a321
CB
898 gdb_assert (regset != nullptr);
899
15244507
PA
900 auto *data = (get_core_registers_cb_data *) cb_data;
901 bool required = false;
6ba0a321 902 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
a616bb94
AH
903
904 if (!variable_size_section)
905 gdb_assert (supply_size == collect_size);
5aa82d05
AA
906
907 if (strcmp (sect_name, ".reg") == 0)
8f0435f7 908 {
15244507 909 required = true;
8f0435f7
AA
910 if (human_name == NULL)
911 human_name = "general-purpose";
912 }
5aa82d05 913 else if (strcmp (sect_name, ".reg2") == 0)
8f0435f7
AA
914 {
915 if (human_name == NULL)
916 human_name = "floating-point";
917 }
918
15244507 919 data->target->get_core_register_section (data->regcache, regset, sect_name,
6ba0a321 920 supply_size, human_name, required);
5aa82d05 921}
de57eccd 922
c906108c
SS
923/* Get the registers out of a core file. This is the machine-
924 independent part. Fetch_core_registers is the machine-dependent
aff410f1
MS
925 part, typically implemented in the xm-file for each
926 architecture. */
c906108c
SS
927
928/* We just get all the registers, so we don't use regno. */
929
f6ac5f3d
PA
930void
931core_target::fetch_registers (struct regcache *regcache, int regno)
c906108c 932{
15244507 933 if (!(m_core_gdbarch != nullptr
6ba0a321 934 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
c906108c 935 {
6cb06a8c
TT
936 gdb_printf (gdb_stderr,
937 "Can't fetch registers from this type of core file\n");
c906108c
SS
938 return;
939 }
940
6ba0a321
CB
941 struct gdbarch *gdbarch = regcache->arch ();
942 get_core_registers_cb_data data = { this, regcache };
943 gdbarch_iterate_over_regset_sections (gdbarch,
944 get_core_registers_cb,
945 (void *) &data, NULL);
c906108c 946
ee99023e 947 /* Mark all registers not found in the core as unavailable. */
6ba0a321 948 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
0ec9f114 949 if (regcache->get_register_status (i) == REG_UNKNOWN)
73e1c03f 950 regcache->raw_supply (i, NULL);
c906108c
SS
951}
952
f6ac5f3d
PA
953void
954core_target::files_info ()
c906108c 955{
15244507 956 print_section_info (&m_core_section_table, core_bfd);
c906108c 957}
e2544d02 958\f
973695d6
KB
959/* Helper method for core_target::xfer_partial. */
960
961enum target_xfer_status
962core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
963 const gdb_byte *writebuf,
964 ULONGEST offset, ULONGEST len,
965 ULONGEST *xfered_len)
966{
967 enum target_xfer_status xfer_status;
968
969 xfer_status = (section_table_xfer_memory_partial
970 (readbuf, writebuf,
971 offset, len, xfered_len,
bb2a6777 972 m_core_file_mappings));
973695d6
KB
973
974 if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
975 return xfer_status;
976
977 /* There are instances - e.g. when debugging within a docker
978 container using the AUFS storage driver - where the pathnames
979 obtained from the note section are incorrect. Despite the path
980 being wrong, just knowing the start and end addresses of the
981 mappings is still useful; we can attempt an access of the file
982 stratum constrained to the address ranges corresponding to the
983 unavailable mappings. */
984
985 ULONGEST memaddr = offset;
986 ULONGEST memend = offset + len;
987
988 for (const auto &mr : m_core_unavailable_mappings)
989 {
990 if (address_in_mem_range (memaddr, &mr))
dda83cd7 991 {
973695d6
KB
992 if (!address_in_mem_range (memend, &mr))
993 len = mr.start + mr.length - memaddr;
994
995 xfer_status = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY,
996 NULL,
997 readbuf,
998 writebuf,
999 offset,
1000 len,
1001 xfered_len);
1002 break;
1003 }
1004 }
1005
1006 return xfer_status;
1007}
1008
f6ac5f3d
PA
1009enum target_xfer_status
1010core_target::xfer_partial (enum target_object object, const char *annex,
1011 gdb_byte *readbuf, const gdb_byte *writebuf,
1012 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
e2544d02
RM
1013{
1014 switch (object)
1015 {
1016 case TARGET_OBJECT_MEMORY:
2735d421
KB
1017 {
1018 enum target_xfer_status xfer_status;
1019
1020 /* Try accessing memory contents from core file data,
1021 restricting consideration to those sections for which
1022 the BFD section flag SEC_HAS_CONTENTS is set. */
1023 auto has_contents_cb = [] (const struct target_section *s)
1024 {
1025 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
1026 };
1027 xfer_status = section_table_xfer_memory_partial
1028 (readbuf, writebuf,
1029 offset, len, xfered_len,
bb2a6777 1030 m_core_section_table,
2735d421
KB
1031 has_contents_cb);
1032 if (xfer_status == TARGET_XFER_OK)
1033 return TARGET_XFER_OK;
1034
db082f59
KB
1035 /* Check file backed mappings. If they're available, use
1036 core file provided mappings (e.g. from .note.linuxcore.file
1037 or the like) as this should provide a more accurate
1038 result. If not, check the stratum beneath us, which should
1bd57575
LM
1039 be the file stratum.
1040
1041 We also check unavailable mappings due to Docker/AUFS driver
1042 issues. */
1043 if (!m_core_file_mappings.empty ()
1044 || !m_core_unavailable_mappings.empty ())
1045 {
1046 xfer_status = xfer_memory_via_mappings (readbuf, writebuf, offset,
1047 len, xfered_len);
1048 }
db082f59
KB
1049 else
1050 xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
1051 writebuf, offset, len,
1052 xfered_len);
2735d421
KB
1053 if (xfer_status == TARGET_XFER_OK)
1054 return TARGET_XFER_OK;
e2544d02 1055
2735d421
KB
1056 /* Finally, attempt to access data in core file sections with
1057 no contents. These will typically read as all zero. */
1058 auto no_contents_cb = [&] (const struct target_section *s)
1059 {
1060 return !has_contents_cb (s);
1061 };
1062 xfer_status = section_table_xfer_memory_partial
1063 (readbuf, writebuf,
1064 offset, len, xfered_len,
bb2a6777 1065 m_core_section_table,
2735d421
KB
1066 no_contents_cb);
1067
1068 return xfer_status;
1069 }
e2544d02
RM
1070 case TARGET_OBJECT_AUXV:
1071 if (readbuf)
1072 {
1073 /* When the aux vector is stored in core file, BFD
1074 represents this with a fake section called ".auxv". */
1075
c4c5b7ba 1076 struct bfd_section *section;
e2544d02 1077 bfd_size_type size;
e2544d02
RM
1078
1079 section = bfd_get_section_by_name (core_bfd, ".auxv");
1080 if (section == NULL)
2ed4b548 1081 return TARGET_XFER_E_IO;
e2544d02 1082
fd361982 1083 size = bfd_section_size (section);
e2544d02 1084 if (offset >= size)
9b409511 1085 return TARGET_XFER_EOF;
e2544d02
RM
1086 size -= offset;
1087 if (size > len)
1088 size = len;
9b409511
YQ
1089
1090 if (size == 0)
1091 return TARGET_XFER_EOF;
1092 if (!bfd_get_section_contents (core_bfd, section, readbuf,
1093 (file_ptr) offset, size))
e2544d02 1094 {
8a3fe4f8 1095 warning (_("Couldn't read NT_AUXV note in core file."));
2ed4b548 1096 return TARGET_XFER_E_IO;
e2544d02
RM
1097 }
1098
9b409511
YQ
1099 *xfered_len = (ULONGEST) size;
1100 return TARGET_XFER_OK;
e2544d02 1101 }
2ed4b548 1102 return TARGET_XFER_E_IO;
e2544d02 1103
403e1656
MK
1104 case TARGET_OBJECT_WCOOKIE:
1105 if (readbuf)
1106 {
1107 /* When the StackGhost cookie is stored in core file, BFD
aff410f1
MS
1108 represents this with a fake section called
1109 ".wcookie". */
403e1656
MK
1110
1111 struct bfd_section *section;
1112 bfd_size_type size;
403e1656
MK
1113
1114 section = bfd_get_section_by_name (core_bfd, ".wcookie");
1115 if (section == NULL)
2ed4b548 1116 return TARGET_XFER_E_IO;
403e1656 1117
fd361982 1118 size = bfd_section_size (section);
403e1656 1119 if (offset >= size)
96c4f946 1120 return TARGET_XFER_EOF;
403e1656
MK
1121 size -= offset;
1122 if (size > len)
1123 size = len;
9b409511
YQ
1124
1125 if (size == 0)
1126 return TARGET_XFER_EOF;
1127 if (!bfd_get_section_contents (core_bfd, section, readbuf,
1128 (file_ptr) offset, size))
403e1656 1129 {
8a3fe4f8 1130 warning (_("Couldn't read StackGhost cookie in core file."));
2ed4b548 1131 return TARGET_XFER_E_IO;
403e1656
MK
1132 }
1133
9b409511
YQ
1134 *xfered_len = (ULONGEST) size;
1135 return TARGET_XFER_OK;
1136
403e1656 1137 }
2ed4b548 1138 return TARGET_XFER_E_IO;
403e1656 1139
de584861 1140 case TARGET_OBJECT_LIBRARIES:
15244507
PA
1141 if (m_core_gdbarch != nullptr
1142 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
de584861
PA
1143 {
1144 if (writebuf)
2ed4b548 1145 return TARGET_XFER_E_IO;
9b409511
YQ
1146 else
1147 {
15244507 1148 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
9b409511
YQ
1149 readbuf,
1150 offset, len);
1151
1152 if (*xfered_len == 0)
1153 return TARGET_XFER_EOF;
1154 else
1155 return TARGET_XFER_OK;
1156 }
de584861 1157 }
0e30a3b0 1158 return TARGET_XFER_E_IO;
de584861 1159
356a5233 1160 case TARGET_OBJECT_LIBRARIES_AIX:
15244507
PA
1161 if (m_core_gdbarch != nullptr
1162 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
356a5233
JB
1163 {
1164 if (writebuf)
2ed4b548 1165 return TARGET_XFER_E_IO;
9b409511
YQ
1166 else
1167 {
1168 *xfered_len
15244507 1169 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
9b409511
YQ
1170 readbuf, offset,
1171 len);
1172
1173 if (*xfered_len == 0)
1174 return TARGET_XFER_EOF;
1175 else
1176 return TARGET_XFER_OK;
1177 }
356a5233 1178 }
0e30a3b0 1179 return TARGET_XFER_E_IO;
356a5233 1180
9015683b
TT
1181 case TARGET_OBJECT_SIGNAL_INFO:
1182 if (readbuf)
9b409511 1183 {
15244507
PA
1184 if (m_core_gdbarch != nullptr
1185 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
9b409511 1186 {
15244507 1187 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
382b69bb
JB
1188 offset, len);
1189
1190 if (l >= 0)
1191 {
1192 *xfered_len = l;
1193 if (l == 0)
1194 return TARGET_XFER_EOF;
1195 else
1196 return TARGET_XFER_OK;
1197 }
9b409511
YQ
1198 }
1199 }
2ed4b548 1200 return TARGET_XFER_E_IO;
9015683b 1201
e2544d02 1202 default:
b6a8c27b
PA
1203 return this->beneath ()->xfer_partial (object, annex, readbuf,
1204 writebuf, offset, len,
1205 xfered_len);
e2544d02
RM
1206 }
1207}
1208
c906108c 1209\f
c906108c
SS
1210
1211/* Okay, let's be honest: threads gleaned from a core file aren't
1212 exactly lively, are they? On the other hand, if we don't claim
1213 that each & every one is alive, then we don't get any of them
1214 to appear in an "info thread" command, which is quite a useful
1215 behaviour.
c5aa993b 1216 */
57810aa7 1217bool
f6ac5f3d 1218core_target::thread_alive (ptid_t ptid)
c906108c 1219{
57810aa7 1220 return true;
c906108c
SS
1221}
1222
4eb0ad19
DJ
1223/* Ask the current architecture what it knows about this core file.
1224 That will be used, in turn, to pick a better architecture. This
1225 wrapper could be avoided if targets got a chance to specialize
15244507 1226 core_target. */
4eb0ad19 1227
f6ac5f3d
PA
1228const struct target_desc *
1229core_target::read_description ()
4eb0ad19 1230{
b93d537f
LM
1231 /* First check whether the target wants us to use the corefile target
1232 description notes. */
1233 if (gdbarch_use_target_description_from_corefile_notes (m_core_gdbarch,
1234 core_bfd))
95ce627a 1235 {
b93d537f
LM
1236 /* If the core file contains a target description note then go ahead and
1237 use that. */
1238 bfd_size_type tdesc_note_size = 0;
1239 struct bfd_section *tdesc_note_section
1240 = bfd_get_section_by_name (core_bfd, ".gdb-tdesc");
1241 if (tdesc_note_section != nullptr)
1242 tdesc_note_size = bfd_section_size (tdesc_note_section);
1243 if (tdesc_note_size > 0)
95ce627a 1244 {
b93d537f
LM
1245 gdb::char_vector contents (tdesc_note_size + 1);
1246 if (bfd_get_section_contents (core_bfd, tdesc_note_section,
1247 contents.data (), (file_ptr) 0,
1248 tdesc_note_size))
1249 {
1250 /* Ensure we have a null terminator. */
1251 contents[tdesc_note_size] = '\0';
1252 const struct target_desc *result
1253 = string_read_description_xml (contents.data ());
1254 if (result != nullptr)
1255 return result;
1256 }
95ce627a
AB
1257 }
1258 }
1259
b93d537f
LM
1260 /* If the architecture provides a corefile target description hook, use
1261 it now. Even if the core file contains a target description in a note
1262 section, it is not useful for targets that can potentially have distinct
1263 descriptions for each thread. One example is AArch64's SVE/SME
1264 extensions that allow per-thread vector length changes, resulting in
1265 registers with different sizes. */
15244507 1266 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
2117c711
TT
1267 {
1268 const struct target_desc *result;
1269
15244507 1270 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
b93d537f 1271 if (result != nullptr)
2117c711
TT
1272 return result;
1273 }
4eb0ad19 1274
b6a8c27b 1275 return this->beneath ()->read_description ();
4eb0ad19
DJ
1276}
1277
a068643d 1278std::string
f6ac5f3d 1279core_target::pid_to_str (ptid_t ptid)
0de3b513 1280{
88f38a04 1281 struct inferior *inf;
a5ee0f0c 1282 int pid;
0de3b513 1283
a5ee0f0c
PA
1284 /* The preferred way is to have a gdbarch/OS specific
1285 implementation. */
15244507
PA
1286 if (m_core_gdbarch != nullptr
1287 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1288 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
c5504eaf 1289
a5ee0f0c
PA
1290 /* Otherwise, if we don't have one, we'll just fallback to
1291 "process", with normal_pid_to_str. */
28439f5e 1292
a5ee0f0c 1293 /* Try the LWPID field first. */
e38504b3 1294 pid = ptid.lwp ();
a5ee0f0c 1295 if (pid != 0)
f2907e49 1296 return normal_pid_to_str (ptid_t (pid));
a5ee0f0c
PA
1297
1298 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1299 only if it isn't a fake PID. */
5b6d1e4f 1300 inf = find_inferior_ptid (this, ptid);
88f38a04 1301 if (inf != NULL && !inf->fake_pid_p)
a5ee0f0c 1302 return normal_pid_to_str (ptid);
0de3b513 1303
a5ee0f0c 1304 /* No luck. We simply don't have a valid PID to print. */
a068643d 1305 return "<main task>";
0de3b513
PA
1306}
1307
f6ac5f3d
PA
1308const char *
1309core_target::thread_name (struct thread_info *thr)
4dfc5dbc 1310{
15244507
PA
1311 if (m_core_gdbarch != nullptr
1312 && gdbarch_core_thread_name_p (m_core_gdbarch))
1313 return gdbarch_core_thread_name (m_core_gdbarch, thr);
4dfc5dbc
JB
1314 return NULL;
1315}
1316
57810aa7 1317bool
f6ac5f3d 1318core_target::has_memory ()
c35b1492
PA
1319{
1320 return (core_bfd != NULL);
1321}
1322
57810aa7 1323bool
f6ac5f3d 1324core_target::has_stack ()
c35b1492
PA
1325{
1326 return (core_bfd != NULL);
1327}
1328
57810aa7 1329bool
f6ac5f3d 1330core_target::has_registers ()
c35b1492
PA
1331{
1332 return (core_bfd != NULL);
1333}
1334
451b7c33
TT
1335/* Implement the to_info_proc method. */
1336
f6ac5f3d
PA
1337bool
1338core_target::info_proc (const char *args, enum info_proc_what request)
451b7c33
TT
1339{
1340 struct gdbarch *gdbarch = get_current_arch ();
1341
1342 /* Since this is the core file target, call the 'core_info_proc'
1343 method on gdbarch, not 'info_proc'. */
1344 if (gdbarch_core_info_proc_p (gdbarch))
1345 gdbarch_core_info_proc (gdbarch, args, request);
c906108c 1346
f6ac5f3d 1347 return true;
c906108c
SS
1348}
1349
68cffbbd
LM
1350/* Implementation of the "supports_memory_tagging" target_ops method. */
1351
1352bool
1353core_target::supports_memory_tagging ()
1354{
1355 /* Look for memory tag sections. If they exist, that means this core file
1356 supports memory tagging. */
1357
1358 return (bfd_get_section_by_name (core_bfd, "memtag") != nullptr);
1359}
1360
1361/* Implementation of the "fetch_memtags" target_ops method. */
1362
1363bool
1364core_target::fetch_memtags (CORE_ADDR address, size_t len,
1365 gdb::byte_vector &tags, int type)
1366{
99d9c3b9 1367 gdbarch *gdbarch = current_inferior ()->arch ();
68cffbbd
LM
1368
1369 /* Make sure we have a way to decode the memory tag notes. */
1370 if (!gdbarch_decode_memtag_section_p (gdbarch))
1371 error (_("gdbarch_decode_memtag_section not implemented for this "
1372 "architecture."));
1373
1374 memtag_section_info info;
1375 info.memtag_section = nullptr;
1376
1377 while (get_next_core_memtag_section (core_bfd, info.memtag_section,
1378 address, info))
1379 {
1380 size_t adjusted_length
1381 = (address + len < info.end_address) ? len : (info.end_address - address);
1382
1383 /* Decode the memory tag note and return the tags. */
1384 gdb::byte_vector tags_read
1385 = gdbarch_decode_memtag_section (gdbarch, info.memtag_section, type,
1386 address, adjusted_length);
1387
1388 /* Transfer over the tags that have been read. */
1389 tags.insert (tags.end (), tags_read.begin (), tags_read.end ());
1390
1391 /* ADDRESS + LEN may cross the boundaries of a particular memory tag
1392 segment. Check if we need to fetch tags from a different section. */
1393 if (!tags_read.empty () && (address + len) < info.end_address)
1394 return true;
1395
1396 /* There are more tags to fetch. Update ADDRESS and LEN. */
1397 len -= (info.end_address - address);
1398 address = info.end_address;
1399 }
1400
1401 return false;
1402}
1403
c689d1fe
JB
1404/* Implementation of the "fetch_x86_xsave_layout" target_ops method. */
1405
1406x86_xsave_layout
1407core_target::fetch_x86_xsave_layout ()
1408{
1409 if (m_core_gdbarch != nullptr &&
1410 gdbarch_core_read_x86_xsave_layout_p (m_core_gdbarch))
1411 {
1412 x86_xsave_layout layout;
1413 if (!gdbarch_core_read_x86_xsave_layout (m_core_gdbarch, layout))
1414 return {};
1415
1416 return layout;
1417 }
1418
1419 return {};
1420}
1421
09c2f5d4
KB
1422/* Get a pointer to the current core target. If not connected to a
1423 core target, return NULL. */
1424
1425static core_target *
1426get_current_core_target ()
1427{
1428 target_ops *proc_target = current_inferior ()->process_target ();
1429 return dynamic_cast<core_target *> (proc_target);
1430}
1431
1432/* Display file backed mappings from core file. */
1433
1434void
1435core_target::info_proc_mappings (struct gdbarch *gdbarch)
1436{
d7a78e5c 1437 if (!m_core_file_mappings.empty ())
09c2f5d4 1438 {
6cb06a8c 1439 gdb_printf (_("Mapped address spaces:\n\n"));
09c2f5d4
KB
1440 if (gdbarch_addr_bit (gdbarch) == 32)
1441 {
6cb06a8c
TT
1442 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1443 "Start Addr",
1444 " End Addr",
1445 " Size", " Offset", "objfile");
09c2f5d4
KB
1446 }
1447 else
1448 {
6cb06a8c
TT
1449 gdb_printf (" %18s %18s %10s %10s %s\n",
1450 "Start Addr",
1451 " End Addr",
1452 " Size", " Offset", "objfile");
09c2f5d4
KB
1453 }
1454 }
1455
d7a78e5c 1456 for (const target_section &tsp : m_core_file_mappings)
09c2f5d4 1457 {
bb2a6777
TT
1458 ULONGEST start = tsp.addr;
1459 ULONGEST end = tsp.endaddr;
1460 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1461 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
09c2f5d4
KB
1462
1463 if (gdbarch_addr_bit (gdbarch) == 32)
6cb06a8c
TT
1464 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1465 paddress (gdbarch, start),
1466 paddress (gdbarch, end),
1467 hex_string (end - start),
1468 hex_string (file_ofs),
1469 filename);
09c2f5d4 1470 else
6cb06a8c
TT
1471 gdb_printf (" %18s %18s %10s %10s %s\n",
1472 paddress (gdbarch, start),
1473 paddress (gdbarch, end),
1474 hex_string (end - start),
1475 hex_string (file_ofs),
1476 filename);
09c2f5d4
KB
1477 }
1478}
1479
1480/* Implement "maintenance print core-file-backed-mappings" command.
1481
1482 If mappings are loaded, the results should be similar to the
1483 mappings shown by "info proc mappings". This command is mainly a
1484 debugging tool for GDB developers to make sure that the expected
1485 mappings are present after loading a core file. For Linux, the
1486 output provided by this command will be very similar (if not
1487 identical) to that provided by "info proc mappings". This is not
1488 necessarily the case for other OSes which might provide
1489 more/different information in the "info proc mappings" output. */
1490
1491static void
1492maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
1493{
1494 core_target *targ = get_current_core_target ();
1495 if (targ != nullptr)
1496 targ->info_proc_mappings (targ->core_gdbarch ());
1497}
1498
6c265988 1499void _initialize_corelow ();
c906108c 1500void
6c265988 1501_initialize_corelow ()
c906108c 1502{
d9f719f1 1503 add_target (core_target_info, core_target_open, filename_completer);
09c2f5d4 1504 add_cmd ("core-file-backed-mappings", class_maintenance,
dda83cd7 1505 maintenance_print_core_file_backed_mappings,
513487e1 1506 _("Print core file's file-backed mappings."),
09c2f5d4 1507 &maintenanceprintlist);
c906108c 1508}