I\'m trying to attach a program with gdb but it returns:
Attaching to process 29139
Could not attach to process. If your uid matches the ui
If permissions are a problem, you probably will want to use gdbserver. (I almost always use gdbserver when I gdb, docker or no, for numerous reasons.) You will need gdbserver (Deb) or gdb-gdbserver (RH) installed in the docker image. Run the program in docker with
$ sudo gdbserver :34567 myprogram arguments
(pick a port number, 1025-65535). Then, in gdb on the host, say
(gdb) target remote 172.17.0.4:34567
where 172.17.0.4
is the IP address of the docker image as reported by /sbin/ip addr list
run in the docker image. This will attach at a point before main
runs. You can tb main
and c
to stop at main
, or wherever you like. Run gdb under cgdb, emacs, vim, or even in some IDE, or plain. You can run gdb in your source or build tree, so it knows where everything is. (If it can't find your sources, use the dir
command.) This is usually much better than running it in the docker image.
gdbserver relies on ptrace
, so you will also need to do the other things suggested above. --privileged --pid=host
sufficed for me.
If you deploy to other OSes or embedded targets, you can run gdbserver or a gdb stub there, and run gdb the same way, connecting across a real network or even via a serial port (/dev/ttyS0
).