Put in remaining pages and wiki contents.
[ikiwiki.git] / docs / user / DebugKernelCrashDumps.mdwn
1 # Debug the DragonFly kernel \r
2 \r
3 This chapter should give you an introduction how to obtain a crash dump after a kernel panic and how to extract useful information for the developers out of the dump.\r
4 \r
5 [[!toc levels=3 ]]
6 *** Contributed by Matthias Schmidt***\r
7 \r
8 ## Configure your system \r
9 Normally a crash dump is saved in your swap partition after a crash.  If you reboot your machine the next time the dump is extracted by [savecore(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#savecore&section8) from the partition and stored into `/var/crash`.  Due to the fact that `/var` is a relatively small partition it could be possible that the dump isn't saved, because the dump size is larger than the remaining size.\r
10 \r
11 To circumvent this problem you can change the default settings in `/etc/rc.conf`:\r
12 \r
13 [[!table  data="""
14 | `dumpdev` | Indicates the device (usually a swap partition) to which a crash dump should be written in the event of a system crash. 
15  `dumpdir` | savecore(8) will save that crash dump and a copy of the kernel to the directory specified by the dumpdir variable.  The default value is /var/crash.  You can set this to another directory on another partition with more space available to safely obtain the dump. |\r
16 """]]\r
17 If you are unsure about your swap partition device, use [swapinfo(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#swapinfo&section8) or look into `/etc/fstab` :\r
18     \r
19     # swapinfo \r
20     Device          1K-blocks     Used    Avail Capacity  Type\r
21     /dev/ad0s1b       1048448        0  1048448     0%    Interleaved\r
22     \r
23     # cat /etc/fstab | grep swap\r
24     /dev/ad0s1b             none            swap    sw              0       0\r
25 \r
26 \r
27 ### Enable debugging options in your kernel config \r
28 \r
29 If you don't know how to configure a custom kernel, look into [Configuring the DragonFly Kernel](kernelconfig.html).  You have to add the following lines to compile your kernel with debugging symbols:\r
30     \r
31     makeoptions     DEBUG=-g                #Build kernel with gdb(1) debug symbols\r
32 \r
33 If you want additional support for the interactive kernel debugger [ddb(4)](http://leaf.dragonflybsd.org/cgi/web-man?command#ddb&section4) and invariant debugging, also add these lines:\r
34     \r
35     # Debugging for Development\r
36     options         DDB\r
37     options         DDB_TRACE\r
38     options         INVARIANTS\r
39 \r
40 ## How does a crash look like? \r
41 \r
42 Easy answer:  Your system stopped working.  Complicated one:  Your system occurred a [panic(9)](http://leaf.dragonflybsd.org/cgi/web-man?command#panic&section9) and drops into [ddb(4)](http://leaf.dragonflybsd.org/cgi/web-man?command=ddb&section=4), the interactive kernel debugger.\r
43 The output while seeing a crash might look this:\r
44     \r
45     Fatal trap 12: page fault while in kernel mode\r
46     fault virtual address   = 0xd0686f55\r
47     fault code              = supervisor read, page not present\r
48     instruction pointer     = 0x8:0xc02ddb9a\r
49     stack pointer           = 0x10:0xcec0fb18\r
50     frame pointer           = 0x10:0xcec0fb18\r
51     code segment            = base 0x0, limit 0xfffff, type 0x1b\r
52                             = DPL 0, pres 1, def32 1, gran 1\r
53     processor eflags        # interrupt enabled, resume, IOPL  0\r
54     current process         = 50725 (sysctl)\r
55     current thread          = pri 6\r
56                                                                                                           \r
57     panic: from debugger\r
58 \r
59 Before your machine reboots a crash dump is saved into your swap partition (if you have one and don't disabled crash dumps).  Writing the dump to disk takes some time depending on your machine and the amount of RAM installed.  This might look like this:\r
60     \r
61     dumping to dev #ad/0x20001, blockno 1049088\r
62     dump 511 510 509 508 507 506 505 \r
63     [...]\r
64     26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 succeeded\r
65 \r
66 Now your machine reboots, checks its file system and finally extracts the crash dump from the swap partition to your `dumpdir` (see `rc.conf` setting above).  If your `/var` partition is to small, you'll see an error similar to the following:\r
67     \r
68     savecore: reboot after panic: from debugger\r
69     savecore: no dump, not enough free space on device (231420 available, need 541840)\r
70 \r
71 If this happens, you have to extract the crash dump yourselves.  See next Section how to do this.\r
72 ### Extract a crash dump manually \r
73 You can use [savecore(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#savecore&section8) to copy your currently running kernel and the associated crash dump to a particular directory you have to specify (we use `/usr/crash` here in the example):\r
74     \r
75     # mkdir -p /usr/crash\r
76     # chmod 700 /usr/crash\r
77     # savecore /usr/crash/\r
78     [...]\r
79 \r
80 This will take some time dependent on the speed of your machine.  See the man page of savecore(8) for more available options.\r
81 ### Upload the crash dump \r
82 If you don't have the ability or skills to debug are crash yourselves, please upload the complete content of your crash directory to a public available location (HTTP, FTP web space or your leaf account) and send a detailed bug report to the bugs@dragonflybsd.org list.  If its possible please tar and compress (gzip, bzip2) the directory to save disk space and bandwith.\r
83 \r
84 ## Debug the crash dump with kgdb \r
85 The [kgdb(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#kgdb&section1) utility is a debugger based on gdb(1) that allows debugging of kernel core files.\r
86 ### kgdb extesions \r
87 To get some handy helper command execute the following command before starting kgdb:\r
88     \r
89     source /usr/src/test/debug/gdb.kernel\r
90 \r
91 This gives you several new commands like kldstat (displays all loaded modules) or psx (displays all running processes).\r
92 Start kgdb as follows:\r
93     \r
94     # cd /usr/crash\r
95     # ls -l\r
96     -rw-r--r--  1 root  wheel     2B Jan  7 17:07 bounds\r
97     -rw-r--r--  1 root  wheel    17M Jan  7 17:08 kernel.0\r
98     -rw-------  1 root  wheel   512M Jan  7 17:08 vmcore.0\r
99     # kgdb kernel.0 vmcore.0\r
100 \r
101 kgdb(1) will show you the panic message after start.  The first thing to do is to obtain a ***backtrace*** with the ***bt*** command:\r
102     \r
103     Unread portion of the kernel message buffer:\r
104     \r
105     \r
106     Fatal trap 12: page fault while in kernel mode\r
107     fault virtual address   = 0xd0686f55\r
108     fault code              = supervisor read, page not present\r
109     instruction pointer     = 0x8:0xc02ddb9a\r
110     stack pointer           = 0x10:0xcec0fb18\r
111     frame pointer           = 0x10:0xcec0fb18\r
112     code segment            = base 0x0, limit 0xfffff, type 0x1b\r
113     current process         = 50725 (sysctl)\r
114     current thread          = pri 6 \r
115     \r
116     panic: from debugger\r
117     \r
118     \r
119     Fatal trap 3: breakpoint instruction fault while in kernel mode\r
120     instruction pointer     = 0x8:0xc03136a4\r
121     stack pointer           = 0x10:0xcec0f92c\r
122     frame pointer           = 0x10:0xcec0f934\r
123     code segment            = base 0x0, limit 0xfffff, type 0x1b\r
124                             = DPL 0, pres 1, def32 1, gran 1\r
125     processor eflags        # interrupt enabled, IOPL  0\r
126     current process         = 50725 (sysctl)\r
127     current thread          = pri 6 \r
128     \r
129     panic: from debugger\r
130     Uptime: 3h57m22s\r
131     \r
132     dumping to dev #ad/0x20001, blockno 1049088\r
133     dump 511 510 509 508 507 506 505 504 503 502 501 500 499 498 \r
134     [...]\r
135     40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 \r
136     \r
137     GNU gdb 6.2.1\r
138     Copyright 2004 Free Software Foundation, Inc.\r
139     GDB is free software, covered by the GNU General Public License, and you are\r
140     welcome to change it and/or distribute copies of it under certain conditions.\r
141     Type "show copying" to see the conditions.\r
142     There is absolutely no warranty for GDB.  Type "show warranty" for details.\r
143     This GDB was configured as "i386-dragonfly".\r
144     (kgdb) bt\r
145     #0  dumpsys () at thread.h:83\r
146     #1  0xc01c4e1b in boot (howto=260) at /usr/src/sys/kern/kern_shutdown.c:375\r
147     #2  0xc01c4f3c in panic (fmt=Variable "fmt" is not available.\r
148     ) at /usr/src/sys/kern/kern_shutdown.c:800\r
149     #3  0xc0149be5 in db_panic (addr=Could not find the frame base for "db_panic".\r
150     ) at /usr/src/sys/ddb/db_command.c:447\r
151     #4  0xc014a250 in db_command_loop () at /usr/src/sys/ddb/db_command.c:343\r
152     #5  0xc014c7bc in db_trap (type#12, code0) at /usr/src/sys/ddb/db_trap.c:71\r
153     #6  0xc03137f7 in kdb_trap (type#12, code0, regs=0xcec0fad0) at /usr/src/sys/platform/pc32/i386/db_interface.c:148\r
154     #7  0xc032384b in trap_fatal (frame#0xcec0fad0, evaVariable "eva" is not available.\r
155     ) at /usr/src/sys/platform/pc32/i386/trap.c:1091\r
156     #8  0xc03239b0 in trap_pfault (frame#0xcec0fad0, usermode0, eva=3496505173)\r
157         at /usr/src/sys/platform/pc32/i386/trap.c:997\r
158     #9  0xc03241a0 in trap (frame=0xcec0fad0) at /usr/src/sys/platform/pc32/i386/trap.c:680\r
159     #10 0xc0314506 in calltrap () at /usr/src/sys/platform/pc32/i386/exception.s:783\r
160     #11 0xc02ddb9a in strlen (str=0xd0686f55 <Address 0xd0686f55 out of bounds>) at /usr/src/sys/libkern/strlen.c:41\r
161     #12 0xc02c2153 in sysctl_vm_zone (oidp#0xc03b42a0, arg10x0, arg2=0, req=0xcec0fc08) at /usr/src/sys/vm/vm_zone.c:447\r
162     #13 0xc01cf935 in sysctl_root (oidp=Variable "oidp" is not available.\r
163     ) at /usr/src/sys/kern/kern_sysctl.c:1193\r
164     #14 0xc01cfa27 in userland_sysctl (name#0xcec0fc90, namelen2, old=0x0, oldlenp=0xbfbfe8f0, inkernel=0, new=0x0, \r
165         newlen#0, retval0xcec0fc8c) at /usr/src/sys/kern/kern_sysctl.c:1268\r
166     #15 0xc01cfc28 in sys___sysctl (uap=0xcec0fcf0) at /usr/src/sys/kern/kern_sysctl.c:1211\r
167     #16 0xc0323ccb in syscall2 (frame=0xcec0fd40) at /usr/src/sys/platform/pc32/i386/trap.c:1339\r
168     #17 0xc03145a5 in Xint0x80_syscall () at /usr/src/sys/platform/pc32/i386/exception.s:872\r
169     #18 0x08055d38 in ?? ()\r
170     #19 0xbfbfe86c in ?? ()\r
171     #20 0x0000002f in ?? ()\r
172     #21 0x00000000 in ?? ()\r
173     #22 0x00000000 in ?? ()\r
174     #23 0x00000000 in ?? ()\r
175     #24 0x00000000 in ?? ()\r
176     #25 0x13c4b000 in ?? ()\r
177     #26 0x00000001 in ?? ()\r
178     #27 0xc03c2bf8 in intr_info_ary ()\r
179     #28 0xcec0f8d4 in ?? ()\r
180     #29 0xcec0f8c4 in ?? ()\r
181     #30 0xc8076300 in ?? ()\r
182     #31 0xc01cac5a in lwkt_preempt (ntd#0x2, critpriCannot access memory at address 0xbfbfe8a4\r
183     ) at /usr/src/sys/kern/lwkt_thread.c:893\r
184     Previous frame inner to this frame (corrupt stack?)\r
185 \r
186 kgdb(1) gives you the ability to look into specific frames, display variable content and obtain the source code (if your kernel was compiled with -g):\r
187     \r
188     (kgdb) f 13\r
189     #13 0xc01cf935 in sysctl_root (oidp=Variable "oidp" is not available.\r
190     ) at /usr/src/sys/kern/kern_sysctl.c:1193\r
191     1193                    error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,\r
192     (kgdb) l\r
193     1188\r
194     1189            if ((oid->oid_kind & CTLTYPE) # CTLTYPE_NODE)\r
195     1190                    error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,\r
196     1191                        req);\r
197     1192            else\r
198     1193                    error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,\r
199     1194                        req);\r
200     1195            return (error);\r
201     1196    }\r
202     1197\r
203     (kgdb) p *oid\r
204     $1 # {oid_parent  0xc03cbda8, oid_link = {sle_next = 0x0}, oid_number = 283, oid_kind = -2147483645, oid_arg1 = 0x0, \r
205       oid_arg2 # 0, oid_name  0xc03616ad "zone", oid_handler = 0xc02c20fa <sysctl_vm_zone>, oid_fmt = 0xc036a56f "A", \r
206       oid_refcnt # 0, oid_descr  0xc036906a "Zone Info"}\r
207 \r
208 ## Further Information \r
209 To get more information about how to use a debugger, look here:\r
210
211 * [Man page of kgdb(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#kgdb&section1)\r
212
213 * [Man page of gdb(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#gdb&section1)\r
214
215 * [How to retrieve symbols from kernel modules](http://leaf.dragonflybsd.org/mailarchive/kernel/2005-11/msg00065.html)\r
216
217 * [FreeBSD Developers Handbook](http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/book.html#KERNELDEBUG)\r
218
219 * [GDB Manual](http://sourceware.org/gdb/documentation/)\r
220
221 * [Debug tutorial from Greg Lehey](http://www.lemis.com/grog/Papers/Debug-tutorial/tutorial.pdf)\r