, #include , , int main () , { , int a = 2; , // infinite loop, i will always be equal to or greater then zero , for(int i = 0; i >= 0; i++) , { , printf(DQ\n%d DQ, i); , } , return 0; , } , ,
, , , Source file needs to be compiled with the g option. , Compile debug-test.c: , gcc -o -g debug-test debug-test.c , , Start the debugger: , (command line) gdb ./debug-test , , Use the layout: , (gdb) layout next , , Set a breakpoint at function main: , (gdb) break main , , Start / run: , (gdb) run , , Watch a variable: , watch (variable) , , Next: , (gdb) n , , , ,
, GDB offers a big list of commands, however the following commands are the ones used most frequently: , , b main - Puts a breakpoint at the beginning of the program , , b - Puts a breakpoint at the current line , , b N - Puts a breakpoint at line N , , b +N - Puts a breakpoint N lines down from the current line , , b fn - Puts a breakpoint at the beginning of function DQfnDQ , , d N - Deletes breakpoint number N , , info break - list breakpoints , , r - Runs the program until a breakpoint or error , , c - Continues running the program until the next breakpoint or error , , f - Runs until the current function is finished , , s - Runs the next line of the program , , s N - Runs the next N lines of the program , , n - Like s, but it does not step into functions , , u N - Runs until you get N lines in front of the current line , , p var - Prints the current value of the variable DQvarDQ , , bt - Prints a stack trace , , u - Goes up a level in the stack , , d - Goes down a level in the stack , , q - Quits gdb ,