gdbfaq
GDB FAQ
GDB is a command line debugging tool written by RMS. A tutorial written by him would be a good starting point for any newbie like me. In this page, I would only talk about my own experiences with GDB.
Table of Contents
Viewing array elements
(gdb) p *array@10 % print first 10 elements
(gdb) p array[5]@20 % print elements between 5-20 elements
(gdb) p *(array[1])@10 % print 10 elements of first row
(gdb) p (array[1])[1]@5 % print elements 1-5 of first row
(gdb) p **array@4 % print 5 consecutive elements of 2-d array
How to Detect 'nan' in a program?
The objective is to make the program stop when a variable becomes 'Nan'.
The method was suggested by Paul Pluzhnikov at gnu.g++.help newsgroup.
// file: nan_detect.cpp
#include<cmath>
using namespace std;
int main()
{
double d;
d = -1.0;
d = sqrt(d);
return 0;
}
swg@insane$ g++ -g nan_detect.cpp
swg@insane$ gdb ./a.out
GNU gdb 6.3-debian
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-linux"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) b main
Breakpoint 1 at 0x80483e4: file nan_detect.cpp, line 13.
(gdb) r
Starting program: /home/swagat/programs/c++/gdb/a.out
Breakpoint 1, main () at nan_detect.cpp:13
13 d = -1.0;
(gdb) n
14 d = sqrt(d);
(gdb) n
15 return 0;
(gdb) n
16 }
(gdb) p d
$1 = -nan(0x8000000000000)
Ok, we've got a NaN. Let's find out what the bit pattern is:
(gdb) x/2x &d
0xbffff850: 0x00000000 0xfff80000
Now set a hardware watch point on the "second half" of the double:
(gdb) watch *(int*)0xbffff854
Hardware watchpoint 2: *(int *) 3221223508
Note the address for watch command. Now set condition such that gdb will stop only if d is becoming NaN:
(gdb) cond 2 *(int*)0xbffff850 == 0 && *(int*)0xbffff854 == 0xfff80000
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x080483e4 in main at nan_detect.cpp:13
breakpoint already hit 1 time
2 hw watchpoint keep y *(int *) 3221223508
We are no longer intersted in the first breakpoint:
(gdb) delete 1
Run the program once again
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/swagat/programs/c++/gdb/a.out
Hardware watchpoint 2: *(int *) 3221223508
Hardware watchpoint 2: *(int *) 3221223508
Hardware watchpoint 2: *(int *) 3221223508
Hardware watchpoint 2: *(int *) 3221223508
Hardware watchpoint 2: *(int *) 3221223508
Hardware watchpoint 2: *(int *) 3221223508
Hardware watchpoint 2: *(int *) 3221223508
Old value = -1074790400
New value = -524288
main () at nan_detect.cpp:15
15 return 0;
Note that gdb stops on the next instruction after the one that
triggered the HW watchpoint.
(gdb) p d
$2 = -nan(0x8000000000000)
Bingo. 'd' just became NaN.
-- 04 July 2006 Tuesday 1111 Hrs IST
Calling Functions from GDB
This is useful in printing matrices. You can write a simple function in a program and call it from GDB using following command:
call display_matrix(A, 2, 3)
where display_matrix is the name of function being called and it is defined somewhere in the program file.
|| Home || FAQs || Research || Images || Random || Blogs || Links || Downloads ||