假设我正在与用户 alice 编写和编译程序。然后该程序由用户 bob 在同一台机器上运行,但从 alice 无法访问的位置运行。
alice@localhost:/home/alice$ g++ helloworld.cpp -o helloworld -g
bob@localhost:/home/bob$ cp ../alice/helloworld .
bob@localhost:/home/bob$ ./helloworld
现在,alice 想要调试 bob 正在做什么。开箱即用,这是不可能的:
alice@localhost:/home/alice$ pidof helloworld
1234
alice@localhost:/home/alice$ gdb
[...]
(gdb) attach <pidof helloworld>
Attaching to process 1234
ptrace: Operation not permitted.
爱丽丝该怎么办?
最佳答案
远程调试
Alice 和 Bob 应该使用远程调试。 Bob 启动 gdbserver:
bob@localhost:/home/bob$ gdbserver :2345 ./helloworld
然后 Alice 连接到它:
alice@localhost:/home/alice$ gdb
[...]
(gdb) file helloworld
Reading symbols from /home/alice/helloworld...done.
(gdb) target remote :2345
Remote debugging using :2345
[...]
0x00007fbdc6329af0 in _start () from /lib64/ld-linux-x86-64.so.2
使用绝对路径远程调试
这适用于这个简单的情况。但是,当 Bob 为他的共享库使用绝对路径时,需要一些更复杂的方法:
bob@localhost:/home/bob$ ls
helloworld libmylib.so
bob@localhost:/home/bob$ LD_LIBRARY_PATH=/home/bob gdbserver :2345 ./helloworld
现在,alice 找不到共享库:
alice@localhost:/home/alice$ gdb
[...]
(gdb) file helloworld
Reading symbols from /home/alice/helloworld...done.
(gdb) target remote :2345
Remote debugging using :2345
[...]
(gdb) break helloWorld()
Breakpoint 1 at 0x400480
(gdb) c
Continuing.
Error while mapping shared library sections:
/home/bob/libmylib.so: No such file or directory.
为了解决这个问题,Alice 创建了一个虚拟根文件夹,其中包含指向其二进制文件的链接:
alice@localhost:/home/alice$ mkdir -p gdb-symbols/home/
alice@localhost:/home/alice$ ln -s /home/alice gdb-symbols/home/bob
alice@localhost:/home/alice$ ln -s /lib gdb-symbols/lib
alice@localhost:/home/alice$ ln -s /lib64 gdb-symbols/lib64
[and so forth for every shared library that cannot be found...]
现在可以在加载所有符号的情况下进行调试:
alice@localhost:/home/alice$ gdb
[...]
(gdb) file helloworld
Reading symbols from /home/alice/helloworld...done.
(gdb) target remote :2345
Remote debugging using :2345
[...]
Reading symbols from /home/alice/gdb-symbols/home/bob/libmylib.so...done.
Loaded symbols from /home/alice/gdb-symbols/home/bob/libmylib.so
(gdb)
关于permissions - 如何调试由不同用户运行的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8725415/