| 1 | /* Work with core dump and executable files, for GDB. |
| 2 | Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | GDB is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 1, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GDB is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GDB; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <errno.h> |
| 22 | #include <signal.h> |
| 23 | #include "defs.h" |
| 24 | #include "param.h" |
| 25 | #include "frame.h" /* required by inferior.h */ |
| 26 | #include "inferior.h" |
| 27 | #include "symtab.h" |
| 28 | #include "command.h" |
| 29 | #include "bfd.h" |
| 30 | #include "target.h" |
| 31 | #include "gdbcore.h" |
| 32 | |
| 33 | extern int xfer_memory (); |
| 34 | extern void child_attach (), child_create_inferior (); |
| 35 | |
| 36 | extern int sys_nerr; |
| 37 | extern char *sys_errlist[]; |
| 38 | extern char *sys_siglist[]; |
| 39 | |
| 40 | extern char registers[]; |
| 41 | |
| 42 | /* Hook for `exec_file_command' command to call. */ |
| 43 | |
| 44 | void (*exec_file_display_hook) () = NULL; |
| 45 | |
| 46 | struct section_table *core_sections, *core_sections_end; |
| 47 | |
| 48 | /* Binary file diddling handle for the core file. */ |
| 49 | |
| 50 | bfd *core_bfd = NULL; |
| 51 | |
| 52 | /* Forward decl */ |
| 53 | extern struct target_ops core_ops; |
| 54 | |
| 55 | \f |
| 56 | /* Discard all vestiges of any previous core file |
| 57 | and mark data and stack spaces as empty. */ |
| 58 | |
| 59 | void |
| 60 | core_close (quitting) |
| 61 | int quitting; |
| 62 | { |
| 63 | if (core_bfd) { |
| 64 | free (bfd_get_filename (core_bfd)); |
| 65 | bfd_close (core_bfd); |
| 66 | core_bfd = NULL; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* This routine opens and sets up the core file bfd */ |
| 71 | |
| 72 | void |
| 73 | core_open (filename, from_tty) |
| 74 | char *filename; |
| 75 | int from_tty; |
| 76 | { |
| 77 | char *p; |
| 78 | int siggy; |
| 79 | struct cleanup *old_chain; |
| 80 | char *temp; |
| 81 | bfd *temp_bfd; |
| 82 | int ontop; |
| 83 | |
| 84 | if (!filename) |
| 85 | { |
| 86 | error (core_bfd? |
| 87 | "No core file specified. (Use `detach' to stop debugging a core file.)" |
| 88 | : "No core file specified."); |
| 89 | } |
| 90 | |
| 91 | filename = tilde_expand (filename); |
| 92 | if (filename[0] != '/') { |
| 93 | temp = concat (current_directory, "/", filename); |
| 94 | free (filename); |
| 95 | filename = temp; |
| 96 | } |
| 97 | |
| 98 | old_chain = make_cleanup (free, filename); |
| 99 | temp_bfd = bfd_openr (filename, NULL); |
| 100 | if (temp_bfd == NULL) |
| 101 | { |
| 102 | perror_with_name (filename); |
| 103 | } |
| 104 | |
| 105 | if (!bfd_check_format (temp_bfd, bfd_core)) |
| 106 | { |
| 107 | bfd_close (temp_bfd); |
| 108 | error ("\"%s\" does not appear to be a core dump", filename); |
| 109 | } |
| 110 | |
| 111 | /* Looks semi-reasonable. Toss the old core file and work on the new. */ |
| 112 | |
| 113 | discard_cleanups (old_chain); /* Don't free filename any more */ |
| 114 | unpush_target (&core_ops); |
| 115 | core_bfd = temp_bfd; |
| 116 | old_chain = make_cleanup (core_close, core_bfd); |
| 117 | |
| 118 | validate_files (); |
| 119 | |
| 120 | /* Find the data section */ |
| 121 | if (build_section_table (core_bfd, &core_sections, &core_sections_end)) |
| 122 | error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd), |
| 123 | bfd_errmsg (bfd_error)); |
| 124 | |
| 125 | ontop = !push_target (&core_ops); |
| 126 | |
| 127 | p = bfd_core_file_failing_command (core_bfd); |
| 128 | if (p) |
| 129 | printf ("Core file invoked as `%s'.\n", p); |
| 130 | |
| 131 | siggy = bfd_core_file_failing_signal (core_bfd); |
| 132 | if (siggy > 0) |
| 133 | printf ("Program terminated with signal %d, %s.\n", siggy, |
| 134 | siggy < NSIG ? sys_siglist[siggy] : "(undocumented)"); |
| 135 | |
| 136 | if (ontop) { |
| 137 | /* Fetch all registers from core file */ |
| 138 | target_fetch_registers (-1); |
| 139 | set_current_frame ( create_new_frame (read_register (FP_REGNUM), |
| 140 | read_pc ())); |
| 141 | select_frame (get_current_frame (), 0); |
| 142 | /* FIXME, handle shared library reading here. */ |
| 143 | print_sel_frame (0); /* Print the top frame and source line */ |
| 144 | } else { |
| 145 | printf ( |
| 146 | "Warning: you won't be able to access this core file until you terminate\n\ |
| 147 | your %s; do ``info files''\n", current_target->to_longname); |
| 148 | } |
| 149 | |
| 150 | discard_cleanups (old_chain); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | core_detach (args, from_tty) |
| 155 | char *args; |
| 156 | int from_tty; |
| 157 | { |
| 158 | dont_repeat (); |
| 159 | if (args) |
| 160 | error ("Too many arguments"); |
| 161 | pop_target (); |
| 162 | if (from_tty) |
| 163 | printf ("No core file now.\n"); |
| 164 | } |
| 165 | |
| 166 | /* Backward compatability with old way of specifying core files. */ |
| 167 | |
| 168 | void |
| 169 | core_file_command (filename, from_tty) |
| 170 | char *filename; |
| 171 | int from_tty; |
| 172 | { |
| 173 | if (!filename) |
| 174 | core_detach (filename, from_tty); |
| 175 | else |
| 176 | core_open (filename, from_tty); |
| 177 | } |
| 178 | |
| 179 | \f |
| 180 | /* Call this to specify the hook for exec_file_command to call back. |
| 181 | This is called from the x-window display code. */ |
| 182 | |
| 183 | void |
| 184 | specify_exec_file_hook (hook) |
| 185 | void (*hook) (); |
| 186 | { |
| 187 | exec_file_display_hook = hook; |
| 188 | } |
| 189 | |
| 190 | /* The exec file must be closed before running an inferior. |
| 191 | If it is needed again after the inferior dies, it must |
| 192 | be reopened. */ |
| 193 | |
| 194 | void |
| 195 | close_exec_file () |
| 196 | { |
| 197 | #ifdef FIXME |
| 198 | if (exec_bfd) |
| 199 | bfd_tempclose (exec_bfd); |
| 200 | #endif |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | reopen_exec_file () |
| 205 | { |
| 206 | #ifdef FIXME |
| 207 | if (exec_bfd) |
| 208 | bfd_reopen (exec_bfd); |
| 209 | #endif |
| 210 | } |
| 211 | \f |
| 212 | /* If we have both a core file and an exec file, |
| 213 | print a warning if they don't go together. |
| 214 | This should really check that the core file came |
| 215 | from that exec file, but I don't know how to do it. */ |
| 216 | |
| 217 | void |
| 218 | validate_files () |
| 219 | { |
| 220 | if (exec_bfd && core_bfd) |
| 221 | { |
| 222 | if (core_file_matches_executable_p (core_bfd, exec_bfd)) |
| 223 | printf ("Warning: core file does not match specified executable file.\n"); |
| 224 | else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd)) |
| 225 | printf ("Warning: exec file is newer than core file.\n"); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* Return the name of the executable file as a string. |
| 230 | ERR nonzero means get error if there is none specified; |
| 231 | otherwise return 0 in that case. */ |
| 232 | |
| 233 | char * |
| 234 | get_exec_file (err) |
| 235 | int err; |
| 236 | { |
| 237 | if (exec_bfd) return bfd_get_filename(exec_bfd); |
| 238 | if (!err) return NULL; |
| 239 | |
| 240 | error ("No executable file specified.\n\ |
| 241 | Use the \"file\" or \"exec-file\" command."); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static void |
| 246 | core_files_info () |
| 247 | { |
| 248 | struct section_table *p; |
| 249 | |
| 250 | printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd)); |
| 251 | |
| 252 | for (p = core_sections; p < core_sections_end; p++) |
| 253 | printf("\tcore file from 0x%08x to 0x%08x is %s\n", |
| 254 | p->addr, p->endaddr, |
| 255 | bfd_section_name (core_bfd, p->sec_ptr)); |
| 256 | } |
| 257 | \f |
| 258 | void |
| 259 | memory_error (status, memaddr) |
| 260 | int status; |
| 261 | CORE_ADDR memaddr; |
| 262 | { |
| 263 | |
| 264 | if (status == EIO) |
| 265 | { |
| 266 | /* Actually, address between memaddr and memaddr + len |
| 267 | was out of bounds. */ |
| 268 | error ("Cannot access memory: address 0x%x out of bounds.", memaddr); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | if (status >= sys_nerr || status < 0) |
| 273 | error ("Error accessing memory address 0x%x: unknown error (%d).", |
| 274 | memaddr, status); |
| 275 | else |
| 276 | error ("Error accessing memory address 0x%x: %s.", |
| 277 | memaddr, sys_errlist[status]); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* Same as target_read_memory, but report an error if can't read. */ |
| 282 | void |
| 283 | read_memory (memaddr, myaddr, len) |
| 284 | CORE_ADDR memaddr; |
| 285 | char *myaddr; |
| 286 | int len; |
| 287 | { |
| 288 | int status; |
| 289 | status = target_read_memory (memaddr, myaddr, len); |
| 290 | if (status != 0) |
| 291 | memory_error (status, memaddr); |
| 292 | } |
| 293 | |
| 294 | /* Same as target_write_memory, but report an error if can't write. */ |
| 295 | void |
| 296 | write_memory (memaddr, myaddr, len) |
| 297 | CORE_ADDR memaddr; |
| 298 | char *myaddr; |
| 299 | int len; |
| 300 | { |
| 301 | int status; |
| 302 | |
| 303 | status = target_write_memory (memaddr, myaddr, len); |
| 304 | if (status != 0) |
| 305 | memory_error (status, memaddr); |
| 306 | } |
| 307 | |
| 308 | /* Read an integer from debugged memory, given address and number of bytes. */ |
| 309 | |
| 310 | long |
| 311 | read_memory_integer (memaddr, len) |
| 312 | CORE_ADDR memaddr; |
| 313 | int len; |
| 314 | { |
| 315 | char cbuf; |
| 316 | short sbuf; |
| 317 | int ibuf; |
| 318 | long lbuf; |
| 319 | |
| 320 | if (len == sizeof (char)) |
| 321 | { |
| 322 | read_memory (memaddr, &cbuf, len); |
| 323 | return cbuf; |
| 324 | } |
| 325 | if (len == sizeof (short)) |
| 326 | { |
| 327 | read_memory (memaddr, (char *)&sbuf, len); |
| 328 | SWAP_TARGET_AND_HOST (&sbuf, sizeof (short)); |
| 329 | return sbuf; |
| 330 | } |
| 331 | if (len == sizeof (int)) |
| 332 | { |
| 333 | read_memory (memaddr, (char *)&ibuf, len); |
| 334 | SWAP_TARGET_AND_HOST (&ibuf, sizeof (int)); |
| 335 | return ibuf; |
| 336 | } |
| 337 | if (len == sizeof (lbuf)) |
| 338 | { |
| 339 | read_memory (memaddr, (char *)&lbuf, len); |
| 340 | SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf)); |
| 341 | return lbuf; |
| 342 | } |
| 343 | error ("Cannot handle integers of %d bytes.", len); |
| 344 | return -1; /* for lint */ |
| 345 | } |
| 346 | \f |
| 347 | /* Read or write the core file. |
| 348 | |
| 349 | Args are address within core file, address within gdb address-space, |
| 350 | length, and a flag indicating whether to read or write. |
| 351 | |
| 352 | Result is a length: |
| 353 | |
| 354 | 0: We cannot handle this address and length. |
| 355 | > 0: We have handled N bytes starting at this address. |
| 356 | (If N == length, we did it all.) We might be able |
| 357 | to handle more bytes beyond this length, but no |
| 358 | promises. |
| 359 | < 0: We cannot handle this address, but if somebody |
| 360 | else handles (-N) bytes, we can start from there. |
| 361 | |
| 362 | The actual work is done by xfer_memory in exec.c, which we share |
| 363 | in common with exec_xfer_memory(). */ |
| 364 | |
| 365 | static int |
| 366 | core_xfer_memory (memaddr, myaddr, len, write) |
| 367 | CORE_ADDR memaddr; |
| 368 | char *myaddr; |
| 369 | int len; |
| 370 | int write; |
| 371 | { |
| 372 | return xfer_memory (memaddr, myaddr, len, write, |
| 373 | core_bfd, core_sections, core_sections_end); |
| 374 | } |
| 375 | \f |
| 376 | /* Get the registers out of a core file. This is the machine- |
| 377 | independent part. Fetch_core_registers is the machine-dependent |
| 378 | part, typically implemented in the xm-file for each architecture. */ |
| 379 | |
| 380 | static int |
| 381 | get_core_registers (regno) |
| 382 | int regno; |
| 383 | { |
| 384 | sec_ptr reg_sec; |
| 385 | unsigned size; |
| 386 | char *the_regs; |
| 387 | |
| 388 | reg_sec = bfd_get_section_by_name (core_bfd, ".reg"); |
| 389 | size = bfd_section_size (core_bfd, reg_sec); |
| 390 | the_regs = alloca (size); |
| 391 | if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, |
| 392 | (unsigned)0, size)) |
| 393 | { |
| 394 | fetch_core_registers (the_regs, size, 0); |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | fprintf (stderr, "Couldn't fetch registers from core file: %s\n", |
| 399 | bfd_errmsg (bfd_error)); |
| 400 | } |
| 401 | |
| 402 | /* Now do it again for the float registers, if they exist. */ |
| 403 | reg_sec = bfd_get_section_by_name (core_bfd, ".reg2"); |
| 404 | if (reg_sec) { |
| 405 | size = bfd_section_size (core_bfd, reg_sec); |
| 406 | the_regs = alloca (size); |
| 407 | if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, |
| 408 | (unsigned)0, size)) |
| 409 | { |
| 410 | fetch_core_registers (the_regs, size, 2); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n", |
| 415 | bfd_errmsg (bfd_error)); |
| 416 | } |
| 417 | } |
| 418 | registers_fetched(); |
| 419 | return 0; /* FIXME, what result goes here? */ |
| 420 | } |
| 421 | \f |
| 422 | struct target_ops core_ops = { |
| 423 | "core", "Local core dump file", |
| 424 | core_open, core_close, |
| 425 | child_attach, core_detach, 0, 0, /* resume, wait */ |
| 426 | get_core_registers, |
| 427 | 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */ |
| 428 | core_xfer_memory, core_files_info, |
| 429 | 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */ |
| 430 | 0, 0, 0, 0, 0, /* terminal stuff */ |
| 431 | 0, 0, 0, 0, 0, /* kill, load, add_syms, call fn, lookup sym */ |
| 432 | child_create_inferior, 0, /* mourn_inferior */ |
| 433 | core_stratum, 0, /* next */ |
| 434 | 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */ |
| 435 | OPS_MAGIC, /* Always the last thing */ |
| 436 | }; |
| 437 | |
| 438 | void |
| 439 | _initialize_core() |
| 440 | { |
| 441 | |
| 442 | add_com ("core-file", class_files, core_file_command, |
| 443 | "Use FILE as core dump for examining memory and registers.\n\ |
| 444 | No arg means have no core file. This command has been superseded by the\n\ |
| 445 | `target core' and `detach' commands."); |
| 446 | add_target (&core_ops); |
| 447 | } |