1 /* Kernel core dump functions below target vector, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 /* $FreeBSD: src/gnu/usr.bin/binutils/gdb/alpha/kvm-fbsd.c,v 1.1.2.1 2000/12/11 01:03:18 obrien Exp $ */
23 /* $DragonFly: src/gnu/usr.bin/binutils/gdb/alpha/Attic/kvm-fbsd.c,v 1.2 2003/06/17 04:25:44 dillon Exp $ */
26 * This works like "remote" but, you use it like this:
27 * target kcore /dev/mem
29 * target kcore /var/crash/host/core.0
31 * This way makes it easy to short-circut the whole bfd monster,
32 * and direct the inferior stuff to our libkvm implementation.
35 #include <sys/param.h>
46 #include "gdb_string.h"
47 #include "frame.h" /* required by inferior.h */
56 kcore_files_info PARAMS ((struct target_ops *));
59 kcore_close PARAMS ((int));
62 get_kcore_registers PARAMS ((int));
65 xfer_mem PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
68 xfer_umem PARAMS ((CORE_ADDR, char *, int, int));
70 static char *core_file;
71 static kvm_t *core_kd;
72 static struct pcb cur_pcb;
74 static struct target_ops kcore_ops;
75 int kernel_writablecore;
78 * Read the "thing" at kernel address 'addr' into the space pointed to
79 * by point. The length of the "thing" is determined by the type of p.
80 * Result is non-zero if transfer fails.
82 #define kvread(addr, p) \
83 (target_read_memory((CORE_ADDR)(addr), (char *)(p), sizeof(*(p))))
90 struct minimal_symbol *sym;
92 sym = lookup_minimal_symbol(name, NULL, NULL);
94 error("kernel symbol `%s' not found.", name);
96 return SYMBOL_VALUE_ADDRESS(sym);
100 * Provide the address of an initial PCB to use.
101 * If this is a crash dump, try for "dumppcb".
102 * If no "dumppcb" or it's /dev/mem, use proc0.
103 * Return the core address of the PCB we found.
108 struct minimal_symbol *sym;
112 /* Make sure things are open... */
113 if (!core_kd || !core_file)
116 /* If this is NOT /dev/mem try for dumppcb. */
117 if (strncmp(core_file, _PATH_DEV, sizeof _PATH_DEV - 1)) {
118 sym = lookup_minimal_symbol("dumppcb", NULL, NULL);
120 addr = SYMBOL_VALUE_ADDRESS(sym);
126 * OK, just use proc0pcb. Note that curproc might
127 * not exist, and if it does, it will point to gdb.
128 * Therefore, just use proc0 and let the user set
129 * some other context if they care about it.
131 addr = ksym_lookup("proc0paddr");
132 if (kvread(addr, &val)) {
133 error("cannot read proc0paddr pointer at %x\n", addr);
137 return ((CORE_ADDR)val);
141 * Set the current context to that of the PCB struct
142 * at the system address passed.
149 if (kvread(addr, &cur_pcb))
150 error("cannot read pcb at %#x", addr);
152 /* Fetch all registers from core file */
153 target_fetch_registers (-1);
155 /* Now, set up the frame cache, and print the top of stack */
156 flush_cached_frames();
157 set_current_frame (create_new_frame (read_fp (), read_pc ()));
158 select_frame (get_current_frame (), 0);
162 /* Discard all vestiges of any previous core file and mark data and stack
167 kcore_close (quitting)
171 inferior_pid = 0; /* Avoid confusion from thread stuff */
181 /* This routine opens and sets up the core file bfd. */
184 kcore_open (filename, from_tty)
185 char *filename; /* the core file */
190 struct cleanup *old_chain;
195 target_preopen (from_tty);
197 /* The exec file is required for symbols. */
198 if (exec_bfd == NULL)
199 error("No kernel exec file specified");
202 error ("No core file specified."
203 " (Use `detach' to stop debugging a core file.)");
208 error ("No core file specified.");
212 filename = tilde_expand (filename);
213 if (filename[0] != '/') {
214 cp = concat (current_directory, "/", filename, NULL);
219 old_chain = make_cleanup (free, filename);
221 kd = kvm_open (bfd_get_filename(exec_bfd), filename, NULL,
222 kernel_writablecore ? O_RDWR: O_RDONLY, 0);
224 perror_with_name (filename);
228 /* Looks semi-reasonable. Toss the old core file and work on the new. */
230 discard_cleanups (old_chain); /* Don't free filename any more */
231 core_file = filename;
232 unpush_target (&kcore_ops);
233 ontop = !push_target (&kcore_ops);
235 /* Note unpush_target (above) calls kcore_close. */
238 /* print out the panic string if there is one */
239 if (kvread(ksym_lookup("panicstr"), &addr) == 0 &&
241 target_read_memory(addr, buf, sizeof(buf)) == 0) {
243 for (cp = buf; cp < &buf[sizeof(buf)] && *cp; cp++)
244 if (!isascii(*cp) || (!isprint(*cp) && !isspace(*cp)))
248 printf_filtered("panic: %s\n", buf);
253 "you won't be able to access this core file until you terminate\n\
254 your %s; do ``info files''", target_longname);
258 /* Now, set up process context, and print the top of stack */
259 (void)set_context(initial_pcb());
260 print_stack_frame (selected_frame, selected_frame_level, 1);
264 kcore_detach (args, from_tty)
269 error ("Too many arguments");
270 unpush_target (&kcore_ops);
271 reinit_frame_cache ();
273 printf_filtered ("No kernel core file now.\n");
276 /* Get the registers out of a core file. This is the machine-
277 independent part. Fetch_core_registers is the machine-dependent
278 part, typically implemented in the xm-file for each architecture. */
280 /* We just get all the registers, so we don't use regno. */
284 get_kcore_registers (regno)
289 * XXX - Only read the pcb when set_context() is called.
290 * When looking at a live kernel this may be a problem,
291 * but the user can do another "proc" or "pcb" command to
292 * grab a new copy of the pcb...
296 * Zero out register set then fill in the ones we know about.
298 fetch_kcore_registers (&cur_pcb);
303 struct target_ops *t;
305 printf_filtered ("\t`%s'\n", core_file);
308 /* If mourn is being called in all the right places, this could be say
309 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
312 ignore (addr, contents)
320 xfer_kmem (memaddr, myaddr, len, write, target)
325 struct target_ops *target;
330 if (it is a user address)
331 return xfer_umem(memaddr, myaddr, len, write);
338 n = kvm_write(core_kd, memaddr, myaddr, len);
340 n = kvm_read (core_kd, memaddr, myaddr, len) ;
342 fprintf_unfiltered (gdb_stderr, "can not access 0x%x, %s\n",
343 memaddr, kvm_geterr(core_kd));
352 xfer_umem (memaddr, myaddr, len, write)
356 int write; /* ignored */
361 if (kvread(cur_proc, &proc))
362 error("cannot read proc at %#x", cur_proc);
363 n = kvm_uread(core_kd, &proc, memaddr, myaddr, len) ;
379 error_no_arg("proc address for the new context");
382 error("no kernel core file");
384 addr = (CORE_ADDR)parse_and_eval_address(arg);
386 /* Read the PCB address in proc structure. */
387 addr += (int) &((struct proc *)0)->p_addr;
388 if (kvread(addr, &val))
389 error("cannot read u area ptr");
391 if (set_context((CORE_ADDR)val))
392 error("invalid proc address");
403 error_no_arg("pcb address for the new context");
406 error("no kernel core file");
408 addr = (CORE_ADDR)parse_and_eval_address(arg);
410 if (set_context(addr))
411 error("invalid pcb address");
417 _initialize_kcorelow()
419 kcore_ops.to_shortname = "kcore";
420 kcore_ops.to_longname = "Kernel core dump file";
422 "Use a core file as a target. Specify the filename of the core file.";
423 kcore_ops.to_open = kcore_open;
424 kcore_ops.to_close = kcore_close;
425 kcore_ops.to_attach = find_default_attach;
426 kcore_ops.to_detach = kcore_detach;
427 kcore_ops.to_fetch_registers = get_kcore_registers;
428 kcore_ops.to_xfer_memory = xfer_kmem;
429 kcore_ops.to_files_info = kcore_files_info;
430 kcore_ops.to_create_inferior = find_default_create_inferior;
431 kcore_ops.to_stratum = kcore_stratum;
432 kcore_ops.to_has_memory = 1;
433 kcore_ops.to_has_stack = 1;
434 kcore_ops.to_has_registers = 1;
435 kcore_ops.to_magic = OPS_MAGIC;
437 add_target (&kcore_ops);
438 add_com ("proc", class_obscure, set_proc_cmd, "Set current process context");