SlideShare a Scribd company logo
1 of 30
Download to read offline
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

More Related Content

What's hot

Ghost Vulnerability CVE-2015-0235
Ghost Vulnerability CVE-2015-0235Ghost Vulnerability CVE-2015-0235
Ghost Vulnerability CVE-2015-0235Rajivarnan (Rajiv)
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPMiller Lee
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingcppfrug
 
C++ AMP 실천 및 적용 전략
C++ AMP 실천 및 적용 전략 C++ AMP 실천 및 적용 전략
C++ AMP 실천 및 적용 전략 명신 김
 
Pepe Vila - Cache and Syphilis [rooted2019]
Pepe Vila - Cache and Syphilis [rooted2019]Pepe Vila - Cache and Syphilis [rooted2019]
Pepe Vila - Cache and Syphilis [rooted2019]RootedCON
 
計算機性能の限界点とその考え方
計算機性能の限界点とその考え方計算機性能の限界点とその考え方
計算機性能の限界点とその考え方Naoto MATSUMOTO
 
This is not your father's monitoring.
This is not your father's monitoring.This is not your father's monitoring.
This is not your father's monitoring.Mathias Herberts
 
Artimon - Apache Flume (incubating) NYC Meetup 20111108
Artimon - Apache Flume (incubating) NYC Meetup 20111108Artimon - Apache Flume (incubating) NYC Meetup 20111108
Artimon - Apache Flume (incubating) NYC Meetup 20111108Mathias Herberts
 
Vc4c development of opencl compiler for videocore4
Vc4c  development of opencl compiler for videocore4Vc4c  development of opencl compiler for videocore4
Vc4c development of opencl compiler for videocore4nomaddo
 
Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2VMware Tanzu
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)Sławomir Zborowski
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言Simen Li
 
Histogram dan Segmentasi 2
Histogram dan Segmentasi 2Histogram dan Segmentasi 2
Histogram dan Segmentasi 2Lusiana Diyan
 

What's hot (20)

Ghost Vulnerability CVE-2015-0235
Ghost Vulnerability CVE-2015-0235Ghost Vulnerability CVE-2015-0235
Ghost Vulnerability CVE-2015-0235
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 
user2015 keynote talk
user2015 keynote talkuser2015 keynote talk
user2015 keynote talk
 
Introduction to Data Oriented Design
Introduction to Data Oriented DesignIntroduction to Data Oriented Design
Introduction to Data Oriented Design
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogramming
 
A Step Towards Data Orientation
A Step Towards Data OrientationA Step Towards Data Orientation
A Step Towards Data Orientation
 
Lec06
Lec06Lec06
Lec06
 
C++ AMP 실천 및 적용 전략
C++ AMP 실천 및 적용 전략 C++ AMP 실천 및 적용 전략
C++ AMP 실천 및 적용 전략
 
C&cpu
C&cpuC&cpu
C&cpu
 
Dafunctor
DafunctorDafunctor
Dafunctor
 
Pepe Vila - Cache and Syphilis [rooted2019]
Pepe Vila - Cache and Syphilis [rooted2019]Pepe Vila - Cache and Syphilis [rooted2019]
Pepe Vila - Cache and Syphilis [rooted2019]
 
計算機性能の限界点とその考え方
計算機性能の限界点とその考え方計算機性能の限界点とその考え方
計算機性能の限界点とその考え方
 
This is not your father's monitoring.
This is not your father's monitoring.This is not your father's monitoring.
This is not your father's monitoring.
 
Artimon - Apache Flume (incubating) NYC Meetup 20111108
Artimon - Apache Flume (incubating) NYC Meetup 20111108Artimon - Apache Flume (incubating) NYC Meetup 20111108
Artimon - Apache Flume (incubating) NYC Meetup 20111108
 
Vc4c development of opencl compiler for videocore4
Vc4c  development of opencl compiler for videocore4Vc4c  development of opencl compiler for videocore4
Vc4c development of opencl compiler for videocore4
 
GCC
GCCGCC
GCC
 
Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
Histogram dan Segmentasi 2
Histogram dan Segmentasi 2Histogram dan Segmentasi 2
Histogram dan Segmentasi 2
 

Viewers also liked

From printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debuggingFrom printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debuggingThe Linux Foundation
 
Advanced Tracing features using GDB and LTTng
Advanced Tracing features using GDB and LTTngAdvanced Tracing features using GDB and LTTng
Advanced Tracing features using GDB and LTTngmarckhouzam
 
Interpreters & Debuggers
Interpreters  &  DebuggersInterpreters  &  Debuggers
Interpreters & DebuggersMalek Sumaiya
 
Introduction to debugging
Introduction to debuggingIntroduction to debugging
Introduction to debuggingPeter Chang
 
LAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel AwarenessLAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel AwarenessLinaro
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDBDavid Khosid
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDBLinaro
 
Développement informatique : Gestion de projet, versioning, debugging, testin...
Développement informatique : Gestion de projet, versioning, debugging, testin...Développement informatique : Gestion de projet, versioning, debugging, testin...
Développement informatique : Gestion de projet, versioning, debugging, testin...ECAM Brussels Engineering School
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to DebuggersSaumil Shah
 
The Art Of Debugging
The Art Of DebuggingThe Art Of Debugging
The Art Of Debuggingsvilen.ivanov
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsXiaozhe Wang
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingAnil Kumar Pugalia
 
An Introduction to Kubernetes
An Introduction to KubernetesAn Introduction to Kubernetes
An Introduction to KubernetesImesh Gunaratne
 

Viewers also liked (20)

From printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debuggingFrom printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debugging
 
Advanced Tracing features using GDB and LTTng
Advanced Tracing features using GDB and LTTngAdvanced Tracing features using GDB and LTTng
Advanced Tracing features using GDB and LTTng
 
Interpreters & Debuggers
Interpreters  &  DebuggersInterpreters  &  Debuggers
Interpreters & Debuggers
 
Mod04 debuggers
Mod04 debuggersMod04 debuggers
Mod04 debuggers
 
Introduction to debugging
Introduction to debuggingIntroduction to debugging
Introduction to debugging
 
LAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel AwarenessLAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel Awareness
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDB
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
kubernetes, pourquoi et comment
kubernetes, pourquoi et commentkubernetes, pourquoi et comment
kubernetes, pourquoi et comment
 
GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
 
Notes on Debugging
Notes on DebuggingNotes on Debugging
Notes on Debugging
 
Debugging
DebuggingDebugging
Debugging
 
Développement informatique : Gestion de projet, versioning, debugging, testin...
Développement informatique : Gestion de projet, versioning, debugging, testin...Développement informatique : Gestion de projet, versioning, debugging, testin...
Développement informatique : Gestion de projet, versioning, debugging, testin...
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to Debuggers
 
The Art Of Debugging
The Art Of DebuggingThe Art Of Debugging
The Art Of Debugging
 
Debugging
DebuggingDebugging
Debugging
 
Debugging
DebuggingDebugging
Debugging
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
An Introduction to Kubernetes
An Introduction to KubernetesAn Introduction to Kubernetes
An Introduction to Kubernetes
 

Similar to GDB Debugger Guide for C/C++ Program Debugging

Debugging Applications with GNU Debugger
Debugging Applications with GNU DebuggerDebugging Applications with GNU Debugger
Debugging Applications with GNU DebuggerPriyank Kapadia
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기Ji Hun Kim
 
Gpu workshop cluster universe: scripting cuda
Gpu workshop cluster universe: scripting cudaGpu workshop cluster universe: scripting cuda
Gpu workshop cluster universe: scripting cudaFerdinand Jamitzky
 
Swug July 2010 - windows debugging by sainath
Swug July 2010 - windows debugging by sainathSwug July 2010 - windows debugging by sainath
Swug July 2010 - windows debugging by sainathDennis Chung
 
淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDBJim Chang
 
PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...
PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...
PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...Faisal Akber
 
OpenTuesday: Neues aus der RRDtool Welt
OpenTuesday: Neues aus der RRDtool WeltOpenTuesday: Neues aus der RRDtool Welt
OpenTuesday: Neues aus der RRDtool WeltDigicomp Academy AG
 
LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness Peter Griffin
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugginglibfetion
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterakaptur
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLinaro
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Data Con LA
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkPatrick Wendell
 

Similar to GDB Debugger Guide for C/C++ Program Debugging (20)

Debugging Applications with GNU Debugger
Debugging Applications with GNU DebuggerDebugging Applications with GNU Debugger
Debugging Applications with GNU Debugger
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
XS Boston 2008 Debugging Xen
XS Boston 2008 Debugging XenXS Boston 2008 Debugging Xen
XS Boston 2008 Debugging Xen
 
Boosting Developer Productivity with Clang
Boosting Developer Productivity with ClangBoosting Developer Productivity with Clang
Boosting Developer Productivity with Clang
 
Gpu workshop cluster universe: scripting cuda
Gpu workshop cluster universe: scripting cudaGpu workshop cluster universe: scripting cuda
Gpu workshop cluster universe: scripting cuda
 
Swug July 2010 - windows debugging by sainath
Swug July 2010 - windows debugging by sainathSwug July 2010 - windows debugging by sainath
Swug July 2010 - windows debugging by sainath
 
05-Debug.pdf
05-Debug.pdf05-Debug.pdf
05-Debug.pdf
 
淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDB
 
Отладка в GDB
Отладка в GDBОтладка в GDB
Отладка в GDB
 
PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...
PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...
PGCon 2014 - What Do You Mean my Database Server Core Dumped? - How to Inspec...
 
OpenTuesday: Neues aus der RRDtool Welt
OpenTuesday: Neues aus der RRDtool WeltOpenTuesday: Neues aus der RRDtool Welt
OpenTuesday: Neues aus der RRDtool Welt
 
LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Experimental dtrace
Experimental dtraceExperimental dtrace
Experimental dtrace
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
ERTS UNIT 3.ppt
ERTS UNIT 3.pptERTS UNIT 3.ppt
ERTS UNIT 3.ppt
 
Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreter
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache Spark
 

Recently uploaded

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Recently uploaded (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

GDB Debugger Guide for C/C++ Program Debugging

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