How to use GDB?
Till now you must have got idea about what is GDB and its commands.
And you must have installed GDB on your machine, if not follow steps mentioned in previous section to install GDB.
In this section, you will get hands on of using GDB to debug program.
GDB can be used in two ways:
- To Debug running program having logical error, crashing or hanging.
- To Debug coredump of pogram generated automatically when program crashes.
To start with, we will see first way to debug live/running program using GDB, and will see second way in advanced usage of GDB in upcoming tutorial.
Below steps will guide how to run program with GDB.
Step 1: Compile and Build program with debugging symbols
$ gcc -g main.cYou can see -g flag is provided to compile program. This will generate debug symbols of program. Which is necessary to debug any program with GDB. To know more about debugging symbols, visit here. You may skip it for now, read later upon.
Step 2: Run program with GDB
$ gdb a.outAs you can see above, GDB read debug symbols from a.out and GDB prompt appeared where we can execute GDB commands.
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
Find the GDB manual and other documentation resources online at:
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
Step 3: Use GDB commands to analyze and debug program
To debug program, use GDB commands mentioned in previous section. Just try executing some commands of Stepping and break points. Its ok even if you are not able to execute command successfully.
Step 4: Exit GDB
(gdb) quitType "quit" on GDB prompt to exit GDB.
This is how you compile and run your program with GDB.
In next section you will see Step 3 in more details.