SlideShare a Scribd company logo
1 of 91
Download to read offline
GDB Rocks!    GDB
             The GNU Project
Kent Chen       Debugger
Kent Chen (chenkaie)
chenkaie@gmail.com
http://chenkaie.blogspot.com
@chenkaie on GitHub
@chenkaie on SlideShare
@chenkaie on LinkedIn
@chenkaie on Twitter
為什麼要學
 GDB
Why everybody learns GDB?
非互動式/交談式
 Non-Interactive
   Debugging
strace - system call, signal

   ltrace - library call
printf / printk
 “打印”久了
也挺煩人的
Debugging by Endless Printing
GDB
Source-Level
  Debugger
互動式/交談式
 你叫它幹麻它就幹麻

Interactive Debugging
有了Debugger
Coding是彩色的
         - by Jserv/宅色夫大大

No Debugger, No Happy Coding
學會了GDB
我有種山頂洞人
學會用火的感動
                       - by 張至


張至是誰?! 我也不認識, Google到的,某某鄉民吧!
GDB
Front Ends
gdbtui
cgdb
ddd (Joe’s Fav)
insight
clewn / vim + gdb
pyclewn
gdbmgr
分享小弟
     入門經驗
Sharing my real-world GDB experience
牛刀小試
幼幼班
GDB Beginner’s training
Change memory contents on-the-fly
Change memory contents on-the-fly
stack backtrace
Attach to a process
Jump $pc (program counter)
core dump
core dump (cont.)
core dump (cont.)
Patch binary file
Patch binary file (cont.)

$objdump -d -S -l -shrt dump1.out   Change “ef01” to “ef00”
奇技淫巧
進階班
Advanced GDB Tricks
奇技淫巧:
奇異而眩人耳目の
  技能或事物

    (from 教育部國語辭典)
SIGSEGV + GDB
C interpreter
1. $ gdb `which gdb`
2. (gdb) start
3. Enjoy your world…
• Example:
  (gdb)  p  1  +  2  +  abs(-­‐3)
  (gdb)  p  strcmp("VIVOTEK",  "AXIS")
  (gdb)  x/s  getenv(“HOME”)
  (gdb)  p  (char*)getenv("HOME")
  (gdb)  p  (char)*getenv("HOME")
  (gdb)  p  printf("%dn",  12345678)
Signal Handler
Terminal hang / Reboot PC
  You have to close terminal (e.g., PuTTY, iTerm,...)
Conventional solution
                          (gdb)  handle  SIGHUP

  GNU Screen / Tmux       Signal                Stop            Print      Pass  to  program  Description
                          SIGHUP                Yes              Yes          Yes                          Hangup
                          (gdb)  handle  SIGHUP  nopass
                          Signal                Stop            Print      Pass  to  program  Description
  nohup                   SIGHUP                Yes              Yes          No                            Hangup

                          Program  received  signal  SIGHUP,  Hangup.
GDB solution              0x0000003ac7a954e0  in  __nanosleep_nocancel  ()  from  /lib64/libc.so.6
                          (gdb)
                          Continuing.

  $ gdb [program] [pid]
  (gdb) handle SIGHUP nopass
  (gdb) continue
經典案例
實戰探討
A real-world case study
案例一、
 Case 1
GNU C Library
   (glibc)
  debugging / 除錯
Why?
追求
    卓越
Pursuit of excellence :)
DieLink
呆吝蚵
江湖中
流傳已久
A well-know issue
某某
Daemon
 之死
Process crash issue
dmesg
cat /proc/`pidof configer`/maps
SIGSEGV
@libc-2.5.90.so
WTF!!
不會吧(驚)
ㄎㄎ
 我有學過
Core dump
無敵の gdb core dump
backtrace (bt)
_IO_strn_overflow ()
     vfprintf ()

C language !?
WTF!!
不會吧(驚驚)
欲窮千里目
更上一層樓
ㄎㄎ我有學過
gdb frame UP
frame [index] / up / down
WTF!!
ARM assembly
組合語言
 什麼鬼呀
大學修完課後就通通還給老師了
C Code & ARM assembly
看似
專業 Pro
 Looks “GEEK”
In fact
實際上
發現 gcc -O3
    TMD
  實在太難看了
It’s god damn hard to read after gcc -O3
我們需要
Source Level
 Debugging
Use the
Source
Loser... Orz
May
The Source
  Be With
    You
How?
RTFM
   Read The
Fucking Manual
load by symbol-file cmd
Re-build
debug version
shared library
  with "-g"
set solib-absolute-prefix
Source be with You
發現傳入
 snprintf()
の資料都正確
OMFG!
電梯繼續向下
gdb frame down
到了
  /lib/libc.so.6
-> libc-2.5.90.so
Shit!
 若仿照
上面作法
難不成要自己
 build debug
版のlibc-2.5.90
Oh No !
使用大廠の
偷偷Solution
你有權利
Say NO
MontaVista
已經幫我們
 Build 好了
lib*.*.so.*.debug
glibc source level debug
DEMO
Null pointer access issue
多虧了
神器 GDB
我們終於學會
Shared Library
  Debugging
某Daemon之死
至今仍是個謎  (驚)
案例二、
 Case 2
劫持 FDs
File Descriptors Hijacking
時間有限
   下回揭曉
File Descriptor Hijacking / 劫持 FDs 之奇技淫巧
Reference
快快樂樂學 GNU Debugger (gdb) Part I + II (Jserv)
  http://jserv.sayya.org/debugger/
http://pyclewn.sourceforge.net/
http://clewn.sourceforge.net/
http://reverse.put.as/
GDB的妙用 (vgod)
[GDB Tricks] File Descriptor Hijacking / 劫持 FDs 之奇技淫巧

More Related Content

What's hot

What's hot (20)

Learn C Programming Language by Using GDB
Learn C Programming Language by Using GDBLearn C Programming Language by Using GDB
Learn C Programming Language by Using GDB
 
What Can Compilers Do for Us?
What Can Compilers Do for Us?What Can Compilers Do for Us?
What Can Compilers Do for Us?
 
用十分鐘 向jserv學習作業系統設計
用十分鐘  向jserv學習作業系統設計用十分鐘  向jserv學習作業系統設計
用十分鐘 向jserv學習作業系統設計
 
淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道 淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道
 
Embedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile DevicesEmbedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile Devices
 
The Internals of "Hello World" Program
The Internals of "Hello World" ProgramThe Internals of "Hello World" Program
The Internals of "Hello World" Program
 
Debugging Applications with GNU Debugger
Debugging Applications with GNU DebuggerDebugging Applications with GNU Debugger
Debugging Applications with GNU Debugger
 
Insecure coding in C (and C++)
Insecure coding in C (and C++)Insecure coding in C (and C++)
Insecure coding in C (and C++)
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 
Virtual Machine Constructions for Dummies
Virtual Machine Constructions for DummiesVirtual Machine Constructions for Dummies
Virtual Machine Constructions for Dummies
 
How shit works: the CPU
How shit works: the CPUHow shit works: the CPU
How shit works: the CPU
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Worksfrom Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDB
 
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)
 
Gstreamer Basics
Gstreamer BasicsGstreamer Basics
Gstreamer Basics
 
Basic of virtual memory of Linux
Basic of virtual memory of LinuxBasic of virtual memory of Linux
Basic of virtual memory of Linux
 
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
 
あるRISC-V CPUの 浮動小数点数(異常なし)
あるRISC-V CPUの 浮動小数点数(異常なし)あるRISC-V CPUの 浮動小数点数(異常なし)
あるRISC-V CPUの 浮動小数点数(異常なし)
 

Viewers also liked

효과적인 임베디드 디버깅 환경구축
효과적인 임베디드 디버깅 환경구축효과적인 임베디드 디버깅 환경구축
효과적인 임베디드 디버깅 환경구축
guest0ad316e
 
How to debug a perl script using gdb
How to debug a perl script using gdbHow to debug a perl script using gdb
How to debug a perl script using gdb
akirahiguchi
 
Hunting segfaults (for beginners)
Hunting segfaults (for beginners)Hunting segfaults (for beginners)
Hunting segfaults (for beginners)
uwevoelker
 
Debugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB TricksDebugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB Tricks
dutor
 
淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDB
Jim Chang
 
簡易的面試心得分享
簡易的面試心得分享簡易的面試心得分享
簡易的面試心得分享
Jack Wang
 

Viewers also liked (18)

面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
Tmux tips and_tricks
Tmux tips and_tricksTmux tips and_tricks
Tmux tips and_tricks
 
Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1
 
효과적인 임베디드 디버깅 환경구축
효과적인 임베디드 디버깅 환경구축효과적인 임베디드 디버깅 환경구축
효과적인 임베디드 디버깅 환경구축
 
How to debug a perl script using gdb
How to debug a perl script using gdbHow to debug a perl script using gdb
How to debug a perl script using gdb
 
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
 
Hunting segfaults (for beginners)
Hunting segfaults (for beginners)Hunting segfaults (for beginners)
Hunting segfaults (for beginners)
 
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
 
Crash Dump Analysis 101
Crash Dump Analysis 101Crash Dump Analysis 101
Crash Dump Analysis 101
 
Debugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB TricksDebugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB Tricks
 
淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDB
 
簡易的面試心得分享
簡易的面試心得分享簡易的面試心得分享
簡易的面試心得分享
 
Aag c45 697761
Aag c45 697761Aag c45 697761
Aag c45 697761
 
Reverse, Multi-Process and Non-Stop Debugging come to the CDT
Reverse, Multi-Process and Non-Stop Debugging come to the CDTReverse, Multi-Process and Non-Stop Debugging come to the CDT
Reverse, Multi-Process and Non-Stop Debugging come to the CDT
 
Real time debugging: using non-intrusive tracepoints to debug live systems
Real time debugging: using non-intrusive tracepoints to debug live systemsReal time debugging: using non-intrusive tracepoints to debug live systems
Real time debugging: using non-intrusive tracepoints to debug live systems
 
面試心得
面試心得面試心得
面試心得
 
Udev
UdevUdev
Udev
 
LAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel AwarenessLAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel Awareness
 

Similar to GDB Rocks!

gdb-tutorial.pdf
gdb-tutorial.pdfgdb-tutorial.pdf
gdb-tutorial.pdf
ligi14
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handout
Suraj Kumar
 
ELC-E Linux Awareness
ELC-E Linux AwarenessELC-E Linux Awareness
ELC-E Linux Awareness
Peter Griffin
 
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
 
Ctrl-C redesign for gcc cauldron in 2022 in prague
Ctrl-C redesign for gcc cauldron in 2022 in pragueCtrl-C redesign for gcc cauldron in 2022 in prague
Ctrl-C redesign for gcc cauldron in 2022 in prague
ssuser866937
 
Debug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code Meetup
Debug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code MeetupDebug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code Meetup
Debug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code Meetup
Jian-Hong Pan
 

Similar to GDB Rocks! (20)

lab1-ppt.pdf
lab1-ppt.pdflab1-ppt.pdf
lab1-ppt.pdf
 
Debuging like a pro
Debuging like a proDebuging like a pro
Debuging like a pro
 
Debugging Modern C++ Application with Gdb
Debugging Modern C++ Application with GdbDebugging Modern C++ Application with Gdb
Debugging Modern C++ Application with Gdb
 
gdb-tutorial.pdf
gdb-tutorial.pdfgdb-tutorial.pdf
gdb-tutorial.pdf
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handout
 
Usage of GDB
Usage of GDBUsage of GDB
Usage of GDB
 
GDB tutorial
GDB tutorialGDB tutorial
GDB tutorial
 
ELC-E Linux Awareness
ELC-E Linux AwarenessELC-E Linux Awareness
ELC-E Linux Awareness
 
LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness
 
05-Debug.pdf
05-Debug.pdf05-Debug.pdf
05-Debug.pdf
 
Introduction to segmentation fault handling
Introduction to segmentation fault handling Introduction to segmentation fault handling
Introduction to segmentation fault handling
 
GPU Accelerated Domain Decomposition
GPU Accelerated Domain DecompositionGPU Accelerated Domain Decomposition
GPU Accelerated Domain Decomposition
 
Ctrl-C redesign for gcc cauldron in 2022 in prague
Ctrl-C redesign for gcc cauldron in 2022 in pragueCtrl-C redesign for gcc cauldron in 2022 in prague
Ctrl-C redesign for gcc cauldron in 2022 in prague
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applications
 
Отладка в GDB
Отладка в GDBОтладка в GDB
Отладка в GDB
 
Gccgdb
GccgdbGccgdb
Gccgdb
 
Extending GDB with Python
Extending GDB with PythonExtending GDB with Python
Extending GDB with Python
 
Debug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code Meetup
Debug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code MeetupDebug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code Meetup
Debug C/C++ Programs More Comfortably @ 2014.12.14 Trace Code Meetup
 
GNU Debugger
GNU DebuggerGNU Debugger
GNU Debugger
 
Gnu debugger
Gnu debuggerGnu debugger
Gnu debugger
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

GDB Rocks!