Passion/Programming

gdb

sunshout 2013. 11. 17. 00:37

Compile 옵션

-g produces debugging information in the OS¹s native format (stabs, COFF,
XCOFF, or DWARF 2).

-ggdb produces debugging information specifically intended for gdb.

-ggdb3 produces extra debugging information, for example: including macro
definitions.  -ggdb by itself without specifying the level defaults to
-ggdb2 (i.e., gdb for level 2).

show sharedlibrary


(gdb) info sharedlibary



To debug libc

apt-get install libc6-dbg



Struct 의 값을 보고 싶을 때


(gdb) set print pretty on

(gdb) print *<struct name>


Thread 정보 보기 (info threads)

(gdb) info threads


특정 쓰레드로 들어가기 (thread #)

(gdb) thread 1


변수 출력 (formatting), 참조: https://sourceware.org/gdb/onlinedocs/gdb/Output-Formats.html

- p/(원하는 포맷팅) 변수명

- Possible format

  x : hexadecimal

  d : signed decimal

  u : unsigned decimal

  o : octal

  t  : binary

  a : address


(gdb) p/x flags

$12 = 0x42

(gdb) p/o flags

$13 = 0102

(gdb) p/t flags

$14 = 1000010

(gdb) p flags

$15 = 66


GDB 에서 assemble 언어와 프로그램을 동시에 보기
 . gdb 창에서 CTRL + X  2

 을 타이팅하면 아래와 같은 창으로 변경된다.

 


디버깅을 하면 코드의 위치와 현재 assembly 의 위치를 표시해 준다.


추가로 현재 하나의 line 이 몇 개의 assembly 로 구성되어 있는지를 파악하기 위해서는

set disassemble-next-line on 설정을 해 주면 디버깅 시 assembly 이 추가로 표시된다.


(gdb) set disassemble-next-line on

=> 0x0000000000400500 <main+19>:        83 45 f8 01     addl   $0x1,-0x8(%rbp)

   0x0000000000400504 <main+23>:        81 7d f8 3f 42 0f 00    cmpl   $0xf423f,-0x8(%rbp)

   0x000000000040050b <main+30>:        7e ed   jle    0x4004fa <main+13>

(gdb) n

=> 0x00000000004004fa <main+13>:        8b 45 f8        mov    -0x8(%rbp),%eax

   0x00000000004004fd <main+16>:        89 45 fc        mov    %eax,-0x4(%rbp)

(gdb)