1 /* Live and postmortem kernel debugging functions for FreeBSD.
2 Copyright 1996 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
28 #include <sys/sysctl.h>
29 #include <sys/param.h>
33 #include "frame.h" /* required by inferior.h */
45 #include <vm/vm_param.h>
47 #include <machine/vmparam.h>
48 #include <machine/pcb.h>
49 #include <machine/tss.h>
50 #include <machine/frame.h>
52 #include <machine/globaldata.h>
55 static void kcore_files_info PARAMS ((struct target_ops *));
57 static void kcore_close PARAMS ((int));
59 static void get_kcore_registers PARAMS ((int));
61 static int kcore_xfer_kmem PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
63 static int xfer_umem PARAMS ((CORE_ADDR, char *, int, int));
65 static CORE_ADDR ksym_lookup PARAMS ((const char *));
67 static int read_pcb PARAMS ((int, CORE_ADDR));
69 static struct proc * curProc PARAMS ((void));
71 static int set_proc_context PARAMS ((CORE_ADDR paddr));
73 static void kcore_open PARAMS ((char *filename, int from_tty));
75 static void kcore_detach PARAMS ((char *args, int from_tty));
77 static void set_proc_cmd PARAMS ((char *arg, int from_tty));
79 static void set_cpu_cmd PARAMS ((char *arg, int from_tty));
81 static CORE_ADDR kvtophys PARAMS ((int, CORE_ADDR));
83 static int physrd PARAMS ((int, u_int, char*, int));
85 static int kvm_open PARAMS ((const char *efile, char *cfile, char *sfile,
86 int perm, char *errout));
88 static int kvm_close PARAMS ((int fd));
90 static int kvm_write PARAMS ((int core_kd, CORE_ADDR memaddr,
91 char *myaddr, int len));
93 static int kvm_read PARAMS ((int core_kd, CORE_ADDR memaddr,
94 char *myaddr, int len));
96 static int kvm_uread PARAMS ((int core_kd, struct proc *p,
97 CORE_ADDR memaddr, char *myaddr,
100 static int kernel_core_file_hook PARAMS ((int fd, CORE_ADDR addr,
101 char *buf, int len));
103 static CORE_ADDR kvm_getpcpu PARAMS ((int cfd, int cpuid));
105 static struct kinfo_proc * kvm_getprocs PARAMS ((int cfd, int op,
106 CORE_ADDR proc, int *cnt));
108 extern struct target_ops kcore_ops; /* Forward decl */
110 /* Non-zero means we are debugging a kernel core file */
111 int kernel_debugging = 0;
112 int kernel_writablecore = 0;
114 static char *core_file;
115 static int core_kd = -1;
116 static struct proc *cur_proc;
117 static CORE_ADDR kernel_start;
119 static CORE_ADDR pcpu;
120 #define PCPU_OFFSET(name) \
121 offsetof(struct globaldata, gd_ ## name)
124 * Symbol names of kernel entry points. Use special frames.
126 #define KSYM_TRAP "calltrap"
127 #define KSYM_INTR "Xintr"
128 #define KSYM_FASTINTR "Xfastintr"
129 #define KSYM_SYSCALL "Xsyscall"
132 * Read the "thing" at kernel address 'addr' into the space pointed to
133 * by point. The length of the "thing" is determined by the type of p.
134 * Result is non-zero if transfer fails.
136 #define kvread(addr, p) \
137 (target_read_memory ((CORE_ADDR)(addr), (char *)(p), sizeof(*(p))))
142 * The following is FreeBSD-specific hackery to decode special frames
143 * and elide the assembly-language stub. This could be made faster by
144 * defining a frame_type field in the machine-dependent frame information,
145 * but we don't think that's too important right now.
147 enum frametype { tf_normal, tf_trap, tf_interrupt, tf_syscall };
150 fbsd_kern_frame_saved_pc (fr)
151 struct frame_info *fr;
153 struct minimal_symbol *sym;
154 CORE_ADDR this_saved_pc;
155 enum frametype frametype;
157 this_saved_pc = read_memory_integer (fr->frame + 4, 4);
158 sym = lookup_minimal_symbol_by_pc (this_saved_pc);
159 frametype = tf_normal;
161 if (strcmp (SYMBOL_NAME(sym), KSYM_TRAP) == 0)
163 else if (strncmp (SYMBOL_NAME(sym), KSYM_INTR,
164 strlen(KSYM_INTR)) == 0 || strncmp (SYMBOL_NAME(sym),
165 KSYM_FASTINTR, strlen(KSYM_FASTINTR)) == 0)
166 frametype = tf_interrupt;
167 else if (strcmp (SYMBOL_NAME(sym), KSYM_SYSCALL) == 0)
168 frametype = tf_syscall;
173 return (this_saved_pc);
175 #define oEIP offsetof(struct trapframe, tf_eip)
178 return (read_memory_integer (fr->frame + 8 + oEIP, 4));
181 return (read_memory_integer (fr->frame + 12 + oEIP, 4));
184 return (read_memory_integer (fr->frame + 8 + oEIP, 4));
193 struct minimal_symbol *sym;
195 sym = lookup_minimal_symbol (name, NULL, NULL);
197 error ("kernel symbol `%s' not found.", name);
199 return SYMBOL_VALUE_ADDRESS (sym);
207 CORE_ADDR addr = pcpu + PCPU_OFFSET (curthread);
209 if (kvread (addr, &td))
210 error ("cannot read thread pointer at %x\n", addr);
211 addr = (CORE_ADDR)td + offsetof(struct thread, td_proc);
212 if (kvread (addr, &p))
213 error ("cannot read proc pointer at %x\n", addr);
218 * Set the process context to that of the proc structure at
219 * system address paddr.
222 set_proc_context (paddr)
227 if (paddr < kernel_start)
230 cur_proc = (struct proc *)paddr;
232 set_kernel_boundaries (cur_proc);
235 /* Fetch all registers from core file */
236 target_fetch_registers (-1);
238 /* Now, set up the frame cache, and print the top of stack */
239 flush_cached_frames ();
240 set_current_frame (create_new_frame (read_fp (), read_pc ()));
241 select_frame (get_current_frame (), 0);
245 /* Discard all vestiges of any previous core file
246 and mark data and stack spaces as empty. */
250 kcore_close (quitting)
253 inferior_pid = 0; /* Avoid confusion from thread stuff */
264 /* This routine opens and sets up the core file bfd */
267 kcore_open (filename, from_tty)
272 struct cleanup *old_chain;
278 target_preopen (from_tty);
280 unpush_target (&kcore_ops);
285 error ( (core_kd >= 0)?
286 "No core file specified. (Use `detach' to stop debugging a core file.)"
287 : "No core file specified.");
290 filename = tilde_expand (filename);
291 if (filename[0] != '/')
293 cp = concat (current_directory, "/", filename, NULL);
298 old_chain = make_cleanup (free, filename);
301 * gdb doesn't really do anything if the exec-file couldn't
302 * be opened (in that case exec_bfd is NULL). Usually that's
303 * no big deal, but kvm_open needs the exec-file's name,
304 * which results in dereferencing a NULL pointer, a real NO-NO !
305 * So, check here if the open of the exec-file succeeded.
307 if (exec_bfd == NULL) /* the open failed */
308 error ("kgdb could not open the exec-file, please check the name you used !");
310 core_kd = kvm_open (exec_bfd->filename, filename, NULL,
311 kernel_writablecore? O_RDWR : O_RDONLY, "kgdb: ");
313 perror_with_name (filename);
315 /* Looks semi-reasonable. Toss the old core file and work on the new. */
317 discard_cleanups (old_chain); /* Don't free filename any more */
318 core_file = filename;
319 ontop = !push_target (&kcore_ops);
321 kernel_start = bfd_get_start_address (exec_bfd); /* XXX */
323 /* print out the panic string if there is one */
324 if (kvread (ksym_lookup ("panicstr"), &addr) == 0
326 && target_read_memory (addr, buf, sizeof (buf)) == 0)
328 for (cp = buf; cp < &buf[sizeof (buf)] && *cp; cp++)
329 if (!isascii (*cp) || (!isprint (*cp) && !isspace (*cp)))
333 printf ("panicstr: %s\n", buf);
336 /* Print all the panic messages if possible. */
337 if (symfile_objfile != NULL)
339 printf ("panic messages:\n---\n");
340 snprintf (buf, sizeof buf,
341 "/sbin/dmesg -N %s -M %s | \
342 /usr/bin/awk '/^(panic:|Fatal trap) / { printing = 1 } \
343 { if (printing) print $0 }'",
344 symfile_objfile->name, filename);
352 warning ("you won't be able to access this core file until you terminate\n\
353 your %s; do ``info files''", target_longname);
357 /* we may need this later */
358 cur_proc = (struct proc *)curProc ();
359 /* Now, set up the frame cache, and print the top of stack */
360 flush_cached_frames ();
361 set_current_frame (create_new_frame (read_fp (), read_pc ()));
362 select_frame (get_current_frame (), 0);
363 print_stack_frame (selected_frame, selected_frame_level, 1);
367 kcore_detach (args, from_tty)
372 error ("Too many arguments");
373 unpush_target (&kcore_ops);
374 reinit_frame_cache ();
376 printf_filtered ("No kernel core file now.\n");
379 /* Get the registers out of a core file. This is the machine-
380 independent part. Fetch_core_registers is the machine-dependent
381 part, typically implemented in the xm-file for each architecture. */
383 /* We just get all the registers, so we don't use regno. */
386 get_kcore_registers (regno)
391 /* find the pcb for the current process */
392 if (cur_proc == NULL || kvread (&cur_proc->p_thread.td_pcb, &pcbaddr)) /* XXXKSE */
393 error ("cannot read u area ptr for proc at %#x", cur_proc);
394 if (read_pcb (core_kd, (CORE_ADDR)pcbaddr) < 0)
395 error ("cannot read pcb at %#x", pcbaddr);
400 struct target_ops *t;
402 printf ("\t`%s'\n", core_file);
406 kcore_xfer_kmem (memaddr, myaddr, len, write, target)
411 struct target_ops *target;
416 if (memaddr >= (CORE_ADDR)VM_MAXUSER_ADDRESS)
420 nu = xfer_umem (memaddr, myaddr, len, write);
426 if (memaddr != (CORE_ADDR)VM_MAXUSER_ADDRESS)
432 ns = (write ? kvm_write : kvm_read) (core_kd, memaddr, myaddr, len);
440 xfer_umem (memaddr, myaddr, len, write)
444 int write; /* ignored */
449 if (cur_proc == NULL || kvread (cur_proc, &proc))
450 error ("cannot read proc at %#x", cur_proc);
451 n = kvm_uread (core_kd, &proc, memaddr, myaddr, len) ;
461 static CORE_ADDR kernbase;
462 struct minimal_symbol *sym;
466 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
470 kernbase = SYMBOL_VALUE_ADDRESS (sym);
476 #define KERNOFF (ksym_kernbase())
477 #define INKERNEL(x) ((x) >= KERNOFF)
479 static CORE_ADDR sbr;
480 static CORE_ADDR curpcb;
481 static int found_pcb;
484 static struct pcb pcb;
487 set_proc_cmd (arg, from_tty)
492 struct kinfo_proc *kp;
496 error_no_arg ("proc address for new current process");
497 if (!kernel_debugging)
498 error ("not debugging kernel");
500 paddr = (CORE_ADDR)parse_and_eval_address (arg);
501 /* assume it's a proc pointer if it's in the kernel */
502 if (paddr >= kernel_start) {
503 if (set_proc_context(paddr))
504 error("invalid proc address");
506 kp = kvm_getprocs(core_kd, KERN_PROC_PID, paddr, &cnt);
508 error("invalid pid");
509 if (set_proc_context((CORE_ADDR)kp->ki_paddr))
510 error("invalid proc address");
515 set_cpu_cmd (arg, from_tty)
521 struct kinfo_proc *kp;
525 error_no_arg ("cpu number");
526 if (!kernel_debugging)
527 error ("not debugging kernel");
530 cpu = (int)parse_and_eval_address (arg);
531 if ((pcaddr = kvm_getpcpu (cfd, cpu)) == NULL)
532 error ("cpu number out of range");
535 curpcb = kvtophys(cfd, pcpu + PCPU_OFFSET (curpcb));
536 physrd (cfd, curpcb, (char*)&curpcb, sizeof curpcb);
539 paddr = ksym_lookup ("dumppcb") - KERNOFF;
541 paddr = kvtophys (cfd, curpcb);
542 read_pcb (cfd, paddr);
543 printf ("initial pcb at %lx\n", (unsigned long)paddr);
545 if ((cur_proc = curProc()))
546 target_fetch_registers (-1);
548 /* Now, set up the frame cache, and print the top of stack */
549 flush_cached_frames ();
550 set_current_frame (create_new_frame (read_fp (), read_pc ()));
551 select_frame (get_current_frame (), 0);
552 print_stack_frame (selected_frame, selected_frame_level, 1);
555 /* substitutes for the stuff in libkvm which doesn't work */
556 /* most of this was taken from the old kgdb */
558 /* we don't need all this stuff, but the call should look the same */
561 kvm_open (efile, cfile, sfile, perm, errout)
564 char *sfile; /* makes this kvm_open more compatible to the one in libkvm */
566 char *errout; /* makes this kvm_open more compatible to the one in libkvm */
572 if ((cfd = open (cfile, perm, 0)) < 0)
575 if ((pcpu = kvm_getpcpu (cfd, 0)) == NULL)
579 if ((stb.st_mode & S_IFMT) == S_IFCHR
580 && stb.st_rdev == makedev (2, 0))
583 kfd = open (_PATH_KMEM, perm, 0);
586 physrd (cfd, ksym_lookup ("IdlePTD") - KERNOFF, (char*)&sbr, sizeof sbr);
587 printf ("IdlePTD %lu\n", (unsigned long)sbr);
588 curpcb = kvtophys(cfd, pcpu + PCPU_OFFSET (curpcb));
589 physrd (cfd, curpcb, (char*)&curpcb, sizeof curpcb);
591 found_pcb = 1; /* for vtophys */
593 paddr = ksym_lookup ("dumppcb") - KERNOFF;
595 paddr = kvtophys (cfd, curpcb);
596 read_pcb (cfd, paddr);
597 printf ("initial pcb at %lx\n", (unsigned long)paddr);
610 kvm_write (core_kd, memaddr, myaddr, len)
622 * Just like kvm_read, only we write.
625 if (lseek (kfd, (off_t)memaddr, 0) < 0
628 error ("kvm_write:invalid address (%x)", memaddr);
631 cc = write (kfd, myaddr, len);
634 error ("kvm_write:write failed");
638 error ("kvm_write:short write");
646 printf ("kvm_write not implemented for dead kernels\n");
653 kvm_read (core_kd, memaddr, myaddr, len)
658 return (kernel_core_file_hook (core_kd, memaddr, myaddr, len));
662 kvm_uread (core_kd, p, memaddr, myaddr, len)
664 register struct proc *p;
670 char procfile[MAXPATHLEN];
676 sprintf (procfile, "/proc/%d/mem", p->p_pid);
677 fd = open (procfile, O_RDONLY, 0);
680 error ("cannot open %s", procfile);
689 if (lseek (fd, (off_t)memaddr, 0) == -1 && errno != 0)
691 error ("invalid address (%x) in %s", memaddr, procfile);
694 amount = read (fd, cp, len);
697 error ("error reading %s", procfile);
702 error ("EOF reading %s", procfile);
711 return ((ssize_t) (cp - myaddr));
714 return (kernel_core_file_hook (core_kd, memaddr, myaddr, len));
717 static struct kinfo_proc kp;
720 * try to do what kvm_proclist in libkvm would do
723 kvm_proclist (cfd, pid, p, cnt)
729 for (; p != NULL; p = LIST_NEXT(&lp, p_list)) {
730 if (!kvm_read(cfd, (CORE_ADDR)p, (char *)&lp, sizeof (lp)))
743 * try to do what kvm_deadprocs in libkvm would do
745 static struct kinfo_proc *
746 kvm_deadprocs (cfd, pid, cnt)
749 CORE_ADDR allproc, zombproc;
752 allproc = ksym_lookup("allproc");
753 if (kvm_read(cfd, allproc, (char *)&p, sizeof (p)) == 0)
755 kvm_proclist (cfd, pid, p, cnt);
757 zombproc = ksym_lookup("zombproc");
758 if (kvm_read(cfd, zombproc, (char *)&p, sizeof (p)) == 0)
760 kvm_proclist (cfd, pid, p, cnt);
766 kvm_getpcpu (cfd, cpuid)
769 SLIST_HEAD(, globaldata) pcpu_head;
770 struct globaldata lgd;
771 struct globaldata *gd;
773 physrd (cfd, ksym_lookup ("cpuhead") - KERNOFF, (char*)&pcpu_head,
775 gd = SLIST_FIRST (&pcpu_head);
776 for (; gd != NULL; gd = SLIST_NEXT (&lgd, gd_allcpu))
778 kvm_read (cfd, (CORE_ADDR)gd, (char*)&lgd, sizeof lgd);
779 if (lgd.gd_cpuid == cpuid)
783 return ((CORE_ADDR)gd);
787 * try to do what kvm_getprocs in libkvm would do
789 static struct kinfo_proc *
790 kvm_getprocs (cfd, op, proc, cnt)
797 /* assume it's a pid */
798 if (devmem) { /* "live" kernel, use sysctl */
801 mib[2] = KERN_PROC_PID;
804 if (sysctl (mib, 4, &kp, &size, NULL, 0) < 0) {
815 return (kvm_deadprocs (cfd, (int)proc, cnt));
819 physrd (cfd, addr, dat, len)
825 if (lseek (cfd, (off_t)addr, L_SET) == -1)
827 return (read (cfd, dat, len));
837 static CORE_ADDR PTD = -1;
838 CORE_ADDR current_ptd;
841 * We may no longer have a linear system page table...
843 * Here's the scoop. IdlePTD contains the physical address
844 * of a page table directory that always maps the kernel.
845 * IdlePTD is in memory that is mapped 1-to-1, so we can
846 * find it easily given its 'virtual' address from ksym_lookup().
847 * For hysterical reasons, the value of IdlePTD is stored in sbr.
849 * To look up a kernel address, we first convert it to a 1st-level
850 * address and look it up in IdlePTD. This gives us the physical
851 * address of a page table page; we extract the 2nd-level part of
852 * VA and read the 2nd-level pte. Finally, we add the offset part
853 * of the VA into the physical address from the pte and return it.
855 * User addresses are a little more complicated. If we don't have
856 * a current PCB from read_pcb(), we use PTD, which is the (fixed)
857 * virtual address of the current ptd. Since it's NOT in 1-to-1
858 * kernel space, we must look it up using IdlePTD. If we do have
859 * a pcb, we get the ptd from pcb_ptd.
864 else if (found_pcb == 0)
867 PTD = kvtophys (fd, ksym_lookup ("PTD"));
871 current_ptd = pcb.pcb_cr3;
874 * Read the first-level page table (ptd).
876 v = current_ptd + ( (unsigned)addr >> PDRSHIFT) * sizeof pte;
877 if (physrd (fd, v, (char *)&pte, sizeof pte) < 0 || (pte&PG_V) == 0)
883 * No second-level page table; ptd describes one 4MB page.
884 * (We assume that the kernel wouldn't set PG_PS without enabling
885 * it cr0, and that the kernel doesn't support 36-bit physical
888 #define PAGE4M_MASK (NBPDR - 1)
889 #define PG_FRAME4M (~PAGE4M_MASK)
890 addr = (pte & PG_FRAME4M) + (addr & PAGE4M_MASK);
895 * Read the second-level page table.
897 v = (pte&PG_FRAME) + ((addr >> PAGE_SHIFT)&(NPTEPG-1)) * sizeof pte;
898 if (physrd (fd, v, (char *) &pte, sizeof (pte)) < 0 || (pte&PG_V) == 0)
901 addr = (pte & PG_FRAME) + (addr & PAGE_MASK);
904 printf ("vtophys (%x) -> %x\n", oldaddr, addr);
916 CORE_ADDR nuaddr = uaddr;
918 /* need this for the `proc' command to work */
920 nuaddr = kvtophys(fd, uaddr);
922 if (physrd (fd, nuaddr, (char *)&pcb, sizeof pcb) < 0)
924 error ("cannot read pcb at %x\n", uaddr);
929 * get the register values out of the sys pcb and
930 * store them where `read_register' will find them.
933 * XXX many registers aren't available.
934 * XXX for the non-core case, the registers are stale - they are for
935 * the last context switch to the debugger.
936 * XXX gcc's register numbers aren't all #defined in tm-i386.h.
939 for (i = 0; i < 3; ++i) /* eax,ecx,edx */
940 supply_register (i, (char *)&noreg);
941 supply_register (3, (char *)&pcb.pcb_ebx);
942 supply_register (SP_REGNUM, (char *)&pcb.pcb_esp);
943 supply_register (FP_REGNUM, (char *)&pcb.pcb_ebp);
944 supply_register (6, (char *)&pcb.pcb_esi);
945 supply_register (7, (char *)&pcb.pcb_edi);
946 supply_register (PC_REGNUM, (char *)&pcb.pcb_eip);
947 for (i = 9; i < 14; ++i) /* eflags, cs, ss, ds, es, fs */
948 supply_register (i, (char *)&noreg);
949 supply_register (15, (char *)&pcb.pcb_gs);
951 /* XXX 80387 registers? */
955 * read len bytes from kernel virtual address 'addr' into local
956 * buffer 'buf'. Return numbert of bytes if read ok, 0 otherwise. On read
957 * errors, portion of buffer not read is zeroed.
961 kernel_core_file_hook (fd, addr, buf, len)
976 paddr = kvtophys (fd, addr);
979 memset (buf, '\000', len);
982 /* we can't read across a page boundary */
983 i = min (len, PAGE_SIZE - (addr & PAGE_MASK));
984 if ( (cc = physrd (fd, paddr, cp, i)) <= 0)
986 memset (cp, '\000', len);
996 static struct target_ops kcore_ops;
999 _initialize_kcorelow()
1001 kcore_ops.to_shortname = "kcore";
1002 kcore_ops.to_longname = "Kernel core dump file";
1004 "Use a core file as a target. Specify the filename of the core file.";
1005 kcore_ops.to_open = kcore_open;
1006 kcore_ops.to_close = kcore_close;
1007 kcore_ops.to_attach = find_default_attach;
1008 kcore_ops.to_detach = kcore_detach;
1009 kcore_ops.to_fetch_registers = get_kcore_registers;
1010 kcore_ops.to_xfer_memory = kcore_xfer_kmem;
1011 kcore_ops.to_files_info = kcore_files_info;
1012 kcore_ops.to_create_inferior = find_default_create_inferior;
1013 kcore_ops.to_stratum = kcore_stratum;
1014 kcore_ops.to_has_memory = 1;
1015 kcore_ops.to_has_stack = 1;
1016 kcore_ops.to_has_registers = 1;
1017 kcore_ops.to_magic = OPS_MAGIC;
1019 add_target (&kcore_ops);
1020 add_com ("proc", class_obscure, set_proc_cmd, "Set current process context");
1021 add_com ("cpu", class_obscure, set_cpu_cmd, "Set current cpu");