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