Debugging code is like hunting. Bug hunting.
- Amit Kalantri
What is GDB
GNU Debugger is a portable debugger for the GNU project that runs on many UNIX-like systems and can debug many programming languages, including C, C ++, Free Pascal, FreeBASIC, Ada, Fortran, and Rust. GDB is free software released under the GPL license.
Source: GNU Debugger - Wikipedia
GDB — , .
, , , GDB.
, — , . , , :
Caesar::shift | Caesar |
Caesar::ChangeShift | Caesar |
Caesar::Crypt | Caesar |
(TUI)
TUI, GDB, , , . TUI GDB tui enable
( ) - . Ctrl + p (previous), Ctrl + n (next), ( ).
: - tui enable
, — tui disable
.
breakpoint
( — b).
:
- ()
- ,
, — .
b Method if variable == value
— .
:
(gdb) b Crypt if Caesar::shift == 1 Note: breakpoint 2 also set at pc 0x4026e3. Breakpoint 4 at 0x4026e3: file Caesar.h, line 55.
Crypt()
, Caesar
shift
1.
: delete
, d
.
: : , , , .
Scope,
next
step
(: n s) , next Scope ( ), step Step In .
: continue
, finish
fin
.
: step
, — next
.
. print
p
, .
display
display
,
(gdb) display Caesar::shift 1: Caesar::shift = <error: Cannot reference non-static field "shift"> (gdb) n Breakpoint 2, Caesar::Crypt (this=0x7fffffffdc30, text="Crypt this text! Btw, xyz") at Caesar.h:55 1: Caesar::shift = 1 (gdb) n 1: Caesar::shift = 1 (gdb)
:
(gdb) display *stringArrayForOutput@lengthOfArray 1: *stringArrayForOutput@lengthOfArray = {"Etarv vjku vgzv! Dvy, zab", "Fubsw wklv whaw! Ewz, abc", "Gvctx xlmw xibx! Fxa, bcd"} (gdb)
, lengthOfArray
( ) . GDB .
explore
, , , explore
.
explore
,
(gdb) explore Caesar::shift 'Caesar::shift' is a scalar value of type 'int'. Caesar::shift = 6 (gdb) explore Caesar 'Caesar' is a struct/class with the following fields: letters = <Enter 0 to explore this field of type 'std::string'> shift = <Enter 1 to explore this field of type 'int'>
: print
explore
display
.
" " , , record
, ,
(gdb) target record-full (gdb) p Caesar::shift $1 = 6 (gdb) n (gdb) p Caesar::shift $2 = 7 (gdb) reverse-next No more reverse-execution history. (gdb) p Caesar::shift $3 = 6
, — record stop
, , record save < >
.
: GDB , , .
info local
:
(gdb) info local mainObj = {letters = "abcdefghijklmnopqrstuvwxyz", shift = 1} textForC = "Crypt this text! Btw, xyz" cryptedText = "Dszqu uijt ufyu! Cux, yza" outputVariants = 0x402280 <_start>
, info variables
:
(gdb) info variables All defined variables: File /usr/include/c++/10/bits/basic_string.h: 101: const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size_type std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos; File /usr/include/c++/10/bits/stl_pair.h: 83: static const std::piecewise_construct_t std::piecewise_construct; File /usr/include/c++/10/iostream: 74: static std::ios_base::Init std::__ioinit; Non-debugging symbols: 0x00000000004002e8 __abi_tag 0x0000000000403000 _IO_stdin_used 0x00000000004031b8 typeinfo for std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > 0x00000000004031e0 typeinfo name for std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > --Type <RET> for more, q to quit, c to continue without paging--
info b
:
(gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000402633 in Caesar::ChangeShift() at Caesar.h:31 breakpoint already hit 1 time 2 breakpoint keep y 0x00000000004026e3 in Caesar::Crypt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at Caesar.h:55 breakpoint already hit 1 time
whatis
:
(gdb) whatis Caesar::shift type = int (gdb) whatis Caesar type = Caesar
, info address
:
(gdb) info address Caesar::Crypt Symbol "Caesar::Crypt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)" is a function at address 0x4026be.
, , , , , . Linux -, . - , Unix- .
This article does not include some commands that are easy to understand , for example: run, stop, quit, etc. I did not specify them on purpose, since the article tries to convey information that GDB users might not know about. If you have any suggestions how to supplement the article, I will gladly read the comments.