Gdb is a debugger for C and C++ program. It is called GNU Debugger. It runs c/c++ program up to a certain point during execution. The runtime error like segmentation fault is easier to find out with the help of gdb.

HOW TO USE GDB

open your terminal and type gdb, we will (gdb) in terminal screen like below.

alen@alen:~$ gdb

GNU gdb (GDB) 7.6.1-ubuntu

Copyright (C) 2013 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”.

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>.

(gdb)

To quit from gdb session just type quit and return.

(gdb) quit

SAMPLE PROGRAM: save below code as a test.c

/* test.c */

#include<stdio.h>

int i=0;

int j=0;

int row=0;

int col=0;

int mat[4][5]={{1,2,3,4,5},{5,6,7,8,9},{9,8,7,6,8},{2,3,4,5,6}};

int main(int argc, char **argv)

{

for(i=0;i<4;i++)

{

for(j=0;j<5;j++)

{

printf(” %d”,mat[i][j]);

}

printf(“n”);

}

return 0;

}

STEP 1: compile this test.c

alen@alen:~$gcc -g test.c -o test

Note: -g swich to gcc inserts debug symbls in executable

STEP 2: run this test exe with gdb

alen@alen:~$gdb ./test

GNU gdb (GDB) 7.6.1-ubuntu

Copyright (C) 2013 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”.

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>…

Reading symbols from /home/jay/myproject/sample/test…done.

(gdb)

LISTING SOURCE CODE

(gdb) l

3 int i=0;

4 int j=0;

5 int row=0;

6 int col=0;

7 int mat[4][5]={{1,2,3,4,5},{5,6,7,8,9},{9,8,7,6,8},{2,3,4,5,6}};

8

9 int main(int argc, char **argv)

10 {

11

12 for(i=0;i<4;i++)

(gdb)

SETTING BREAKPOINT

Breakpoint in a program is a point where gdb stop executing your program. The command to set a breakpoint is break or simply type b and line number.

If your project contains large number of files then you can set breakpoint as

b /path of your file/filename:linenumber

(gdb) b main

Breakpoint 1 at 0x40058c: file test.c, line 12.

(gdb)

You can also tell gdb to break at a particular function. Let your function name is void abc(int a) the by command break abc when abc function get called program stops.

(gdb) break abc

RUN PROGRAM

(gdb) run

Starting program: ./test

Breakpoint 1, main (argc=1, argv=0x7fffffffdfd8) at test.c:12

12 for(i=0;i<4;i++)

(gdb)

Here your program stops at line number 12, which is main function in test.c beacuse of there is breakpoints at line number 12

BREAKPOINTS INFORMATION

By command info breakpoints, gdb gives breakpoint information

(gdb) info breakpoints

Num Type Disp Enb Address What

1 breakpoint keep y 0x000000000040058c in main at test.c:12

breakpoint already hit 1 time

(gdb)

GO TO NEXT LINE

By next or n command program advances to next line

(gdb) n

14 for(j=0;j<5;j++)

(gdb) n

16 printf(” %d”,mat[i][j]);

(gdb)

STEPING INTO FUNCTION

By command step or s at gdb we can go into function. Here printf is a function defined in c librarary.

(gdb) step

__printf (format=0x4006d4 ” %d”) at printf.c:29

29 printf.c: No such file or directory.

CONTINUING EXECUTION

(gdb) c

Continuing.

1 2 3 4 5

5 6 7 8 9

9 8 7 6 8

2 3 4 5 6

[Inferior 1 (process 6498) exited normally]

(gdb)

GDBHELP COMMANDS

(gdb) help all

Command class: aliases

ni -- Step one instruction

rc -- Continue program being debugged but run it in reverse

rni -- Step backward one instruction

rsi -- Step backward exactly one instruction

si -- Step one instruction exactly

stepping -- Specify single-stepping behavior at a tracepoint

tp -- Set a tracepoint at specified line or function

tty -- Set terminal for future runs of program being debugged

where -- Print backtrace of all stack frames

ws -- Specify single-stepping behavior at a tracepoint

Command class: breakpoints

awatch -- Set a watchpoint for an expression

break -- Set breakpoint at specified line or function

break-range -- Set a breakpoint for an address range

catch -- Set catchpoints to catch events

catch assert -- Catch failed Ada assertions

catch catch -- Catch an exception

OTHER IMPORTANT COMMANDS

backtrace

produces a stack trace of the function calls.

where

same as backtrace

finish

runs until the current function is finished

delete

deletes a specified breakpoint

The above example is very basic. Let us take another example for more gdb details.

How to debug a GCC segmentation fault readonly memory access

Let us write a buggy code

#include <stdio.h>

#include <stdlib.h>

 

int main(int argc, char** argv)

{

char *arr = "mbuggy program to learn segfault";

arr[0]='t';

return 0;

}

when we compile and run the above program ,it will crash giving segmemtation fault:

alen@Alen-Vostro-1550:~$ gcc -g segf.c -o segf

alen@Alen-Vostro-1550:~$ gdb segf

GNU gdb (GDB) 7.6.1-ubuntu

Copyright (C) 2013 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”.

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>…

Reading symbols from /home/jay/myproject/sample/segf…done.

(gdb) r

Starting program: /home/jay/myproject/sample/segf

Program received signal SIGSEGV, Segmentation fault.

0x0000000000400504 in main (argc=1, argv=0x7fffffffdfe8) at segf.c:9

9 arr[0]=’t’;

(gdb)

  • Now examine where program is geting crashed

(gdb) backtrace

#0 0x0000000000400504 in main (argc=1, argv=0x7fffffffdfe8) at segf.c:9

(gdb)

from the above backtrace line no 9 in segf.c is getting crash when executing

arr[0]='t';

  • Now find where arr points to

(gdb) print arr

$1 = 0x4005b8 "mbuggy program to learn segfault"

Now see the memory section of your program

(gdb) maintenance info sections

Exec file:

`segf', file type elf64-x86-64.

0x004005b0->0x004005d9 at 0x000005b0: .rodata ALLOC LOAD READONLY DATA HAS_CONTENTS

from the above address 0x4005b8 adress is readonly memory. So the statement

arr[0]='t';

will seg fault beacause we are trying to overwrite readonly memory



Related Contents to follow