Comandos de GDB que quizás no conozca







Depurar el código es como cazar. Caza de insectos.

- Amit Kalantri


¿Qué es GDB?



GNU Debugger es un depurador portátil para el proyecto GNU que se ejecuta en muchos sistemas similares a UNIX y puede depurar muchos lenguajes de programación, incluidos C, C ++, Free Pascal, FreeBASIC, Ada, Fortran y Rust. GDB es un software gratuito publicado bajo la licencia GPL.

Fuente: 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- .







Este artículo no incluye algunos comandos que son fáciles de entender , por ejemplo: ejecutar, detener, salir, etc. No los especifiqué a propósito, ya que el artículo intenta transmitir información que los usuarios de GDB podrían no conocer. Si tiene alguna sugerencia sobre cómo complementar el artículo, con mucho gusto leeré los comentarios.








All Articles