socialyzehub

How to Install Gdb on Ubuntu or Debian

In this article, we will see how to install gdb on Ubuntu or Debian. GDB, or the GNU Debugger, is a powerful debugging tool used primarily for troubleshooting and analyzing the behavior of compiled C and C++ programs. It is a part of the GNU Project and is widely used in the development of Unix and Linux software. GDB offers developers deep insights into the internals of their programs during execution. It provides some of the vital information required for troubleshooting and understanding of low level programs. It is easily available in almost all the famous linux platforms. Here we will see the steps to install Gdb debugger on Ubuntu or Debian based systems.

 

How to Install Gdb on Ubuntu or Debian

How to Install Gdb on Ubuntu or Debian

Also Read: How to Install jq (JSON Processor) on Ubuntu 22.04 LTS (Jammy Jellyfish)

Step 1: Prerequisites

a) You should have a running Ubuntu or Debian Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get utility available in your Server.

d) You should have gcc compiler installed in your system. To install gcc compiler on Ubuntu 20.04 LTS, check How to Install GCC(C and C++ Compiler) on Ubuntu 20.04 LTS 

 

Step 2: Update Your Server

There are lot of new security vulnerabilities and bugs gets detected almost on daily basis on Ubuntu or Debian based systems. To provide stability and security to the system, it is important that we regularly check for updates and install them as and when it is available. To check and install, run sudo apt update && sudo apt upgrade command as shown below.

socialyzehub@ubuntu:~$ sudo apt update && sudo apt upgrade
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:3 https://dl.winehq.org/wine-builds/ubuntu focal InRelease
Ign:4 https://pkg.jenkins.io/debian-stable binary/ InRelease
Get:5 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:6 https://pkg.jenkins.io/debian-stable binary/ Release
Hit:7 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:8 https://d3nt0h4h6pmmc4.cloudfront.net/ubuntu focal InRelease
Hit:9 https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/focal pgadmin4 InRelease
Hit:10 https://download.sublimetext.com apt/stable/ InRelease
Hit:11 https://ngrok-agent.s3.amazonaws.com buster InRelease
Hit:12 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:13 http://ppa.launchpad.net/flatpak/stable/ubuntu focal InRelease
Hit:14 http://ppa.launchpad.net/gencfsm/ppa/ubuntu focal InRelease
Hit:16 http://ppa.launchpad.net/juju/stable/ubuntu focal InRelease
Get:17 https://repositories.timber.io/public/vector/deb/ubuntu focal InRelease [4,947 B]
Hit:18 http://ppa.launchpad.net/libreoffice/ppa/ubuntu focal InRelease
Hit:19 http://ppa.launchpad.net/mojo-maintainers/ppa/ubuntu focal InRelease
Hit:20 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal InRelease
Fetched 119 kB in 7s (17.5 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
......................................................

 

 

Step 3: Install Gdb

You can install gdb debugger from default Ubuntu repo by using sudo apt install gdb command as shown below. This will download and install the package along with all its dependencies.

socialyzehub@ubuntu:~$ sudo apt install gdb
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
gdb-doc
The following NEW packages will be installed:
gdb
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 3,282 kB of archives.
After this operation, 10.2 MB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal/main amd64 gdb amd64 10.2-0ubuntu1~20.04~1 [3,282 kB]
Fetched 3,282 kB in 3s (1,072 kB/s)
Selecting previously unselected package gdb.
(Reading database ... 293288 files and directories currently installed.)
Preparing to unpack .../gdb_10.2-0ubuntu1~20.04~1_amd64.deb ...
Unpacking gdb (10.2-0ubuntu1~20.04~1) ...
Setting up gdb (10.2-0ubuntu1~20.04~1) ...
Processing triggers for man-db (2.9.1-1) ...

 

 

Step 4: Verify Installation

After successful installation, you can verify the installation status by using dpkg -s gdb command as you can see below.

socialyzehub@ubuntu:~$ dpkg -s gdb
Package: gdb
Status: install ok installed
Priority: optional
Section: devel
Installed-Size: 9942
Maintainer: Ubuntu Developers <[email protected]>
Architecture: amd64
Version: 10.2-0ubuntu1~20.04~1
Replaces: gdb
Provides: gdb-minimal (= 10.2-0ubuntu1~20.04~1)
Depends: libbabeltrace1 (>= 1.5.4), libc6 (>= 2.29), libexpat1 (>= 2.0.1), libgcc-s1 (>= 3.4), libipt2 (>= 2.0), liblzma5 (>= 5.1.1alpha+20110809), libmpfr6 (>= 3.1.3), libncursesw6 (>= 6), libpython3.8 (>= 3.8.2), libreadline8 (>= 8), libsource-highlight4v5, libstdc++6 (>= 7), libtinfo6 (>= 6), libxxhash0 (>= 0.6.5), zlib1g (>= 1:1.2.0)
Recommends: libc-dbg
Suggests: gdb-doc, gdbserver
Conflicts: gdb
.....................................................

 

 

Step 5: Check Version

You can also check the current installed version by running gdb --version command as shown below.

socialyzehub@ubuntu:~$ gdb --version
GNU gdb (Ubuntu 10.2-0ubuntu1~20.04~1) 10.2
Copyright (C) 2021 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.

 

 

Step 6: Using Gdb

Now that gdb is installed, let's take below C program to see its usage. In below hello.c program, we are only displaying "Hi, This is from socialyzehub.com!" message on the output.

socialyzehub@ubuntu:~$ nano hello.c
#include <stdio.h>
int main() {
   printf("Hi, This is from socialyzehub.com!");
   return 0;
}

Before debugging above program, you have to generate the debugging symbols. This can be done by compiling program with -g flag as shown below.

socialyzehub@ubuntu:~$ gcc -g hello.c

You will notice that compiling hello.c program generates a program called a.out. Run this program with gdb to start with the debugging.

socialyzehub@ubuntu:~$ gdb a.out
GNU gdb (Ubuntu 10.2-0ubuntu1~20.04~1) 10.2
Copyright (C) 2021 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:
<https://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 a.out...

To set a breakpoint at the beginning of main() function, use b main command as shown below.

(gdb) b main
Breakpoint 1 at 0x1151: file hello.c, line 3.

Then start running program using run command until a breakpoint or end of program reached.

(gdb) run
Starting program: /home/socialyzehub/a.out

Breakpoint 1, main () at hello.c:3
3 printf("Hi, This is from socialyzehub.com!");

Then run next line of program using s command.

(gdb) s
__printf (format=0x555555556008 "Hi, This is from socialyzehub.com!") at printf.c:28
28 printf.c: No such file or directory.
(gdb) s
32 in printf.c
(gdb) s
33 in printf.c
(gdb) s
0x00007ffff7e2d881 in __vfprintf_internal (s=0x7ffff7fa46a0 <_IO_2_1_stdout_>, format=0x555555556008 "Hi, This is from socialyzehub.com!",
ap=ap@entry=0x7fffffffe190, mode_flags=mode_flags@entry=0) at vfprintf-internal.c:1289
1289 vfprintf-internal.c: No such file or directory.

To quit debugging, run quit command as shown below.

(gdb) quit

 

 

Step 7: Check all available options

You can also explore all the other options available with gdb utility using gdb --help command as shown below.

socialyzehub@ubuntu:~$ gdb --help
This is the GNU debugger. Usage:

   gdb  [options] [executable-file [core-file or process-id]]
   gdb  [options] --args executable-file [inferior-arguments ...]

Selection of debuggee and its files:

  --args             Arguments after executable-file are passed to inferior
  --core=COREFILE    Analyze the core dump COREFILE.
  --exec=EXECFILE    Use EXECFILE as the executable.
  --pid=PID          Attach to running process PID.
  --directory=DIR    Search for source files in DIR.
  --se=FILE          Use FILE as symbol file and executable file.
  --symbols=SYMFILE  Read symbols from SYMFILE.
  --readnow          Fully read symbol files on first access.
  --readnever        Do not read symbol files.
  --write            Set writing into executable and core files.
........................................................

 

 

Step 8: Uninstall Gdb

You can also think about removing gdb from your system in case you don't need it anymore. You just have to run sudo apt remove gdb command as shown below. To remove all the dependencies, use --auto-remove option with below command.

socialyzehub@ubuntu:~$ sudo apt remove gdb
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gdbserver libbabeltrace1 libc6-dbg libdw1 libipt2 libsource-highlight-common libsource-highlight4v5 libxxhash0
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
gdb
0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded.
After this operation, 10.2 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 293369 files and directories currently installed.)
Removing gdb (10.2-0ubuntu1~20.04~1) ...
Processing triggers for man-db (2.9.1-1) ...

Leave a Comment