魏晓东 发表于 2025-2-16 23:42:37

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重新停止
https://img2024.cnblogs.com/blog/3374027/202502/3374027-20250216163233549-465392421.png
5.结束调试

下令解析detach直接使用detach下令,可以从历程中分离GDB并让程序继续运行attach PID重新将GDB附加到某个历程上https://img2024.cnblogs.com/blog/3374027/202502/3374027-20250216163242746-598465822.png
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]
查看完整版本: GDB调试(二)