🍗 Wiki

GDB

GDB

GDB, also known as GNU debugger is a popular debugger, widely used in Linux.

1. Build

1.1. Building GDB in Linux

Obtain the source code of GNU debugger first. Like most of GNU projects, it is not available in Github excepts for unofficial mirror.

Go to the link, and get the copy of GDB by desired version.

2. How to use

2.1. Cross Compiling

The most important part of compiling GDB and GDBserver, is to know the architecture of your target and host. GDB is not only for PC, but also widely used to debug the system while you have to work with embedded systems, that can be routers, IoT devices, or even automotive software.

  • Build: The system architecture where the make command will be run.

  • Target: The architecture the target board or binary will be run.

  • Host: The architecture that the compiled gdb or gdbserver program will be run.

So, if you want to…​

  1. Debug program on MIPS board

  2. …​with your Arm laptop

  3. But you have to build debugger on your x86_64 server (for performance for example)

Then you have to set…​

  1. --target=mips-linux-gnu-gcc

  2. --host=arm-linux-gnueabi

  3. --build=x86_64-unknown-linux-gnu

up on running ./configure script.

The official wiki is clearly describing it.

2.2. GDB

cd gdb-xx.y if you downloaded the source code and unpacked.

./configure command for configuration. If you want to run the debugger to work with another architecture, remember that you have to set flags properly.

make if the configuration is done.

If you want to install it system wide, run make install.

If you do not want some components to be compiled, --disable-foo flags are available. For example, --disable-gas to disable gas the GNU assembler.

2.3. GDB Server

The gdbserver is included in the source code of gdb. If you want to build gdbserver only, you should pass some options.

To me, building the whole gdb takes few minutes, and it is easy to obtain precompiled gdb, so I prefer building the gdbserver only.

cd gdb-xx.y
./configure --disable-gdb
make all-gdbserver

# Optional you know
#make install
cd gdb-xx.y
./configure --disable-gdb LDFLAGS="-static"
make all-gdbserver LDFLAGS="-static"

2.4. With qemu

If you might want to debug binary from exotic architecture, TriCore or mips for example, Qemu is always the option. With -g option, you can easily create a gdb server with emulation thanks to qemu.

$ qemu-ppc -g 31337 sample.bin

This command will open a gdb server backed by qemu, and wait for attach.