corelow.c 36 KB

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