3.基础命令进阶

3.1break 命令进阶

break 在(file文件的)function函数中设置一个断点
[file:]function

3.1.1 break<行号>

wangguangbo@DESKTOP-O5D0MDL:/mnt/d/CMD$ g++ main.cpp FUN2.h FUN2.cpp -o cmd -g
wangguangbo@DESKTOP-O5D0MDL:/mnt/d/CMD$ gdb -q cmd
(gdb) info b
No breakpoints, watchpoints, tracepoints, or catchpoints.
(gdb) i b
No breakpoints, watchpoints, tracepoints, or catchpoints.
(gdb) b 49
Breakpoint 1 at 0x15b2: file main.cpp, line 49.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000000015b2 in main(int, char**) at main.cpp:49
(gdb) b main.cpp:5
Breakpoint 2 at 0x17ac: file main.cpp, line 12.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000000015b2 in main(int, char**) at main.cpp:49
2       breakpoint     keep y   0x00000000000017ac in FUNC::testFun1() at main.cpp:12

3.1.2 break<文件名>:<行号>#跨文件指定行

(gdb) b main.cpp:5
Breakpoint 2 at 0x17ac: file main.cpp, line 12.

3.1.3 break<函数名>

(gdb) b testFun
Breakpoint 3 at 0x1379: testFun. (2 locations)

3.1.4 break<类名><函数名>(C++类成员函数)

(gdb) b FUNC::testFun1() 
Note: breakpoint 2 also set at pc 0x17ac.
Breakpoint 8 at 0x17ac: file main.cpp, line 12.

3.1.5 break<文件名>:<函数名>(跨文件指定函数)

(gdb) b main.cpp:testFun
FUNC::testFun1()
testFun(int)
testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)
(gdb) b main.cpp:testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) 
Breakpoint 6 at 0x555555555379: file main.cpp, line 17.

3.1.6 break<位置>if<条件表达式>

(gdb) b 61 if sum > 500
Breakpoint 7 at 0x555555555662: file main.cpp, line 61.
(gdb) b i
Function "i" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) i b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
5       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
6       breakpoint     keep y   0x0000555555555379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
        breakpoint already hit 1 time
7       breakpoint     keep y   0x0000555555555662 in main(int, char**) at main.cpp:61
        stop only if sum > 500
        
        
(gdb) c
Continuing.
str:xxxx
str:vvvv
FUNC1 FUNC1 FUNC1
FUNC2 FUNC2 FUNC2

Breakpoint 7, main (argc=1, argv=0x7fffffffdea8) at main.cpp:61
61              sum+=i;                                                                                                   
(gdb) p i
$1 = 33
(gdb) p sum
$2 = 528
(gdb) 

3.1.7 tbreak<位置>(语法与break相同)

(gdb) tb 51
Temporary breakpoint 8 at 0x5555555555e0: file main.cpp, line 51.
(gdb) i b\
Quit
(gdb) i b 
Undefined info command: "bi b ".  Try "help info".
(gdb) b i
Function "i" not defined.
Make breakpoint pending on future shared library load? (y or [n]) Quit
(gdb) i b
Num     Type           Disp Enb Address            What
8       breakpoint     del  y   0x00005555555555e0 in main(int, char**) at main.cpp:51
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /mnt/d/CMD/cmd                                                                                          
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
argv[0]:/mnt/d/CMD/cmd
num:9988
cnt:199
buf:zzxx

Temporary breakpoint 8, main (argc=1, argv=0x7fffffffdea8) at main.cpp:51
51          testFun("xxxx");                                                                                              
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /mnt/d/CMD/cmd                                                                                          
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
argv[0]:/mnt/d/CMD/cmd
num:9988
cnt:199
buf:zzxx
str:xxxx
str:vvvv
FUNC1 FUNC1 FUNC1
FUNC2 FUNC2 FUNC2
[Inferior 1 (process 10523) exited normally]

3.1.8 break*<内存地址>

(gdb) r
Starting program: /mnt/d/CMD/cmd                                                                                          
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, 0x0000555555555443 in main ()
(gdb) disassemble
Dump of assembler code for function main:
   0x000055555555543b <+0>:     endbr64                                                                                   
   0x000055555555543f <+4>:     push   %rbp                                                                               
   0x0000555555555440 <+5>:     mov    %rsp,%rbp                                                                          
=> 0x0000555555555443 <+8>:     push   %rbx                                                                               
   0x0000555555555444 <+9>:     sub    $0x118,%rsp                                                                        
   0x000055555555544b <+16>:    mov    %edi,-0x114(%rbp)
   0x0000555555555451 <+22>:    mov    %rsi,-0x120(%rbp)
   0x0000555555555458 <+29>:    mov    %fs:0x28,%rax                                                                      
   0x0000555555555461 <+38>:    mov    %rax,-0x18(%rbp)
   0x0000555555555465 <+42>:    xor    %eax,%eax                                                                          
   0x0000555555555467 <+44>:    movl   $0x7b,-0xf4(%rbp)
   0x0000555555555471 <+54>:    movq   $0x363534,-0xa0(%rbp)
   0x000055555555547c <+65>:    movq   $0x0,-0x98(%rbp)
   0x0000555555555487 <+76>:    movq   $0x0,-0x90(%rbp)
   0x0000555555555492 <+87>:    movq   $0x0,-0x88(%rbp)
   0x000055555555549d <+98>:    movq   $0x0,-0x80(%rbp)
--Type <RET> for more, q to quit, c to continue without paging--
   0x00005555555554a5 <+106>:   movq   $0x0,-0x78(%rbp)
   0x00005555555554ad <+114>:   movq   $0x0,-0x70(%rbp)
   0x00005555555554b5 <+122>:   movq   $0x0,-0x68(%rbp)
   0x00005555555554bd <+130>:   movq   $0x0,-0x60(%rbp)
   0x00005555555554c5 <+138>:   movq   $0x0,-0x58(%rbp)
   0x00005555555554cd <+146>:   movq   $0x0,-0x50(%rbp)
   0x00005555555554d5 <+154>:   movq   $0x0,-0x48(%rbp)
   0x00005555555554dd <+162>:   movq   $0x0,-0x40(%rbp)
   0x00005555555554e5 <+170>:   movq   $0x0,-0x38(%rbp)
   0x00005555555554ed <+178>:   movq   $0x0,-0x30(%rbp)
   0x00005555555554f5 <+186>:   movq   $0x0,-0x28(%rbp)
   0x00005555555554fd <+194>:   lea    -0x101(%rbp),%rax                                                                  
   0x0000555555555504 <+201>:   mov    %rax,-0xf0(%rbp)
   0x000055555555550b <+208>:   nop                                                                                       
   0x000055555555550c <+209>:   nop                                                                                       
   0x000055555555550d <+210>:   lea    -0x101(%rbp),%rdx                                                                  
   0x0000555555555514 <+217>:   lea    -0xe0(%rbp),%rax                                                                   
--Type <RET> for more, q to quit, c to continue without paging--q
Quit
(gdb) b *0x000055555555544b
Breakpoint 2 at 0x55555555544b
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555555443 <main+8>
        breakpoint already hit 1 time
2       breakpoint     keep y   0x000055555555544b <main+16>
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /mnt/d/CMD/cmd                                                                                          
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, 0x0000555555555443 in main ()
(gdb) c
Continuing.

Breakpoint 2, 0x000055555555544b in main ()

3.1.9 rb<模糊函数名>

(gdb) rb testFun
Breakpoint 1 at 0x1aa8: file FUN2.cpp, line 8.
int FUN2::testFun2();
Breakpoint 2 at 0x17ac: file main.cpp, line 12.
void FUNC::testFun1();
Breakpoint 3 at 0x13f1: file main.cpp, line 26.
int testFun(int);
Breakpoint 4 at 0x1379: file main.cpp, line 17.
void testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >);
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001aa8 in FUN2::testFun2() at FUN2.cpp:8
2       breakpoint     keep y   0x00000000000017ac in FUNC::testFun1() at main.cpp:12
3       breakpoint     keep y   0x00000000000013f1 in testFun(int) at main.cpp:26
4       breakpoint     keep y   0x0000000000001379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17

也可以在命令后添加* ,效果相同

(gdb) d
Delete all breakpoints, watchpoints, tracepoints, and catchpoints? (y or n) y
(gdb) rb testFun*
Breakpoint 5 at 0x1aa8: file FUN2.cpp, line 8.
int FUN2::testFun2();
Breakpoint 6 at 0x17ac: file main.cpp, line 12.
void FUNC::testFun1();
Breakpoint 7 at 0x13f1: file main.cpp, line 26.
int testFun(int);
Breakpoint 8 at 0x1379: file main.cpp, line 17.
void testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >);
(gdb) i b
Num     Type           Disp Enb Address            What
5       breakpoint     keep y   0x0000000000001aa8 in FUN2::testFun2() at FUN2.cpp:8
6       breakpoint     keep y   0x00000000000017ac in FUNC::testFun1() at main.cpp:12
7       breakpoint     keep y   0x00000000000013f1 in testFun(int) at main.cpp:26
8       breakpoint     keep y   0x0000000000001379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17

3.1.10 info breakpoints(查看所有断点信息)

(gdb) i b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
5       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
6       breakpoint     keep y   0x0000555555555379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17

会提示每个断点被击中多少次

(gdb) i b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
5       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
6       breakpoint     keep y   0x0000555555555379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
        breakpoint already hit 1 time

3.1.11 delete<断点编号>(删除指定断点)

(gdb) i b 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001597 in main(int, char**) at main.cpp:46
2       breakpoint     keep y   <PENDING>          FUN
3       breakpoint     keep y   <MULTIPLE>
3.1                         y   0x0000000000001379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
3.2                         y   0x00000000000013f1 in testFun(int) at main.cpp:26
(gdb) clear
No source file specified.
(gdb) d 2
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001597 in main(int, char**) at main.cpp:46
3       breakpoint     keep y   <MULTIPLE>
3.1                         y   0x0000000000001379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
3.2                         y   0x00000000000013f1 in testFun(int) at main.cpp:26

3.1.12 delete<断点编号>(删除指定断点)

d命令可以直接删除全部断点

(gdb) c
Continuing.
num:9988
cnt:199
buf:zzxx

Breakpoint 3.1, testFun (str="xxxx") at main.cpp:17
17          printf("str:%s\n", str.c_str());                                                                              
(gdb) d
Delete all breakpoints, watchpoints, tracepoints, and catchpoints? (y or n) y
(gdb) i b
No breakpoints, watchpoints, tracepoints, or catchpoints.
(gdb) 

3.1.13 disable<断点编号>(禁用断点(保留但不触发))

(gdb) disable 6
(gdb) i b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
5       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
6       breakpoint     keep n   0x0000555555555379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
        breakpoint already hit 1 time

3.1.14 enable<断点编号>(启用已禁用的断点)

(gdb) enable 6
(gdb) i b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
5       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
6       breakpoint     keep y   0x0000555555555379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
        breakpoint already hit 1 time

3.1.15 enable once <断点编号>(启用一次自动禁用)

(gdb) i b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
5       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
6       breakpoint     keep y   0x0000555555555379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
        breakpoint already hit 1 time
7       breakpoint     keep y   0x0000555555555662 in main(int, char**) at main.cpp:61
        stop only if sum > 500
        breakpoint already hit 1 time
(gdb) enable once
Argument required (one or more breakpoint numbers).
(gdb) enable once 7
(gdb) i b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
5       breakpoint     keep y   0x0000555555555458 in main(int, char**) at main.cpp:36
        breakpoint already hit 1 time
6       breakpoint     keep y   0x0000555555555379 in testFun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at main.cpp:17
        breakpoint already hit 1 time
7       breakpoint     dis  y   0x0000555555555662 in main(int, char**) at main.cpp:61
        stop only if sum > 500
        breakpoint already hit 1 time
posted @ 2026-04-07 21:13  BlackSnow  阅读(4)  评论(0)    收藏  举报