GDB调试过程及调试命令

目录

GDB调试

GDB多进程调试


GDB调试

生成包含调试信息的可执行文件时加上 -g

g++ main.cpp -o app1
g++ main.cpp -o app2 -g

ll -h app1 app2
-rwxrwxr-x 1 nowcoder nowcoder 8.8K 3月  23 19:42 app1*
-rwxrwxr-x 1 nowcoder nowcoder  41K 3月  23 19:42 app2* 

可以看到 加了调试信息的app2 占用空间更多

输入 gdb app2

之后

        1)set args 10. 20 可以给程序设置参数 

        2) show args 可以获取设置参数

        3) l (list)查看当前.cpp代码文件     

            l 行号 从指定行显示

            l 函数名 从指定函数显示

             l 文件名 :行号                                 

             l  文件名:函数名

        4)set list 20 设置显示行号为20

        5)   b(break)  打断点       b 8     b test.cpp 6  在当前文件 8行和test.cpp 6行打断点 使用方法同3)l

                i(info) b 查看断点信息  d(delete) 断点编号  删除断点

i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000000085e in main() at main.cpp:8
2       breakpoint     keep y   0x00000000000008e4 in test() at test.cpp:6

                dis(disable) 断点标号  使其无效           ena(enable)使其生效 

    条件断点

                b 16  if i==5;  (函数里 15 16行 for(int i=0;i<10;i++) cout<<i<<endl;) 

                

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000000862 in main() at main.cpp:8
	stop only if i==5

调试命令

        start  程序停在第一行             run 程序停到断点

        c/continue 继续运行,到下次断点                        n/next 单步跳过

        s/step 单步跳入                     finish跳出当前函数体

        p/print  i 打印变量值i            ptype i 打印变量类型                

        until 跳出循环                       display i 设置一次可一直自动展示i值   undisplay i  取消展示

        set var i= x 设置i=x 

GDB多进程调试

int main()
{
    //成功则在父进程中返回创建的子进程的pid_t. 在子进程中返回0.父进程返回-1则创建失败
    //失败:1 当前进程数达到系统规定上线,errno:EAGAIN  2 系统内存不足,errno:ENOMEM
    pid_t pid1 = fork();
	
    if(pid1>0)
    {//父进程
        cout<<pid1<<endl;
        printf("I am father pid %d %d\n",getpid(),getppid());
        for(int i=0;i<5;i++)   
        {
            cout<<i<<" "<<getpid()<<endl;
            sleep(1);
        }
    }
    else if(pid1==0)
    {
        //子进程
        cout<<pid1<<endl;
        printf("I am child pid%d %d\n",getpid(),getppid());
        for(int i=0;i<10;i++)   
        {
            cout<<i<<" "<<getpid()<<endl;
            sleep(1);
            
        }
    }
    return 0;
}

父子进程分别打断点后,系统默认追踪父进程,所以在父进程断点处停止,子进程继续运行。

 follow-fork-mode 可以自己设置追踪 

(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "parent".
(gdb) set follow-fork-mode child
(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "child".

设置为追踪子进程,则在子进程断点处停止,父进程继续运行。

 此时,父进程运行完毕,子进程变为孤儿进程,被init进程接管。 getppid()为1. 

 detach-on-fork 默认on。 表示调试当前进程,其他进程继续运行,off 则其他进程被GDB挂起

(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is on.
(gdb) set detach-on-fork off
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is off.

info inferiors 查看进程信息

 inferior id 可以切换当前调试的进程

detach/remove  inferior id 使进程脱离GDB调试

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值