gdb的break命令设置断点

一 指定在源代码的某行处设置断点

1 源代码

#include 
using namespace std;
void zww(int age)
{
    int a, b, c;
    if (age > 60)
        cout << "I am old\n";
    else
        cout << "I am young\n";
}
int main()
{
    int a = 5, b = 6;
    zww(70);

    a++; //line 16
    b++;
    if (a > b)
        cout << a << endl;
    else
        cout << b << endl;   
    return 0;
}

2 编译源代码

[root@localhost 2.26]# g++ test.cpp -g -o test

3 启动gdb调试test

[root@localhost 2.26]# gdb test
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /root/C++/ch02/2.26/test...done.
(gdb) b 16
Breakpoint 1 at 0x400910: file test.cpp, line 16.
(gdb) run
Starting program: /root/C++/ch02/2.26/test
I am old

Breakpoint 1, main () at test.cpp:16
16        a++; //line 16
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64 libgcc-4.8.5-16.el7_4.1.x86_64 libstdc++-4.8.5-16.el7_4.1.x86_64

可以看到程序在第16行停下来,并且前面有打印语句的地方也打印出来了,而第16行后面的代码因为没执行到,所以没输出。

二 在源代码的某函数处设置断点

[root@localhost 2.26]# gdb test
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /root/C++/ch02/2.26/test...done.
(gdb) b zww
Breakpoint 1 at 0x4008c8: file test.cpp, line 6.
(gdb) run
Starting program: /root/C++/ch02/2.26/test

Breakpoint 1, zww (age=70) at test.cpp:6
6        if (age > 60)
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64 libgcc-4.8.5-16.el7_4.1.x86_64 libstdc++-4.8.5-16.el7_4.1.x86_64

 

你可能感兴趣的:(C++)