GDB调试(二)
GDB调试运行中程序GDB调试
测试程序
//test2.c
//功能:从0开始每秒打印
#include <stdio.h>
#include <unistd.h>
int aaa();
int bbb(int n);
int main()
{
aaa();
}
int aaa()
{
bbb(0);
}
int bbb(int n)
{
for(int i = n; i < n+10000; i++)
{
printf("i:%d\n", i);
n++;
sleep(1);
}
}gcc -g -o test2 test2.c
./test2
//结果
:0
i:1
i:2
i:3
i:4
i:5
i:6
i:7
......操作步骤
1.运行程序
在终端窗口运行编译好的程序
./test22.进程ID
在另一个终端窗口,使用ps命令找到正在运行程序的进程ID,其中进程ID是第二个
ps aux | grep test2
//结果如下,其中正在运行程序的进程ID是15554
username 155540.00.0 27761408 pts/1 S+ 22:38 0:00 ./test2
username 155570.00.0121922432 pts/2 S+ 22:39 0:00 grep --color=auto test3.附加GDB
使用GDB附加到正在运行的程序上
sudo gdb test2 -p 15554这里的sudo加不加看环境,有些不用加
4.GDB调试
在GDB中,你可以使用常用的调试命令如bt(查看调用堆栈),print(打印变量值),continue(继续执行程序),等等
这里因为sleep延时,直接continue后,test2继续运行,gdb这里卡住了,可以用CTRL-C重新中断
5.结束调试
命令解析detach直接使用detach命令,可以从进程中分离GDB并让程序继续运行attach PID重新将GDB附加到某个进程上
GDB调试多进程
测试程序
#include #include #include int main(){ printf("begin\n"); if ( fork() != 0 ) { printf("我是父进程:进程pid=%d,父进程ppid=%d\n",getpid(),getppid()); int ii; for(ii=0; ii
页:
[1]