View this PageEdit this PageUploads to this PageVersions of this PageHomeRecent ChangesSearchHelp Guide

Cómo usar gdbserver para remote debugging

Hay que seguir las instrucciones de este post ("Linus's blog: How to debug your initramfs init"):


gdbserver

gdbserver is a component of gdb that allows exposing debugging of a process via the network or a serial interface.

It has two modes: one where it launches the to-be-debugged child process and waits for a client to connect:

$ gdbserver localhost:3333 sleep infinity
Process hello created; pid = 7780
Listening on port 3333

(meanwhile, in another terminal)

(gdb) target remote localhost:3333
Remote debugging using localhost:3333
Reading /nix/store/w8vm09hri2zz7yacryzzzxvsapik4ps4-coreutils-9.1/bin/coreutils from remote target...
[...]
(gdb) bt
  1. 0 0x00007f2c0715ed24 in pause ()
from target:/nix/store/whypqfa83z4bsn43n4byvmw80n4mg3r8-glibc-2.37-45/lib/libc.so.6
  1. 1 0x00000000004d96c5 in xnanosleep ()
  2. 2 0x0000000000486858 in single_binary_main_sleep ()
  3. 3 0x00000000004094ef in launch_program ()
  4. 4 0x000000000040882b in main ()
(gdb) continue
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x00007f2c0715ed24 in pause ()
from target:/nix/store/whypqfa83z4bsn43n4byvmw80n4mg3r8-glibc-2.37-45/lib/libc.so.6
(gdb)

And another mode where it attaches to an already running process.

$ sleep infinity &
[1] 3945492

$ gdbserver --attach localhost:3333 3945492
Attached; pid = 3945492
Listening on port 3333

and attach:

(gdb) target remote localhost:3333
Remote debugging using localhost:3333
Reading /nix/store/jmy11m3c935yyvs4njz3s52p9azgvg6f-coreutils-full-9.3/bin/coreutils from remote target...
[...]
0x00007f5b562fd6c4 in pause () from target:/nix/store/qn3ggz5sf3hkjs2c797xf7nan3amdxmp-glibc-2.38-27/lib/libc.so.6
(gdb) bt
  1. 0 0x00007f5b562fd6c4 in pause () from target:/nix/store/qn3ggz5sf3hkjs2c797xf7nan3amdxmp-glibc-2.38-27/lib/libc.so.6
  2. 1 0x00000000004d3225 in xnanosleep ()
  3. 2 0x0000000000488ab8 in single_binary_main_sleep ()
  4. 3 0x000000000040a6cf in launch_program ()
  5. 4 0x0000000000409a0b in main ()