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 |
== Use gdb with QEMU == |
||
You can run |
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 |
...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 |
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 |
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 |
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 |
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 == |