Debugging



Remote debugging is the process of debugging a program running on a different system (called target) from a different system (called host). learn debugging c program using gdb .

The debugger in the host can then control the execution of the program on the remote system and retrieve information about its state.

We need the following two utilities to perform a remote debugging.

1.gdbserver – Run this on your target system (say board)
2.gdb – Execute this on your host system to connect to your target system (PC)

GDB and gdbserver communicate via either a serial line or a network, using the standard gdb remote serial protocol.

sudo apt-get install gdbserver (on ubuntu).

TARGET::

$ gdbserver localhost:2222 a.out

Process program created; pid = 2045
Listening on port 2222

HOST::

$ gdb a.out

(gdb) target remote localhost:2222

Remote debugging using localhost:2222
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007ffff7dddaf0 in ?? () from /lib64/ld-linux-x86-64.so.2
(gdb) b main
Breakpoint 1 at 0x400550
(gdb) continue
Continuing.

Breakpoint 1, 0x0000000000400550 in main ()


TARGET::

Remote debugging from host 192.168.1.20
Program to calculate power
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512

Child exited with status 0
GDBserver exiting

------------------------------------------------------------------------------------
Attach gdb to a Running Process on Target
First you have to find the process ID of the running process in target.
On Host,
(gdb) attach PID
Now the gdb will suspend the process 3850 in the target and you can debug the program using normal gdb commands.
------------------------------------------------------------------------------------
Launch gdbserver in Multi-process Mode::
On Target, run the gdbserver with –multi and without a program name.
TARGET::
gdbserver --multi localhost:2222

Listening on port 2222
HOST::

$ gdb
(gdb) target extended-remote 192.168.1.10:2000
Remote debugging using 192.168.1.10:2000

(gdb) (gdb) set remote exec-file /my_prg
(gdb) file /my_prg
Reading symbols from /my_prg...(no debugging symbols found)...done.
(gdb) b main
Note: breakpoint 1 also set at pc 0x400550.
Breakpoint 2 at 0x400550
(gdb) run
Starting program: /my_prg
Breakpoint 1, 0x0000000000400550 in main ()



  1. ‘target extended-remote’ is used to run gdbserver in multi process mode.
  2. ‘set remote exec-file /my_prg’ is used to set the program which you want to debug in the target.
  3. ‘file /my_prg’ is used to load the debugging symbols from the program in the host.
  4. ‘b main’ is used to set breakpoint at main() function.
  5. ‘run’ is used to run the program, which stops at the breakpoint main().
Note: In the above case, the executable “my_prg” is present under “/” on both target and host.

No comments:

Post a Comment