Gdb or GNU Project Debugger is a great tool when you need to debug a program. You can set breakpoints, watch for value change of a variable, or even change a value for the program while it is halted at a point of it’s state, then continue, just to pick some of the features of gdb.
In this tutorial we will install gdb on RHEL 8, and test how it is working with a simple C application.
In this tutorial you will learn:
- How to install gdb
- How to compile a simple C application with debug symbols
- How to set breakpoints in the running application with gdb
- How to print actual values of given variables within the application
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Red Hat Enterprise Linux 8 |
Software | gdb 8.2 |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
How to install gdb in Redhat Linux 8 step by step instructions
For this tutorial, we’ll use a simple C application that sets some variables, print some text, and change the values of it’s variables later. It is built only to show some features of gdb
, and has no real world usage.
If you are not familiar with the C programming language, you can check out C development on Linux Introduction to get you started. For now consider the following source code, that we will put into the vars.c
text file:
#include <stdio.h>
int main()
{
int i = 1;
int j = 10;
printf("Variables set\n)";
i++;
j = 20;
printf("Variable values modified\n");
return 0;
}
We’ll use this simple program to test gdb
. You may notice from the code that the values variables i
and j
take will never be exposed under normal run of the program, so we will not know what their values where, and when. In this case it is a simple waste of memory, but think about a use case where i
would hold something important, maybe a hard-coded password, cheat-code or other treasure (or simply can’t find where your program’s calculations go wrong).
gdb
is part of theDevelopment Tools
package group, so if you have installed Development tools, you already have gdb. If not, you can install it by itself:# dnf install gdb
We’ll also need debuginfo for glibc for our tests:
# dnf debuginfo-install glibc-2.28-18.el8.x86_64
- We got the debugger tool, and we got the source code. For the debugging to be useful, we need to compile our program with debug symbols (we add the
-g
option):$ gcc -g -o vars vars.c
If we run our
vars
program, it will output the strings in theprintf
lines, but will not mentioni
andj
, as expected.$ ./vars Variables set Variable values modified
- We need to know the values of
i
andj
when they where first set, and before the program exited. Consider lines #7 and #10 (theprintf
lines) in the source. It would be ideal if we could stop the execution at those lines, get the values, then let the program loose again, etc. We’ll exactly do this to testgdb
. We start it with the compiledvars
executable as an argument:$ gdb vars GNU gdb (GDB) Red Hat Enterprise Linux 8.2-3.el8 Copyright (C) 2018 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-redhat-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from vars...done. (gdb)
gdb
reads the symbols in the program, and gives us the prompt to act. Thevars
program isn’t started at this point. We check our notes, and set abreakpoint
at line #7:(gdb) break 7 Breakpoint 1 at 0x40059c: file vars.c, line 7.
And line #10:
(gdb) break 10 Breakpoint 2 at 0x4005b1: file vars.c, line 10.
With the breakpoints set, we start the execution:
(gdb) run Starting program: /tmp/devel/vars Breakpoint 1, main () at vars.c:7 7 printf("Variables set\n");
Execution stops at the first breakpoint, and we can print the values of the hidden variables:
(gdb) print i $1 = 1 (gdb) print j $2 = 10
We got the first part of the needed information, let’s continue the execution:
(gdb) continue Continuing. Variables set Breakpoint 2, main () at vars.c:10 10 printf("Variable values modified\n");
We can print the values the same way at the next breakpoint:
(gdb) print i $3 = 2 (gdb) print j $4 = 20
We got all we needed. There are no more breakpoints left, so the application will exit normally after printing the last line of text.
(gdb) continue Continuing. Variable values modified [Inferior 1 (process 2330) exited normally] (gdb) q
We got the values of the secret variables, and tested that our
gdb
is as useful as it is meant to be.