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