| 1 | /* Core dump and executable file functions below target vector, for GDB. |
| 2 | |
| 3 | Copyright (C) 1986-2018 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "arch-utils.h" |
| 22 | #include <signal.h> |
| 23 | #include <fcntl.h> |
| 24 | #ifdef HAVE_SYS_FILE_H |
| 25 | #include <sys/file.h> /* needed for F_OK and friends */ |
| 26 | #endif |
| 27 | #include "frame.h" /* required by inferior.h */ |
| 28 | #include "inferior.h" |
| 29 | #include "infrun.h" |
| 30 | #include "symtab.h" |
| 31 | #include "command.h" |
| 32 | #include "bfd.h" |
| 33 | #include "target.h" |
| 34 | #include "gdbcore.h" |
| 35 | #include "gdbthread.h" |
| 36 | #include "regcache.h" |
| 37 | #include "regset.h" |
| 38 | #include "symfile.h" |
| 39 | #include "exec.h" |
| 40 | #include "readline/readline.h" |
| 41 | #include "solib.h" |
| 42 | #include "filenames.h" |
| 43 | #include "progspace.h" |
| 44 | #include "objfiles.h" |
| 45 | #include "gdb_bfd.h" |
| 46 | #include "completer.h" |
| 47 | #include "filestuff.h" |
| 48 | |
| 49 | #ifndef O_LARGEFILE |
| 50 | #define O_LARGEFILE 0 |
| 51 | #endif |
| 52 | |
| 53 | /* The core file target. */ |
| 54 | |
| 55 | class core_target final : public target_ops |
| 56 | { |
| 57 | public: |
| 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 _("\ |
| 70 | Use 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 | |
| 86 | bool thread_alive (ptid_t ptid) override; |
| 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 | |
| 93 | bool has_memory () override; |
| 94 | bool has_stack () override; |
| 95 | bool has_registers () override; |
| 96 | bool info_proc (const char *, enum info_proc_what) override; |
| 97 | }; |
| 98 | |
| 99 | /* See gdbcore.h. */ |
| 100 | struct target_ops *the_core_target; |
| 101 | |
| 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. */ |
| 105 | |
| 106 | static struct core_fns *core_file_fns = NULL; |
| 107 | |
| 108 | /* The core_fns for a core file handler that is prepared to read the |
| 109 | core file currently open on core_bfd. */ |
| 110 | |
| 111 | static struct core_fns *core_vec = NULL; |
| 112 | |
| 113 | /* FIXME: kettenis/20031023: Eventually this variable should |
| 114 | disappear. */ |
| 115 | |
| 116 | static struct gdbarch *core_gdbarch = NULL; |
| 117 | |
| 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. */ |
| 124 | static struct target_section_table *core_data; |
| 125 | |
| 126 | static struct core_fns *sniff_core_bfd (bfd *); |
| 127 | |
| 128 | static int gdb_check_format (bfd *); |
| 129 | |
| 130 | static void core_close_cleanup (void *ignore); |
| 131 | |
| 132 | static void add_to_thread_list (bfd *, asection *, void *); |
| 133 | |
| 134 | static core_target core_ops; |
| 135 | |
| 136 | /* An arbitrary identifier for the core inferior. */ |
| 137 | #define CORELOW_PID 1 |
| 138 | |
| 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 |
| 141 | reader, to register information about each format the reader is |
| 142 | prepared to handle. */ |
| 143 | |
| 144 | void |
| 145 | deprecated_add_core_fns (struct core_fns *cf) |
| 146 | { |
| 147 | cf->next = core_file_fns; |
| 148 | core_file_fns = cf; |
| 149 | } |
| 150 | |
| 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 |
| 153 | reading the core file. */ |
| 154 | |
| 155 | int |
| 156 | default_core_sniffer (struct core_fns *our_fns, bfd *abfd) |
| 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 |
| 165 | handle the core file open on ABFD. Returns pointer to set that is |
| 166 | selected. */ |
| 167 | |
| 168 | static struct core_fns * |
| 169 | sniff_core_bfd (bfd *abfd) |
| 170 | { |
| 171 | struct core_fns *cf; |
| 172 | struct core_fns *yummy = NULL; |
| 173 | int matches = 0; |
| 174 | |
| 175 | /* Don't sniff if we have support for register sets in |
| 176 | CORE_GDBARCH. */ |
| 177 | if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch)) |
| 178 | return NULL; |
| 179 | |
| 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 | { |
| 190 | warning (_("\"%s\": ambiguous core format, %d handlers match"), |
| 191 | bfd_get_filename (abfd), matches); |
| 192 | } |
| 193 | else if (matches == 0) |
| 194 | error (_("\"%s\": no core file handler recognizes format"), |
| 195 | bfd_get_filename (abfd)); |
| 196 | |
| 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 |
| 202 | core file handler that recognizes it. */ |
| 203 | |
| 204 | int |
| 205 | default_check_format (bfd *abfd) |
| 206 | { |
| 207 | return (0); |
| 208 | } |
| 209 | |
| 210 | /* Attempt to recognize core file formats that BFD rejects. */ |
| 211 | |
| 212 | static int |
| 213 | gdb_check_format (bfd *abfd) |
| 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 | { |
| 221 | return (1); |
| 222 | } |
| 223 | } |
| 224 | return (0); |
| 225 | } |
| 226 | |
| 227 | /* Discard all vestiges of any previous core file and mark data and |
| 228 | stack spaces as empty. */ |
| 229 | |
| 230 | static void |
| 231 | core_close () |
| 232 | { |
| 233 | if (core_bfd) |
| 234 | { |
| 235 | int pid = ptid_get_pid (inferior_ptid); |
| 236 | inferior_ptid = null_ptid; /* Avoid confusion from thread |
| 237 | stuff. */ |
| 238 | if (pid != 0) |
| 239 | exit_inferior_silent (pid); |
| 240 | |
| 241 | /* Clear out solib state while the bfd is still open. See |
| 242 | comments in clear_solib in solib.c. */ |
| 243 | clear_solib (); |
| 244 | |
| 245 | if (core_data) |
| 246 | { |
| 247 | xfree (core_data->sections); |
| 248 | xfree (core_data); |
| 249 | core_data = NULL; |
| 250 | } |
| 251 | |
| 252 | gdb_bfd_unref (core_bfd); |
| 253 | core_bfd = NULL; |
| 254 | } |
| 255 | core_vec = NULL; |
| 256 | core_gdbarch = NULL; |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | core_close_cleanup (void *ignore) |
| 261 | { |
| 262 | core_close (); |
| 263 | } |
| 264 | |
| 265 | void |
| 266 | core_target::close () |
| 267 | { |
| 268 | core_close (); |
| 269 | } |
| 270 | |
| 271 | /* Look for sections whose names start with `.reg/' so that we can |
| 272 | extract the list of threads in a core file. */ |
| 273 | |
| 274 | static void |
| 275 | add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg) |
| 276 | { |
| 277 | ptid_t ptid; |
| 278 | int core_tid; |
| 279 | int pid, lwpid; |
| 280 | asection *reg_sect = (asection *) reg_sect_arg; |
| 281 | int fake_pid_p = 0; |
| 282 | struct inferior *inf; |
| 283 | |
| 284 | if (!startswith (bfd_section_name (abfd, asect), ".reg/")) |
| 285 | return; |
| 286 | |
| 287 | core_tid = atoi (bfd_section_name (abfd, asect) + 5); |
| 288 | |
| 289 | pid = bfd_core_file_pid (core_bfd); |
| 290 | if (pid == 0) |
| 291 | { |
| 292 | fake_pid_p = 1; |
| 293 | pid = CORELOW_PID; |
| 294 | } |
| 295 | |
| 296 | lwpid = core_tid; |
| 297 | |
| 298 | inf = current_inferior (); |
| 299 | if (inf->pid == 0) |
| 300 | { |
| 301 | inferior_appeared (inf, pid); |
| 302 | inf->fake_pid_p = fake_pid_p; |
| 303 | } |
| 304 | |
| 305 | ptid = ptid_build (pid, lwpid, 0); |
| 306 | |
| 307 | add_thread (ptid); |
| 308 | |
| 309 | /* Warning, Will Robinson, looking at BFD private data! */ |
| 310 | |
| 311 | if (reg_sect != NULL |
| 312 | && asect->filepos == reg_sect->filepos) /* Did we find .reg? */ |
| 313 | inferior_ptid = ptid; /* Yes, make it current. */ |
| 314 | } |
| 315 | |
| 316 | /* This routine opens and sets up the core file bfd. */ |
| 317 | |
| 318 | void |
| 319 | core_target::open (const char *arg, int from_tty) |
| 320 | { |
| 321 | const char *p; |
| 322 | int siggy; |
| 323 | struct cleanup *old_chain; |
| 324 | int scratch_chan; |
| 325 | int flags; |
| 326 | |
| 327 | target_preopen (from_tty); |
| 328 | if (!arg) |
| 329 | { |
| 330 | if (core_bfd) |
| 331 | error (_("No core file specified. (Use `detach' " |
| 332 | "to stop debugging a core file.)")); |
| 333 | else |
| 334 | error (_("No core file specified.")); |
| 335 | } |
| 336 | |
| 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)); |
| 341 | |
| 342 | flags = O_BINARY | O_LARGEFILE; |
| 343 | if (write_files) |
| 344 | flags |= O_RDWR; |
| 345 | else |
| 346 | flags |= O_RDONLY; |
| 347 | scratch_chan = gdb_open_cloexec (filename.get (), flags, 0); |
| 348 | if (scratch_chan < 0) |
| 349 | perror_with_name (filename.get ()); |
| 350 | |
| 351 | gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget, |
| 352 | write_files ? FOPEN_RUB : FOPEN_RB, |
| 353 | scratch_chan)); |
| 354 | if (temp_bfd == NULL) |
| 355 | perror_with_name (filename.get ()); |
| 356 | |
| 357 | if (!bfd_check_format (temp_bfd.get (), bfd_core) |
| 358 | && !gdb_check_format (temp_bfd.get ())) |
| 359 | { |
| 360 | /* Do it after the err msg */ |
| 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). */ |
| 364 | error (_("\"%s\" is not a core dump: %s"), |
| 365 | filename.get (), bfd_errmsg (bfd_get_error ())); |
| 366 | } |
| 367 | |
| 368 | /* Looks semi-reasonable. Toss the old core file and work on the |
| 369 | new. */ |
| 370 | |
| 371 | unpush_target (&core_ops); |
| 372 | core_bfd = temp_bfd.release (); |
| 373 | old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/); |
| 374 | |
| 375 | core_gdbarch = gdbarch_from_bfd (core_bfd); |
| 376 | |
| 377 | /* Find a suitable core file handler to munch on core_bfd */ |
| 378 | core_vec = sniff_core_bfd (core_bfd); |
| 379 | |
| 380 | validate_files (); |
| 381 | |
| 382 | core_data = XCNEW (struct target_section_table); |
| 383 | |
| 384 | /* Find the data section */ |
| 385 | if (build_section_table (core_bfd, |
| 386 | &core_data->sections, |
| 387 | &core_data->sections_end)) |
| 388 | error (_("\"%s\": Can't find sections: %s"), |
| 389 | bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ())); |
| 390 | |
| 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); |
| 397 | |
| 398 | push_target (&core_ops); |
| 399 | discard_cleanups (old_chain); |
| 400 | |
| 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 | |
| 407 | inferior_ptid = null_ptid; |
| 408 | |
| 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 | |
| 417 | /* Build up thread list from BFD sections, and possibly set the |
| 418 | current thread to the .reg/NN section matching the .reg |
| 419 | section. */ |
| 420 | bfd_map_over_sections (core_bfd, add_to_thread_list, |
| 421 | bfd_get_section_by_name (core_bfd, ".reg")); |
| 422 | |
| 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); |
| 432 | |
| 433 | if (thread == NULL) |
| 434 | { |
| 435 | inferior_appeared (current_inferior (), CORELOW_PID); |
| 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 | |
| 443 | post_create_inferior (&core_ops, from_tty); |
| 444 | |
| 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. */ |
| 449 | TRY |
| 450 | { |
| 451 | target_update_thread_list (); |
| 452 | } |
| 453 | |
| 454 | CATCH (except, RETURN_MASK_ERROR) |
| 455 | { |
| 456 | exception_print (gdb_stderr, except); |
| 457 | } |
| 458 | END_CATCH |
| 459 | |
| 460 | p = bfd_core_file_failing_command (core_bfd); |
| 461 | if (p) |
| 462 | printf_filtered (_("Core was generated by `%s'.\n"), p); |
| 463 | |
| 464 | /* Clearing any previous state of convenience variables. */ |
| 465 | clear_exit_convenience_vars (); |
| 466 | |
| 467 | siggy = bfd_core_file_failing_signal (core_bfd); |
| 468 | if (siggy > 0) |
| 469 | { |
| 470 | /* If we don't have a CORE_GDBARCH to work with, assume a native |
| 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. */ |
| 476 | enum gdb_signal sig = (core_gdbarch != NULL |
| 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)); |
| 481 | |
| 482 | printf_filtered (_("Program terminated with signal %s, %s.\n"), |
| 483 | gdb_signal_to_name (sig), gdb_signal_to_string (sig)); |
| 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); |
| 489 | } |
| 490 | |
| 491 | /* Fetch all registers from core file. */ |
| 492 | target_fetch_registers (get_current_regcache (), -1); |
| 493 | |
| 494 | /* Now, set up the frame cache, and print the top of stack. */ |
| 495 | reinit_frame_cache (); |
| 496 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); |
| 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 | { |
| 503 | TRY |
| 504 | { |
| 505 | thread_command (NULL, from_tty); |
| 506 | } |
| 507 | CATCH (except, RETURN_MASK_ERROR) |
| 508 | { |
| 509 | exception_print (gdb_stderr, except); |
| 510 | } |
| 511 | END_CATCH |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void |
| 516 | core_target::detach (inferior *inf, int from_tty) |
| 517 | { |
| 518 | unpush_target (this); |
| 519 | reinit_frame_cache (); |
| 520 | if (from_tty) |
| 521 | printf_filtered (_("No core file now.\n")); |
| 522 | } |
| 523 | |
| 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 | |
| 528 | If ptid's lwp member is zero, do the single-threaded |
| 529 | thing: look for a section named NAME. If ptid's lwp |
| 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 |
| 532 | representation of ptid's lwp member. |
| 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 |
| 538 | have a section by the appropriate name. Otherwise, just do |
| 539 | nothing. */ |
| 540 | |
| 541 | static void |
| 542 | get_core_register_section (struct regcache *regcache, |
| 543 | const struct regset *regset, |
| 544 | const char *name, |
| 545 | int min_size, |
| 546 | int which, |
| 547 | const char *human_name, |
| 548 | int required) |
| 549 | { |
| 550 | struct bfd_section *section; |
| 551 | bfd_size_type size; |
| 552 | char *contents; |
| 553 | bool variable_size_section = (regset != NULL |
| 554 | && regset->flags & REGSET_VARIABLE_SIZE); |
| 555 | |
| 556 | thread_section_name section_name (name, regcache->ptid ()); |
| 557 | |
| 558 | section = bfd_get_section_by_name (core_bfd, section_name.c_str ()); |
| 559 | if (! section) |
| 560 | { |
| 561 | if (required) |
| 562 | warning (_("Couldn't find %s registers in core file."), |
| 563 | human_name); |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | size = bfd_section_size (core_bfd, section); |
| 568 | if (size < min_size) |
| 569 | { |
| 570 | warning (_("Section `%s' in core file too small."), |
| 571 | section_name.c_str ()); |
| 572 | return; |
| 573 | } |
| 574 | if (size != min_size && !variable_size_section) |
| 575 | { |
| 576 | warning (_("Unexpected size of section `%s' in core file."), |
| 577 | section_name.c_str ()); |
| 578 | } |
| 579 | |
| 580 | contents = (char *) alloca (size); |
| 581 | if (! bfd_get_section_contents (core_bfd, section, contents, |
| 582 | (file_ptr) 0, size)) |
| 583 | { |
| 584 | warning (_("Couldn't read %s registers from `%s' section in core file."), |
| 585 | human_name, section_name.c_str ()); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | if (regset != NULL) |
| 590 | { |
| 591 | regset->supply_regset (regset, regcache, -1, contents, size); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | gdb_assert (core_vec); |
| 596 | core_vec->core_read_registers (regcache, contents, size, which, |
| 597 | ((CORE_ADDR) |
| 598 | bfd_section_vma (core_bfd, section))); |
| 599 | } |
| 600 | |
| 601 | /* Callback for get_core_registers that handles a single core file |
| 602 | register note section. */ |
| 603 | |
| 604 | static void |
| 605 | get_core_registers_cb (const char *sect_name, int size, |
| 606 | const struct regset *regset, |
| 607 | const char *human_name, void *cb_data) |
| 608 | { |
| 609 | struct regcache *regcache = (struct regcache *) cb_data; |
| 610 | int required = 0; |
| 611 | |
| 612 | if (strcmp (sect_name, ".reg") == 0) |
| 613 | { |
| 614 | required = 1; |
| 615 | if (human_name == NULL) |
| 616 | human_name = "general-purpose"; |
| 617 | } |
| 618 | else if (strcmp (sect_name, ".reg2") == 0) |
| 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); |
| 628 | } |
| 629 | |
| 630 | /* Get the registers out of a core file. This is the machine- |
| 631 | independent part. Fetch_core_registers is the machine-dependent |
| 632 | part, typically implemented in the xm-file for each |
| 633 | architecture. */ |
| 634 | |
| 635 | /* We just get all the registers, so we don't use regno. */ |
| 636 | |
| 637 | void |
| 638 | core_target::fetch_registers (struct regcache *regcache, int regno) |
| 639 | { |
| 640 | int i; |
| 641 | struct gdbarch *gdbarch; |
| 642 | |
| 643 | if (!(core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch)) |
| 644 | && (core_vec == NULL || core_vec->core_read_registers == NULL)) |
| 645 | { |
| 646 | fprintf_filtered (gdb_stderr, |
| 647 | "Can't fetch registers from this type of core file\n"); |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | gdbarch = regcache->arch (); |
| 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); |
| 656 | else |
| 657 | { |
| 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); |
| 662 | } |
| 663 | |
| 664 | /* Mark all registers not found in the core as unavailable. */ |
| 665 | for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++) |
| 666 | if (regcache_register_status (regcache, i) == REG_UNKNOWN) |
| 667 | regcache_raw_supply (regcache, i, NULL); |
| 668 | } |
| 669 | |
| 670 | void |
| 671 | core_target::files_info () |
| 672 | { |
| 673 | print_section_info (core_data, core_bfd); |
| 674 | } |
| 675 | \f |
| 676 | struct spuid_list |
| 677 | { |
| 678 | gdb_byte *buf; |
| 679 | ULONGEST offset; |
| 680 | LONGEST len; |
| 681 | ULONGEST pos; |
| 682 | ULONGEST written; |
| 683 | }; |
| 684 | |
| 685 | static void |
| 686 | add_to_spuid_list (bfd *abfd, asection *asect, void *list_p) |
| 687 | { |
| 688 | struct spuid_list *list = (struct spuid_list *) list_p; |
| 689 | enum bfd_endian byte_order |
| 690 | = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE; |
| 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 | |
| 706 | enum target_xfer_status |
| 707 | core_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) |
| 710 | { |
| 711 | switch (object) |
| 712 | { |
| 713 | case TARGET_OBJECT_MEMORY: |
| 714 | return section_table_xfer_memory_partial (readbuf, writebuf, |
| 715 | offset, len, xfered_len, |
| 716 | core_data->sections, |
| 717 | core_data->sections_end, |
| 718 | NULL); |
| 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 | |
| 726 | struct bfd_section *section; |
| 727 | bfd_size_type size; |
| 728 | |
| 729 | section = bfd_get_section_by_name (core_bfd, ".auxv"); |
| 730 | if (section == NULL) |
| 731 | return TARGET_XFER_E_IO; |
| 732 | |
| 733 | size = bfd_section_size (core_bfd, section); |
| 734 | if (offset >= size) |
| 735 | return TARGET_XFER_EOF; |
| 736 | size -= offset; |
| 737 | if (size > len) |
| 738 | size = len; |
| 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)) |
| 744 | { |
| 745 | warning (_("Couldn't read NT_AUXV note in core file.")); |
| 746 | return TARGET_XFER_E_IO; |
| 747 | } |
| 748 | |
| 749 | *xfered_len = (ULONGEST) size; |
| 750 | return TARGET_XFER_OK; |
| 751 | } |
| 752 | return TARGET_XFER_E_IO; |
| 753 | |
| 754 | case TARGET_OBJECT_WCOOKIE: |
| 755 | if (readbuf) |
| 756 | { |
| 757 | /* When the StackGhost cookie is stored in core file, BFD |
| 758 | represents this with a fake section called |
| 759 | ".wcookie". */ |
| 760 | |
| 761 | struct bfd_section *section; |
| 762 | bfd_size_type size; |
| 763 | |
| 764 | section = bfd_get_section_by_name (core_bfd, ".wcookie"); |
| 765 | if (section == NULL) |
| 766 | return TARGET_XFER_E_IO; |
| 767 | |
| 768 | size = bfd_section_size (core_bfd, section); |
| 769 | if (offset >= size) |
| 770 | return TARGET_XFER_EOF; |
| 771 | size -= offset; |
| 772 | if (size > len) |
| 773 | size = len; |
| 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)) |
| 779 | { |
| 780 | warning (_("Couldn't read StackGhost cookie in core file.")); |
| 781 | return TARGET_XFER_E_IO; |
| 782 | } |
| 783 | |
| 784 | *xfered_len = (ULONGEST) size; |
| 785 | return TARGET_XFER_OK; |
| 786 | |
| 787 | } |
| 788 | return TARGET_XFER_E_IO; |
| 789 | |
| 790 | case TARGET_OBJECT_LIBRARIES: |
| 791 | if (core_gdbarch |
| 792 | && gdbarch_core_xfer_shared_libraries_p (core_gdbarch)) |
| 793 | { |
| 794 | if (writebuf) |
| 795 | return TARGET_XFER_E_IO; |
| 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 | } |
| 807 | } |
| 808 | /* FALL THROUGH */ |
| 809 | |
| 810 | case TARGET_OBJECT_LIBRARIES_AIX: |
| 811 | if (core_gdbarch |
| 812 | && gdbarch_core_xfer_shared_libraries_aix_p (core_gdbarch)) |
| 813 | { |
| 814 | if (writebuf) |
| 815 | return TARGET_XFER_E_IO; |
| 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 | } |
| 828 | } |
| 829 | /* FALL THROUGH */ |
| 830 | |
| 831 | case TARGET_OBJECT_SPU: |
| 832 | if (readbuf && annex) |
| 833 | { |
| 834 | /* When the SPU contexts are stored in a core file, BFD |
| 835 | represents this with a fake section called |
| 836 | "SPU/<annex>". */ |
| 837 | |
| 838 | struct bfd_section *section; |
| 839 | bfd_size_type size; |
| 840 | char sectionstr[100]; |
| 841 | |
| 842 | xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex); |
| 843 | |
| 844 | section = bfd_get_section_by_name (core_bfd, sectionstr); |
| 845 | if (section == NULL) |
| 846 | return TARGET_XFER_E_IO; |
| 847 | |
| 848 | size = bfd_section_size (core_bfd, section); |
| 849 | if (offset >= size) |
| 850 | return TARGET_XFER_EOF; |
| 851 | size -= offset; |
| 852 | if (size > len) |
| 853 | size = len; |
| 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)) |
| 859 | { |
| 860 | warning (_("Couldn't read SPU section in core file.")); |
| 861 | return TARGET_XFER_E_IO; |
| 862 | } |
| 863 | |
| 864 | *xfered_len = (ULONGEST) size; |
| 865 | return TARGET_XFER_OK; |
| 866 | } |
| 867 | else if (readbuf) |
| 868 | { |
| 869 | /* NULL annex requests list of all present spuids. */ |
| 870 | struct spuid_list list; |
| 871 | |
| 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); |
| 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 | } |
| 886 | } |
| 887 | return TARGET_XFER_E_IO; |
| 888 | |
| 889 | case TARGET_OBJECT_SIGNAL_INFO: |
| 890 | if (readbuf) |
| 891 | { |
| 892 | if (core_gdbarch |
| 893 | && gdbarch_core_xfer_siginfo_p (core_gdbarch)) |
| 894 | { |
| 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 | } |
| 906 | } |
| 907 | } |
| 908 | return TARGET_XFER_E_IO; |
| 909 | |
| 910 | default: |
| 911 | return this->beneath->xfer_partial (object, annex, readbuf, |
| 912 | writebuf, offset, len, |
| 913 | xfered_len); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | \f |
| 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. |
| 924 | */ |
| 925 | bool |
| 926 | core_target::thread_alive (ptid_t ptid) |
| 927 | { |
| 928 | return true; |
| 929 | } |
| 930 | |
| 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 | |
| 936 | const struct target_desc * |
| 937 | core_target::read_description () |
| 938 | { |
| 939 | if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch)) |
| 940 | { |
| 941 | const struct target_desc *result; |
| 942 | |
| 943 | result = gdbarch_core_read_description (core_gdbarch, this, core_bfd); |
| 944 | if (result != NULL) |
| 945 | return result; |
| 946 | } |
| 947 | |
| 948 | return this->beneath->read_description (); |
| 949 | } |
| 950 | |
| 951 | const char * |
| 952 | core_target::pid_to_str (ptid_t ptid) |
| 953 | { |
| 954 | static char buf[64]; |
| 955 | struct inferior *inf; |
| 956 | int pid; |
| 957 | |
| 958 | /* The preferred way is to have a gdbarch/OS specific |
| 959 | implementation. */ |
| 960 | if (core_gdbarch |
| 961 | && gdbarch_core_pid_to_str_p (core_gdbarch)) |
| 962 | return gdbarch_core_pid_to_str (core_gdbarch, ptid); |
| 963 | |
| 964 | /* Otherwise, if we don't have one, we'll just fallback to |
| 965 | "process", with normal_pid_to_str. */ |
| 966 | |
| 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. */ |
| 974 | inf = find_inferior_ptid (ptid); |
| 975 | if (inf != NULL && !inf->fake_pid_p) |
| 976 | return normal_pid_to_str (ptid); |
| 977 | |
| 978 | /* No luck. We simply don't have a valid PID to print. */ |
| 979 | xsnprintf (buf, sizeof buf, "<main task>"); |
| 980 | return buf; |
| 981 | } |
| 982 | |
| 983 | const char * |
| 984 | core_target::thread_name (struct thread_info *thr) |
| 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 | |
| 992 | bool |
| 993 | core_target::has_memory () |
| 994 | { |
| 995 | return (core_bfd != NULL); |
| 996 | } |
| 997 | |
| 998 | bool |
| 999 | core_target::has_stack () |
| 1000 | { |
| 1001 | return (core_bfd != NULL); |
| 1002 | } |
| 1003 | |
| 1004 | bool |
| 1005 | core_target::has_registers () |
| 1006 | { |
| 1007 | return (core_bfd != NULL); |
| 1008 | } |
| 1009 | |
| 1010 | /* Implement the to_info_proc method. */ |
| 1011 | |
| 1012 | bool |
| 1013 | core_target::info_proc (const char *args, enum info_proc_what request) |
| 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); |
| 1021 | |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
| 1025 | void |
| 1026 | _initialize_corelow (void) |
| 1027 | { |
| 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; |
| 1033 | |
| 1034 | add_target_with_completer (&core_ops, filename_completer); |
| 1035 | } |