How To Install GDB on CentOS 6

GDB is a debugger for C, C++, Objective-C, Pascal, Fortran, Go, D, OpenCL C, Ada, and Modula-2.

CentOS is based on RHEL (Red Hat Enterprise Linux). One of the main goals of RHEL is to be a stable server operating system, meaning that newer versions of software packages aren’t always available.

At the time of writing, CentOS 6 offers GDB v7.2. The GDB team however, recently released code for v7.91.

It is officially suggested to run a different Linux distro in order to use a newer version of GDB. This isn’t always ideal. Fortunately, it is possible to install the newer versions of GDB on CentOS 6. Since GDB is a debugger rather than a system core component, you are fairly safe to use a newer version.

This article explains how to install both supported and unsupported versions of GDB on CentOS 6.

I will also explain how to set up GDB to give you easier-to-read debugging information when using the C++ Standard Library (like string) and the Standard Template Library (like vector). This feature is called pretty printing.

Log in to your VPS, and set up your user account

  1. Login to your VPS. This can be done by clicking “View Console” in the Vultr control panel, or with an SSH client.

    (a) Login as root.

    (b) Create your own user account. Set the password.

    adduser <username>passwd <username>

    (c) Grant the user sudo access.

    visudo    After the line "root   ALL=(ALL)   ALL"    Add the line "<username>   ALL=(ALL)   ALL"    --- If you aren't familiar with vi, go to the line "root   ALL=(ALL)   ALL".    ---   Hit "o" to create a new line after that line and enter insert mode.    ---   Type "<username>   ALL=(ALL)   ALL".    ---   Hit ESC.    ---   Type "ZZ" to save.

    (d) Log out as root, then log back in with your user account. It is much more secure to never actually log in as root. Using sudo is a better practice.

If you want to install the officially supported (older) version of GDB

  1. Install GDB.

    sudo yum install gdb
  2. Check the installed version, and see its location.

    gdb --version    May say: GNU gdb (GDB) Red Hat Enterprise Linux (7.2-75.el6)which gdb    /usr/bin/gdb

If you want to install a newer version of GDB from source

  1. Install a C compiler, such as GCC. A C++ compiler is not needed to build GDB from source, but is needed to demonstrate GDB’s pretty printing feature. You can build a more recent version of GCC from source by performing the steps in the article How to Install GCC on CentOS 6. Or, you can install the CentOS 6 officially supported version of GCC by running:

    sudo yum install gcc gcc-c++
  2. Install additionally required packages.

    sudo yum install wget tar gzip ncurses-devel texinfo svn python-devel
  3. Decide which version of GDB that you want to build from source. Visit the GDB FTP site to see the versions that are available for download.

  4. Get the source of the version of GDB that you want. The rest of this article is written for v7.9.1 and will download the sources into ~/sourceInstallations/gdb-7.9.1/ – you will have to substitute the proper version number for newer versions.

    mkdir ~/sourceInstallationscd ~/sourceInstallationswget .tar -zxvf gdb-7.9.1.tar.gz
  5. Build GDB. If this completes correctly, the last line you will see will say “success”. It is normal to see some error-looking messages scrolling by quickly. These are safe to ignore.

    mkdir gdb-7.9.1.buildcd --with-python=yes && make && sudo make install && echo success    --- If your VPS has multiple cores, you can speed up the build by changing the middle part    ---  of this line from "&& make &&" to "&& make -j <number of cores> &&".    --- You can see the number of cores your VPS has by running "nproc"    --- The parameter "--with-python=yes" is necessary for the pretty printing feature
  6. Install C++ pretty printing.

    cd ~/svn co svn:// gdb_printersCreate a file ~/.gdbinit of:    python    import sys    sys.path.insert(0, '/home/<yourUserName>/gdb_printers/')    from libstdcxx.v6.printers import register_libstdcxx_printers    end--- One way to create this file is to run "vi ~/.gdbinit", hitting "i" to enter insert mode,---  typing the above file, hitting ESC, and hitting "ZZ" to save.
  7. Check the installed version, and its location.

    gdb --version    May say: GNU gdb (GDB) 7.9.1which gdb    /usr/local/bin/gdb
  8. Optionally run GDB and see pretty printing.

    mkdir ~/gdbExamplecd ~/gdbExampleCreate a file gdbExample.cpp of:    #include <string>    #include <vector>    using namespace std;    int main() {       string foo = "bar";       vector<string> vec;       vec.push_back("foo");       vec.push_back("bar");       vec.push_back("foobar");    }--- One way to create this file is to run "vi gdbExample.cpp", hitting "i" to enter insert mode,---  typing the above file, hitting ESC, and hitting "ZZ" to save.g++ -ggdb gdbExample.cpp -o gdbExampleStart GDB traditionally, by running "gdb ./gdbExample".  Or, start GDB by using its terminal user interface (basically a text mode GUI), by running "gdb --tui ./gdbExample".Enter "break main" to set a breakpoint at the beginning of function main() -- and it will say:    Breakpoint 1 at 0x<someAddress>: file gdbExample.cpp, line 6.Enter "run" to start the program, which will immediately hit the breakpoint you just set -- and it will say:    Starting program: /home/<yourUserName>/gdbExample/gdbExample    Breakpoint 1, main () at gdbExample.cpp:6    6          string foo = "bar";Enter "next" and hit enter four times, and gdb will move up to just before executing:    10         vec.push_back("foobar"); Enter "print foo" and gdb will show:    $1 = "bar"Enter "print vec" and gdb will show:    $2 = std::vector of length 2, capacity 2 = {"foo", "bar"}    --- Remember, line 10 hasn't executed yet to add "foobar" to the vectorEnter "quit" and "y" to quit anyway.
  9. Optionally reclaim hard drive space. Your ~/sourceInstallations directory will be taking up around 386MB. It’s probably wise to keep the directories, as there are optional configuration options you may need to use at some point in the future, and it would be faster to have a lot already done. Also, the build process makes logs that you can later check and work from if something goes wrong. But, after running sudo make install earlier, your installed GDB isn’t depending on anything in this directory, and space can be at a premium, so you can do this step and reclaim the 386MB or so. IMPORTANT: do not delete the ~/gdb_printers/ directory! The contents of this directory are loaded each time you run GDB. They were NOT compiled into GDB itself.

    cd ~/rm -rf sourceInstallations--- Again, if you can spare the space, you may someday be happy to have left it there.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article

No comments

Powered by Blogger.