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