| 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
| 2 | Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program 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 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program 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 this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "frame.h" |
| 22 | #include "inferior.h" |
| 23 | |
| 24 | #include <sys/param.h> |
| 25 | #include <sys/dir.h> |
| 26 | #include <signal.h> |
| 27 | #include <sys/user.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <fcntl.h> |
| 30 | |
| 31 | #include "gdbcore.h" |
| 32 | |
| 33 | #include <sys/file.h> |
| 34 | #include "gdb_stat.h" |
| 35 | |
| 36 | \f |
| 37 | /* Work with core dump and executable files, for GDB. |
| 38 | This code would be in corefile.c if it weren't machine-dependent. */ |
| 39 | |
| 40 | void |
| 41 | core_file_command (filename, from_tty) |
| 42 | char *filename; |
| 43 | int from_tty; |
| 44 | { |
| 45 | int val; |
| 46 | extern char registers[]; |
| 47 | |
| 48 | /* Discard all vestiges of any previous core file |
| 49 | and mark data and stack spaces as empty. */ |
| 50 | |
| 51 | if (corefile) |
| 52 | free (corefile); |
| 53 | corefile = 0; |
| 54 | |
| 55 | if (corechan >= 0) |
| 56 | close (corechan); |
| 57 | corechan = -1; |
| 58 | |
| 59 | data_start = 0; |
| 60 | data_end = 0; |
| 61 | stack_start = STACK_END_ADDR; |
| 62 | stack_end = STACK_END_ADDR; |
| 63 | |
| 64 | /* Now, if a new core file was specified, open it and digest it. */ |
| 65 | |
| 66 | if (filename) |
| 67 | { |
| 68 | filename = tilde_expand (filename); |
| 69 | make_cleanup (free, filename); |
| 70 | |
| 71 | if (have_inferior_p ()) |
| 72 | error ("To look at a core file, you must kill the program with \"kill\"."); |
| 73 | corechan = open (filename, O_RDONLY, 0); |
| 74 | if (corechan < 0) |
| 75 | perror_with_name (filename); |
| 76 | /* 4.2-style (and perhaps also sysV-style) core dump file. */ |
| 77 | { |
| 78 | struct user u; |
| 79 | int reg_offset; |
| 80 | |
| 81 | val = myread (corechan, &u, sizeof u); |
| 82 | if (val < 0) |
| 83 | perror_with_name (filename); |
| 84 | data_start = exec_data_start; |
| 85 | |
| 86 | data_end = data_start + NBPG * u.u_dsize; |
| 87 | stack_start = stack_end - NBPG * u.u_ssize; |
| 88 | data_offset = NBPG * UPAGES; |
| 89 | stack_offset = NBPG * (UPAGES + u.u_dsize); |
| 90 | reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR; |
| 91 | |
| 92 | /* I don't know where to find this info. |
| 93 | So, for now, mark it as not available. */ |
| 94 | core_aouthdr.a_magic = 0; |
| 95 | |
| 96 | /* Read the register values out of the core file and store |
| 97 | them where `read_register' will find them. */ |
| 98 | |
| 99 | { |
| 100 | register int regno; |
| 101 | |
| 102 | for (regno = 0; regno < NUM_REGS; regno++) |
| 103 | { |
| 104 | char buf[MAX_REGISTER_RAW_SIZE]; |
| 105 | |
| 106 | val = lseek (corechan, register_addr (regno, reg_offset), 0); |
| 107 | if (val < 0) |
| 108 | perror_with_name (filename); |
| 109 | |
| 110 | val = myread (corechan, buf, sizeof buf); |
| 111 | if (val < 0) |
| 112 | perror_with_name (filename); |
| 113 | supply_register (regno, buf); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | if (filename[0] == '/') |
| 118 | corefile = savestring (filename, strlen (filename)); |
| 119 | else |
| 120 | { |
| 121 | corefile = concat (current_directory, "/", filename, NULL); |
| 122 | } |
| 123 | |
| 124 | flush_cached_frames (); |
| 125 | select_frame (get_current_frame (), 0); |
| 126 | validate_files (); |
| 127 | } |
| 128 | else if (from_tty) |
| 129 | printf ("No core file now.\n"); |
| 130 | } |