About Me

My photo
Computer Science & Engineering Student @NIT Durgapur,Senior

Saturday, February 19, 2011

GNU debugger(GDB)


Gdb is a debugger for C (and C++). It allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line. It uses a command line interface.

Quick tutorial for using the debugger .
A.Compiling a program(using debugging symbol):
1.C program:gcc -g -o pnm pnm.c
(just add -g symbol in the compilation command whatever command u use to compile normally)
2.C++ program:g++ -g -o pnm pnm.cpp
(replace gcc with g++)
3.Assembly language:two commands
->as -gstabs -o pnm.o pnm.s
->ld -o pnm pnm.o
(Just add -gstabs to the normal compilation command to load with debugging symbols)
Here "pnm" satands for your program file name
B.Running the program compiled:(For all above language)
->./pnm
C.Start the debugger
->gdb program_name
This only starts the debugger; it does not start running the program in the debugger

D.Look at the source code and set a breakpoint  a line number(say x).
->b  x
or   break x
or   b main                       //to set break point at the initiation of program
E.Now, start to run the program in the debugger.
->run
F.To move forward(means to execute next line)
->next
G.To know the value of variables while debugging
->print variable_name
H.To run the program normally upto the next breakpoint
->continue
I.To delete a breakpoint
->delete line_number
J.To print a some source code around a line number:
->list line_number
K.To run a shell command inside the debugger
->SHELL command
L.To exit from debugger
->q

Hope this tutorial help you learn basics of GNU Debugger.





No comments:

Post a Comment