惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - Franky
D
Docker
B
Blog RSS Feed
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
B
Blog
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Basic GDB Tutorial
Ashish Bhatia · 2010-06-01 · via ashishb.net

So, you believe there are bugs in your C/C++ code or you have encountered SEGFAULT while executing your code. What will you do?

  1. write a lot of printf statements at various places in your code under suspicion OR
  2. use gdb (GNU code debugger)

This article is about Approach 2

Disclaimer: This article is just a basic gdb tutorial and is nowhere meant to be comprehensive.

Using GDB

  1. Compile code using -ggdb switch For example, g++ -ggdb -o hellowworld helloworld.cc
  2. Run your code under gdb using gdb helloworld
  3. Set a breakpoint at the location where you suspect the problem to be. For example, if you suspect the problem could be at line 12 then set breakpoint at line 12 using break 12 likewise, you can set a breakpoint at the function also. For example, break main will stop the code execution as soon as the main function is called.
  4. now run the code using run
  5. If the program crashes then set the breakpoint at a different [much earlier] location.
  6. Now the code has stopped the execution, you can continue step-by-step execution using a step or you can continue the execution continuously using continue or you can print the values of various variables at this stage using print var_name
  7. To find the stack trace use backtrace
  8. use quit to exit gdb [and stop to stop a program that is still under execution]

Happy code debugging

Reference

  1. GDB Man page