Kernel Debugging: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
Line 57: Line 57:
This is however rather tricky, since it requires additional hardware, and special support coded into your kernel. You might want to read the [http://web.archive.org/web/20070415113206/http://www.kernelhacking.org/docs/kernelhacking-HOWTO/indexs09.html kernel hacking how-to] and (at minimum) [http://sourceware.org/gdb/current/onlinedocs/gdb/Remote-Debugging.html#Remote-Debugging chapter 20 of the GDB manual], and chances are likely that your debugger will introduce even more bugs at first.
This is however rather tricky, since it requires additional hardware, and special support coded into your kernel. You might want to read the [http://web.archive.org/web/20070415113206/http://www.kernelhacking.org/docs/kernelhacking-HOWTO/indexs09.html kernel hacking how-to] and (at minimum) [http://sourceware.org/gdb/current/onlinedocs/gdb/Remote-Debugging.html#Remote-Debugging chapter 20 of the GDB manual], and chances are likely that your debugger will introduce even more bugs at first.


== Use gdb with Qemu ==
== Use gdb with QEMU ==


You can run Qemu to listen for a "gdb connection" before it starts executing any code to debug it.
You can run QEMU to listen for a "GDB connection" before it starts executing any code to debug it.


qemu -s -S <harddrive.img>
qemu -s -S <harddrive.img>


...will setup Qemu to listen on port 1234 and wait for a gdb connection to it. Then, from a remote or local shell:
...will setup QEMU to listen on port 1234 and wait for a GDB connection to it. Then, from a remote or local shell:


gdb
gdb
Line 70: Line 70:
(Replace localhost with remote IP / URL if necessary.) Then start execution:
(Replace localhost with remote IP / URL if necessary.) Then start execution:


But that's not all, you can compile your source code under gcc with debugging symbols using "-g". This will add all the debugging symbols in the kernel image itself (Thus making it bigger ). There is also a way to put all of the debugging information in a separate file using the "objcopy" tool, which is part of the GNU binutils package.
But that's not all, you can compile your source code under GCC with debugging symbols using "-g". This will add all the debugging symbols in the kernel image itself (Thus making it bigger). There is also a way to put all of the debugging information in a separate file using the "objcopy" tool, which is part of the GNU Binutils package.


objcopy --only-keep-debug kernel.elf kernel.sym
objcopy --only-keep-debug kernel.elf kernel.sym
Line 84: Line 84:
To produce a flat binary which can be debugged using the previously extracted debug information
To produce a flat binary which can be debugged using the previously extracted debug information


You can import the symbols in gdb by pointing gdb to the file containing debug information
You can import the symbols in GDB by pointing GDB to the file containing debug information


(gdb) symbol-file kernel.elf ;kernel.elf is the actual unstripped kernel image in this case
(gdb) symbol-file kernel.elf ;kernel.elf is the actual unstripped kernel image in this case


From there, you can see the actual C source code as it runs line per line! (Use the stepi instruction in gdb to execute the code line per line.)
From there, you can see the actual C source code as it runs line per line! (Use the stepi instruction in GDB to execute the code line per line.)


Example :
Example :
Line 115: Line 115:
(gdb) info registers
(gdb) info registers


I won't start explaining all the nice things about gdb, but as you can see, it is a very powerful tool for debugging OSes.
I won't start explaining all the nice things about GDB, but as you can see, it is a very powerful tool for debugging OSes.


== GUI frontends ==
== GUI frontends ==