IT评测·应用市场-qidao123.com
标题:
GDB调试(二)
[打印本页]
作者:
魏晓东
时间:
2025-2-16 23:42
标题:
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.运行程序
在终端窗口运行编译好的程序
./test2
复制代码
2.历程ID
在另一个终端窗口,使用ps下令找到正在运行程序的历程ID,此中历程ID是第二个
ps aux | grep test2
//结果如下,其中正在运行程序的进程ID是15554
username 15554 0.0 0.0 2776 1408 pts/1 S+ 22:38 0:00 ./test2
username 15557 0.0 0.0 12192 2432 pts/2 S+ 22:39 0:00 grep --color=auto test
复制代码
3.附加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调试多历程
测试程序
[code]#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
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4