3 <title>The NewOS Operating System: Docs
</title>
5 <body BGCOLOR=
"#FFFFFF">
7 <h1 align=
"center">How-to debug kernel crashes with GDB
</h1>
13 <table border=
"0" width=
"100%" bgcolor=
"#E8E8E8">
15 <td width=
"100%" height=
"100%" valign=
"top" align=
"left">
16 If you are hacking the Kernel and experiencing Kernel Crashes,
17 the kernel now supports some minimal remote debugging capabilities
18 to hook into GDB. Here are some simple indications.
25 <table border=
"0" width=
"100%" bgcolor=
"#E8E8E8">
27 <td width=
"100%" height=
"100%" valign=
"top" align=
"left">
28 <center><h3>What you need
</h3></center>
30 <li> A GDB enabled machine and OS (I use FreeBSD myself, but any
31 Linux or even Windows+CygWin will do.)
</li>
33 <li> A null modem cable. If you are hacking the kernel you probably
34 already have one for the serial debug output.
</li>
36 <li> A kernel file with symbols. In the root makefile edit the lines
37 for GLOBAL_CFLAGS and GLOBAL_LDFLAGS and add -g to them. Rebuild the kernel.
<br><br>
38 <b>*You dont need to boot off a kernel with symbols*
</b><br>
39 The kernel with symbols is only required in the
40 host running GDB. However, it is important that
41 both kernels (the running one and the one in the
42 GDB host) perfectly match each other: this means
43 they must be compiled from the same source tree
44 with the same compile options (except for the -g).
46 <li> A kernel crash to analyse.
</li>
54 <table border=
"0" width=
"100%" bgcolor=
"#E8E8E8">
56 <td width=
"100%" height=
"100%" valign=
"top" align=
"left">
57 <center><h3>How to debug
</h3></center>
59 <li> once the kernel has crashed, close the terminal emulator (if you are using one)
60 used for serial debug output.
</li>
62 <li> launch GDB specifying the path to kernel file as argument.
</li>
64 <li> in GDB type the following commands:
66 set remotebaud XXXX
<br>
67 target remote YYYY
<br>
69 XXXX is the baud rate your kernel is using for
70 serial debug output (
115200 by default, unless
71 you changed it). YYYY is the device path to
72 the serial port used for debug output.
<br><br>
74 Once you type the
"target remote YYYY" command
75 you will be debugging your kernel. The kernel
76 debugger will remain in GDB mode until you quit
77 from GDB, at that point it will return to the
78 normal crash prompt.
</li>
86 <table border=
"0" width=
"100%" bgcolor=
"#E8E8E8">
88 <td width=
"100%" height=
"100%" valign=
"top" align=
"left">
89 <center><h3>Notes
</h3></center>
91 <li>Personally I have obtained better results by using
92 57600 as serial port speed. Your mileage may vary
93 depending on your serial ports, the length of your
94 serial cable and the phase of the moon. If you feel
95 that it is sluggish try reducing the serial port
96 speed in your kernel (kernel/arch/i386/ arch_dbg_console.c)
</li>
98 <li>Currently only inspecting the crash is supported,
99 you cannot single step, set breakpoints etc.
</li>
101 <li>For better debugging you may want to have your
102 kernel debugged without optimizations. In the
103 GLOBAL_CFLAGS change the -O to -O0. When compiling
104 with optimizations the compiler may assign some
105 of your local variables to registers, and you
106 will not be able to inspect some values for
107 functions other than the call stack tip.
</li>
109 <li>After the
"target remote" command you may get an eror message similar to:
111 ptrace(PT_GETDBREGS) failed: No such process
113 you can safely ignore it. It's just GDB being silly.
</li>
121 <table border=
"0" width=
"100%" bgcolor=
"#E8E8E8">
123 <td width=
"100%" height=
"100%" valign=
"top" align=
"left">
124 <center><h3>Troubleshooting
</h3></center>
125 If you have troubles, before typing the
"target remote" command. Type the
126 following command:
<br>
127 set remotedebug
1<br>
128 This will enable tracing all (actually most) of the data packets exchanged
129 between GDB and the kernel. Now try to reproduce your troubles and send the
130 traces to the NewOS developer list.
137 <table border=
"0" width=
"100%" bgcolor=
"#E8E8E8">
139 <td width=
"100%" height=
"100%" valign=
"top" align=
"left">
140 <center><h3>Sample debug session
</h3></center>
141 [freston@cairo
~/NewOS/newos]
<br>
142 [freston@cairo
~/NewOS/newos]
gdb
kernel/obj.i386/system
<br>
143 GNU
gdb
4.18<br>
144 Copyright
1998 Free
Software
Foundation,
Inc.
<br>
145 GDB
is
free
software,
covered
by
the
GNU
General
Public
License,
and
you
are
<br>
146 welcome
to
change
it
and/or
distribute
copies
of
it
under
certain
conditions.
<br>
147 Type
"show copying" to
see
the
conditions.
<br>
148 There
is
absolutely
no
warranty
for
GDB.
Type
"show warranty" for
details.
<br>
149 This
GDB
was
configured
as
"i386-unknown-freebsd"...
<br>
150 (gdb)
set
remotebaud
57600<br>
151 (gdb)
target
remote
/dev/cuaa0
<br>
152 Remote
debugging
using
/dev/cuaa0
<br>
153 ptrace(PT_GETDBREGS)
failed:
No
such
process
<br>
154 0x80009132 in
kernel_debugger
()
at
kernel/debug.c:
227<br>
155 227 dbg_save_registers(&(dbg_register_file[smp_get_current_cpu()][
0]));
<br>
156 (gdb)
where
<br>
157 #
0 0x80009132 in
kernel_debugger
()
at
kernel/debug.c:
227<br>
158 #
1 0x800091d5 in
panic
(fmt=
0x80036de0 "Keyboard Requested Halt\n")
<br>
159 at
kernel/debug.c:
257<br>
160 #
2 0x8002bf85 in
handle_keyboard_interrupt
(data=
0x0)
<br>
161 at
kernel/dev/arch/i386/keyboard/keyboard.c:
185<br>
162 #
3 0x80008aca in
int_io_interrupt_handler
(vector=
33)
at
kernel/int.c:
135<br>
163 #
4 0x80025981 in
i386_handle_trap
(frame={gs
=
16,
fs
=
16,
es
=
16,
<br>
164 ds
=
2147745808,
edi
=
0,
esi
=
0,
ebp
=
2147786692,
esp
=
2147786648,
<br>
165 ebx
=
0,
edx
=
39,
ecx
=
1,
eax
=
39,
vector
=
33,
error_code
=
0,
<br>
166 eip
=
2147506764,
cs
=
8,
flags
=
646,
user_esp
=
0,
user_ss
=
0})
<br>
167 at
kernel/arch/i386/arch_int.c:
232<br>
168 #
5 0x8002555c in
int_bottom
()
<br>
169 #
6 0x0 in
??
()
<br>
170 (gdb)
list
<br>
171 222 }
<br>
172 223 }
<br>
174 225 void
kernel_debugger()
<br>
175 226 {
<br>
176 227 dbg_save_registers(&(dbg_register_file[smp_get_current_cpu()][
0]));
<br>
178 229 kernel_debugger_loop();
<br>
179 230 }
<br>
181 (gdb)
up
<br>
182 #
1 0x800091d5 in
panic
(fmt=
0x80036de0 "Keyboard Requested Halt\n")
<br>
183 at
kernel/debug.c:
257<br>
184 257 kernel_debugger();
<br>
185 (gdb)
up
<br>
186 #
2 0x8002bf85 in
handle_keyboard_interrupt
(data=
0x0)
<br>
187 at
kernel/dev/arch/i386/keyboard/keyboard.c:
185<br>
188 185 panic(
"Keyboard Requested Halt\n");
<br>
189 (gdb)
list
<br>
190 180 else
<br>
191 181 leds
|=
LED_NUM;
<br>
192 182 set_leds();
<br>
193 183 break;
<br>
194 184 case
SYSREQ:
<br>
195 185 panic(
"Keyboard Requested Halt\n");
<br>
196 186 break;
<br>
197 187 case
F12:
<br>
198 188 reboot();
<br>
199 189 break;
<br>
200 (gdb)
print
leds
<br>
201 $
1 =
1<br>
202 (gdb)
up
<br>
203 #
3 0x80008aca in
int_io_interrupt_handler
(vector=
33)
at
kernel/int.c:
135<br>
204 135 temp_ret
=
io-
>func(io-
>data);
<br>
205 (gdb)
list
<br>
206 130 struct
io_handler
*io;
<br>
207 131 int
temp_ret;
<br>
209 133 io
=
io_handlers[vector];
<br>
210 134 while(io
!=
NULL)
{
<br>
211 135 temp_ret
=
io-
>func(io-
>data);
<br>
212 136 if(temp_ret
==
INT_RESCHEDULE)
<br>
213 137 ret
=
INT_RESCHEDULE;
<br>
214 138 io
=
io-
>next;
<br>
215 139 }
<br>
216 (gdb)
print
vector
<br>
217 $
2 =
33<br>
218 (gdb)
print
io_handlers[vector]
<br>
219 $
3 =
(struct
io_handler
*)
0x8008fb20<br>
220 (gdb)
print
io
<br>
221 $
4 =
(struct
io_handler
*)
0x8008fb20<br>
222 (gdb)
print
*io
<br>
223 $
5 =
{next
=
0x0,
func
=
0x8002be6c <handle_keyboard_interrupt>,
data
=
0x0}
<br>
224 (gdb)
print
*io-
>next
<br>
225 Cannot
access
memory
at
address
0x0.
<br>
226 (gdb)
quit
<br>
227 The
program
is
running.
Exit
anyway?
(y
or
n)
y
<br>
228 [freston@cairo
~/NewOS/newos]
<br>
235 <table border=
"1" width=
"100%" bgcolor=
"#D9D5FF">
237 <td width=
"100%" height=
"49" valign=
"top" align=
"center"><small><font color=
"#000000">This
238 page copyright
2002 </font></small><font size=
"2">Manuel J. Petit
240 Any questions, comments, gripes, whatever:
241 <a href="mailto:geist@newos.org">geist@newos.org</a></font>