This post will provide a brief introduction on how to use GDB & its commands.
- GDB is GNU debugger or C and C++
- GDB allows you to know the behavior of the program
- Compile with -g flag
How does it work?
- Start your program, specifying anything that might affect its behavior
- Make your program stop on specified conditions
- Examine what has happened, when your program has stopped
- Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another
How to compile?
To prepare your program for debugging with gdb, you must have to compile it with the -g flag.
$ gcc gdbTest.c -g -o gdbTest.out ( or ) $ gcc -g -o gdbTest.out gdbTest.c
How to start & examine?
$ gdb gdbTest.out ( or ) $ gdb ./gdbTest.out
To run the program until finished or error
$ r ( or ) $ run
To set a breakpoint at the beginning of the program
$ b main ( or ) $ br main
To set a breakpoint at a specific function
$ b <functionName> (or) $ br <functionName> ( or ) $ b <lineNumber> ( or ) $ br <lineNumber>
To set a breakpoint at a specific function in a specific file
$ b <fileName>:<functionName> ( or ) $ br <fileName>:<functionName>
To set a breakpoint at the current line
$ b ( or ) $ br
To continue the program until the next breakpoints or error
$ c ( or ) $ continue
To continue until the next line in the current function
$ n ( or ) $ next
To print program into the stack
To stepping or run the next line of the program
To print variable / expression value
$ p <varName> ( or ) $ print <varName> ( or ) $ print <expression>
To list the source code
$ l ( or ) $ list
To Delete all the breakpoints
$ q ( or ) $ quit
Hope, it would be helpful !!
To contribute :
” If you like Advance Computing and would like to contribute, you can mail your article to “email@example.com”. You will get credit as your name , email id, designation with article on this blog. “