Something On GDB And Debugging
dutor
Gdutor@gmail.com

TAIR
September 26, 2013
Outline

1

Debugger Under the Hood
Outline

1

Debugger Under the Hood

2

GDB the Basics
Outline

1

Debugger Under the Hood

2

GDB the Basics

3

Bonus
Outline

1

Debugger Under the Hood

2

GDB the Basics

3

Bonus

4

A Little Assembly
Debugger Under the Hood Basics Bonus A Little Assembly

What and How

what debuggers do
how to take control of the target
...
Debugger Under the Hood Basics Bonus A Little Assembly

ptrace

long ptrace(request, pid, addr, data)
tracer/tracee
signal...
Debugger Under the Hood Basics Bonus A Little Assembly

breakpoints

hardware, the debug registers
software, INT 3

dutor
...
Debugger Under the Hood Basics Bonus A Little Assembly

Debug Info

DWARF, designed for various languages and ABIs
source ...
Debugger Under the Hood Basics Bonus A Little Assembly

Basic Usage of GDB

start
running

$
$
$
$

cc test . cpp -g
gdb a...
Debugger Under the Hood Basics Bonus A Little Assembly

Basic Usage of GDB

start
running
breakpoints
print/x
others

( gd...
Debugger Under the Hood Basics Bonus A Little Assembly

Basic Usage of GDB

start
running
breakpoints
print/x
others
help
...
Debugger Under the Hood Basics Bonus A Little Assembly

Basic Usage of GDB

start
running
breakpoints
print/x

( gdb )
( g...
Debugger Under the Hood Basics Bonus A Little Assembly

Basic Usage of GDB

start
running
breakpoints
print/x
others

( gd...
Debugger Under the Hood Basics Bonus A Little Assembly

Basic Usage of GDB

start
running
breakpoints
print/x

( gdb ) hel...
Debugger Under the Hood Basics Bonus A Little Assembly

Miscs and Bonus
p $rip

set follow-fork-mode child

i reg

set sch...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Syntax

instruction src, ...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Common Used Registers

ra...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Stack Frame
+------+
+---...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Calling Conventions

Argu...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Data Representatoin

Inte...
That’s All.
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Basic Format
1
2
3
4
5

a...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Constraints
r register op...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Constraints
r register op...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Constraints Modifiers
= o...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Constraints Modifiers
= o...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Call C Functions

1
2
3
4...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Examples
Do System Call

...
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Disassembling
Using GDB

...
Upcoming SlideShare
Loading in …5
×

Debugger Principle Overview & GDB Tricks

723 views
537 views

Published on

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
723
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Debugger Principle Overview & GDB Tricks

  1. 1. Something On GDB And Debugging dutor Gdutor@gmail.com TAIR September 26, 2013
  2. 2. Outline 1 Debugger Under the Hood
  3. 3. Outline 1 Debugger Under the Hood 2 GDB the Basics
  4. 4. Outline 1 Debugger Under the Hood 2 GDB the Basics 3 Bonus
  5. 5. Outline 1 Debugger Under the Hood 2 GDB the Basics 3 Bonus 4 A Little Assembly
  6. 6. Debugger Under the Hood Basics Bonus A Little Assembly What and How what debuggers do how to take control of the target how to set up breakpoints how to map instructions with source code dutor Something On GDB And Debugging
  7. 7. Debugger Under the Hood Basics Bonus A Little Assembly ptrace long ptrace(request, pid, addr, data) tracer/tracee signal/waitpid PTRACE_TRACEME PTRACE_ATTACH PTRACE_CONT/SINGLESTEP PTRACE_PEEKDATA/POKEDATA dutor Something On GDB And Debugging
  8. 8. Debugger Under the Hood Basics Bonus A Little Assembly breakpoints hardware, the debug registers software, INT 3 dutor Something On GDB And Debugging
  9. 9. Debugger Under the Hood Basics Bonus A Little Assembly Debug Info DWARF, designed for various languages and ABIs source lines and instructions names and adresses utilities readelf strip nm/strings c++filt dutor Something On GDB And Debugging
  10. 10. Debugger Under the Hood Basics Bonus A Little Assembly Basic Usage of GDB start running $ $ $ $ cc test . cpp -g gdb a . out gdb a . out core .1234 gdb -- args a . out arg1 arg2 breakpoints print/x others help $ gdb ( gdb ) ( gdb ) ( gdb ) ( gdb ) file a . out start arg1 arg2 run arg1 arg2 run < if > of dutor Something On GDB And Debugging
  11. 11. Debugger Under the Hood Basics Bonus A Little Assembly Basic Usage of GDB start running breakpoints print/x others ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) next step continue nexti stepi finish until LINE help dutor Something On GDB And Debugging
  12. 12. Debugger Under the Hood Basics Bonus A Little Assembly Basic Usage of GDB start running breakpoints print/x others help ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) set p end break break test . cpp :32 break foo if p == NULL break 32 thread 0 info breakpoints disable 1 enable 2 delete 3 ignore 4 123 commands 5 = malloc (1024) dutor Something On GDB And Debugging
  13. 13. Debugger Under the Hood Basics Bonus A Little Assembly Basic Usage of GDB start running breakpoints print/x ( gdb ) ( gdb ) ( gdb ) ( gdb ) print n print / x p print object x &n others help dutor Something On GDB And Debugging
  14. 14. Debugger Under the Hood Basics Bonus A Little Assembly Basic Usage of GDB start running breakpoints print/x others ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) ( gdb ) backtrace info locals info threads thread 16 list list foo list - help dutor Something On GDB And Debugging
  15. 15. Debugger Under the Hood Basics Bonus A Little Assembly Basic Usage of GDB start running breakpoints print/x ( gdb ) help print ( gdb ) help set ( gdb ) help set print others help dutor Something On GDB And Debugging
  16. 16. Debugger Under the Hood Basics Bonus A Little Assembly Miscs and Bonus p $rip set follow-fork-mode child i reg set scheduler-locking on p {tair::StorageManager}0x608048 symbol-file p *array@10 add-symbol-file display/i $rip i sharedlibrary x/40a $rsp gcc test.cpp -g -g3 l *0x608048 maintenance info sections watch expr set logging on return 0 set print pretty on enable breakpoints gdb -p 1234 -ex ‘set n=0’ -batch gcore Ctrl-X Ctrl-A dutor Something On GDB And Debugging
  17. 17. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Syntax instruction src, des The first operand is the source, the second is the destination %register Register names are prefixed with a %, %% in inlined assembly $literal Literal values are prefixed with $ instruction{b,w,l,q} The instruction suffix denotes the operand size seg:off(base, index, scale) Memory access dutor Something On GDB And Debugging
  18. 18. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Common Used Registers rax, eax, ax, al rbx, ebx, bx, bl rcx, ecx, cx, cl rdx, edx, dx, dl rsi, esi, si rdi, edi, di rbp, rsp r8-r15 xmm0-xmm7 dutor Something On GDB And Debugging
  19. 19. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Stack Frame +------+ +-----------------------+ | | | | return address | | | v +-----------------------+ | | %ebp----> | old %ebp | | | +-----------------------+ | | %esp----> | local variables | | | +-----------------------+ stack | | | argument 2 | | growing | +-----------------------+ direction | | | argument 1 | | | +-----------------------+ | | | return address | | | +-----------------------+ | +--------------+ old %ebp | <-----%ebp | +-----------------------+ | | local variables | <-----%esp | +-----------------------+ | | | _|_ | Red Zone | / | | ’ +-----------------------+ dutor Something On GDB And Debugging
  20. 20. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Calling Conventions Arguments, rdi, rsi, rdx, rcx, r8, r9, xmm0-xmm7 Arguments on stack Return, rax, xmm0 Stack Frame, rbp, rsp dutor Something On GDB And Debugging
  21. 21. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Data Representatoin Integers Floats Structures Classes Arrays PC-relative Reference dutor Something On GDB And Debugging
  22. 22. That’s All.
  23. 23. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Basic Format 1 2 3 4 5 asm ( < assembler template > : [ " constraints " ( var ) ] [ , " constraints " ( var ) ] : [ " constraints " ( var ) ] [ , " constraints " ( var ) ] : [ " register " ] [ , " register " ] [ , " memory " ] ); /* output operands */ /* input operands */ /* clobbered registers */ Examples asm("nop":::) asm("incl %%eax") asm("movl $1, %0":"m"(ret)) asm("movl %0, %%eax"::"m"(ret)) asm("addl %1, %0":"+"(a):"r"(b)) asm("incl global_var":::"memory") nop Access register directly Write to C variable Read from C variable a =a+b Write to arbitrary memory dutor Something On GDB And Debugging
  24. 24. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Constraints r register operands, any of the followings. a %rax, etc. b %rbx, etc. c %rcx, etc. d %rdx, etc. S %rsi, etc. D %rdi, etc. q register operands, any of a, b, c, d. m memory operands. f floating poing register. [0-9] matching constraints, both for input and output. dutor Something On GDB And Debugging
  25. 25. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Constraints r register operands, any of the followings. a %rax, etc. b %rbx, etc. c %rcx, etc. d %rdx, etc. S %rsi, etc. D %rdi, etc. q register operands, any of a, b, c, d. m memory operands. f floating poing register. [0-9] matching constraints, both for input and output. dutor asm ( " int $0x80 n t " : " = a " ( ret ) : " 0 " (4) , " b " ( fd ) , " c " ( buf ) , " d " ( n ) ); Something On GDB And Debugging
  26. 26. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Constraints Modifiers = operand is write-only. + operand is used as both read and write. Write-Only Example asm ( " movl $1 , %0 " : " = r " ( ret ) ) ; dutor Something On GDB And Debugging
  27. 27. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Constraints Modifiers = operand is write-only. + operand is used as both read and write. Read-Write Example asm ( " incl %0 " : " + r " ( ret ) ) ; dutor Something On GDB And Debugging
  28. 28. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Call C Functions 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char * fmt = " Hello , % s n " ; char * s = " World " ; int ret = 0; asm ( " callq printf n t " : " = a " ( ret ) : " D " ( fmt ) , " S " ( s ) ) ; printf ( " ret : % d n " , ret ) ; return 0; } dutor Something On GDB And Debugging
  29. 29. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Examples Do System Call 1 2 3 4 5 6 7 8 9 10 /* syscall write */ inline int as_write ( int fd , char * buf , size_t n ) { int ret ; asm ( " int $0x80 n t " : " = a " ( ret ) : " 0 " (1) , " D " ( fd ) , " S " ( buf ) , " d " ( n ) ); return ret ; } dutor Something On GDB And Debugging
  30. 30. Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Disassembling Using GDB dutor Something On GDB And Debugging

×