gpt4 book ai didi

c++ - 如何在启动时将 gdbserver 附加到进程?

转载 作者:行者123 更新时间:2023-11-30 03:30:16 24 4
gpt4 key购买 nike

我正在使用 gdbserver 调试远程进程。我可以在 gdbserver 启动并等待输入后将其附加到该进程。

但是,我想在启动时将 gdbserver 附加到该进程。该进程是通过 shell 脚本启动的,我无法更改该进程的启动方式,即我无法修改 shell 脚本以通过调用 gdbserver 启动该进程。

如何在 gdbserver 启动后立即将其附加到此进程?

编辑:我能够在 main() 的开头创建一个等待循环。例如,等待直到在预定位置找到文件的循环:

#include <unistd.h>
int main() {

while( access("/home/username/CONTINUE", F_OK) == -1)
sleep(1);

/*
...all the rest of main()
*/

return 0;
}

我们可以在进程忙于此循环时附加 gdbserver,根据需要设置断点并说 touch/home/username/CONTINUE 退出循环。但这需要我们能够访问源代码,编译二进制文件并将其放置在目标机器上。我正在寻找比这更好、更简单的方法。

最佳答案

But this requires us to be able to access the source code1, compile the binary and place it on the target machine. I am looking for a better, easier way than this.

看起来您正在使用类似 linux/unix 的远程操作系统。

如果您对远程系统具有管理员访问权限,我能想到的最简单的方法是重命名原始可执行文件,并将其替换为与原始可执行文件同名的 shell 脚本,该脚本在 gdbserver 的控制下启动现在重命名的文件。

在目标机器上类似(假定可执行文件 /usr/bin/foo ):

root:# cd /usr/bin
root:# mv foo foo_
root:# echo "#!/bin/sh\ngdbserver /dev/com1 foo_ foo.txt" > foo
root:# chmod a+x foo

正如它在 gdbserver man(1) page 中所说:

This tells gdbserver to debug foo_ with an argument of foo.txt, and to communicate with GDB via /dev/com1. gdbserver now waits patiently for the host GDB to communicate with it.


另一种我能想到的完全不修改原始进程的方法,可能是一个小程序,它监视 /dev/proc 目录中的更改,并在此类事件中附加 gdbserver 和相关的 pid。
尽管这有点幸运,但如果附加了 gdbserver,则在该进程已经安排到 main() 之前。


1无论如何,您应该有权访问源代码,以便使用 gdb 进行合理调试。尽管在某些情况下您也可以使用(反)汇编代码。

关于c++ - 如何在启动时将 gdbserver 附加到进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45116818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com