Operating System CentOS

How to Install and Use GDB on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9

Install the packaged GDB on the RHEL 9 family, get debug symbols for system libraries, and step through a first debugging session and a core dump.

This guide installs the GNU Debugger (GDB) on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9, sets up debug symbols for system libraries, and walks through a first debugging session on a small C program. At the end you can set breakpoints, inspect variables, read a backtrace and open a core dump.

CentOS 6 reached end of life in November 2020. It shipped GDB 7.2, which is why older tutorials built a newer GDB from source. On the RHEL 9 family that is no longer necessary: the packaged debugger is current (GDB 16.3 on AlmaLinux 9.8) and is updated with the distribution.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9.
  • A user with sudo rights. The examples use a user called deploy.
  • The default BaseOS and AppStream repositories. No third-party repository is needed.

Install GDB and a compiler

GDB is in the AppStream repository. Install it together with GCC so you can build the test program:

sudo dnf install gdb gcc

Check the version:

gdb --version

The same source package also provides gdb-gdbserver (for remote debugging), gdb-minimal (a stripped-down build used by packaging tools) and gdb-doc. Install gdb-gdbserver only on machines where you want to attach from another host.

A first debugging session

Create a small program with a deliberate bug. Save it as ~/average.c:

#include <stdio.h>

static int average(const int *values, int count)
{
    int sum = 0;
    for (int i = 0; i <= count; i++) {
        sum += values[i];
    }
    return sum / count;
}

int main(void)
{
    int data[] = { 10, 20, 30, 40 };
    printf("average: %d\n", average(data, 4));
    return 0;
}

Compile it with debug information (-g) and without optimisation (-O0), so that every source line and variable is visible to the debugger:

gcc -g -O0 -o average average.c

The loop reads one element past the end of the array, so the result is wrong. Start the program under GDB:

gdb ./average

At the (gdb) prompt, set a breakpoint on the function, run the program and step through it:

break average
run
next
next
print count
print values[0]@4
display sum
next

What these commands do:

CommandShort formPurpose
break averagebStop when the function average is entered. break average.c:7 stops at a line.
runrStart the program. Arguments go after run.
next / stepn / sExecute one line; step also enters called functions.
print exprpEvaluate an expression. values[0]@4 prints four array elements.
display exprPrint the expression again after every step.
backtracebtShow the call stack.
continuecRun until the next breakpoint or the end.
quitqLeave GDB.

To catch the bad iteration directly, use a conditional breakpoint on the line that adds to sum:

break average.c:7 if i == count
continue
print i
backtrace

GDB stops when i is 4, an index that does not exist in a four-element array. Change <= to < in the source, recompile and the program prints average: 25.

For a view of the source while you step, start GDB with gdb -tui ./average or press Ctrl+X then A inside a session.

Install debug symbols for system libraries

Your own program has symbols because you compiled it with -g. Distribution packages keep theirs in separate -debuginfo packages. When they are missing, GDB prints a line starting with Missing separate debuginfos, use: dnf debuginfo-install followed by the package names. There are two ways to get them.

dnf debuginfo-install

The debuginfo-install plugin enables the distribution's debuginfo repositories for the transaction and installs the matching -debuginfo and -debugsource packages. For the C library:

sudo dnf debuginfo-install glibc

If the subcommand is not available, install the plugin package first with sudo dnf install dnf-plugins-core. Debuginfo packages must match the installed package version exactly, so run sudo dnf upgrade first if the plugin reports that no matching debuginfo is available. The packages are large; remove them with sudo dnf remove 'glibc-debuginfo*' when you are finished.

debuginfod

GDB on the RHEL 9 family is built with debuginfod support: instead of installing packages, it can download symbols and source files on demand from a debuginfod server, keyed by the build ID of each binary. The servers are taken from the DEBUGINFOD_URLS environment variable. Check whether your system sets one:

echo "$DEBUGINFOD_URLS"

If the output is empty, no server is configured and GDB will not ask to download anything; use dnf debuginfo-install instead. The CentOS project runs a public server for CentOS Stream at https://debuginfod.centos.org/. To use it for one shell session:

export DEBUGINFOD_URLS="https://debuginfod.centos.org/"
gdb ./average

With a server configured, an interactive GDB session asks Enable debuginfod for this session? the first time it needs symbols. To answer permanently, add this line to ~/.gdbinit:

set debuginfod enabled on

Use off instead of on on servers that must not make outbound requests. Inside GDB, show debuginfod urls lists the servers in use.

Open a core dump

On the RHEL 9 family, crashes are captured by systemd-coredump, so you do not need to hunt for core files in the working directory. List recorded crashes and open the most recent one for a program in GDB:

coredumpctl list
coredumpctl debug average

Inside the session, bt shows where the program died and info locals prints the variables of the selected frame. Use sudo for crashes of programs that ran as another user. To attach to a process that is still running, use sudo gdb -p PID.

GCC Toolset GDB packages

The RHEL 9 family also offers optional GCC Toolsets that install under /opt/rh without replacing system packages. Two of them contain their own GDB build:

PackageGDB version (AlmaLinux 9.8)
gdb (system package)16.3
gcc-toolset-13-gdb12.1
gcc-toolset-12-gdb11.2

The system package is now newer than both toolset builds, and GCC Toolset 14 and 15 do not ship a GDB package in the AlmaLinux 9 AppStream repository. Use the system gdb even when you compile with a newer toolset compiler. If you need a toolset build for a reproducible older environment, install it and run it through scl:

sudo dnf install gcc-toolset-13-gdb
scl enable gcc-toolset-13 'gdb --version'

Troubleshooting

  • "No symbol table is loaded" or "No debugging symbols found": the binary was built without -g, or it was stripped. Rebuild with -g.
  • Variables show as <optimized out>: the program was compiled with optimisation. Rebuild with -O0 (or -Og) for debugging.
  • "ptrace: Operation not permitted" when attaching: you can only attach to your own processes. Use sudo gdb -p PID for processes owned by another user.
  • coredumpctl shows a crash but no core: the dump was larger than the configured limit or has been cleaned up. Limits are set in /etc/systemd/coredump.conf.

Frequently asked questions

Do I still need to build GDB from source to get a recent version?

No. That was a CentOS 6 problem. The RHEL 9 family rebases the packaged GDB during the life of the release, and the package receives security and bug fixes through dnf upgrade. A source build is only worth it if you need a feature from a release newer than the packaged one.

Can I debug a program on another server?

Yes. Install gdb-gdbserver on the target, start the program with gdbserver :2345 ./average, and connect from your workstation with target remote 203.0.113.10:2345 inside GDB. The connection is not encrypted or authenticated, so tunnel the port through SSH rather than opening it in the firewall.

Is it safe to keep GDB installed on a production server?

GDB itself does not run any service. Attaching to a process pauses it while you inspect it, so on production prefer collecting a core dump with coredumpctl and analysing it afterwards, ideally on another machine with the same package versions.

Does the C++ standard library print readably?

GDB loads Python pretty-printers for libstdc++ automatically when they are installed, so print on a std::string or std::vector shows the contents rather than the internal fields. If you see raw structures, check info pretty-printer to confirm the libstdc++ printers are registered.

Share story:

Continue reading other guides on How VPS.