What is GDB?
The GNU Debugger (GDB) is a command line tool used to debug C/C++ applications.
Installing GDB
Easiest way is to use the min-gw package on Windows.
Using GDB
Compile your program using ` gcc -Wall -g myprog.c `
- -Wall flag gives all warnings
- -g flag compiles in debug mode. Thus is preserves the identifiers and symbols
Start your program using gdb usign gdb a.out
If your program uses command line arguments you can do it using
gdb a.out argument1 argument2 ...
Demo
#include<stdio.h>
#include<string.h>
int main(){
int n = 5;
for(int i = 0; i < n; i++){
printf("%d\n", i);
}
}
Commands
list
Typing list
prints 10 lines of code each time you invoke it. Invoking list
again will print the next 10 lines.
list n
can be used to print 10 lines starting from the nth line where n is a number. Eg list 6
will print lines starting from 6 to 16.
list FUNCNAME
can be used to print a function. Eg list main
.
list START:END
can be used to print lines starting from START and ending at END