Linux – Remote Debugging With GDB

Embedded Linux development is a complex task. You need tools on the target and the host and its important to setup everything so we can build and debug easily.

Let go over the process. You can do it yourself , just build an Embedded Linux system with QEMU

Building For The Target

To build code for the target we need a cross toolchain and to build the code add debugging information:

Lets start with a simple code example:

Compile it for the target with debugging information:

You don’t need the debugging information on the target so let strip the target executable

Copy app4target to the target file system

Debugging with GDB Server

To debug the code we need gdbserver on the target. gdbserver is a simple agent that the real GDB debugger can connect and interact with. We can use networking or serial port to connect the client and the server

on the target run:

gdbserver runs the executable and suspend it

on the host run cross GDB debugger and load the version with the debugging information

On GDB session connect to the target, set a breakpoint in the main function and continue running

From this point you can continue with GDB , you will get the information from the target for example inspect the target CPU registers

Working with shared objects

You probably noticed the warning message about shared libraries we get while we connect to the target. To debug also shared objects, we need to set some options:

First lets build the library and the client

mylib.c

build the library with debugging information:

usemylib.c

build the client with debug info

you can strip the target binaries, copy the library to /mylibs on the target file system

Run the gdbserver and load the client

Start the gdb client and before connecting to the target set the following 2 variables:

Add all the shared libraries locations to solid-search-path variable

Now you can also debug the shared object initializers for example if we add the following code to the shared library:

Now connect to the target and add a breakpoint in lib_init:

 

Using GDB server in Multi-Process mode

You can run GDB server without specifying a process to load with –multi option. You can select which process to load via gdb client for example if we have 2 different apps:

Compile both apps and copy to the target

Run gdbserver on the target with –multi option:

On the host run cross gdb without loading any ELF file and connect to the target using extended-remote:

To load the file to debug on the target and host run the following commands :

Now add a breakpoint and run the remote process:

To load a new process to debug:

 

 

 

 

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *