Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[games.git] / gnu / usr.bin / binutils / gdb / alpha / kvm-fbsd.c
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.
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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
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 $ */
24
25 /*
26  * This works like "remote" but, you use it like this:
27  *     target kcore /dev/mem
28  * or
29  *     target kcore /var/crash/host/core.0
30  *
31  * This way makes it easy to short-circut the whole bfd monster,
32  * and direct the inferior stuff to our libkvm implementation.
33  */
34
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/proc.h>
38 #include <sys/user.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <fcntl.h>
42 #include <kvm.h>
43 #include <paths.h>
44
45 #include "defs.h"
46 #include "gdb_string.h"
47 #include "frame.h"  /* required by inferior.h */
48 #include "inferior.h"
49 #include "symtab.h"
50 #include "command.h"
51 #include "bfd.h"
52 #include "target.h"
53 #include "gdbcore.h"
54
55 static void
56 kcore_files_info PARAMS ((struct target_ops *));
57
58 static void
59 kcore_close PARAMS ((int));
60
61 static void
62 get_kcore_registers PARAMS ((int));
63
64 static int
65 xfer_mem PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
66
67 static int
68 xfer_umem PARAMS ((CORE_ADDR, char *, int, int));
69
70 static char             *core_file;
71 static kvm_t            *core_kd;
72 static struct pcb       cur_pcb;
73
74 static struct target_ops kcore_ops;
75 int kernel_writablecore;
76
77 /*
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.
81  */
82 #define kvread(addr, p) \
83 (target_read_memory((CORE_ADDR)(addr), (char *)(p), sizeof(*(p))))
84
85
86 CORE_ADDR
87 ksym_lookup(name)
88   const char *name;
89 {
90   struct minimal_symbol *sym;
91
92   sym = lookup_minimal_symbol(name, NULL, NULL);
93   if (sym == NULL)
94     error("kernel symbol `%s' not found.", name);
95
96   return SYMBOL_VALUE_ADDRESS(sym);
97 }
98
99 /*
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.
104  */
105 static CORE_ADDR
106 initial_pcb()
107 {
108   struct minimal_symbol *sym;
109   CORE_ADDR addr;
110   void *val;
111
112   /* Make sure things are open... */
113   if (!core_kd || !core_file)
114     return (0);
115
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);
119     if (sym != NULL) {
120       addr = SYMBOL_VALUE_ADDRESS(sym);
121       return (addr);
122     }
123   }
124
125   /*
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.
130    */
131   addr = ksym_lookup("proc0paddr");
132   if (kvread(addr, &val)) {
133     error("cannot read proc0paddr pointer at %x\n", addr);
134     val = 0;
135   }
136
137   return ((CORE_ADDR)val);
138 }
139
140 /*
141  * Set the current context to that of the PCB struct
142  * at the system address passed.
143  */
144 static int
145 set_context(addr)
146   CORE_ADDR addr;
147 {
148
149   if (kvread(addr, &cur_pcb))
150     error("cannot read pcb at %#x", addr);
151
152   /* Fetch all registers from core file */
153   target_fetch_registers (-1);
154
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);
159   return (0);
160 }
161
162 /* Discard all vestiges of any previous core file and mark data and stack
163    spaces as empty.  */
164
165 /* ARGSUSED */
166 static void
167 kcore_close (quitting)
168      int quitting;
169 {
170
171   inferior_pid = 0;             /* Avoid confusion from thread stuff */
172
173   if (core_kd) {
174     kvm_close(core_kd);
175     free(core_file);
176     core_file = NULL;
177     core_kd = NULL;
178   }
179 }
180
181 /* This routine opens and sets up the core file bfd.  */
182
183 static void
184 kcore_open (filename, from_tty)
185      char *filename;    /* the core file */
186      int from_tty;
187 {
188   kvm_t         *kd;
189   const char *p;
190   struct cleanup *old_chain;
191   char buf[256], *cp;
192   int ontop;
193   CORE_ADDR addr;
194
195   target_preopen (from_tty);
196
197   /* The exec file is required for symbols. */
198   if (exec_bfd == NULL)
199     error("No kernel exec file specified");
200
201   if (core_kd) {
202     error ("No core file specified."
203            "  (Use `detach' to stop debugging a core file.)");
204     return;
205   }
206
207   if (!filename) {
208     error ("No core file specified.");
209     return;
210   }
211
212   filename = tilde_expand (filename);
213   if (filename[0] != '/') {
214     cp = concat (current_directory, "/", filename, NULL);
215     free (filename);
216     filename = cp;
217   }
218
219   old_chain = make_cleanup (free, filename);
220
221   kd = kvm_open (bfd_get_filename(exec_bfd), filename, NULL,
222                  kernel_writablecore ? O_RDWR: O_RDONLY, 0);
223   if (kd == NULL) {
224     perror_with_name (filename);
225     return;
226   }
227
228   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
229
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);
234
235   /* Note unpush_target (above) calls kcore_close. */
236   core_kd = kd;
237
238   /* print out the panic string if there is one */
239   if (kvread(ksym_lookup("panicstr"), &addr) == 0 &&
240       addr != 0 && 
241       target_read_memory(addr, buf, sizeof(buf)) == 0) {
242
243     for (cp = buf; cp < &buf[sizeof(buf)] && *cp; cp++)
244       if (!isascii(*cp) || (!isprint(*cp) && !isspace(*cp)))
245         *cp = '?';
246     *cp = '\0';
247     if (buf[0] != '\0')
248       printf_filtered("panic: %s\n", buf);
249   }
250
251   if (!ontop) {
252     warning (
253 "you won't be able to access this core file until you terminate\n\
254 your %s; do ``info files''", target_longname);
255     return;
256   }
257
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);
261 }
262
263 static void
264 kcore_detach (args, from_tty)
265      char *args;
266      int from_tty;
267 {
268   if (args)
269     error ("Too many arguments");
270   unpush_target (&kcore_ops);
271   reinit_frame_cache ();
272   if (from_tty)
273     printf_filtered ("No kernel core file now.\n");
274 }
275
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.  */
279
280 /* We just get all the registers, so we don't use regno.  */
281
282 /* ARGSUSED */
283 static void
284 get_kcore_registers (regno)
285      int regno;
286 {
287
288   /*
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...
293    */
294
295   /*
296    * Zero out register set then fill in the ones we know about.
297    */
298   fetch_kcore_registers (&cur_pcb);
299 }
300
301 static void
302 kcore_files_info (t)
303   struct target_ops *t;
304 {
305   printf_filtered ("\t`%s'\n", core_file);
306 }
307 \f
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).  */
310
311 static int
312 ignore (addr, contents)
313      CORE_ADDR addr;
314      char *contents;
315 {
316   return 0;
317 }
318
319 static int
320 xfer_kmem (memaddr, myaddr, len, write, target)
321   CORE_ADDR memaddr;
322   char *myaddr;
323   int len;
324   int write;
325   struct target_ops *target;
326 {
327   int n;
328
329 #if 0   /* XXX */
330   if (it is a user address)
331     return xfer_umem(memaddr, myaddr, len, write);
332 #endif
333
334   if (core_kd == NULL)
335     return 0;
336
337   if (write)
338     n = kvm_write(core_kd, memaddr, myaddr, len);
339   else
340     n = kvm_read (core_kd, memaddr, myaddr, len) ;
341   if (n < 0) {
342     fprintf_unfiltered (gdb_stderr, "can not access 0x%x, %s\n",
343                         memaddr, kvm_geterr(core_kd));
344     n = 0;
345   }
346
347   return n;
348 }
349
350 #if 0   /* XXX */
351 static int
352 xfer_umem (memaddr, myaddr, len, write)
353   CORE_ADDR memaddr;
354   char *myaddr;
355   int len;
356   int write; /* ignored */
357 {
358   int n;
359   struct proc proc;
360
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) ;
364
365   if (n < 0)
366     return 0;
367   return n;
368 }
369 #endif
370
371 static void
372 set_proc_cmd(arg)
373   char *arg;
374 {
375   CORE_ADDR addr;
376   void *val;
377
378   if (!arg)
379     error_no_arg("proc address for the new context");
380
381   if (core_kd == NULL)
382     error("no kernel core file");
383
384   addr = (CORE_ADDR)parse_and_eval_address(arg);
385
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");
390
391   if (set_context((CORE_ADDR)val))
392     error("invalid proc address");
393 }
394
395 static void
396 set_pcb_cmd(arg)
397   char *arg;
398 {
399   CORE_ADDR addr;
400   void *val;
401
402   if (!arg)
403     error_no_arg("pcb address for the new context");
404
405   if (core_kd == NULL)
406     error("no kernel core file");
407
408   addr = (CORE_ADDR)parse_and_eval_address(arg);
409
410   if (set_context(addr))
411     error("invalid pcb address");
412 }
413
414
415
416 void
417 _initialize_kcorelow()
418 {
419   kcore_ops.to_shortname = "kcore";
420   kcore_ops.to_longname = "Kernel core dump file";
421   kcore_ops.to_doc =
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;
436
437   add_target (&kcore_ops);
438   add_com ("proc", class_obscure, set_proc_cmd, "Set current process context");
439 }