GDB

GDB (GNU Project Debugger): Lets you look inside programs and debug them.

Three Types of Errors:

  1. Syntax Errors: Occur during compilation.
  2. Runtime Errors: Occur while the program is executing.
  1. Logical Errors: Occur while the program is executing.

Debugger Terms:

Common GDB Commands

Basic Execution:

Manipulation:

Help:

Demo: Debugging a Program with GDB

Here is a program with a logical error:

#include <stdio.h>
int main()
{
    int i, num, j;
    printf("Enter a number: ");
    scanf("%d", &num);
    for (i=1;i<num;i++)
        j=j*i;
    printf("Factorial of %d is %d\n", num, j);
}

Here is an example interaction showing the error:

$ ./factorial
Enter a number: 6
Factorial of 6 is 3931680

To debug our program we need to compile our program with the -g flag before running it with gdb.

$ $ gcc -o factorial -std=c99 -g factorial.c
$ gdb --quiet factorial
Reading symbols from factorial...
(gdb) break 7
Breakpoint 1 at 0x119b: file factorial.c, line 7.
(gdb) run
Starting program: /tmp/temporary/factorial 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Enter a number: 7

Breakpoint 1, main () at factorial.c:7
7               for (i=1;i<num;i++)
(gdb) print j
$1 = 32767

Note: If compiled with -Wall, the following warning is given, warning us about j being uninitialized.

hello.c:6:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-re
sult]
    6 |         scanf("%d", &num);
      |         ^~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:906,
                 from hello.c:1:
In function ‘printf’,
    inlined from ‘main’ at hello.c:9:2:
/usr/include/bits/stdio2.h:86:10: warning: ‘j’ may be used uninitialized [-Wmaybe-uninitialized]
   86 |   return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hello.c: In function ‘main’:
hello.c:4:21: note: ‘j’ was declared here
    4 |         int i, num, j;
      |                     ^

Example: Playing with gdb

$ gdb --quiet factorial
Reading symbols from factorial...
(gdb) break 6
Breakpoint 1 at 0x1180: file factorial.c, line 6.
(gdb) run
Starting program: /tmp/temporary/factorial 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at factorial.c:6
6               scanf("%d", &num);
(gdb) print num
$1 = 32767
(gdb) next
Enter a number: 42
7               for (i=1;i<num;i++)
(gdb) print num
$2 = 42