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