gdb/testsuite: restore configure script
[deliverable/binutils-gdb.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <signal.h>
23 #include <fcntl.h>
24 #include "frame.h" /* required by inferior.h */
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "symtab.h"
28 #include "command.h"
29 #include "bfd.h"
30 #include "target.h"
31 #include "process-stratum-target.h"
32 #include "gdbcore.h"
33 #include "gdbthread.h"
34 #include "regcache.h"
35 #include "regset.h"
36 #include "symfile.h"
37 #include "exec.h"
38 #include "readline/tilde.h"
39 #include "solib.h"
40 #include "solist.h"
41 #include "filenames.h"
42 #include "progspace.h"
43 #include "objfiles.h"
44 #include "gdb_bfd.h"
45 #include "completer.h"
46 #include "gdbsupport/filestuff.h"
47 #include "build-id.h"
48 #include "gdbsupport/pathstuff.h"
49 #include <unordered_map>
50 #include <unordered_set>
51 #include "gdbcmd.h"
52 #include "xml-tdesc.h"
53
54 #ifndef O_LARGEFILE
55 #define O_LARGEFILE 0
56 #endif
57
58 /* The core file target. */
59
60 static const target_info core_target_info = {
61 "core",
62 N_("Local core dump file"),
63 N_("Use a core file as a target.\n\
64 Specify the filename of the core file.")
65 };
66
67 class core_target final : public process_stratum_target
68 {
69 public:
70 core_target ();
71
72 const target_info &info () const override
73 { return core_target_info; }
74
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
87 bool thread_alive (ptid_t ptid) override;
88 const struct target_desc *read_description () override;
89
90 std::string pid_to_str (ptid_t) override;
91
92 const char *thread_name (struct thread_info *) override;
93
94 bool has_all_memory () override { return true; }
95 bool has_memory () override;
96 bool has_stack () override;
97 bool has_registers () override;
98 bool has_execution (inferior *inf) override { return false; }
99
100 bool info_proc (const char *, enum info_proc_what) override;
101
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,
114 int section_min_size,
115 const char *human_name,
116 bool required);
117
118 /* See definition. */
119 void info_proc_mappings (struct gdbarch *gdbarch);
120
121 private: /* per-core data */
122
123 /* The core's section table. Note that these target sections are
124 *not* mapped in the current address spaces' set of target
125 sections --- those should come only from pure executable or
126 shared library bfds. The core bfd sections are an implementation
127 detail of the core target, just like ptrace is for unix child
128 targets. */
129 target_section_table m_core_section_table;
130
131 /* File-backed address space mappings: some core files include
132 information about memory mapped files. */
133 target_section_table m_core_file_mappings;
134
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
140 /* Build m_core_file_mappings. Called from the constructor. */
141 void build_file_mappings ();
142
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
150 /* FIXME: kettenis/20031023: Eventually this field should
151 disappear. */
152 struct gdbarch *m_core_gdbarch = NULL;
153 };
154
155 core_target::core_target ()
156 {
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. */
159 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
160
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;
168 info.abfd = core_bfd;
169 info.target_desc = tdesc;
170 m_core_gdbarch = gdbarch_find_by_info (info);
171 }
172
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));
177
178 /* Find the data section */
179 m_core_section_table = build_section_table (core_bfd);
180
181 build_file_mappings ();
182 }
183
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
198 void
199 core_target::build_file_mappings ()
200 {
201 std::unordered_map<std::string, struct bfd *> bfd_map;
202 std::unordered_set<std::string> unavailable_paths;
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
209 will invoke this lambda. */
210 [&] (ULONGEST)
211 {
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,
217 const char *filename)
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 {
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);
240 return;
241 }
242
243 bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (),
244 "binary");
245
246 if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
247 {
248 m_core_unavailable_mappings.emplace_back (start, end - start);
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. */
284 m_core_file_mappings.emplace_back (start, end, sec);
285 });
286
287 normalize_mem_ranges (&m_core_unavailable_mappings);
288 }
289
290 /* An arbitrary identifier for the core inferior. */
291 #define CORELOW_PID 1
292
293 /* Close the core target. */
294
295 void
296 core_target::close ()
297 {
298 if (core_bfd)
299 {
300 switch_to_no_thread (); /* Avoid confusion from thread
301 stuff. */
302 exit_inferior_silent (current_inferior ());
303
304 /* Clear out solib state while the bfd is still open. See
305 comments in clear_solib in solib.c. */
306 clear_solib ();
307
308 current_program_space->cbfd.reset (nullptr);
309 }
310
311 /* Core targets are heap-allocated (see core_target_open), so here
312 we delete ourselves. */
313 delete this;
314 }
315
316 /* Look for sections whose names start with `.reg/' so that we can
317 extract the list of threads in a core file. */
318
319 static void
320 add_to_thread_list (asection *asect, asection *reg_sect)
321 {
322 int core_tid;
323 int pid, lwpid;
324 bool fake_pid_p = false;
325 struct inferior *inf;
326
327 if (!startswith (bfd_section_name (asect), ".reg/"))
328 return;
329
330 core_tid = atoi (bfd_section_name (asect) + 5);
331
332 pid = bfd_core_file_pid (core_bfd);
333 if (pid == 0)
334 {
335 fake_pid_p = true;
336 pid = CORELOW_PID;
337 }
338
339 lwpid = core_tid;
340
341 inf = current_inferior ();
342 if (inf->pid == 0)
343 {
344 inferior_appeared (inf, pid);
345 inf->fake_pid_p = fake_pid_p;
346 }
347
348 ptid_t ptid (pid, lwpid);
349
350 thread_info *thr = add_thread (inf->process_target (), ptid);
351
352 /* Warning, Will Robinson, looking at BFD private data! */
353
354 if (reg_sect != NULL
355 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
356 switch_to_thread (thr); /* Yes, make it current. */
357 }
358
359 /* Issue a message saying we have no core to debug, if FROM_TTY. */
360
361 static void
362 maybe_say_no_core_file_now (int from_tty)
363 {
364 if (from_tty)
365 printf_filtered (_("No core file now.\n"));
366 }
367
368 /* Backward compatibility with old way of specifying core files. */
369
370 void
371 core_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
389 /* Locate (and load) an executable file (and symbols) given the core file
390 BFD ABFD. */
391
392 static void
393 locate_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
410 /* See gdbcore.h. */
411
412 void
413 core_target_open (const char *arg, int from_tty)
414 {
415 const char *p;
416 int siggy;
417 int scratch_chan;
418 int flags;
419
420 target_preopen (from_tty);
421 if (!arg)
422 {
423 if (core_bfd)
424 error (_("No core file specified. (Use `detach' "
425 "to stop debugging a core file.)"));
426 else
427 error (_("No core file specified."));
428 }
429
430 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
431 if (!IS_ABSOLUTE_PATH (filename.get ()))
432 filename = gdb_abspath (filename.get ());
433
434 flags = O_BINARY | O_LARGEFILE;
435 if (write_files)
436 flags |= O_RDWR;
437 else
438 flags |= O_RDONLY;
439 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
440 if (scratch_chan < 0)
441 perror_with_name (filename.get ());
442
443 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
444 write_files ? FOPEN_RUB : FOPEN_RB,
445 scratch_chan));
446 if (temp_bfd == NULL)
447 perror_with_name (filename.get ());
448
449 if (!bfd_check_format (temp_bfd.get (), bfd_core))
450 {
451 /* Do it after the err msg */
452 /* FIXME: should be checking for errors from bfd_close (for one
453 thing, on error it does not free all the storage associated
454 with the bfd). */
455 error (_("\"%s\" is not a core dump: %s"),
456 filename.get (), bfd_errmsg (bfd_get_error ()));
457 }
458
459 current_program_space->cbfd = std::move (temp_bfd);
460
461 core_target *target = new core_target ();
462
463 /* Own the target until it is successfully pushed. */
464 target_ops_up target_holder (target);
465
466 validate_files ();
467
468 /* If we have no exec file, try to set the architecture from the
469 core file. We don't do this unconditionally since an exec file
470 typically contains more information that helps us determine the
471 architecture than a core file. */
472 if (!current_program_space->exec_bfd ())
473 set_gdbarch_from_file (core_bfd);
474
475 current_inferior ()->push_target (std::move (target_holder));
476
477 switch_to_no_thread ();
478
479 /* Need to flush the register cache (and the frame cache) from a
480 previous debug session. If inferior_ptid ends up the same as the
481 last debug session --- e.g., b foo; run; gcore core1; step; gcore
482 core2; core core1; core core2 --- then there's potential for
483 get_current_regcache to return the cached regcache of the
484 previous session, and the frame cache being stale. */
485 registers_changed ();
486
487 /* Build up thread list from BFD sections, and possibly set the
488 current thread to the .reg/NN section matching the .reg
489 section. */
490 asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
491 for (asection *sect : gdb_bfd_sections (core_bfd))
492 add_to_thread_list (sect, reg_sect);
493
494 if (inferior_ptid == null_ptid)
495 {
496 /* Either we found no .reg/NN section, and hence we have a
497 non-threaded core (single-threaded, from gdb's perspective),
498 or for some reason add_to_thread_list couldn't determine
499 which was the "main" thread. The latter case shouldn't
500 usually happen, but we're dealing with input here, which can
501 always be broken in different ways. */
502 thread_info *thread = first_thread_of_inferior (current_inferior ());
503
504 if (thread == NULL)
505 {
506 inferior_appeared (current_inferior (), CORELOW_PID);
507 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
508 }
509
510 switch_to_thread (thread);
511 }
512
513 if (current_program_space->exec_bfd () == nullptr)
514 locate_exec_from_corefile_build_id (core_bfd, from_tty);
515
516 post_create_inferior (from_tty);
517
518 /* Now go through the target stack looking for threads since there
519 may be a thread_stratum target loaded on top of target core by
520 now. The layer above should claim threads found in the BFD
521 sections. */
522 try
523 {
524 target_update_thread_list ();
525 }
526
527 catch (const gdb_exception_error &except)
528 {
529 exception_print (gdb_stderr, except);
530 }
531
532 p = bfd_core_file_failing_command (core_bfd);
533 if (p)
534 printf_filtered (_("Core was generated by `%s'.\n"), p);
535
536 /* Clearing any previous state of convenience variables. */
537 clear_exit_convenience_vars ();
538
539 siggy = bfd_core_file_failing_signal (core_bfd);
540 if (siggy > 0)
541 {
542 gdbarch *core_gdbarch = target->core_gdbarch ();
543
544 /* If we don't have a CORE_GDBARCH to work with, assume a native
545 core (map gdb_signal from host signals). If we do have
546 CORE_GDBARCH to work with, but no gdb_signal_from_target
547 implementation for that gdbarch, as a fallback measure,
548 assume the host signal mapping. It'll be correct for native
549 cores, but most likely incorrect for cross-cores. */
550 enum gdb_signal sig = (core_gdbarch != NULL
551 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
552 ? gdbarch_gdb_signal_from_target (core_gdbarch,
553 siggy)
554 : gdb_signal_from_host (siggy));
555
556 printf_filtered (_("Program terminated with signal %s, %s"),
557 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
558 if (gdbarch_report_signal_info_p (core_gdbarch))
559 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
560 printf_filtered (_(".\n"));
561
562 /* Set the value of the internal variable $_exitsignal,
563 which holds the signal uncaught by the inferior. */
564 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
565 siggy);
566 }
567
568 /* Fetch all registers from core file. */
569 target_fetch_registers (get_current_regcache (), -1);
570
571 /* Now, set up the frame cache, and print the top of stack. */
572 reinit_frame_cache ();
573 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
574
575 /* Current thread should be NUM 1 but the user does not know that.
576 If a program is single threaded gdb in general does not mention
577 anything about threads. That is why the test is >= 2. */
578 if (thread_count (target) >= 2)
579 {
580 try
581 {
582 thread_command (NULL, from_tty);
583 }
584 catch (const gdb_exception_error &except)
585 {
586 exception_print (gdb_stderr, except);
587 }
588 }
589 }
590
591 void
592 core_target::detach (inferior *inf, int from_tty)
593 {
594 /* Note that 'this' is dangling after this call. unpush_target
595 closes the target, and our close implementation deletes
596 'this'. */
597 inf->unpush_target (this);
598
599 /* Clear the register cache and the frame cache. */
600 registers_changed ();
601 reinit_frame_cache ();
602 maybe_say_no_core_file_now (from_tty);
603 }
604
605 /* Try to retrieve registers from a section in core_bfd, and supply
606 them to REGSET.
607
608 If ptid's lwp member is zero, do the single-threaded
609 thing: look for a section named NAME. If ptid's lwp
610 member is non-zero, do the multi-threaded thing: look for a section
611 named "NAME/LWP", where LWP is the shortest ASCII decimal
612 representation of ptid's lwp member.
613
614 HUMAN_NAME is a human-readable name for the kind of registers the
615 NAME section contains, for use in error messages.
616
617 If REQUIRED is true, print an error if the core file doesn't have a
618 section by the appropriate name. Otherwise, just do nothing. */
619
620 void
621 core_target::get_core_register_section (struct regcache *regcache,
622 const struct regset *regset,
623 const char *name,
624 int section_min_size,
625 const char *human_name,
626 bool required)
627 {
628 gdb_assert (regset != nullptr);
629
630 struct bfd_section *section;
631 bfd_size_type size;
632 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
633
634 thread_section_name section_name (name, regcache->ptid ());
635
636 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
637 if (! section)
638 {
639 if (required)
640 warning (_("Couldn't find %s registers in core file."),
641 human_name);
642 return;
643 }
644
645 size = bfd_section_size (section);
646 if (size < section_min_size)
647 {
648 warning (_("Section `%s' in core file too small."),
649 section_name.c_str ());
650 return;
651 }
652 if (size != section_min_size && !variable_size_section)
653 {
654 warning (_("Unexpected size of section `%s' in core file."),
655 section_name.c_str ());
656 }
657
658 gdb::byte_vector contents (size);
659 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
660 (file_ptr) 0, size))
661 {
662 warning (_("Couldn't read %s registers from `%s' section in core file."),
663 human_name, section_name.c_str ());
664 return;
665 }
666
667 regset->supply_regset (regset, regcache, -1, contents.data (), size);
668 }
669
670 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
671 struct get_core_registers_cb_data
672 {
673 core_target *target;
674 struct regcache *regcache;
675 };
676
677 /* Callback for get_core_registers that handles a single core file
678 register note section. */
679
680 static void
681 get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
682 const struct regset *regset,
683 const char *human_name, void *cb_data)
684 {
685 gdb_assert (regset != nullptr);
686
687 auto *data = (get_core_registers_cb_data *) cb_data;
688 bool required = false;
689 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
690
691 if (!variable_size_section)
692 gdb_assert (supply_size == collect_size);
693
694 if (strcmp (sect_name, ".reg") == 0)
695 {
696 required = true;
697 if (human_name == NULL)
698 human_name = "general-purpose";
699 }
700 else if (strcmp (sect_name, ".reg2") == 0)
701 {
702 if (human_name == NULL)
703 human_name = "floating-point";
704 }
705
706 data->target->get_core_register_section (data->regcache, regset, sect_name,
707 supply_size, human_name, required);
708 }
709
710 /* Get the registers out of a core file. This is the machine-
711 independent part. Fetch_core_registers is the machine-dependent
712 part, typically implemented in the xm-file for each
713 architecture. */
714
715 /* We just get all the registers, so we don't use regno. */
716
717 void
718 core_target::fetch_registers (struct regcache *regcache, int regno)
719 {
720 if (!(m_core_gdbarch != nullptr
721 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
722 {
723 fprintf_filtered (gdb_stderr,
724 "Can't fetch registers from this type of core file\n");
725 return;
726 }
727
728 struct gdbarch *gdbarch = regcache->arch ();
729 get_core_registers_cb_data data = { this, regcache };
730 gdbarch_iterate_over_regset_sections (gdbarch,
731 get_core_registers_cb,
732 (void *) &data, NULL);
733
734 /* Mark all registers not found in the core as unavailable. */
735 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
736 if (regcache->get_register_status (i) == REG_UNKNOWN)
737 regcache->raw_supply (i, NULL);
738 }
739
740 void
741 core_target::files_info ()
742 {
743 print_section_info (&m_core_section_table, core_bfd);
744 }
745 \f
746 /* Helper method for core_target::xfer_partial. */
747
748 enum target_xfer_status
749 core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
750 const gdb_byte *writebuf,
751 ULONGEST offset, ULONGEST len,
752 ULONGEST *xfered_len)
753 {
754 enum target_xfer_status xfer_status;
755
756 xfer_status = (section_table_xfer_memory_partial
757 (readbuf, writebuf,
758 offset, len, xfered_len,
759 m_core_file_mappings));
760
761 if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
762 return xfer_status;
763
764 /* There are instances - e.g. when debugging within a docker
765 container using the AUFS storage driver - where the pathnames
766 obtained from the note section are incorrect. Despite the path
767 being wrong, just knowing the start and end addresses of the
768 mappings is still useful; we can attempt an access of the file
769 stratum constrained to the address ranges corresponding to the
770 unavailable mappings. */
771
772 ULONGEST memaddr = offset;
773 ULONGEST memend = offset + len;
774
775 for (const auto &mr : m_core_unavailable_mappings)
776 {
777 if (address_in_mem_range (memaddr, &mr))
778 {
779 if (!address_in_mem_range (memend, &mr))
780 len = mr.start + mr.length - memaddr;
781
782 xfer_status = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY,
783 NULL,
784 readbuf,
785 writebuf,
786 offset,
787 len,
788 xfered_len);
789 break;
790 }
791 }
792
793 return xfer_status;
794 }
795
796 enum target_xfer_status
797 core_target::xfer_partial (enum target_object object, const char *annex,
798 gdb_byte *readbuf, const gdb_byte *writebuf,
799 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
800 {
801 switch (object)
802 {
803 case TARGET_OBJECT_MEMORY:
804 {
805 enum target_xfer_status xfer_status;
806
807 /* Try accessing memory contents from core file data,
808 restricting consideration to those sections for which
809 the BFD section flag SEC_HAS_CONTENTS is set. */
810 auto has_contents_cb = [] (const struct target_section *s)
811 {
812 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
813 };
814 xfer_status = section_table_xfer_memory_partial
815 (readbuf, writebuf,
816 offset, len, xfered_len,
817 m_core_section_table,
818 has_contents_cb);
819 if (xfer_status == TARGET_XFER_OK)
820 return TARGET_XFER_OK;
821
822 /* Check file backed mappings. If they're available, use
823 core file provided mappings (e.g. from .note.linuxcore.file
824 or the like) as this should provide a more accurate
825 result. If not, check the stratum beneath us, which should
826 be the file stratum.
827
828 We also check unavailable mappings due to Docker/AUFS driver
829 issues. */
830 if (!m_core_file_mappings.empty ()
831 || !m_core_unavailable_mappings.empty ())
832 {
833 xfer_status = xfer_memory_via_mappings (readbuf, writebuf, offset,
834 len, xfered_len);
835 }
836 else
837 xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
838 writebuf, offset, len,
839 xfered_len);
840 if (xfer_status == TARGET_XFER_OK)
841 return TARGET_XFER_OK;
842
843 /* Finally, attempt to access data in core file sections with
844 no contents. These will typically read as all zero. */
845 auto no_contents_cb = [&] (const struct target_section *s)
846 {
847 return !has_contents_cb (s);
848 };
849 xfer_status = section_table_xfer_memory_partial
850 (readbuf, writebuf,
851 offset, len, xfered_len,
852 m_core_section_table,
853 no_contents_cb);
854
855 return xfer_status;
856 }
857 case TARGET_OBJECT_AUXV:
858 if (readbuf)
859 {
860 /* When the aux vector is stored in core file, BFD
861 represents this with a fake section called ".auxv". */
862
863 struct bfd_section *section;
864 bfd_size_type size;
865
866 section = bfd_get_section_by_name (core_bfd, ".auxv");
867 if (section == NULL)
868 return TARGET_XFER_E_IO;
869
870 size = bfd_section_size (section);
871 if (offset >= size)
872 return TARGET_XFER_EOF;
873 size -= offset;
874 if (size > len)
875 size = len;
876
877 if (size == 0)
878 return TARGET_XFER_EOF;
879 if (!bfd_get_section_contents (core_bfd, section, readbuf,
880 (file_ptr) offset, size))
881 {
882 warning (_("Couldn't read NT_AUXV note in core file."));
883 return TARGET_XFER_E_IO;
884 }
885
886 *xfered_len = (ULONGEST) size;
887 return TARGET_XFER_OK;
888 }
889 return TARGET_XFER_E_IO;
890
891 case TARGET_OBJECT_WCOOKIE:
892 if (readbuf)
893 {
894 /* When the StackGhost cookie is stored in core file, BFD
895 represents this with a fake section called
896 ".wcookie". */
897
898 struct bfd_section *section;
899 bfd_size_type size;
900
901 section = bfd_get_section_by_name (core_bfd, ".wcookie");
902 if (section == NULL)
903 return TARGET_XFER_E_IO;
904
905 size = bfd_section_size (section);
906 if (offset >= size)
907 return TARGET_XFER_EOF;
908 size -= offset;
909 if (size > len)
910 size = len;
911
912 if (size == 0)
913 return TARGET_XFER_EOF;
914 if (!bfd_get_section_contents (core_bfd, section, readbuf,
915 (file_ptr) offset, size))
916 {
917 warning (_("Couldn't read StackGhost cookie in core file."));
918 return TARGET_XFER_E_IO;
919 }
920
921 *xfered_len = (ULONGEST) size;
922 return TARGET_XFER_OK;
923
924 }
925 return TARGET_XFER_E_IO;
926
927 case TARGET_OBJECT_LIBRARIES:
928 if (m_core_gdbarch != nullptr
929 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
930 {
931 if (writebuf)
932 return TARGET_XFER_E_IO;
933 else
934 {
935 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
936 readbuf,
937 offset, len);
938
939 if (*xfered_len == 0)
940 return TARGET_XFER_EOF;
941 else
942 return TARGET_XFER_OK;
943 }
944 }
945 /* FALL THROUGH */
946
947 case TARGET_OBJECT_LIBRARIES_AIX:
948 if (m_core_gdbarch != nullptr
949 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
950 {
951 if (writebuf)
952 return TARGET_XFER_E_IO;
953 else
954 {
955 *xfered_len
956 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
957 readbuf, offset,
958 len);
959
960 if (*xfered_len == 0)
961 return TARGET_XFER_EOF;
962 else
963 return TARGET_XFER_OK;
964 }
965 }
966 /* FALL THROUGH */
967
968 case TARGET_OBJECT_SIGNAL_INFO:
969 if (readbuf)
970 {
971 if (m_core_gdbarch != nullptr
972 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
973 {
974 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
975 offset, len);
976
977 if (l >= 0)
978 {
979 *xfered_len = l;
980 if (l == 0)
981 return TARGET_XFER_EOF;
982 else
983 return TARGET_XFER_OK;
984 }
985 }
986 }
987 return TARGET_XFER_E_IO;
988
989 default:
990 return this->beneath ()->xfer_partial (object, annex, readbuf,
991 writebuf, offset, len,
992 xfered_len);
993 }
994 }
995
996 \f
997
998 /* Okay, let's be honest: threads gleaned from a core file aren't
999 exactly lively, are they? On the other hand, if we don't claim
1000 that each & every one is alive, then we don't get any of them
1001 to appear in an "info thread" command, which is quite a useful
1002 behaviour.
1003 */
1004 bool
1005 core_target::thread_alive (ptid_t ptid)
1006 {
1007 return true;
1008 }
1009
1010 /* Ask the current architecture what it knows about this core file.
1011 That will be used, in turn, to pick a better architecture. This
1012 wrapper could be avoided if targets got a chance to specialize
1013 core_target. */
1014
1015 const struct target_desc *
1016 core_target::read_description ()
1017 {
1018 /* If the core file contains a target description note then we will use
1019 that in preference to anything else. */
1020 bfd_size_type tdesc_note_size = 0;
1021 struct bfd_section *tdesc_note_section
1022 = bfd_get_section_by_name (core_bfd, ".gdb-tdesc");
1023 if (tdesc_note_section != nullptr)
1024 tdesc_note_size = bfd_section_size (tdesc_note_section);
1025 if (tdesc_note_size > 0)
1026 {
1027 gdb::char_vector contents (tdesc_note_size + 1);
1028 if (bfd_get_section_contents (core_bfd, tdesc_note_section,
1029 contents.data (), (file_ptr) 0,
1030 tdesc_note_size))
1031 {
1032 /* Ensure we have a null terminator. */
1033 contents[tdesc_note_size] = '\0';
1034 const struct target_desc *result
1035 = string_read_description_xml (contents.data ());
1036 if (result != nullptr)
1037 return result;
1038 }
1039 }
1040
1041 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
1042 {
1043 const struct target_desc *result;
1044
1045 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
1046 if (result != NULL)
1047 return result;
1048 }
1049
1050 return this->beneath ()->read_description ();
1051 }
1052
1053 std::string
1054 core_target::pid_to_str (ptid_t ptid)
1055 {
1056 struct inferior *inf;
1057 int pid;
1058
1059 /* The preferred way is to have a gdbarch/OS specific
1060 implementation. */
1061 if (m_core_gdbarch != nullptr
1062 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1063 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
1064
1065 /* Otherwise, if we don't have one, we'll just fallback to
1066 "process", with normal_pid_to_str. */
1067
1068 /* Try the LWPID field first. */
1069 pid = ptid.lwp ();
1070 if (pid != 0)
1071 return normal_pid_to_str (ptid_t (pid));
1072
1073 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1074 only if it isn't a fake PID. */
1075 inf = find_inferior_ptid (this, ptid);
1076 if (inf != NULL && !inf->fake_pid_p)
1077 return normal_pid_to_str (ptid);
1078
1079 /* No luck. We simply don't have a valid PID to print. */
1080 return "<main task>";
1081 }
1082
1083 const char *
1084 core_target::thread_name (struct thread_info *thr)
1085 {
1086 if (m_core_gdbarch != nullptr
1087 && gdbarch_core_thread_name_p (m_core_gdbarch))
1088 return gdbarch_core_thread_name (m_core_gdbarch, thr);
1089 return NULL;
1090 }
1091
1092 bool
1093 core_target::has_memory ()
1094 {
1095 return (core_bfd != NULL);
1096 }
1097
1098 bool
1099 core_target::has_stack ()
1100 {
1101 return (core_bfd != NULL);
1102 }
1103
1104 bool
1105 core_target::has_registers ()
1106 {
1107 return (core_bfd != NULL);
1108 }
1109
1110 /* Implement the to_info_proc method. */
1111
1112 bool
1113 core_target::info_proc (const char *args, enum info_proc_what request)
1114 {
1115 struct gdbarch *gdbarch = get_current_arch ();
1116
1117 /* Since this is the core file target, call the 'core_info_proc'
1118 method on gdbarch, not 'info_proc'. */
1119 if (gdbarch_core_info_proc_p (gdbarch))
1120 gdbarch_core_info_proc (gdbarch, args, request);
1121
1122 return true;
1123 }
1124
1125 /* Get a pointer to the current core target. If not connected to a
1126 core target, return NULL. */
1127
1128 static core_target *
1129 get_current_core_target ()
1130 {
1131 target_ops *proc_target = current_inferior ()->process_target ();
1132 return dynamic_cast<core_target *> (proc_target);
1133 }
1134
1135 /* Display file backed mappings from core file. */
1136
1137 void
1138 core_target::info_proc_mappings (struct gdbarch *gdbarch)
1139 {
1140 if (!m_core_file_mappings.empty ())
1141 {
1142 printf_filtered (_("Mapped address spaces:\n\n"));
1143 if (gdbarch_addr_bit (gdbarch) == 32)
1144 {
1145 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1146 "Start Addr",
1147 " End Addr",
1148 " Size", " Offset", "objfile");
1149 }
1150 else
1151 {
1152 printf_filtered (" %18s %18s %10s %10s %s\n",
1153 "Start Addr",
1154 " End Addr",
1155 " Size", " Offset", "objfile");
1156 }
1157 }
1158
1159 for (const target_section &tsp : m_core_file_mappings)
1160 {
1161 ULONGEST start = tsp.addr;
1162 ULONGEST end = tsp.endaddr;
1163 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1164 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
1165
1166 if (gdbarch_addr_bit (gdbarch) == 32)
1167 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1168 paddress (gdbarch, start),
1169 paddress (gdbarch, end),
1170 hex_string (end - start),
1171 hex_string (file_ofs),
1172 filename);
1173 else
1174 printf_filtered (" %18s %18s %10s %10s %s\n",
1175 paddress (gdbarch, start),
1176 paddress (gdbarch, end),
1177 hex_string (end - start),
1178 hex_string (file_ofs),
1179 filename);
1180 }
1181 }
1182
1183 /* Implement "maintenance print core-file-backed-mappings" command.
1184
1185 If mappings are loaded, the results should be similar to the
1186 mappings shown by "info proc mappings". This command is mainly a
1187 debugging tool for GDB developers to make sure that the expected
1188 mappings are present after loading a core file. For Linux, the
1189 output provided by this command will be very similar (if not
1190 identical) to that provided by "info proc mappings". This is not
1191 necessarily the case for other OSes which might provide
1192 more/different information in the "info proc mappings" output. */
1193
1194 static void
1195 maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
1196 {
1197 core_target *targ = get_current_core_target ();
1198 if (targ != nullptr)
1199 targ->info_proc_mappings (targ->core_gdbarch ());
1200 }
1201
1202 void _initialize_corelow ();
1203 void
1204 _initialize_corelow ()
1205 {
1206 add_target (core_target_info, core_target_open, filename_completer);
1207 add_cmd ("core-file-backed-mappings", class_maintenance,
1208 maintenance_print_core_file_backed_mappings,
1209 _("Print core file's file-backed mappings."),
1210 &maintenanceprintlist);
1211 }
This page took 0.086336 seconds and 4 git commands to generate.