5.3.15 · HinglishBuild Systems & Toolchain

GDB debugging — breakpoints, watchpoints, step, next, backtrace

1,889 words9 min readRead in English

5.3.15 · Coding › Build Systems & Toolchain


KAUNSA tool kya hai


KYUN breakpoints kaam karte hain (first principles, koi magic nahi)

Toh ek breakpoint byte substitution + ek trap hai. Koi formula derive karne wala nahi hai, lekin neeche ek overhead model zaroor hai (neeche).


Cost model derive karna (Forecast-then-Verify)

Figure — GDB debugging — breakpoints, watchpoints, step, next, backtrace

Ek poora worked debugging session

Buggy program (bug.c):

int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++)
        result *= i;          // overflow for big n
    return result;
}
int main() {
    int x = factorial(5);
    return 0;
}

Common mistakes (Steel-man + fix)


Flashcards

GDB x86 pe software breakpoint implement karne ke liye kaun sa instruction byte insert karta hai?
0xCC (the INT3 trap), original byte save karne ke baad.
step aur next mein kya fark hai?
step function calls ke andar utarta hai; next unhe completion tak run karta hai aur current level par rehta hai.
Watchpoint kya hota hai (breakpoint se alag)?
Ruk jaata hai jab kisi expression ki value change ho (data), na ki jab koi line reach ho (location).
Software watchpoints slow kyun hain par hardware wale fast?
Software har line ko single-step karta hai aur value dobara read karta hai (); hardware CPU debug registers use karta hai jo write par automatically trap karte hain (~koi overhead nahi).
bt (backtrace) kya dikhata hai, aur crash pe pehle use kyun karo?
Active function calls ki chain (the stack); reveal karta hai ki crashing function ko kisne call kiya, wahi real bug aksar hoti hai.
Debugging se pehle -g -O0 se compile kyun karo?
-g debug symbols add karta hai (names/line numbers); -O0 optimizer ko variables delete/merge karne se rokta hai jinhe tum inspect karna chahte ho.
finish kya karta hai?
Current function ke return hone tak run karta hai aur uski return value print karta hai.
Software watchpoint ka slowdown formula kya hai?
jahan =lines, =per-line trap cost, =normal time.
x86 typically kitne hardware watchpoints allow karta hai?
~4 (debug registers DR0–DR3), har ek ≤8 bytes watch karta hai.
Tum kaun sa stack frame ke variables inspect kar rahe ho woh switch karne ka command?
frame <n> (ya up/down).

Recall Feynman: 12-saal ke bachche ko samjhao

Ek super-fast cartoon flipbook imagine karo jo ek second mein ek million pages flip karta hai — tum koi ek drawing nahi dekh sakte. Ek breakpoint ek chosen page par bookmark lagana hai taaki flipping wahan ruk jaaye aur tum dekh sako. Ek watchpoint kehna hai "jis pal bhi is character ki hat ka color badle wahan ruk jao" chahe koi bhi page ho. Step matlab hai "bilkul ek page palto, aur agar woh page tumhe kisi sub-story mein bheje, toh andar jaao." Next matlab hai "ek page palto, lekin agar woh tumhe kisi sub-story mein bheje, toh poori sub-story khatam hone do aur wapas aao." Backtrace unhe bookmarks ka trail hai jo dikhata hai ki tum yahan pahunchne ke liye kaun se doors khole the. Bugs fast motion mein chhupte hain; debugger time ko rokta hai taaki tum unhe pakad sako.

Connections

  • Compilation & -g debug symbols
  • Optimization levels -O0 -O2 -O3
  • Stack frames & calling conventions
  • Signals — SIGSEGV SIGTRAP
  • CPU debug registers DR0–DR7
  • Memory debugging — Valgrind & AddressSanitizer
  • The scientific method in debugging

Concept Map

freezes

inserts

implements

byte substitution + SIGTRAP

stops on value change

software impl

hardware impl

derives

near-zero overhead

descends into calls

steps over

prints

GDB debugger

Running program

Traps / INT3 0xCC

Breakpoint at line

Watchpoint on expression

Single-step whole program

CPU debug registers DR0-DR3

Slowdown S = 1 + Nc/T0

Fast but limited to 4

step s

Function calls

next n

backtrace bt

Call stack chain