SlideShare a Scribd company logo
How to debug using GDB
AIdrifter
COSCUP 2019 0817
作者簡介
ID : AIdrifter
綽號: Pinky
工作: IC廠打雜小工程師
興趣:鐵人三項 重訓 畫畫
HackMD
https://hackmd.io/@iST40ExoQtubds5LhuuaAw/rypeUnYSb?type=view
Linkedin
https://www.linkedin.com/in/pinkus-chang-a3b81b9b/
Outline
● 為什麼你要學GDB?
○ Debug 上班 vs 作業
○ 複雜的caller and callee
● GDB好處
○ run time知道code flow
○ 輕鬆觀察複雜的資料結構
● 常見的實用情境
○ 狀況一
○ thread apply all bt
○ commands
○ remote GDB
● 補充
○ valgrind
○ strace
○ addr2line
● Reference
為什麼你要學GDB?
Debug
上班 vs 作業
1. 錯綜複雜的compiler flag
a. 某某大尾客戶的客製化
b. 某某chip的特殊bug
2. 假日被客戶call來debug
a. 不知道客戶的環境
b. 客戶自己也不知道
c. 我要寫個慘字
3. 不是每個人都有良好的寫code
習慣
複雜的caller和callee
Q: 現在可憐Activity.onRestoreaIstanceState死掉了,
猜猜是那位嫌疑犯害死他的??
複雜的caller和callee
Q: 現在可憐Activity.onRestoreaIstanceState死掉了,猜猜是那位嫌疑犯害死他
的??
隔壁正妹同事
你厲害的mentor
自己昨天三點
熬夜寫完的
code
隔壁部門的帥哥同事
你主管十年前寫的code
新手作法
嘗試用偉大的printf()夾code
問題一:土法煉鋼 一個一個印很麻煩 很花時間
問題二:不知道其他人的模組怎麼編譯 也沒有文件!
a. 正妹同事請假去過情人節了
b. Mentor剛好在忙
c. Boss出差去
d. 隔壁的帥哥同事去夏威夷渡假了
別怕,有功夫,無懦夫,我們還有人民的救星GDB
GDB
GDB的好處
知道run time的code flow
gdb> b Activiry.onRestoreinstanceState
gdb> bt
自己昨天三點
熬夜寫完的
code
原來兇手是自己阿,隨便懷疑別人是不好的喔:)
輕鬆觀察複雜的資料結構
現在有一個vector<int> A,
你想要檢查是否存在為0的元素,
要怎麼寫這段code呢?
GDB
gdb> p A
gdb> p *l1
好處
可以快速驗證你的想法,節省掉編譯project的時間,想看看萬一今天你改的是
AOSP(Android Open Source Project)...
不用printf()一行一行夾code
生命可以浪費在更美好的事物上
常見的實用情境
狀況一
在開始前先下
gdb> lay src
可以顯示出source code
方便你trace code,
或是你根本沒有對方的code
退出: `ctrl+x` +` a`
狀況一
把斷點(break point)設在你懷疑的function
gdb> b dump
之後 c: continue 讓PC跳到該function位址
gdb> c
狀況一
想要觀察一下ptr所代的參數,
嘗試打印
gdb> p ptr
狀況一
想要知道dump的caller是誰,用以下command
gdb> bt
觀察
狀況一
bt確認caller後,沒有問題想要繼續前進,可以用n:next,直接執行下一行,或是用
c:continue 跳到下一個斷點。
gdb> n
gdb> c
想要看看自己有哪些斷點,可以用以下command
gdb> info break
commands
我只想觀察某function內的變數
不想一直壓continue和 p ptr
我還有其他作法嘛?
gdb > commands 1
> ....
> end
thread apply all bt
在多執行序環境,遇到race condition
或是dead lock,要如何知道threads 的
狀態去對症下藥?
gdb> thread apply all bt
remote gdb
嵌入式系統,通常都需要有gdbserver和client:
# gdb client (personal PC
> set solib-absolute-prefix ./symbols
> target remote 172.50.45.23:2345
> set pagination off
> handles $signal $action
> handle SIG38 nostop
> info handle
# gdb server (target board
gdbserver host:2345 --attach $(pidof
process_name)
./a.out
server
client >target remote 172.50.45.23:2345
意猶未盡嘛?其實除了gdb以外也有很多其他好用的工具...
補充
valgrind
可以用來做memory leak的偵測。
> gcc -g -o leak_main main.c
> valgrind --leak-check=full ./leak_main
strace
跟蹤程式執行的系統呼叫和接收的singal
列出呼叫到的system call。
> strace ./a.out
/proc/$pid/maps
沒有gdb或symbol的好夥伴
Referene
valgrind :C語言的記憶體洩漏(Memory Leak)偵測 - Valgrind
http://blog.yslin.tw/2011/05/mtrace-memory-leak.html
strace : 立你斯學習記錄
https://b8807053.pixnet.net/blog/post/3611602
addr2line : AIdrifter CS 浮生筆錄 : GNU Binary Utilities
https://hackmd.io/@iST40ExoQtubds5LhuuaAw/SyHdmr-bQ
solib-absolute-prefix 和solib-search-path的區別
https://blog.csdn.net/CaspianSea/article/details/16798735
C/C++程序在GDB調試狀態時的信號響應
http://www.wuzesheng.com/?p=1188
這邊感謝指導過我的前輩與同事,
如果你還有任何問題...
MAIL: aidrifter@gmail.com
HackMD: AIdrifter CS 浮生筆錄
https://hackmd.io/@iST40ExoQtubds5LhuuaAw/rypeUnYSb?type=view
謝謝各位的參與~

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

How to debug using GDB