假设一个正在运行的Linux程序出了问题,我们想看一下这个程序运行到了哪里,我们可以使用GDB工具连接到正在运行的进程上,并察看。
例如下面这段代码:
#include <stdio.h> // printf()
#include <unistd.h> // getpid()
void a()
{
}
void b()
{
}
void c()
{
}
int main()
{
printf("My pid is: %d\n", getpid());
for (;;) {
a();
b();
c();
}
}
编译运行:
$ gcc a.c && ./a.out
My pid is: 17153
打开另外一个终端窗口,运行:
$ gdb -p 17153
(gdb) bt
#0 0x000000000040052d in a ()
#1 0x0000000000400560 in main ()
(gdb) c
Continuing.
可以看到,这个程序正运行到函数a()。
参考
https://stackoverflow.com/questions/14370972/how-to-attach-a-process-in-gdb
网友评论