rotate gdb/ChangeLog
[deliverable/binutils-gdb.git] / gdb / corelow.c
CommitLineData
c906108c 1/* Core dump and executable file functions below target vector, for GDB.
4646aa9d 2
e2882c85 3 Copyright (C) 1986-2018 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"
0e24ac5d 21#include "arch-utils.h"
c906108c
SS
22#include <signal.h>
23#include <fcntl.h>
fc24370e
MS
24#ifdef HAVE_SYS_FILE_H
25#include <sys/file.h> /* needed for F_OK and friends */
26#endif
c5aa993b 27#include "frame.h" /* required by inferior.h */
c906108c 28#include "inferior.h"
45741a9c 29#include "infrun.h"
c906108c
SS
30#include "symtab.h"
31#include "command.h"
32#include "bfd.h"
33#include "target.h"
3b3dac9b 34#include "process-stratum-target.h"
c906108c
SS
35#include "gdbcore.h"
36#include "gdbthread.h"
4e052eda 37#include "regcache.h"
0e24ac5d 38#include "regset.h"
990f9fe3 39#include "symfile.h"
4646aa9d 40#include "exec.h"
dbda9972 41#include "readline/readline.h"
a77053c2 42#include "solib.h"
f90c07ac 43#include "filenames.h"
6c95b8df 44#include "progspace.h"
516ba659 45#include "objfiles.h"
cbb099e8 46#include "gdb_bfd.h"
9852c492 47#include "completer.h"
614c279d 48#include "filestuff.h"
8e860359 49
ee28ca0f
AC
50#ifndef O_LARGEFILE
51#define O_LARGEFILE 0
52#endif
53
15244507
PA
54static core_fns *sniff_core_bfd (gdbarch *core_gdbarch,
55 bfd *abfd);
56
f6ac5f3d
PA
57/* The core file target. */
58
d9f719f1
PA
59static const target_info core_target_info = {
60 "core",
61 N_("Local core dump file"),
62 N_("Use a core file as a target. Specify the filename of the core file.")
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
89 const char *pid_to_str (ptid_t) override;
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;
f3d11a9a
PA
97 bool has_execution (ptid_t) override { return false; }
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;
88f38a04
PA
291 int fake_pid_p = 0;
292 struct inferior *inf;
c906108c 293
61012eef 294 if (!startswith (bfd_section_name (abfd, asect), ".reg/"))
c906108c
SS
295 return;
296
3cdd9356 297 core_tid = atoi (bfd_section_name (abfd, asect) + 5);
c906108c 298
261b8d08
PA
299 pid = bfd_core_file_pid (core_bfd);
300 if (pid == 0)
3cdd9356 301 {
88f38a04 302 fake_pid_p = 1;
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
PA
316
317 add_thread (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
335/* Backward compatability with old way of specifying core files. */
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
d9f719f1 356/* See gdbcore.h. */
c906108c 357
f6ac5f3d 358void
d9f719f1 359core_target_open (const char *arg, int from_tty)
c906108c
SS
360{
361 const char *p;
362 int siggy;
c906108c 363 int scratch_chan;
ee28ca0f 364 int flags;
c906108c
SS
365
366 target_preopen (from_tty);
014f9477 367 if (!arg)
c906108c 368 {
8a3fe4f8 369 if (core_bfd)
3e43a32a
MS
370 error (_("No core file specified. (Use `detach' "
371 "to stop debugging a core file.)"));
8a3fe4f8
AC
372 else
373 error (_("No core file specified."));
c906108c
SS
374 }
375
ee0c3293
TT
376 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
377 if (!IS_ABSOLUTE_PATH (filename.get ()))
378 filename.reset (concat (current_directory, "/",
379 filename.get (), (char *) NULL));
c906108c 380
ee28ca0f
AC
381 flags = O_BINARY | O_LARGEFILE;
382 if (write_files)
383 flags |= O_RDWR;
384 else
385 flags |= O_RDONLY;
ee0c3293 386 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
c906108c 387 if (scratch_chan < 0)
ee0c3293 388 perror_with_name (filename.get ());
c906108c 389
ee0c3293 390 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
192b62ce
TT
391 write_files ? FOPEN_RUB : FOPEN_RB,
392 scratch_chan));
c906108c 393 if (temp_bfd == NULL)
ee0c3293 394 perror_with_name (filename.get ());
c906108c 395
192b62ce
TT
396 if (!bfd_check_format (temp_bfd.get (), bfd_core)
397 && !gdb_check_format (temp_bfd.get ()))
c906108c
SS
398 {
399 /* Do it after the err msg */
aff410f1
MS
400 /* FIXME: should be checking for errors from bfd_close (for one
401 thing, on error it does not free all the storage associated
402 with the bfd). */
8a3fe4f8 403 error (_("\"%s\" is not a core dump: %s"),
ee0c3293 404 filename.get (), bfd_errmsg (bfd_get_error ()));
c906108c
SS
405 }
406
06333fea 407 current_program_space->cbfd = std::move (temp_bfd);
c906108c 408
15244507 409 core_target *target = new core_target ();
0e24ac5d 410
15244507
PA
411 /* Own the target until it is successfully pushed. */
412 target_ops_up target_holder (target);
2acceee2 413
c906108c
SS
414 validate_files ();
415
2f1b5984
MK
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 (!exec_bfd)
421 set_gdbarch_from_file (core_bfd);
cbda0a99 422
15244507
PA
423 push_target (target);
424 target_holder.release ();
c906108c 425
3cdd9356 426 inferior_ptid = null_ptid;
0de3b513 427
739fc47a
PA
428 /* Need to flush the register cache (and the frame cache) from a
429 previous debug session. If inferior_ptid ends up the same as the
430 last debug session --- e.g., b foo; run; gcore core1; step; gcore
431 core2; core core1; core core2 --- then there's potential for
432 get_current_regcache to return the cached regcache of the
433 previous session, and the frame cache being stale. */
434 registers_changed ();
435
0de3b513
PA
436 /* Build up thread list from BFD sections, and possibly set the
437 current thread to the .reg/NN section matching the .reg
aff410f1 438 section. */
0de3b513
PA
439 bfd_map_over_sections (core_bfd, add_to_thread_list,
440 bfd_get_section_by_name (core_bfd, ".reg"));
441
d7e15655 442 if (inferior_ptid == null_ptid)
3cdd9356
PA
443 {
444 /* Either we found no .reg/NN section, and hence we have a
445 non-threaded core (single-threaded, from gdb's perspective),
446 or for some reason add_to_thread_list couldn't determine
447 which was the "main" thread. The latter case shouldn't
448 usually happen, but we're dealing with input here, which can
449 always be broken in different ways. */
00431a78 450 thread_info *thread = first_thread_of_inferior (current_inferior ());
c5504eaf 451
3cdd9356
PA
452 if (thread == NULL)
453 {
c45ceae0 454 inferior_appeared (current_inferior (), CORELOW_PID);
f2907e49 455 inferior_ptid = ptid_t (CORELOW_PID);
3cdd9356
PA
456 add_thread_silent (inferior_ptid);
457 }
458 else
00431a78 459 switch_to_thread (thread);
3cdd9356
PA
460 }
461
15244507 462 post_create_inferior (target, from_tty);
959b8724 463
0de3b513
PA
464 /* Now go through the target stack looking for threads since there
465 may be a thread_stratum target loaded on top of target core by
466 now. The layer above should claim threads found in the BFD
467 sections. */
492d29ea 468 TRY
8e7b59a5 469 {
e8032dde 470 target_update_thread_list ();
8e7b59a5
KS
471 }
472
492d29ea
PA
473 CATCH (except, RETURN_MASK_ERROR)
474 {
475 exception_print (gdb_stderr, except);
476 }
477 END_CATCH
0de3b513 478
c906108c
SS
479 p = bfd_core_file_failing_command (core_bfd);
480 if (p)
a3f17187 481 printf_filtered (_("Core was generated by `%s'.\n"), p);
c906108c 482
0c557179
SDJ
483 /* Clearing any previous state of convenience variables. */
484 clear_exit_convenience_vars ();
485
c906108c
SS
486 siggy = bfd_core_file_failing_signal (core_bfd);
487 if (siggy > 0)
423ec54c 488 {
15244507
PA
489 gdbarch *core_gdbarch = target->core_gdbarch ();
490
22203bbf 491 /* If we don't have a CORE_GDBARCH to work with, assume a native
1f8cf220
PA
492 core (map gdb_signal from host signals). If we do have
493 CORE_GDBARCH to work with, but no gdb_signal_from_target
494 implementation for that gdbarch, as a fallback measure,
495 assume the host signal mapping. It'll be correct for native
496 cores, but most likely incorrect for cross-cores. */
2ea28649 497 enum gdb_signal sig = (core_gdbarch != NULL
1f8cf220
PA
498 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
499 ? gdbarch_gdb_signal_from_target (core_gdbarch,
500 siggy)
501 : gdb_signal_from_host (siggy));
423ec54c 502
2d503272
PM
503 printf_filtered (_("Program terminated with signal %s, %s.\n"),
504 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
0c557179
SDJ
505
506 /* Set the value of the internal variable $_exitsignal,
507 which holds the signal uncaught by the inferior. */
508 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
509 siggy);
423ec54c 510 }
c906108c 511
87ab71f0
PA
512 /* Fetch all registers from core file. */
513 target_fetch_registers (get_current_regcache (), -1);
c906108c 514
87ab71f0
PA
515 /* Now, set up the frame cache, and print the top of stack. */
516 reinit_frame_cache ();
08d72866 517 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
f0e8c4c5
JK
518
519 /* Current thread should be NUM 1 but the user does not know that.
520 If a program is single threaded gdb in general does not mention
521 anything about threads. That is why the test is >= 2. */
522 if (thread_count () >= 2)
523 {
492d29ea 524 TRY
f0e8c4c5
JK
525 {
526 thread_command (NULL, from_tty);
527 }
492d29ea
PA
528 CATCH (except, RETURN_MASK_ERROR)
529 {
530 exception_print (gdb_stderr, except);
531 }
532 END_CATCH
f0e8c4c5 533 }
c906108c
SS
534}
535
f6ac5f3d
PA
536void
537core_target::detach (inferior *inf, int from_tty)
c906108c 538{
15244507
PA
539 /* Note that 'this' is dangling after this call. unpush_target
540 closes the target, and our close implementation deletes
541 'this'. */
f6ac5f3d 542 unpush_target (this);
15244507 543
c906108c 544 reinit_frame_cache ();
451953fa 545 maybe_say_no_core_file_now (from_tty);
c906108c
SS
546}
547
de57eccd 548/* Try to retrieve registers from a section in core_bfd, and supply
15244507
PA
549 them to m_core_vec->core_read_registers, as the register set
550 numbered WHICH.
de57eccd 551
11a33714
SM
552 If ptid's lwp member is zero, do the single-threaded
553 thing: look for a section named NAME. If ptid's lwp
0de3b513
PA
554 member is non-zero, do the multi-threaded thing: look for a section
555 named "NAME/LWP", where LWP is the shortest ASCII decimal
11a33714 556 representation of ptid's lwp member.
de57eccd
JM
557
558 HUMAN_NAME is a human-readable name for the kind of registers the
559 NAME section contains, for use in error messages.
560
15244507
PA
561 If REQUIRED is true, print an error if the core file doesn't have a
562 section by the appropriate name. Otherwise, just do nothing. */
de57eccd 563
15244507
PA
564void
565core_target::get_core_register_section (struct regcache *regcache,
566 const struct regset *regset,
567 const char *name,
dbd534fe 568 int section_min_size,
15244507
PA
569 int which,
570 const char *human_name,
571 bool required)
de57eccd 572{
7be0c536 573 struct bfd_section *section;
de57eccd
JM
574 bfd_size_type size;
575 char *contents;
874a1c8c
AT
576 bool variable_size_section = (regset != NULL
577 && regset->flags & REGSET_VARIABLE_SIZE);
de57eccd 578
3c3ae77e 579 thread_section_name section_name (name, regcache->ptid ());
de57eccd 580
3c3ae77e 581 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
de57eccd
JM
582 if (! section)
583 {
584 if (required)
aff410f1
MS
585 warning (_("Couldn't find %s registers in core file."),
586 human_name);
de57eccd
JM
587 return;
588 }
589
590 size = bfd_section_size (core_bfd, section);
dbd534fe 591 if (size < section_min_size)
8f0435f7 592 {
3c3ae77e
PA
593 warning (_("Section `%s' in core file too small."),
594 section_name.c_str ());
8f0435f7
AA
595 return;
596 }
dbd534fe 597 if (size != section_min_size && !variable_size_section)
f962539a
AA
598 {
599 warning (_("Unexpected size of section `%s' in core file."),
3c3ae77e 600 section_name.c_str ());
f962539a 601 }
8f0435f7 602
224c3ddb 603 contents = (char *) alloca (size);
de57eccd
JM
604 if (! bfd_get_section_contents (core_bfd, section, contents,
605 (file_ptr) 0, size))
606 {
8a3fe4f8 607 warning (_("Couldn't read %s registers from `%s' section in core file."),
3c3ae77e 608 human_name, section_name.c_str ());
de57eccd
JM
609 return;
610 }
611
8f0435f7
AA
612 if (regset != NULL)
613 {
9eefc95f 614 regset->supply_regset (regset, regcache, -1, contents, size);
0e24ac5d
MK
615 return;
616 }
617
15244507
PA
618 gdb_assert (m_core_vec != nullptr);
619 m_core_vec->core_read_registers (regcache, contents, size, which,
620 ((CORE_ADDR)
621 bfd_section_vma (core_bfd, section)));
de57eccd
JM
622}
623
15244507
PA
624/* Data passed to gdbarch_iterate_over_regset_sections's callback. */
625struct get_core_registers_cb_data
626{
627 core_target *target;
628 struct regcache *regcache;
629};
630
5aa82d05
AA
631/* Callback for get_core_registers that handles a single core file
632 register note section. */
633
634static void
a616bb94 635get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
8f0435f7 636 const struct regset *regset,
5aa82d05
AA
637 const char *human_name, void *cb_data)
638{
15244507
PA
639 auto *data = (get_core_registers_cb_data *) cb_data;
640 bool required = false;
a616bb94
AH
641 bool variable_size_section = (regset != NULL
642 && regset->flags & REGSET_VARIABLE_SIZE);
643
644 if (!variable_size_section)
645 gdb_assert (supply_size == collect_size);
5aa82d05
AA
646
647 if (strcmp (sect_name, ".reg") == 0)
8f0435f7 648 {
15244507 649 required = true;
8f0435f7
AA
650 if (human_name == NULL)
651 human_name = "general-purpose";
652 }
5aa82d05 653 else if (strcmp (sect_name, ".reg2") == 0)
8f0435f7
AA
654 {
655 if (human_name == NULL)
656 human_name = "floating-point";
657 }
658
659 /* The 'which' parameter is only used when no regset is provided.
660 Thus we just set it to -1. */
15244507 661 data->target->get_core_register_section (data->regcache, regset, sect_name,
a616bb94
AH
662 supply_size, -1, human_name,
663 required);
5aa82d05 664}
de57eccd 665
c906108c
SS
666/* Get the registers out of a core file. This is the machine-
667 independent part. Fetch_core_registers is the machine-dependent
aff410f1
MS
668 part, typically implemented in the xm-file for each
669 architecture. */
c906108c
SS
670
671/* We just get all the registers, so we don't use regno. */
672
f6ac5f3d
PA
673void
674core_target::fetch_registers (struct regcache *regcache, int regno)
c906108c 675{
9c5ea4d9 676 int i;
5aa82d05 677 struct gdbarch *gdbarch;
c906108c 678
15244507
PA
679 if (!(m_core_gdbarch != nullptr
680 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
681 && (m_core_vec == NULL || m_core_vec->core_read_registers == NULL))
c906108c
SS
682 {
683 fprintf_filtered (gdb_stderr,
c5aa993b 684 "Can't fetch registers from this type of core file\n");
c906108c
SS
685 return;
686 }
687
ac7936df 688 gdbarch = regcache->arch ();
5aa82d05 689 if (gdbarch_iterate_over_regset_sections_p (gdbarch))
15244507
PA
690 {
691 get_core_registers_cb_data data = { this, regcache };
692 gdbarch_iterate_over_regset_sections (gdbarch,
693 get_core_registers_cb,
694 (void *) &data, NULL);
695 }
1b1818e4
UW
696 else
697 {
8f0435f7
AA
698 get_core_register_section (regcache, NULL,
699 ".reg", 0, 0, "general-purpose", 1);
700 get_core_register_section (regcache, NULL,
701 ".reg2", 0, 2, "floating-point", 0);
1b1818e4 702 }
c906108c 703
ee99023e 704 /* Mark all registers not found in the core as unavailable. */
ac7936df 705 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
0ec9f114 706 if (regcache->get_register_status (i) == REG_UNKNOWN)
73e1c03f 707 regcache->raw_supply (i, NULL);
c906108c
SS
708}
709
f6ac5f3d
PA
710void
711core_target::files_info ()
c906108c 712{
15244507 713 print_section_info (&m_core_section_table, core_bfd);
c906108c 714}
e2544d02 715\f
efcbbd14
UW
716struct spuid_list
717{
718 gdb_byte *buf;
719 ULONGEST offset;
720 LONGEST len;
721 ULONGEST pos;
722 ULONGEST written;
723};
724
725static void
726add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
727{
9a3c8263 728 struct spuid_list *list = (struct spuid_list *) list_p;
efcbbd14 729 enum bfd_endian byte_order
aff410f1 730 = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
efcbbd14
UW
731 int fd, pos = 0;
732
733 sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
734 if (pos == 0)
735 return;
736
737 if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
738 {
739 store_unsigned_integer (list->buf + list->pos - list->offset,
740 4, byte_order, fd);
741 list->written += 4;
742 }
743 list->pos += 4;
744}
745
f6ac5f3d
PA
746enum target_xfer_status
747core_target::xfer_partial (enum target_object object, const char *annex,
748 gdb_byte *readbuf, const gdb_byte *writebuf,
749 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
e2544d02
RM
750{
751 switch (object)
752 {
753 case TARGET_OBJECT_MEMORY:
15244507
PA
754 return (section_table_xfer_memory_partial
755 (readbuf, writebuf,
756 offset, len, xfered_len,
757 m_core_section_table.sections,
758 m_core_section_table.sections_end,
759 NULL));
e2544d02
RM
760
761 case TARGET_OBJECT_AUXV:
762 if (readbuf)
763 {
764 /* When the aux vector is stored in core file, BFD
765 represents this with a fake section called ".auxv". */
766
c4c5b7ba 767 struct bfd_section *section;
e2544d02 768 bfd_size_type size;
e2544d02
RM
769
770 section = bfd_get_section_by_name (core_bfd, ".auxv");
771 if (section == NULL)
2ed4b548 772 return TARGET_XFER_E_IO;
e2544d02
RM
773
774 size = bfd_section_size (core_bfd, section);
775 if (offset >= size)
9b409511 776 return TARGET_XFER_EOF;
e2544d02
RM
777 size -= offset;
778 if (size > len)
779 size = len;
9b409511
YQ
780
781 if (size == 0)
782 return TARGET_XFER_EOF;
783 if (!bfd_get_section_contents (core_bfd, section, readbuf,
784 (file_ptr) offset, size))
e2544d02 785 {
8a3fe4f8 786 warning (_("Couldn't read NT_AUXV note in core file."));
2ed4b548 787 return TARGET_XFER_E_IO;
e2544d02
RM
788 }
789
9b409511
YQ
790 *xfered_len = (ULONGEST) size;
791 return TARGET_XFER_OK;
e2544d02 792 }
2ed4b548 793 return TARGET_XFER_E_IO;
e2544d02 794
403e1656
MK
795 case TARGET_OBJECT_WCOOKIE:
796 if (readbuf)
797 {
798 /* When the StackGhost cookie is stored in core file, BFD
aff410f1
MS
799 represents this with a fake section called
800 ".wcookie". */
403e1656
MK
801
802 struct bfd_section *section;
803 bfd_size_type size;
403e1656
MK
804
805 section = bfd_get_section_by_name (core_bfd, ".wcookie");
806 if (section == NULL)
2ed4b548 807 return TARGET_XFER_E_IO;
403e1656
MK
808
809 size = bfd_section_size (core_bfd, section);
810 if (offset >= size)
96c4f946 811 return TARGET_XFER_EOF;
403e1656
MK
812 size -= offset;
813 if (size > len)
814 size = len;
9b409511
YQ
815
816 if (size == 0)
817 return TARGET_XFER_EOF;
818 if (!bfd_get_section_contents (core_bfd, section, readbuf,
819 (file_ptr) offset, size))
403e1656 820 {
8a3fe4f8 821 warning (_("Couldn't read StackGhost cookie in core file."));
2ed4b548 822 return TARGET_XFER_E_IO;
403e1656
MK
823 }
824
9b409511
YQ
825 *xfered_len = (ULONGEST) size;
826 return TARGET_XFER_OK;
827
403e1656 828 }
2ed4b548 829 return TARGET_XFER_E_IO;
403e1656 830
de584861 831 case TARGET_OBJECT_LIBRARIES:
15244507
PA
832 if (m_core_gdbarch != nullptr
833 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
de584861
PA
834 {
835 if (writebuf)
2ed4b548 836 return TARGET_XFER_E_IO;
9b409511
YQ
837 else
838 {
15244507 839 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
9b409511
YQ
840 readbuf,
841 offset, len);
842
843 if (*xfered_len == 0)
844 return TARGET_XFER_EOF;
845 else
846 return TARGET_XFER_OK;
847 }
de584861
PA
848 }
849 /* FALL THROUGH */
850
356a5233 851 case TARGET_OBJECT_LIBRARIES_AIX:
15244507
PA
852 if (m_core_gdbarch != nullptr
853 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
356a5233
JB
854 {
855 if (writebuf)
2ed4b548 856 return TARGET_XFER_E_IO;
9b409511
YQ
857 else
858 {
859 *xfered_len
15244507 860 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
9b409511
YQ
861 readbuf, offset,
862 len);
863
864 if (*xfered_len == 0)
865 return TARGET_XFER_EOF;
866 else
867 return TARGET_XFER_OK;
868 }
356a5233
JB
869 }
870 /* FALL THROUGH */
871
efcbbd14
UW
872 case TARGET_OBJECT_SPU:
873 if (readbuf && annex)
874 {
875 /* When the SPU contexts are stored in a core file, BFD
aff410f1
MS
876 represents this with a fake section called
877 "SPU/<annex>". */
efcbbd14
UW
878
879 struct bfd_section *section;
880 bfd_size_type size;
efcbbd14 881 char sectionstr[100];
c5504eaf 882
efcbbd14
UW
883 xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
884
885 section = bfd_get_section_by_name (core_bfd, sectionstr);
886 if (section == NULL)
2ed4b548 887 return TARGET_XFER_E_IO;
efcbbd14
UW
888
889 size = bfd_section_size (core_bfd, section);
890 if (offset >= size)
9b409511 891 return TARGET_XFER_EOF;
efcbbd14
UW
892 size -= offset;
893 if (size > len)
894 size = len;
9b409511
YQ
895
896 if (size == 0)
897 return TARGET_XFER_EOF;
898 if (!bfd_get_section_contents (core_bfd, section, readbuf,
899 (file_ptr) offset, size))
efcbbd14
UW
900 {
901 warning (_("Couldn't read SPU section in core file."));
2ed4b548 902 return TARGET_XFER_E_IO;
efcbbd14
UW
903 }
904
9b409511
YQ
905 *xfered_len = (ULONGEST) size;
906 return TARGET_XFER_OK;
efcbbd14
UW
907 }
908 else if (readbuf)
909 {
910 /* NULL annex requests list of all present spuids. */
911 struct spuid_list list;
c5504eaf 912
efcbbd14
UW
913 list.buf = readbuf;
914 list.offset = offset;
915 list.len = len;
916 list.pos = 0;
917 list.written = 0;
918 bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
9b409511
YQ
919
920 if (list.written == 0)
921 return TARGET_XFER_EOF;
922 else
923 {
924 *xfered_len = (ULONGEST) list.written;
925 return TARGET_XFER_OK;
926 }
efcbbd14 927 }
2ed4b548 928 return TARGET_XFER_E_IO;
efcbbd14 929
9015683b
TT
930 case TARGET_OBJECT_SIGNAL_INFO:
931 if (readbuf)
9b409511 932 {
15244507
PA
933 if (m_core_gdbarch != nullptr
934 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
9b409511 935 {
15244507 936 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
382b69bb
JB
937 offset, len);
938
939 if (l >= 0)
940 {
941 *xfered_len = l;
942 if (l == 0)
943 return TARGET_XFER_EOF;
944 else
945 return TARGET_XFER_OK;
946 }
9b409511
YQ
947 }
948 }
2ed4b548 949 return TARGET_XFER_E_IO;
9015683b 950
e2544d02 951 default:
b6a8c27b
PA
952 return this->beneath ()->xfer_partial (object, annex, readbuf,
953 writebuf, offset, len,
954 xfered_len);
e2544d02
RM
955 }
956}
957
c906108c 958\f
c906108c
SS
959
960/* Okay, let's be honest: threads gleaned from a core file aren't
961 exactly lively, are they? On the other hand, if we don't claim
962 that each & every one is alive, then we don't get any of them
963 to appear in an "info thread" command, which is quite a useful
964 behaviour.
c5aa993b 965 */
57810aa7 966bool
f6ac5f3d 967core_target::thread_alive (ptid_t ptid)
c906108c 968{
57810aa7 969 return true;
c906108c
SS
970}
971
4eb0ad19
DJ
972/* Ask the current architecture what it knows about this core file.
973 That will be used, in turn, to pick a better architecture. This
974 wrapper could be avoided if targets got a chance to specialize
15244507 975 core_target. */
4eb0ad19 976
f6ac5f3d
PA
977const struct target_desc *
978core_target::read_description ()
4eb0ad19 979{
15244507 980 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
2117c711
TT
981 {
982 const struct target_desc *result;
983
15244507 984 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
2117c711
TT
985 if (result != NULL)
986 return result;
987 }
4eb0ad19 988
b6a8c27b 989 return this->beneath ()->read_description ();
4eb0ad19
DJ
990}
991
f6ac5f3d
PA
992const char *
993core_target::pid_to_str (ptid_t ptid)
0de3b513
PA
994{
995 static char buf[64];
88f38a04 996 struct inferior *inf;
a5ee0f0c 997 int pid;
0de3b513 998
a5ee0f0c
PA
999 /* The preferred way is to have a gdbarch/OS specific
1000 implementation. */
15244507
PA
1001 if (m_core_gdbarch != nullptr
1002 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1003 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
c5504eaf 1004
a5ee0f0c
PA
1005 /* Otherwise, if we don't have one, we'll just fallback to
1006 "process", with normal_pid_to_str. */
28439f5e 1007
a5ee0f0c 1008 /* Try the LWPID field first. */
e38504b3 1009 pid = ptid.lwp ();
a5ee0f0c 1010 if (pid != 0)
f2907e49 1011 return normal_pid_to_str (ptid_t (pid));
a5ee0f0c
PA
1012
1013 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1014 only if it isn't a fake PID. */
c9657e70 1015 inf = find_inferior_ptid (ptid);
88f38a04 1016 if (inf != NULL && !inf->fake_pid_p)
a5ee0f0c 1017 return normal_pid_to_str (ptid);
0de3b513 1018
a5ee0f0c
PA
1019 /* No luck. We simply don't have a valid PID to print. */
1020 xsnprintf (buf, sizeof buf, "<main task>");
0de3b513
PA
1021 return buf;
1022}
1023
f6ac5f3d
PA
1024const char *
1025core_target::thread_name (struct thread_info *thr)
4dfc5dbc 1026{
15244507
PA
1027 if (m_core_gdbarch != nullptr
1028 && gdbarch_core_thread_name_p (m_core_gdbarch))
1029 return gdbarch_core_thread_name (m_core_gdbarch, thr);
4dfc5dbc
JB
1030 return NULL;
1031}
1032
57810aa7 1033bool
f6ac5f3d 1034core_target::has_memory ()
c35b1492
PA
1035{
1036 return (core_bfd != NULL);
1037}
1038
57810aa7 1039bool
f6ac5f3d 1040core_target::has_stack ()
c35b1492
PA
1041{
1042 return (core_bfd != NULL);
1043}
1044
57810aa7 1045bool
f6ac5f3d 1046core_target::has_registers ()
c35b1492
PA
1047{
1048 return (core_bfd != NULL);
1049}
1050
451b7c33
TT
1051/* Implement the to_info_proc method. */
1052
f6ac5f3d
PA
1053bool
1054core_target::info_proc (const char *args, enum info_proc_what request)
451b7c33
TT
1055{
1056 struct gdbarch *gdbarch = get_current_arch ();
1057
1058 /* Since this is the core file target, call the 'core_info_proc'
1059 method on gdbarch, not 'info_proc'. */
1060 if (gdbarch_core_info_proc_p (gdbarch))
1061 gdbarch_core_info_proc (gdbarch, args, request);
c906108c 1062
f6ac5f3d 1063 return true;
c906108c
SS
1064}
1065
c906108c 1066void
fba45db2 1067_initialize_corelow (void)
c906108c 1068{
d9f719f1 1069 add_target (core_target_info, core_target_open, filename_completer);
c906108c 1070}
This page took 1.559845 seconds and 4 git commands to generate.