[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: GDB to detect NAN
From: |
Paul Pluzhnikov |
Subject: |
Re: GDB to detect NAN |
Date: |
Sun, 02 Jul 2006 10:00:52 -0700 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux) |
"swagat" <swagat.kumar@gmail.com> writes:
> I don't know if it is the right forum to ask for gdb problems. IF it is
> not, please do let me know.
Asking in OS-specific group (and indicating what OS you are on)
may yield better results.
> In my c++ program, a particular variable takes up "nan" value after
> sometime. I want my program to stop executing at a line where it takes
> up a NAN value.
>
> How can I use GDB to perform this task?
You appear to be on Linux, so here is Linux/x86-specific answer:
$ cat junk.c
#include <math.h>
double d; /* global to avoid kernel stack randomization */
int main()
{
d = -1.0;
d = sqrt(d);
return 0;
}
$ gcc -g junk.c -lm
$ gdb -q ./a.out
(gdb) b main
Breakpoint 1 at 0x80483a8: file junk.c, line 6.
(gdb) r
Breakpoint 1, main () at junk.c:6
6 d = -1.0;
(gdb) n
7 d = sqrt(d);
(gdb) n
8 return 0;
(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
0x80495d0 <d>: 0x00000000 0xfff80000
Now set a hardware watch point on the "second half" of the double:
(gdb) watch *(int*)0x80495d4
Hardware watchpoint 2: *(int *) 134518228
And set condition such that gdb will stop only if d is becoming NaN:
(gdb) cond 2 *(int*)0x80495d0 == 0 && *(int*)0x80495d4 == 0xfff80000
We are no longer intersted in the first breakpoint:
(gdb) delete 1
(gdb) r
Hardware watchpoint 2: *(int *) 134518228
Hardware watchpoint 2: *(int *) 134518228
Hardware watchpoint 2: *(int *) 134518228
Hardware watchpoint 2: *(int *) 134518228
Old value = -1074790400
New value = -524288
main () at junk.c:8
8 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.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.