1 /* Core dump and executable file functions below target vector, for GDB.
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
24 #include "frame.h" /* required by inferior.h */
31 #include "process-stratum-target.h"
33 #include "gdbthread.h"
38 #include "readline/tilde.h"
40 #include "filenames.h"
41 #include "progspace.h"
44 #include "completer.h"
45 #include "gdbsupport/filestuff.h"
47 #include "gdbsupport/pathstuff.h"
53 /* The core file target. */
55 static const target_info core_target_info
= {
57 N_("Local core dump file"),
58 N_("Use a core file as a target.\n\
59 Specify the filename of the core file.")
62 class core_target final
: public process_stratum_target
66 ~core_target () override
;
68 const target_info
&info () const override
69 { return core_target_info
; }
71 void close () override
;
72 void detach (inferior
*, int) override
;
73 void fetch_registers (struct regcache
*, int) override
;
75 enum target_xfer_status
xfer_partial (enum target_object object
,
78 const gdb_byte
*writebuf
,
79 ULONGEST offset
, ULONGEST len
,
80 ULONGEST
*xfered_len
) override
;
81 void files_info () override
;
83 bool thread_alive (ptid_t ptid
) override
;
84 const struct target_desc
*read_description () override
;
86 std::string
pid_to_str (ptid_t
) override
;
88 const char *thread_name (struct thread_info
*) override
;
90 bool has_all_memory () override
{ return false; }
91 bool has_memory () override
;
92 bool has_stack () override
;
93 bool has_registers () override
;
94 bool has_execution (inferior
*inf
) override
{ return false; }
96 bool info_proc (const char *, enum info_proc_what
) override
;
100 /* Getter, see variable definition. */
101 struct gdbarch
*core_gdbarch ()
103 return m_core_gdbarch
;
106 /* See definition. */
107 void get_core_register_section (struct regcache
*regcache
,
108 const struct regset
*regset
,
110 int section_min_size
,
111 const char *human_name
,
114 private: /* per-core data */
116 /* The core's section table. Note that these target sections are
117 *not* mapped in the current address spaces' set of target
118 sections --- those should come only from pure executable or
119 shared library bfds. The core bfd sections are an implementation
120 detail of the core target, just like ptrace is for unix child
122 target_section_table m_core_section_table
{};
124 /* FIXME: kettenis/20031023: Eventually this field should
126 struct gdbarch
*m_core_gdbarch
= NULL
;
129 core_target::core_target ()
131 m_core_gdbarch
= gdbarch_from_bfd (core_bfd
);
134 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
))
135 error (_("\"%s\": Core file format not supported"),
136 bfd_get_filename (core_bfd
));
138 /* Find the data section */
139 if (build_section_table (core_bfd
,
140 &m_core_section_table
.sections
,
141 &m_core_section_table
.sections_end
))
142 error (_("\"%s\": Can't find sections: %s"),
143 bfd_get_filename (core_bfd
), bfd_errmsg (bfd_get_error ()));
146 core_target::~core_target ()
148 xfree (m_core_section_table
.sections
);
151 static void add_to_thread_list (bfd
*, asection
*, void *);
153 /* An arbitrary identifier for the core inferior. */
154 #define CORELOW_PID 1
156 /* Close the core target. */
159 core_target::close ()
163 inferior_ptid
= null_ptid
; /* Avoid confusion from thread
165 exit_inferior_silent (current_inferior ());
167 /* Clear out solib state while the bfd is still open. See
168 comments in clear_solib in solib.c. */
171 current_program_space
->cbfd
.reset (nullptr);
174 /* Core targets are heap-allocated (see core_target_open), so here
175 we delete ourselves. */
179 /* Look for sections whose names start with `.reg/' so that we can
180 extract the list of threads in a core file. */
183 add_to_thread_list (bfd
*abfd
, asection
*asect
, void *reg_sect_arg
)
188 asection
*reg_sect
= (asection
*) reg_sect_arg
;
189 bool fake_pid_p
= false;
190 struct inferior
*inf
;
192 if (!startswith (bfd_section_name (asect
), ".reg/"))
195 core_tid
= atoi (bfd_section_name (asect
) + 5);
197 pid
= bfd_core_file_pid (core_bfd
);
206 inf
= current_inferior ();
209 inferior_appeared (inf
, pid
);
210 inf
->fake_pid_p
= fake_pid_p
;
213 ptid
= ptid_t (pid
, lwpid
, 0);
215 add_thread (inf
->process_target (), ptid
);
217 /* Warning, Will Robinson, looking at BFD private data! */
220 && asect
->filepos
== reg_sect
->filepos
) /* Did we find .reg? */
221 inferior_ptid
= ptid
; /* Yes, make it current. */
224 /* Issue a message saying we have no core to debug, if FROM_TTY. */
227 maybe_say_no_core_file_now (int from_tty
)
230 printf_filtered (_("No core file now.\n"));
233 /* Backward compatibility with old way of specifying core files. */
236 core_file_command (const char *filename
, int from_tty
)
238 dont_repeat (); /* Either way, seems bogus. */
240 if (filename
== NULL
)
242 if (core_bfd
!= NULL
)
244 target_detach (current_inferior (), from_tty
);
245 gdb_assert (core_bfd
== NULL
);
248 maybe_say_no_core_file_now (from_tty
);
251 core_target_open (filename
, from_tty
);
254 /* Locate (and load) an executable file (and symbols) given the core file
258 locate_exec_from_corefile_build_id (bfd
*abfd
, int from_tty
)
260 const bfd_build_id
*build_id
= build_id_bfd_get (abfd
);
261 if (build_id
== nullptr)
264 gdb_bfd_ref_ptr execbfd
265 = build_id_to_exec_bfd (build_id
->size
, build_id
->data
);
267 if (execbfd
!= nullptr)
269 exec_file_attach (bfd_get_filename (execbfd
.get ()), from_tty
);
270 symbol_file_add_main (bfd_get_filename (execbfd
.get ()),
271 symfile_add_flag (from_tty
? SYMFILE_VERBOSE
: 0));
278 core_target_open (const char *arg
, int from_tty
)
285 target_preopen (from_tty
);
289 error (_("No core file specified. (Use `detach' "
290 "to stop debugging a core file.)"));
292 error (_("No core file specified."));
295 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
296 if (!IS_ABSOLUTE_PATH (filename
.get ()))
297 filename
= gdb_abspath (filename
.get ());
299 flags
= O_BINARY
| O_LARGEFILE
;
304 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0);
305 if (scratch_chan
< 0)
306 perror_with_name (filename
.get ());
308 gdb_bfd_ref_ptr
temp_bfd (gdb_bfd_fopen (filename
.get (), gnutarget
,
309 write_files
? FOPEN_RUB
: FOPEN_RB
,
311 if (temp_bfd
== NULL
)
312 perror_with_name (filename
.get ());
314 if (!bfd_check_format (temp_bfd
.get (), bfd_core
))
316 /* Do it after the err msg */
317 /* FIXME: should be checking for errors from bfd_close (for one
318 thing, on error it does not free all the storage associated
320 error (_("\"%s\" is not a core dump: %s"),
321 filename
.get (), bfd_errmsg (bfd_get_error ()));
324 current_program_space
->cbfd
= std::move (temp_bfd
);
326 core_target
*target
= new core_target ();
328 /* Own the target until it is successfully pushed. */
329 target_ops_up
target_holder (target
);
333 /* If we have no exec file, try to set the architecture from the
334 core file. We don't do this unconditionally since an exec file
335 typically contains more information that helps us determine the
336 architecture than a core file. */
338 set_gdbarch_from_file (core_bfd
);
340 push_target (std::move (target_holder
));
342 inferior_ptid
= null_ptid
;
344 /* Need to flush the register cache (and the frame cache) from a
345 previous debug session. If inferior_ptid ends up the same as the
346 last debug session --- e.g., b foo; run; gcore core1; step; gcore
347 core2; core core1; core core2 --- then there's potential for
348 get_current_regcache to return the cached regcache of the
349 previous session, and the frame cache being stale. */
350 registers_changed ();
352 /* Build up thread list from BFD sections, and possibly set the
353 current thread to the .reg/NN section matching the .reg
355 bfd_map_over_sections (core_bfd
, add_to_thread_list
,
356 bfd_get_section_by_name (core_bfd
, ".reg"));
358 if (inferior_ptid
== null_ptid
)
360 /* Either we found no .reg/NN section, and hence we have a
361 non-threaded core (single-threaded, from gdb's perspective),
362 or for some reason add_to_thread_list couldn't determine
363 which was the "main" thread. The latter case shouldn't
364 usually happen, but we're dealing with input here, which can
365 always be broken in different ways. */
366 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
370 inferior_appeared (current_inferior (), CORELOW_PID
);
371 inferior_ptid
= ptid_t (CORELOW_PID
);
372 add_thread_silent (target
, inferior_ptid
);
375 switch_to_thread (thread
);
378 if (exec_bfd
== nullptr)
379 locate_exec_from_corefile_build_id (core_bfd
, from_tty
);
381 post_create_inferior (target
, from_tty
);
383 /* Now go through the target stack looking for threads since there
384 may be a thread_stratum target loaded on top of target core by
385 now. The layer above should claim threads found in the BFD
389 target_update_thread_list ();
392 catch (const gdb_exception_error
&except
)
394 exception_print (gdb_stderr
, except
);
397 p
= bfd_core_file_failing_command (core_bfd
);
399 printf_filtered (_("Core was generated by `%s'.\n"), p
);
401 /* Clearing any previous state of convenience variables. */
402 clear_exit_convenience_vars ();
404 siggy
= bfd_core_file_failing_signal (core_bfd
);
407 gdbarch
*core_gdbarch
= target
->core_gdbarch ();
409 /* If we don't have a CORE_GDBARCH to work with, assume a native
410 core (map gdb_signal from host signals). If we do have
411 CORE_GDBARCH to work with, but no gdb_signal_from_target
412 implementation for that gdbarch, as a fallback measure,
413 assume the host signal mapping. It'll be correct for native
414 cores, but most likely incorrect for cross-cores. */
415 enum gdb_signal sig
= (core_gdbarch
!= NULL
416 && gdbarch_gdb_signal_from_target_p (core_gdbarch
)
417 ? gdbarch_gdb_signal_from_target (core_gdbarch
,
419 : gdb_signal_from_host (siggy
));
421 printf_filtered (_("Program terminated with signal %s, %s.\n"),
422 gdb_signal_to_name (sig
), gdb_signal_to_string (sig
));
424 /* Set the value of the internal variable $_exitsignal,
425 which holds the signal uncaught by the inferior. */
426 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
430 /* Fetch all registers from core file. */
431 target_fetch_registers (get_current_regcache (), -1);
433 /* Now, set up the frame cache, and print the top of stack. */
434 reinit_frame_cache ();
435 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
437 /* Current thread should be NUM 1 but the user does not know that.
438 If a program is single threaded gdb in general does not mention
439 anything about threads. That is why the test is >= 2. */
440 if (thread_count (target
) >= 2)
444 thread_command (NULL
, from_tty
);
446 catch (const gdb_exception_error
&except
)
448 exception_print (gdb_stderr
, except
);
454 core_target::detach (inferior
*inf
, int from_tty
)
456 /* Note that 'this' is dangling after this call. unpush_target
457 closes the target, and our close implementation deletes
459 unpush_target (this);
461 /* Clear the register cache and the frame cache. */
462 registers_changed ();
463 reinit_frame_cache ();
464 maybe_say_no_core_file_now (from_tty
);
467 /* Try to retrieve registers from a section in core_bfd, and supply
470 If ptid's lwp member is zero, do the single-threaded
471 thing: look for a section named NAME. If ptid's lwp
472 member is non-zero, do the multi-threaded thing: look for a section
473 named "NAME/LWP", where LWP is the shortest ASCII decimal
474 representation of ptid's lwp member.
476 HUMAN_NAME is a human-readable name for the kind of registers the
477 NAME section contains, for use in error messages.
479 If REQUIRED is true, print an error if the core file doesn't have a
480 section by the appropriate name. Otherwise, just do nothing. */
483 core_target::get_core_register_section (struct regcache
*regcache
,
484 const struct regset
*regset
,
486 int section_min_size
,
487 const char *human_name
,
490 gdb_assert (regset
!= nullptr);
492 struct bfd_section
*section
;
494 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
496 thread_section_name
section_name (name
, regcache
->ptid ());
498 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
502 warning (_("Couldn't find %s registers in core file."),
507 size
= bfd_section_size (section
);
508 if (size
< section_min_size
)
510 warning (_("Section `%s' in core file too small."),
511 section_name
.c_str ());
514 if (size
!= section_min_size
&& !variable_size_section
)
516 warning (_("Unexpected size of section `%s' in core file."),
517 section_name
.c_str ());
520 gdb::byte_vector
contents (size
);
521 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
524 warning (_("Couldn't read %s registers from `%s' section in core file."),
525 human_name
, section_name
.c_str ());
529 regset
->supply_regset (regset
, regcache
, -1, contents
.data (), size
);
532 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
533 struct get_core_registers_cb_data
536 struct regcache
*regcache
;
539 /* Callback for get_core_registers that handles a single core file
540 register note section. */
543 get_core_registers_cb (const char *sect_name
, int supply_size
, int collect_size
,
544 const struct regset
*regset
,
545 const char *human_name
, void *cb_data
)
547 gdb_assert (regset
!= nullptr);
549 auto *data
= (get_core_registers_cb_data
*) cb_data
;
550 bool required
= false;
551 bool variable_size_section
= (regset
->flags
& REGSET_VARIABLE_SIZE
);
553 if (!variable_size_section
)
554 gdb_assert (supply_size
== collect_size
);
556 if (strcmp (sect_name
, ".reg") == 0)
559 if (human_name
== NULL
)
560 human_name
= "general-purpose";
562 else if (strcmp (sect_name
, ".reg2") == 0)
564 if (human_name
== NULL
)
565 human_name
= "floating-point";
568 data
->target
->get_core_register_section (data
->regcache
, regset
, sect_name
,
569 supply_size
, human_name
, required
);
572 /* Get the registers out of a core file. This is the machine-
573 independent part. Fetch_core_registers is the machine-dependent
574 part, typically implemented in the xm-file for each
577 /* We just get all the registers, so we don't use regno. */
580 core_target::fetch_registers (struct regcache
*regcache
, int regno
)
582 if (!(m_core_gdbarch
!= nullptr
583 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch
)))
585 fprintf_filtered (gdb_stderr
,
586 "Can't fetch registers from this type of core file\n");
590 struct gdbarch
*gdbarch
= regcache
->arch ();
591 get_core_registers_cb_data data
= { this, regcache
};
592 gdbarch_iterate_over_regset_sections (gdbarch
,
593 get_core_registers_cb
,
594 (void *) &data
, NULL
);
596 /* Mark all registers not found in the core as unavailable. */
597 for (int i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
598 if (regcache
->get_register_status (i
) == REG_UNKNOWN
)
599 regcache
->raw_supply (i
, NULL
);
603 core_target::files_info ()
605 print_section_info (&m_core_section_table
, core_bfd
);
608 enum target_xfer_status
609 core_target::xfer_partial (enum target_object object
, const char *annex
,
610 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
611 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
615 case TARGET_OBJECT_MEMORY
:
616 return (section_table_xfer_memory_partial
618 offset
, len
, xfered_len
,
619 m_core_section_table
.sections
,
620 m_core_section_table
.sections_end
,
623 case TARGET_OBJECT_AUXV
:
626 /* When the aux vector is stored in core file, BFD
627 represents this with a fake section called ".auxv". */
629 struct bfd_section
*section
;
632 section
= bfd_get_section_by_name (core_bfd
, ".auxv");
634 return TARGET_XFER_E_IO
;
636 size
= bfd_section_size (section
);
638 return TARGET_XFER_EOF
;
644 return TARGET_XFER_EOF
;
645 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
646 (file_ptr
) offset
, size
))
648 warning (_("Couldn't read NT_AUXV note in core file."));
649 return TARGET_XFER_E_IO
;
652 *xfered_len
= (ULONGEST
) size
;
653 return TARGET_XFER_OK
;
655 return TARGET_XFER_E_IO
;
657 case TARGET_OBJECT_WCOOKIE
:
660 /* When the StackGhost cookie is stored in core file, BFD
661 represents this with a fake section called
664 struct bfd_section
*section
;
667 section
= bfd_get_section_by_name (core_bfd
, ".wcookie");
669 return TARGET_XFER_E_IO
;
671 size
= bfd_section_size (section
);
673 return TARGET_XFER_EOF
;
679 return TARGET_XFER_EOF
;
680 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
681 (file_ptr
) offset
, size
))
683 warning (_("Couldn't read StackGhost cookie in core file."));
684 return TARGET_XFER_E_IO
;
687 *xfered_len
= (ULONGEST
) size
;
688 return TARGET_XFER_OK
;
691 return TARGET_XFER_E_IO
;
693 case TARGET_OBJECT_LIBRARIES
:
694 if (m_core_gdbarch
!= nullptr
695 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch
))
698 return TARGET_XFER_E_IO
;
701 *xfered_len
= gdbarch_core_xfer_shared_libraries (m_core_gdbarch
,
705 if (*xfered_len
== 0)
706 return TARGET_XFER_EOF
;
708 return TARGET_XFER_OK
;
713 case TARGET_OBJECT_LIBRARIES_AIX
:
714 if (m_core_gdbarch
!= nullptr
715 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch
))
718 return TARGET_XFER_E_IO
;
722 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch
,
726 if (*xfered_len
== 0)
727 return TARGET_XFER_EOF
;
729 return TARGET_XFER_OK
;
734 case TARGET_OBJECT_SIGNAL_INFO
:
737 if (m_core_gdbarch
!= nullptr
738 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch
))
740 LONGEST l
= gdbarch_core_xfer_siginfo (m_core_gdbarch
, readbuf
,
747 return TARGET_XFER_EOF
;
749 return TARGET_XFER_OK
;
753 return TARGET_XFER_E_IO
;
756 return this->beneath ()->xfer_partial (object
, annex
, readbuf
,
757 writebuf
, offset
, len
,
764 /* Okay, let's be honest: threads gleaned from a core file aren't
765 exactly lively, are they? On the other hand, if we don't claim
766 that each & every one is alive, then we don't get any of them
767 to appear in an "info thread" command, which is quite a useful
771 core_target::thread_alive (ptid_t ptid
)
776 /* Ask the current architecture what it knows about this core file.
777 That will be used, in turn, to pick a better architecture. This
778 wrapper could be avoided if targets got a chance to specialize
781 const struct target_desc
*
782 core_target::read_description ()
784 if (m_core_gdbarch
&& gdbarch_core_read_description_p (m_core_gdbarch
))
786 const struct target_desc
*result
;
788 result
= gdbarch_core_read_description (m_core_gdbarch
, this, core_bfd
);
793 return this->beneath ()->read_description ();
797 core_target::pid_to_str (ptid_t ptid
)
799 struct inferior
*inf
;
802 /* The preferred way is to have a gdbarch/OS specific
804 if (m_core_gdbarch
!= nullptr
805 && gdbarch_core_pid_to_str_p (m_core_gdbarch
))
806 return gdbarch_core_pid_to_str (m_core_gdbarch
, ptid
);
808 /* Otherwise, if we don't have one, we'll just fallback to
809 "process", with normal_pid_to_str. */
811 /* Try the LWPID field first. */
814 return normal_pid_to_str (ptid_t (pid
));
816 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
817 only if it isn't a fake PID. */
818 inf
= find_inferior_ptid (this, ptid
);
819 if (inf
!= NULL
&& !inf
->fake_pid_p
)
820 return normal_pid_to_str (ptid
);
822 /* No luck. We simply don't have a valid PID to print. */
823 return "<main task>";
827 core_target::thread_name (struct thread_info
*thr
)
829 if (m_core_gdbarch
!= nullptr
830 && gdbarch_core_thread_name_p (m_core_gdbarch
))
831 return gdbarch_core_thread_name (m_core_gdbarch
, thr
);
836 core_target::has_memory ()
838 return (core_bfd
!= NULL
);
842 core_target::has_stack ()
844 return (core_bfd
!= NULL
);
848 core_target::has_registers ()
850 return (core_bfd
!= NULL
);
853 /* Implement the to_info_proc method. */
856 core_target::info_proc (const char *args
, enum info_proc_what request
)
858 struct gdbarch
*gdbarch
= get_current_arch ();
860 /* Since this is the core file target, call the 'core_info_proc'
861 method on gdbarch, not 'info_proc'. */
862 if (gdbarch_core_info_proc_p (gdbarch
))
863 gdbarch_core_info_proc (gdbarch
, args
, request
);
868 void _initialize_corelow ();
870 _initialize_corelow ()
872 add_target (core_target_info
, core_target_open
, filename_completer
);