使用GDB的简要指南


在这个简短的教程中,我们将介绍使用GDB的基本技术,并了解如何(应该)准备用于GDB调试的文件。


GDB是可移植的GNU项目调试器,可在许多类似UNIX的系统上运行,并且可以调试许多编程语言,包括C,C ++,Free Pascal,FreeBASIC,Ada,Fortran,Python3,Swift,NASM和Rust。


Gdb


GDB? , UNIX- , . , VSCode ( Vim, NeoVim ( ), Emacs, Atom )



.cpp .


G++ -g ( , , GDB).


g++ -g file_name.cpp -o output_name
gdb output_name

Python- :


gdb -ex r --args python program_name.py <arguments>

Java jdb, c JDK.


, , OnlineGDB, debug, , .


GDB


GDB :


GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from 3_Hero's_Inventory.cpp...done.

, .


, (, ), cpp main(). c list b ( b main ):


(gdb) list
1   #include <iostream>
2   #include <string>
3   
4   using namespace std;
5   
6   int main(int argc, char *argv[])
7   {
8       // Hero's Inventory - ,     
9   
10      const int MAX_ITEMS = 10; //  ,    

(gdb) b 6
Breakpoint 1 at 0xcb5: file ./3_Hero's_Inventory.cpp, line 6.

r:


(gdb) r
Starting program: /home/username77177/gitprojects/learning/cpp/build_folder/3_Hero's_Inventory.cpp 

Breakpoint 1, main (argc=1, argv=0x7fffffffdd18) at ./3_Hero's_Inventory.cpp:7
7   {

TUI, <Ctrl-x a>


, , f:


(gdb) f
#0  main (argc=1, argv=0x7fffffffdd18) at ./3_Hero's_Inventory.cpp:14
14      items[itemnum++] = "Sword";

, , n ( next):


(gdb) n
10      const int MAX_ITEMS = 10; //  ,    

GDB ( ) .
, , n , , " " (step-in) s:


(gdb) s
11      string items[MAX_ITEMS]; //     c 10 

( , step-in , )


() info locals:


(gdb) info locals
MAX_ITEMS = 10
items = {"", "", "", "", "", "", "", "", "", ""}
itemnum = 0
game = 247

, print _:


(gdb) print MAX_ITEMS 
$1 = 10

set:


(gdb) set x = 77177
(gdb) print x
$1 = 77177

watch:


watch x

, (info registers):


(gdb) info registers 
rax            0x7fffffffdc00   140737488346112
rbx            0xffffffffffffffff   -1
rcx            0xa0 160
rdx            0x7fffffffdd28   140737488346408
rsi            0x7fffffffdd18   140737488346392
rdi            0x7fffffffdbf0   140737488346096
rbp            0x7fffffffdc30   0x7fffffffdc30
rsp            0x7fffffffdab0   0x7fffffffdab0
r8             0x7ffff782fd80   140737345944960
r9             0x0  0
r10            0x6  6
r11            0x7ffff7b77020   140737349382176
r12            0x7fffffffdc10   140737488346128
r13            0x7fffffffdd10   140737488346384
r14            0x0  0
r15            0x0  0
rip            0x555555554cfe   0x555555554cfe <main(int, char**)+100>
eflags         0x286    [ PF SF IF ]
cs             0x33 51
ss             0x2b 43
ds             0x0  0
es             0x0  0
fs             0x0  0
gs             0x0  0

breakpoints ( ) info breakpoints:


(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555554cb5 in main(int, char**) 
                                                   at ./3_Hero's_Inventory.cpp:6
    breakpoint already hit 1 time
2       breakpoint     keep y   0x0000555555554cfe in main(int, char**) 
                                                   at ./3_Hero's_Inventory.cpp:14

del breakpoint_num:


(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555554cb5 in main(int, char**) 
                                                   at ./3_Hero's_Inventory.cpp:6
    breakpoint already hit 1 time
2       breakpoint     keep y   0x0000555555554cfe in main(int, char**) 
                                                   at ./3_Hero's_Inventory.cpp:14

(gdb) del 1

(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x0000555555554cfe in main(int, char**) 
                                                   at ./3_Hero's_Inventory.cpp:14

c:


(gdb) r
Starting program: /home/username77177/gitprojects/learning/cpp/build_folder/3_Hero's_Inventory.cpp 

Breakpoint 3, main (argc=1, argv=0x7fffffffdd18) at ./3_Hero's_Inventory.cpp:7
7   {
(gdb) c
Continuing.

Breakpoint 2, main (argc=1, argv=0x7fffffffdd18) at ./3_Hero's_Inventory.cpp:14
14      items[itemnum++] = "Sword";

() call:


(gdb) call MyFunction()

要继续执行功能并在程序(功能)结束时停止程序,您需要编写finishfin


(gdb) fin

值得说明的是,它不能finish在main方法中使用。


要完成该程序,您需要编写kill


(gdb) kill
Kill the program being debugged? (y or n) y

您也可以随时编写help并获得有关如何使用调试器的简要帮助。


(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

All Articles