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
how to set up breakpoints
how to map instructions with source code

dutor

Something On GDB And Debugging
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
Debugger Under the Hood Basics Bonus A Little Assembly

breakpoints

hardware, the debug registers
software, INT 3

dutor

Something On GDB And Debugging
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
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
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
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
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
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
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
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
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
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
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
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
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
That’s All.
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
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
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
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
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
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
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
Debugger Under the Hood Basics Bonus A Little Assembly

Inlined Assembly Examples Disassembling

Disassembling
Using GDB

dutor

Something On GDB And Debugging

Debugger Principle Overview & GDB Tricks

  • 1.
    Something On GDBAnd Debugging dutor Gdutor@gmail.com TAIR September 26, 2013
  • 2.
  • 3.
    Outline 1 Debugger Under theHood 2 GDB the Basics
  • 4.
    Outline 1 Debugger Under theHood 2 GDB the Basics 3 Bonus
  • 5.
    Outline 1 Debugger Under theHood 2 GDB the Basics 3 Bonus 4 A Little Assembly
  • 6.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood Basics Bonus A Little Assembly breakpoints hardware, the debug registers software, INT 3 dutor Something On GDB And Debugging
  • 9.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
  • 23.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood 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.
    Debugger Under theHood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling Disassembling Using GDB dutor Something On GDB And Debugging