Provide access to non SEC_HAS_CONTENTS core file sections
[binutils-gdb.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2
3 Copyright (C) 1986-2020 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 "filenames.h"
41 #include "progspace.h"
42 #include "objfiles.h"
43 #include "gdb_bfd.h"
44 #include "completer.h"
45 #include "gdbsupport/filestuff.h"
46 #include "build-id.h"
47 #include "gdbsupport/pathstuff.h"
48
49 #ifndef O_LARGEFILE
50 #define O_LARGEFILE 0
51 #endif
52
53 /* The core file target. */
54
55 static const target_info core_target_info = {
56 "core",
57 N_("Local core dump file"),
58 N_("Use a core file as a target.\n\
59 Specify the filename of the core file.")
60 };
61
62 class core_target final : public process_stratum_target
63 {
64 public:
65 core_target ();
66 ~core_target () override;
67
68 const target_info &info () const override
69 { return core_target_info; }
70
71 void close () override;
72 void detach (inferior *, int) override;
73 void fetch_registers (struct regcache *, int) override;
74
75 enum target_xfer_status xfer_partial (enum target_object object,
76 const char *annex,
77 gdb_byte *readbuf,
78 const gdb_byte *writebuf,
79 ULONGEST offset, ULONGEST len,
80 ULONGEST *xfered_len) override;
81 void files_info () override;
82
83 bool thread_alive (ptid_t ptid) override;
84 const struct target_desc *read_description () override;
85
86 std::string pid_to_str (ptid_t) override;
87
88 const char *thread_name (struct thread_info *) override;
89
90 bool has_all_memory () override { return true; }
91 bool has_memory () override;
92 bool has_stack () override;
93 bool has_registers () override;
94 bool has_execution (inferior *inf) override { return false; }
95
96 bool info_proc (const char *, enum info_proc_what) override;
97
98 /* A few helpers. */
99
100 /* Getter, see variable definition. */
101 struct gdbarch *core_gdbarch ()
102 {
103 return m_core_gdbarch;
104 }
105
106 /* See definition. */
107 void get_core_register_section (struct regcache *regcache,
108 const struct regset *regset,
109 const char *name,
110 int section_min_size,
111 const char *human_name,
112 bool required);
113
114 private: /* per-core data */
115
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
121 targets. */
122 target_section_table m_core_section_table {};
123
124 /* FIXME: kettenis/20031023: Eventually this field should
125 disappear. */
126 struct gdbarch *m_core_gdbarch = NULL;
127 };
128
129 core_target::core_target ()
130 {
131 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
132
133 if (!m_core_gdbarch
134 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
135 error (_("\"%s\": Core file format not supported"),
136 bfd_get_filename (core_bfd));
137
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 ()));
144 }
145
146 core_target::~core_target ()
147 {
148 xfree (m_core_section_table.sections);
149 }
150
151 static void add_to_thread_list (bfd *, asection *, void *);
152
153 /* An arbitrary identifier for the core inferior. */
154 #define CORELOW_PID 1
155
156 /* Close the core target. */
157
158 void
159 core_target::close ()
160 {
161 if (core_bfd)
162 {
163 switch_to_no_thread (); /* Avoid confusion from thread
164 stuff. */
165 exit_inferior_silent (current_inferior ());
166
167 /* Clear out solib state while the bfd is still open. See
168 comments in clear_solib in solib.c. */
169 clear_solib ();
170
171 current_program_space->cbfd.reset (nullptr);
172 }
173
174 /* Core targets are heap-allocated (see core_target_open), so here
175 we delete ourselves. */
176 delete this;
177 }
178
179 /* Look for sections whose names start with `.reg/' so that we can
180 extract the list of threads in a core file. */
181
182 static void
183 add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
184 {
185 int core_tid;
186 int pid, lwpid;
187 asection *reg_sect = (asection *) reg_sect_arg;
188 bool fake_pid_p = false;
189 struct inferior *inf;
190
191 if (!startswith (bfd_section_name (asect), ".reg/"))
192 return;
193
194 core_tid = atoi (bfd_section_name (asect) + 5);
195
196 pid = bfd_core_file_pid (core_bfd);
197 if (pid == 0)
198 {
199 fake_pid_p = true;
200 pid = CORELOW_PID;
201 }
202
203 lwpid = core_tid;
204
205 inf = current_inferior ();
206 if (inf->pid == 0)
207 {
208 inferior_appeared (inf, pid);
209 inf->fake_pid_p = fake_pid_p;
210 }
211
212 ptid_t ptid (pid, lwpid);
213
214 thread_info *thr = add_thread (inf->process_target (), ptid);
215
216 /* Warning, Will Robinson, looking at BFD private data! */
217
218 if (reg_sect != NULL
219 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
220 switch_to_thread (thr); /* Yes, make it current. */
221 }
222
223 /* Issue a message saying we have no core to debug, if FROM_TTY. */
224
225 static void
226 maybe_say_no_core_file_now (int from_tty)
227 {
228 if (from_tty)
229 printf_filtered (_("No core file now.\n"));
230 }
231
232 /* Backward compatibility with old way of specifying core files. */
233
234 void
235 core_file_command (const char *filename, int from_tty)
236 {
237 dont_repeat (); /* Either way, seems bogus. */
238
239 if (filename == NULL)
240 {
241 if (core_bfd != NULL)
242 {
243 target_detach (current_inferior (), from_tty);
244 gdb_assert (core_bfd == NULL);
245 }
246 else
247 maybe_say_no_core_file_now (from_tty);
248 }
249 else
250 core_target_open (filename, from_tty);
251 }
252
253 /* Locate (and load) an executable file (and symbols) given the core file
254 BFD ABFD. */
255
256 static void
257 locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
258 {
259 const bfd_build_id *build_id = build_id_bfd_get (abfd);
260 if (build_id == nullptr)
261 return;
262
263 gdb_bfd_ref_ptr execbfd
264 = build_id_to_exec_bfd (build_id->size, build_id->data);
265
266 if (execbfd != nullptr)
267 {
268 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
269 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
270 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
271 }
272 }
273
274 /* See gdbcore.h. */
275
276 void
277 core_target_open (const char *arg, int from_tty)
278 {
279 const char *p;
280 int siggy;
281 int scratch_chan;
282 int flags;
283
284 target_preopen (from_tty);
285 if (!arg)
286 {
287 if (core_bfd)
288 error (_("No core file specified. (Use `detach' "
289 "to stop debugging a core file.)"));
290 else
291 error (_("No core file specified."));
292 }
293
294 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
295 if (!IS_ABSOLUTE_PATH (filename.get ()))
296 filename = gdb_abspath (filename.get ());
297
298 flags = O_BINARY | O_LARGEFILE;
299 if (write_files)
300 flags |= O_RDWR;
301 else
302 flags |= O_RDONLY;
303 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
304 if (scratch_chan < 0)
305 perror_with_name (filename.get ());
306
307 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
308 write_files ? FOPEN_RUB : FOPEN_RB,
309 scratch_chan));
310 if (temp_bfd == NULL)
311 perror_with_name (filename.get ());
312
313 if (!bfd_check_format (temp_bfd.get (), bfd_core))
314 {
315 /* Do it after the err msg */
316 /* FIXME: should be checking for errors from bfd_close (for one
317 thing, on error it does not free all the storage associated
318 with the bfd). */
319 error (_("\"%s\" is not a core dump: %s"),
320 filename.get (), bfd_errmsg (bfd_get_error ()));
321 }
322
323 current_program_space->cbfd = std::move (temp_bfd);
324
325 core_target *target = new core_target ();
326
327 /* Own the target until it is successfully pushed. */
328 target_ops_up target_holder (target);
329
330 validate_files ();
331
332 /* If we have no exec file, try to set the architecture from the
333 core file. We don't do this unconditionally since an exec file
334 typically contains more information that helps us determine the
335 architecture than a core file. */
336 if (!exec_bfd)
337 set_gdbarch_from_file (core_bfd);
338
339 push_target (std::move (target_holder));
340
341 switch_to_no_thread ();
342
343 /* Need to flush the register cache (and the frame cache) from a
344 previous debug session. If inferior_ptid ends up the same as the
345 last debug session --- e.g., b foo; run; gcore core1; step; gcore
346 core2; core core1; core core2 --- then there's potential for
347 get_current_regcache to return the cached regcache of the
348 previous session, and the frame cache being stale. */
349 registers_changed ();
350
351 /* Build up thread list from BFD sections, and possibly set the
352 current thread to the .reg/NN section matching the .reg
353 section. */
354 bfd_map_over_sections (core_bfd, add_to_thread_list,
355 bfd_get_section_by_name (core_bfd, ".reg"));
356
357 if (inferior_ptid == null_ptid)
358 {
359 /* Either we found no .reg/NN section, and hence we have a
360 non-threaded core (single-threaded, from gdb's perspective),
361 or for some reason add_to_thread_list couldn't determine
362 which was the "main" thread. The latter case shouldn't
363 usually happen, but we're dealing with input here, which can
364 always be broken in different ways. */
365 thread_info *thread = first_thread_of_inferior (current_inferior ());
366
367 if (thread == NULL)
368 {
369 inferior_appeared (current_inferior (), CORELOW_PID);
370 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
371 }
372
373 switch_to_thread (thread);
374 }
375
376 if (exec_bfd == nullptr)
377 locate_exec_from_corefile_build_id (core_bfd, from_tty);
378
379 post_create_inferior (target, from_tty);
380
381 /* Now go through the target stack looking for threads since there
382 may be a thread_stratum target loaded on top of target core by
383 now. The layer above should claim threads found in the BFD
384 sections. */
385 try
386 {
387 target_update_thread_list ();
388 }
389
390 catch (const gdb_exception_error &except)
391 {
392 exception_print (gdb_stderr, except);
393 }
394
395 p = bfd_core_file_failing_command (core_bfd);
396 if (p)
397 printf_filtered (_("Core was generated by `%s'.\n"), p);
398
399 /* Clearing any previous state of convenience variables. */
400 clear_exit_convenience_vars ();
401
402 siggy = bfd_core_file_failing_signal (core_bfd);
403 if (siggy > 0)
404 {
405 gdbarch *core_gdbarch = target->core_gdbarch ();
406
407 /* If we don't have a CORE_GDBARCH to work with, assume a native
408 core (map gdb_signal from host signals). If we do have
409 CORE_GDBARCH to work with, but no gdb_signal_from_target
410 implementation for that gdbarch, as a fallback measure,
411 assume the host signal mapping. It'll be correct for native
412 cores, but most likely incorrect for cross-cores. */
413 enum gdb_signal sig = (core_gdbarch != NULL
414 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
415 ? gdbarch_gdb_signal_from_target (core_gdbarch,
416 siggy)
417 : gdb_signal_from_host (siggy));
418
419 printf_filtered (_("Program terminated with signal %s, %s"),
420 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
421 if (gdbarch_report_signal_info_p (core_gdbarch))
422 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
423 printf_filtered (_(".\n"));
424
425 /* Set the value of the internal variable $_exitsignal,
426 which holds the signal uncaught by the inferior. */
427 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
428 siggy);
429 }
430
431 /* Fetch all registers from core file. */
432 target_fetch_registers (get_current_regcache (), -1);
433
434 /* Now, set up the frame cache, and print the top of stack. */
435 reinit_frame_cache ();
436 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
437
438 /* Current thread should be NUM 1 but the user does not know that.
439 If a program is single threaded gdb in general does not mention
440 anything about threads. That is why the test is >= 2. */
441 if (thread_count (target) >= 2)
442 {
443 try
444 {
445 thread_command (NULL, from_tty);
446 }
447 catch (const gdb_exception_error &except)
448 {
449 exception_print (gdb_stderr, except);
450 }
451 }
452 }
453
454 void
455 core_target::detach (inferior *inf, int from_tty)
456 {
457 /* Note that 'this' is dangling after this call. unpush_target
458 closes the target, and our close implementation deletes
459 'this'. */
460 unpush_target (this);
461
462 /* Clear the register cache and the frame cache. */
463 registers_changed ();
464 reinit_frame_cache ();
465 maybe_say_no_core_file_now (from_tty);
466 }
467
468 /* Try to retrieve registers from a section in core_bfd, and supply
469 them to REGSET.
470
471 If ptid's lwp member is zero, do the single-threaded
472 thing: look for a section named NAME. If ptid's lwp
473 member is non-zero, do the multi-threaded thing: look for a section
474 named "NAME/LWP", where LWP is the shortest ASCII decimal
475 representation of ptid's lwp member.
476
477 HUMAN_NAME is a human-readable name for the kind of registers the
478 NAME section contains, for use in error messages.
479
480 If REQUIRED is true, print an error if the core file doesn't have a
481 section by the appropriate name. Otherwise, just do nothing. */
482
483 void
484 core_target::get_core_register_section (struct regcache *regcache,
485 const struct regset *regset,
486 const char *name,
487 int section_min_size,
488 const char *human_name,
489 bool required)
490 {
491 gdb_assert (regset != nullptr);
492
493 struct bfd_section *section;
494 bfd_size_type size;
495 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
496
497 thread_section_name section_name (name, regcache->ptid ());
498
499 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
500 if (! section)
501 {
502 if (required)
503 warning (_("Couldn't find %s registers in core file."),
504 human_name);
505 return;
506 }
507
508 size = bfd_section_size (section);
509 if (size < section_min_size)
510 {
511 warning (_("Section `%s' in core file too small."),
512 section_name.c_str ());
513 return;
514 }
515 if (size != section_min_size && !variable_size_section)
516 {
517 warning (_("Unexpected size of section `%s' in core file."),
518 section_name.c_str ());
519 }
520
521 gdb::byte_vector contents (size);
522 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
523 (file_ptr) 0, size))
524 {
525 warning (_("Couldn't read %s registers from `%s' section in core file."),
526 human_name, section_name.c_str ());
527 return;
528 }
529
530 regset->supply_regset (regset, regcache, -1, contents.data (), size);
531 }
532
533 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
534 struct get_core_registers_cb_data
535 {
536 core_target *target;
537 struct regcache *regcache;
538 };
539
540 /* Callback for get_core_registers that handles a single core file
541 register note section. */
542
543 static void
544 get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
545 const struct regset *regset,
546 const char *human_name, void *cb_data)
547 {
548 gdb_assert (regset != nullptr);
549
550 auto *data = (get_core_registers_cb_data *) cb_data;
551 bool required = false;
552 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
553
554 if (!variable_size_section)
555 gdb_assert (supply_size == collect_size);
556
557 if (strcmp (sect_name, ".reg") == 0)
558 {
559 required = true;
560 if (human_name == NULL)
561 human_name = "general-purpose";
562 }
563 else if (strcmp (sect_name, ".reg2") == 0)
564 {
565 if (human_name == NULL)
566 human_name = "floating-point";
567 }
568
569 data->target->get_core_register_section (data->regcache, regset, sect_name,
570 supply_size, human_name, required);
571 }
572
573 /* Get the registers out of a core file. This is the machine-
574 independent part. Fetch_core_registers is the machine-dependent
575 part, typically implemented in the xm-file for each
576 architecture. */
577
578 /* We just get all the registers, so we don't use regno. */
579
580 void
581 core_target::fetch_registers (struct regcache *regcache, int regno)
582 {
583 if (!(m_core_gdbarch != nullptr
584 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
585 {
586 fprintf_filtered (gdb_stderr,
587 "Can't fetch registers from this type of core file\n");
588 return;
589 }
590
591 struct gdbarch *gdbarch = regcache->arch ();
592 get_core_registers_cb_data data = { this, regcache };
593 gdbarch_iterate_over_regset_sections (gdbarch,
594 get_core_registers_cb,
595 (void *) &data, NULL);
596
597 /* Mark all registers not found in the core as unavailable. */
598 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
599 if (regcache->get_register_status (i) == REG_UNKNOWN)
600 regcache->raw_supply (i, NULL);
601 }
602
603 void
604 core_target::files_info ()
605 {
606 print_section_info (&m_core_section_table, core_bfd);
607 }
608 \f
609 enum target_xfer_status
610 core_target::xfer_partial (enum target_object object, const char *annex,
611 gdb_byte *readbuf, const gdb_byte *writebuf,
612 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
613 {
614 switch (object)
615 {
616 case TARGET_OBJECT_MEMORY:
617 {
618 enum target_xfer_status xfer_status;
619
620 /* Try accessing memory contents from core file data,
621 restricting consideration to those sections for which
622 the BFD section flag SEC_HAS_CONTENTS is set. */
623 auto has_contents_cb = [] (const struct target_section *s)
624 {
625 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
626 };
627 xfer_status = section_table_xfer_memory_partial
628 (readbuf, writebuf,
629 offset, len, xfered_len,
630 m_core_section_table.sections,
631 m_core_section_table.sections_end,
632 has_contents_cb);
633 if (xfer_status == TARGET_XFER_OK)
634 return TARGET_XFER_OK;
635
636 /* Now check the stratum beneath us; this should be file_stratum. */
637 xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
638 writebuf, offset, len,
639 xfered_len);
640 if (xfer_status == TARGET_XFER_OK)
641 return TARGET_XFER_OK;
642
643 /* Finally, attempt to access data in core file sections with
644 no contents. These will typically read as all zero. */
645 auto no_contents_cb = [&] (const struct target_section *s)
646 {
647 return !has_contents_cb (s);
648 };
649 xfer_status = section_table_xfer_memory_partial
650 (readbuf, writebuf,
651 offset, len, xfered_len,
652 m_core_section_table.sections,
653 m_core_section_table.sections_end,
654 no_contents_cb);
655
656 return xfer_status;
657 }
658 case TARGET_OBJECT_AUXV:
659 if (readbuf)
660 {
661 /* When the aux vector is stored in core file, BFD
662 represents this with a fake section called ".auxv". */
663
664 struct bfd_section *section;
665 bfd_size_type size;
666
667 section = bfd_get_section_by_name (core_bfd, ".auxv");
668 if (section == NULL)
669 return TARGET_XFER_E_IO;
670
671 size = bfd_section_size (section);
672 if (offset >= size)
673 return TARGET_XFER_EOF;
674 size -= offset;
675 if (size > len)
676 size = len;
677
678 if (size == 0)
679 return TARGET_XFER_EOF;
680 if (!bfd_get_section_contents (core_bfd, section, readbuf,
681 (file_ptr) offset, size))
682 {
683 warning (_("Couldn't read NT_AUXV note in core file."));
684 return TARGET_XFER_E_IO;
685 }
686
687 *xfered_len = (ULONGEST) size;
688 return TARGET_XFER_OK;
689 }
690 return TARGET_XFER_E_IO;
691
692 case TARGET_OBJECT_WCOOKIE:
693 if (readbuf)
694 {
695 /* When the StackGhost cookie is stored in core file, BFD
696 represents this with a fake section called
697 ".wcookie". */
698
699 struct bfd_section *section;
700 bfd_size_type size;
701
702 section = bfd_get_section_by_name (core_bfd, ".wcookie");
703 if (section == NULL)
704 return TARGET_XFER_E_IO;
705
706 size = bfd_section_size (section);
707 if (offset >= size)
708 return TARGET_XFER_EOF;
709 size -= offset;
710 if (size > len)
711 size = len;
712
713 if (size == 0)
714 return TARGET_XFER_EOF;
715 if (!bfd_get_section_contents (core_bfd, section, readbuf,
716 (file_ptr) offset, size))
717 {
718 warning (_("Couldn't read StackGhost cookie in core file."));
719 return TARGET_XFER_E_IO;
720 }
721
722 *xfered_len = (ULONGEST) size;
723 return TARGET_XFER_OK;
724
725 }
726 return TARGET_XFER_E_IO;
727
728 case TARGET_OBJECT_LIBRARIES:
729 if (m_core_gdbarch != nullptr
730 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
731 {
732 if (writebuf)
733 return TARGET_XFER_E_IO;
734 else
735 {
736 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
737 readbuf,
738 offset, len);
739
740 if (*xfered_len == 0)
741 return TARGET_XFER_EOF;
742 else
743 return TARGET_XFER_OK;
744 }
745 }
746 /* FALL THROUGH */
747
748 case TARGET_OBJECT_LIBRARIES_AIX:
749 if (m_core_gdbarch != nullptr
750 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
751 {
752 if (writebuf)
753 return TARGET_XFER_E_IO;
754 else
755 {
756 *xfered_len
757 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
758 readbuf, offset,
759 len);
760
761 if (*xfered_len == 0)
762 return TARGET_XFER_EOF;
763 else
764 return TARGET_XFER_OK;
765 }
766 }
767 /* FALL THROUGH */
768
769 case TARGET_OBJECT_SIGNAL_INFO:
770 if (readbuf)
771 {
772 if (m_core_gdbarch != nullptr
773 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
774 {
775 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
776 offset, len);
777
778 if (l >= 0)
779 {
780 *xfered_len = l;
781 if (l == 0)
782 return TARGET_XFER_EOF;
783 else
784 return TARGET_XFER_OK;
785 }
786 }
787 }
788 return TARGET_XFER_E_IO;
789
790 default:
791 return this->beneath ()->xfer_partial (object, annex, readbuf,
792 writebuf, offset, len,
793 xfered_len);
794 }
795 }
796
797 \f
798
799 /* Okay, let's be honest: threads gleaned from a core file aren't
800 exactly lively, are they? On the other hand, if we don't claim
801 that each & every one is alive, then we don't get any of them
802 to appear in an "info thread" command, which is quite a useful
803 behaviour.
804 */
805 bool
806 core_target::thread_alive (ptid_t ptid)
807 {
808 return true;
809 }
810
811 /* Ask the current architecture what it knows about this core file.
812 That will be used, in turn, to pick a better architecture. This
813 wrapper could be avoided if targets got a chance to specialize
814 core_target. */
815
816 const struct target_desc *
817 core_target::read_description ()
818 {
819 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
820 {
821 const struct target_desc *result;
822
823 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
824 if (result != NULL)
825 return result;
826 }
827
828 return this->beneath ()->read_description ();
829 }
830
831 std::string
832 core_target::pid_to_str (ptid_t ptid)
833 {
834 struct inferior *inf;
835 int pid;
836
837 /* The preferred way is to have a gdbarch/OS specific
838 implementation. */
839 if (m_core_gdbarch != nullptr
840 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
841 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
842
843 /* Otherwise, if we don't have one, we'll just fallback to
844 "process", with normal_pid_to_str. */
845
846 /* Try the LWPID field first. */
847 pid = ptid.lwp ();
848 if (pid != 0)
849 return normal_pid_to_str (ptid_t (pid));
850
851 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
852 only if it isn't a fake PID. */
853 inf = find_inferior_ptid (this, ptid);
854 if (inf != NULL && !inf->fake_pid_p)
855 return normal_pid_to_str (ptid);
856
857 /* No luck. We simply don't have a valid PID to print. */
858 return "<main task>";
859 }
860
861 const char *
862 core_target::thread_name (struct thread_info *thr)
863 {
864 if (m_core_gdbarch != nullptr
865 && gdbarch_core_thread_name_p (m_core_gdbarch))
866 return gdbarch_core_thread_name (m_core_gdbarch, thr);
867 return NULL;
868 }
869
870 bool
871 core_target::has_memory ()
872 {
873 return (core_bfd != NULL);
874 }
875
876 bool
877 core_target::has_stack ()
878 {
879 return (core_bfd != NULL);
880 }
881
882 bool
883 core_target::has_registers ()
884 {
885 return (core_bfd != NULL);
886 }
887
888 /* Implement the to_info_proc method. */
889
890 bool
891 core_target::info_proc (const char *args, enum info_proc_what request)
892 {
893 struct gdbarch *gdbarch = get_current_arch ();
894
895 /* Since this is the core file target, call the 'core_info_proc'
896 method on gdbarch, not 'info_proc'. */
897 if (gdbarch_core_info_proc_p (gdbarch))
898 gdbarch_core_info_proc (gdbarch, args, request);
899
900 return true;
901 }
902
903 void _initialize_corelow ();
904 void
905 _initialize_corelow ()
906 {
907 add_target (core_target_info, core_target_open, filename_completer);
908 }