GDB: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
m Bot: fixing lint errors, replacing obsolete HTML tags:
 
(17 intermediate revisions by 14 users not shown)
Line 1:
'''GDB''' ishas become the standard debugger on Linux and other free Unices. It is sourcea source-level, debugger rather than machinea machine-level, likeone such as the Bochs debugger; this could be good or bad, depending on your viewpoint.
[[Category:Debuggers]]
'''GDB''' is the standard debugger on Linux and other Unices. It is source level, rather than machine level, like the Bochs debugger; this could be good or bad, depending on your viewpoint.
 
While debugging system code is not its intended use, it does a fine job of it; it can be used directly with some emulators, without modifying your code at all, or it can be used over a serial line. The latter option involves implementing a Remote Serial Protocol stub in your operating system.
 
==Remote Debugging==
We are interested in the remote debugging facility of GDB. From the [http://sourceware.org/gdb/current/onlinedocs/gdb_18gdb/Remote-Debugging.html GDB manual], “If you are trying to debug a program running on a machine that cannot run GDB in the usual way, it is often useful to use remote debugging. For example, you might use remote debugging on an operating system kernel, or on a small system which does not have a general purpose operating system powerful enough to run a full-featured debugger.”
 
For remote debugging, remote stub should be implemented in the remote program that needs debugging. That means, the kernel should contain the remote stub to talk to the host gdb during the debug session. This requires kernel source change and it is must if you are running your kernel on a test(real) machine and debugging from other machine. If you are using an emulator(bochs or qemu), then you can use the gdb-stub complied into the emulator.
 
For remote debugging, a remote stub should be implemented in the remote program that needs debuggingto be debugged. That means, the kernel should contain the remote stub to talk to the host gdbGDB during the debug session. This requires a kernel source change and it is a must if you are running your kernel on a test(real) machine and debugging from otheranother machine. If you are using an emulator (bochsBochs or qemuQEMU), then you can use the gdb-GDB stub compliedcompiled into the emulator.
===Implementing GDB Stub===
To debug (using GDB) a kernel running on a real machine, the kernel needs to contain a GDB stub. For the i386 platform, GDB source includes a reference implementation of gdb-stub.c. It requires the following three functions from the kernel to read/write from a serial port and to setupset up the exception handlers.
 
<syntaxhighlight lang="C">
To debug (using GDB) a kernel running on a real machine, the kernel needs to contain GDB stub. For i386 platform, GDB source includes a reference implementation of gdb-stub.c. It requires the following three functions from kernel to read/write from serial port and to setup the exception handlers.
getDebugChar(); // to read a character from serial port
putDebugChar(int); // to write a character to serial port
exceptionHandler(int exception_number, void *exception_address); // to set exception handler
</syntaxhighlight>
 
Implement the above functions in kernelyour andkernel, include the gdb-stub.c in the compilation, and also call the following functions during kernel start to connect to the gdb host.
<syntaxhighlight lang="C">
 
InitSerialPort(sys_gdb_port, 9600, UART_DATA_BIT_8, UART_PARITY_NONE, UART_STOP_BIT_1); /*set up the serial port*/
set_debug_traps(); /*setup exception handlers*/
kprintf("Waiting for GDB(0x%X) : ", sys_gdb_port );
__asm__("int3"); /*break point exception to sync with GDB host*/
</syntaxhighlight>
 
Now start your test machine and your kernel will wait for a GDB host connection.
 
===Using Emulator Stubs===
'''QemuQEMU''' - GDB can be connected to [[QEMU]] by simply starting qemu with the <tt>-s -S</tt> command line switches.
 
'''Bochs''' - For GDB to be able to interface with [[Bochs]], Bochs will need to be configured with the <tt>--enable-gdb-stub</tt> argument.
The <tt>bochssrc</tt> (or whatever configuration script is being used) will need to have the <tt>gdbstub</tt> line set to something like ''gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0''.
 
==Starting GDB==
After an emulator/kernel is configured it will then wait for a connection from GDB. GDB on the host machine can be started like this:
Line 41 ⟶ 36:
0x0000fff0 in ?? ()
(gdb)
 
If you are debugging a kernel running on a real machine then use ''target remote /dev/tty2'' instead of the network port.
 
If it prompts you after the "<tt>target remote</tt>" line asking whether to kill a program that is already being debugged answer "<tt>n</tt>".
 
If you are using 64a 64-bit kernel, you may need to setupset up the address size using set command.
===gdbinit files===
 
Put commonly-used commands in a .gdbinit file in your working directory. For example:
file kernel.bin
target remote localhost:1234
This will connect to GDB-stub whenever you start GDB and will load symbols from kernel.bin.
===GDB in Emacs===
Emacs is well integrated with GDB. Try using <tt>M-x gdb</tt> and when it asks you for command-line arguments to GDB, use <tt>--annotate=3</tt>. This will allow Emacs to follow along as you place breakpoints and single-step through your kernel.
===Virtual Serial Ports===
If you have gdbstub inside your kernel and run your kernel inside aan emulator, you can use a [http://en.[wikipedia.org/wiki/COM_port_redirector:COM port redirector|COM Port redirector]] to create a virtual serial port. After creating a virtual serailserial port, one port in the pair should be used for the emulator; the other one should specified in the gdb for remote debugging.
 
[http://com0com.sourceforge.net/ com0com] can be used in Windows and a pseudo terminal might be used in Linux to create virtual serial port pairs.
== See Also ==
=== External Links ===
* [http://stackoverflow.com/questions/1471226/most-tricky-useful-commands-for-gdb-debugger StackOverflow: Most tricky / useful commands for GDB debugger]
[[Category:DebuggersDebugging]]
[[Category:Tools]]
[[de:GDB]]