| 1 | /* Core dump and executable file functions below target vector, for GDB. |
| 2 | Copyright 1986, 1987, 1989, 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include <errno.h> |
| 22 | #include <signal.h> |
| 23 | #include <fcntl.h> |
| 24 | #include "frame.h" /* required by inferior.h */ |
| 25 | #include "inferior.h" |
| 26 | #include "symtab.h" |
| 27 | #include "command.h" |
| 28 | #include "bfd.h" |
| 29 | #include "target.h" |
| 30 | #include "gdbcore.h" |
| 31 | |
| 32 | static void |
| 33 | core_files_info PARAMS ((struct target_ops *)); |
| 34 | |
| 35 | #ifdef SOLIB_ADD |
| 36 | static int |
| 37 | solib_add_stub PARAMS ((char *)); |
| 38 | #endif |
| 39 | |
| 40 | static void |
| 41 | core_close PARAMS ((int)); |
| 42 | |
| 43 | static void |
| 44 | get_core_registers PARAMS ((int)); |
| 45 | |
| 46 | /* Discard all vestiges of any previous core file |
| 47 | and mark data and stack spaces as empty. */ |
| 48 | |
| 49 | /* ARGSUSED */ |
| 50 | static void |
| 51 | core_close (quitting) |
| 52 | int quitting; |
| 53 | { |
| 54 | inferior_pid = 0; /* Avoid confusion from thread stuff */ |
| 55 | |
| 56 | if (core_bfd) { |
| 57 | free (bfd_get_filename (core_bfd)); |
| 58 | bfd_close (core_bfd); |
| 59 | core_bfd = NULL; |
| 60 | #ifdef CLEAR_SOLIB |
| 61 | CLEAR_SOLIB (); |
| 62 | #endif |
| 63 | if (core_ops.to_sections) { |
| 64 | free ((PTR)core_ops.to_sections); |
| 65 | core_ops.to_sections = NULL; |
| 66 | core_ops.to_sections_end = NULL; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #ifdef SOLIB_ADD |
| 72 | /* Stub function for catch_errors around shared library hacking. */ |
| 73 | |
| 74 | static int |
| 75 | solib_add_stub (from_tty) |
| 76 | char *from_tty; |
| 77 | { |
| 78 | SOLIB_ADD (NULL, (int)from_tty, &core_ops); |
| 79 | return 0; |
| 80 | } |
| 81 | #endif /* SOLIB_ADD */ |
| 82 | |
| 83 | /* Look for sections whose names start with `.reg/' so that we can extract the |
| 84 | list of threads in a core file. */ |
| 85 | |
| 86 | static void |
| 87 | add_to_thread_list (abfd, asect, reg_sect) |
| 88 | bfd *abfd; |
| 89 | asection *asect; |
| 90 | asection *reg_sect; |
| 91 | { |
| 92 | int thread_id; |
| 93 | |
| 94 | if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) |
| 95 | return; |
| 96 | |
| 97 | thread_id = atoi (bfd_section_name (abfd, asect) + 5); |
| 98 | |
| 99 | add_thread (thread_id); |
| 100 | |
| 101 | /* Warning, Will Robinson, looking at BFD private data! */ |
| 102 | |
| 103 | if (asect->filepos == reg_sect->filepos) /* Did we find .reg? */ |
| 104 | inferior_pid = thread_id; /* Yes, make it current */ |
| 105 | } |
| 106 | |
| 107 | /* This routine opens and sets up the core file bfd */ |
| 108 | |
| 109 | void |
| 110 | core_open (filename, from_tty) |
| 111 | char *filename; |
| 112 | int from_tty; |
| 113 | { |
| 114 | const char *p; |
| 115 | int siggy; |
| 116 | struct cleanup *old_chain; |
| 117 | char *temp; |
| 118 | bfd *temp_bfd; |
| 119 | int ontop; |
| 120 | int scratch_chan; |
| 121 | |
| 122 | target_preopen (from_tty); |
| 123 | if (!filename) |
| 124 | { |
| 125 | error (core_bfd? |
| 126 | "No core file specified. (Use `detach' to stop debugging a core file.)" |
| 127 | : "No core file specified."); |
| 128 | } |
| 129 | |
| 130 | filename = tilde_expand (filename); |
| 131 | if (filename[0] != '/') { |
| 132 | temp = concat (current_directory, "/", filename, NULL); |
| 133 | free (filename); |
| 134 | filename = temp; |
| 135 | } |
| 136 | |
| 137 | old_chain = make_cleanup (free, filename); |
| 138 | |
| 139 | scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0); |
| 140 | if (scratch_chan < 0) |
| 141 | perror_with_name (filename); |
| 142 | |
| 143 | temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan); |
| 144 | if (temp_bfd == NULL) |
| 145 | { |
| 146 | perror_with_name (filename); |
| 147 | } |
| 148 | |
| 149 | if (!bfd_check_format (temp_bfd, bfd_core)) |
| 150 | { |
| 151 | /* Do it after the err msg */ |
| 152 | make_cleanup (bfd_close, temp_bfd); |
| 153 | error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error)); |
| 154 | } |
| 155 | |
| 156 | /* Looks semi-reasonable. Toss the old core file and work on the new. */ |
| 157 | |
| 158 | discard_cleanups (old_chain); /* Don't free filename any more */ |
| 159 | unpush_target (&core_ops); |
| 160 | core_bfd = temp_bfd; |
| 161 | old_chain = make_cleanup (core_close, core_bfd); |
| 162 | |
| 163 | validate_files (); |
| 164 | |
| 165 | /* Find the data section */ |
| 166 | if (build_section_table (core_bfd, &core_ops.to_sections, |
| 167 | &core_ops.to_sections_end)) |
| 168 | error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd), |
| 169 | bfd_errmsg (bfd_error)); |
| 170 | |
| 171 | ontop = !push_target (&core_ops); |
| 172 | discard_cleanups (old_chain); |
| 173 | |
| 174 | p = bfd_core_file_failing_command (core_bfd); |
| 175 | if (p) |
| 176 | printf_filtered ("Core was generated by `%s'.\n", p); |
| 177 | |
| 178 | siggy = bfd_core_file_failing_signal (core_bfd); |
| 179 | if (siggy > 0) |
| 180 | printf_filtered ("Program terminated with signal %d, %s.\n", siggy, |
| 181 | safe_strsignal (siggy)); |
| 182 | |
| 183 | /* Build up thread list from BFD sections. */ |
| 184 | |
| 185 | init_thread_list (); |
| 186 | bfd_map_over_sections (core_bfd, add_to_thread_list, |
| 187 | bfd_get_section_by_name (core_bfd, ".reg")); |
| 188 | |
| 189 | if (ontop) { |
| 190 | /* Fetch all registers from core file */ |
| 191 | target_fetch_registers (-1); |
| 192 | |
| 193 | /* Add symbols and section mappings for any shared libraries */ |
| 194 | #ifdef SOLIB_ADD |
| 195 | catch_errors (solib_add_stub, (char *)from_tty, (char *)0, |
| 196 | RETURN_MASK_ALL); |
| 197 | #endif |
| 198 | |
| 199 | /* Now, set up the frame cache, and print the top of stack */ |
| 200 | set_current_frame (create_new_frame (read_fp (), |
| 201 | read_pc ())); |
| 202 | select_frame (get_current_frame (), 0); |
| 203 | print_stack_frame (selected_frame, selected_frame_level, 1); |
| 204 | } else { |
| 205 | warning ( |
| 206 | "you won't be able to access this core file until you terminate\n\ |
| 207 | your %s; do ``info files''", current_target->to_longname); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | core_detach (args, from_tty) |
| 213 | char *args; |
| 214 | int from_tty; |
| 215 | { |
| 216 | if (args) |
| 217 | error ("Too many arguments"); |
| 218 | unpush_target (&core_ops); |
| 219 | reinit_frame_cache (); |
| 220 | if (from_tty) |
| 221 | printf_filtered ("No core file now.\n"); |
| 222 | } |
| 223 | |
| 224 | /* Get the registers out of a core file. This is the machine- |
| 225 | independent part. Fetch_core_registers is the machine-dependent |
| 226 | part, typically implemented in the xm-file for each architecture. */ |
| 227 | |
| 228 | /* We just get all the registers, so we don't use regno. */ |
| 229 | /* ARGSUSED */ |
| 230 | static void |
| 231 | get_core_registers (regno) |
| 232 | int regno; |
| 233 | { |
| 234 | sec_ptr reg_sec; |
| 235 | unsigned size; |
| 236 | char *the_regs; |
| 237 | char secname[10]; |
| 238 | |
| 239 | /* Thread support. If inferior_pid is non-zero, then we have found a core |
| 240 | file with threads (or multiple processes). In that case, we need to |
| 241 | use the appropriate register section, else we just use `.reg'. */ |
| 242 | |
| 243 | /* XXX - same thing needs to be done for floating-point (.reg2) sections. */ |
| 244 | |
| 245 | if (inferior_pid) |
| 246 | sprintf (secname, ".reg/%d", inferior_pid); |
| 247 | else |
| 248 | strcpy (secname, ".reg"); |
| 249 | |
| 250 | reg_sec = bfd_get_section_by_name (core_bfd, secname); |
| 251 | if (!reg_sec) goto cant; |
| 252 | size = bfd_section_size (core_bfd, reg_sec); |
| 253 | the_regs = alloca (size); |
| 254 | if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size)) |
| 255 | { |
| 256 | fetch_core_registers (the_regs, size, 0, |
| 257 | (unsigned) bfd_section_vma (abfd,reg_sec)); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | cant: |
| 262 | fprintf_filtered (stderr, "Couldn't fetch registers from core file: %s\n", |
| 263 | bfd_errmsg (bfd_error)); |
| 264 | } |
| 265 | |
| 266 | /* Now do it again for the float registers, if they exist. */ |
| 267 | reg_sec = bfd_get_section_by_name (core_bfd, ".reg2"); |
| 268 | if (reg_sec) { |
| 269 | size = bfd_section_size (core_bfd, reg_sec); |
| 270 | the_regs = alloca (size); |
| 271 | if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, |
| 272 | size)) |
| 273 | { |
| 274 | fetch_core_registers (the_regs, size, 2, |
| 275 | (unsigned) bfd_section_vma (abfd,reg_sec)); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | fprintf_filtered (stderr, "Couldn't fetch register set 2 from core file: %s\n", |
| 280 | bfd_errmsg (bfd_error)); |
| 281 | } |
| 282 | } |
| 283 | registers_fetched(); |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | core_files_info (t) |
| 288 | struct target_ops *t; |
| 289 | { |
| 290 | print_section_info (t, core_bfd); |
| 291 | } |
| 292 | \f |
| 293 | /* If mourn is being called in all the right places, this could be say |
| 294 | `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */ |
| 295 | |
| 296 | static int |
| 297 | ignore (addr, contents) |
| 298 | CORE_ADDR addr; |
| 299 | char *contents; |
| 300 | { |
| 301 | } |
| 302 | |
| 303 | struct target_ops core_ops = { |
| 304 | "core", "Local core dump file", |
| 305 | "Use a core file as a target. Specify the filename of the core file.", |
| 306 | core_open, core_close, |
| 307 | find_default_attach, core_detach, 0, 0, /* resume, wait */ |
| 308 | get_core_registers, |
| 309 | 0, 0, /* store_regs, prepare_to_store */ |
| 310 | xfer_memory, core_files_info, |
| 311 | ignore, ignore, /* core_insert_breakpoint, core_remove_breakpoint, */ |
| 312 | 0, 0, 0, 0, 0, /* terminal stuff */ |
| 313 | 0, 0, 0, /* kill, load, lookup sym */ |
| 314 | find_default_create_inferior, 0, /* mourn_inferior */ |
| 315 | 0, /* can_run */ |
| 316 | 0, /* notice_signals */ |
| 317 | core_stratum, 0, /* next */ |
| 318 | 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */ |
| 319 | 0, 0, /* section pointers */ |
| 320 | OPS_MAGIC, /* Always the last thing */ |
| 321 | }; |
| 322 | |
| 323 | void |
| 324 | _initialize_corelow() |
| 325 | { |
| 326 | add_target (&core_ops); |
| 327 | } |