* gdb problem
@ 1997-05-14 2:10 Richard Holmes
0 siblings, 0 replies; 3+ messages in thread
From: Richard Holmes @ 1997-05-14 2:10 UTC (permalink / raw)
Hello,
I have a problem with gdb that perhaps someone can solve.
I compiled the following code with gcc 2.7.2.1 using the -g flag
#include <stdio.h>
double f();
int main(void)
{
double x;
x = f();
printf("x = %lf\n", x);
}
double f()
{
return 1.0;
}
It will print the proper value , 1.0000, if I run it. If, however,
I use gdb I can get some strange behavior. I have a typescript at the
end of the message that shows all the gory details, but basically
what I did was put breakpoints on the "x = f();" and "printf ... " lines.
If I run to the first breakpoint, use the continue command to execute
the function and let it stop at the second breakpoint. If I print x
I get 1, as I should. However, if intead of using continue to get
through the function I use next I get garbage when I print x.
Any idea what might be happening? I'm using gdb 4.16 that came from
the gdb-4.16-6.axp.rpm RPM. I'm running it on an Alpha PC164 using
the 2.0.27 kernel.
Thanks,
Rich
Script started on Tue May 13 19:54:26 1997
nancy 267> gdb a.out
GDB is free software and you are welcome to 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.
GDB 4.16 (alpha-unknown-linux), Copyright 1996 Free Software Foundation, Inc...
(gdb) l 1,16
1 #include <stdio.h>
2 double f();
3
4 int main(void)
5 {
6 double x;
7
8 x = f();
9
10 printf("x = %lf\n", x);
11 }
12
13 double f()
14 {
15 return 1.0;
16 }
(gdb) break 8
Breakpoint 1 at 0x1200008f8: file test.c, line 8.
(gdb) break 10
Breakpoint 2 at 0x120000910: file test.c, line 10.
(gdb) r
Starting program: /home/holmes/test/a.out
Breakpoint 1, main () at test.c:8
8 x = f();
(gdb) next
10 printf("x = %lf\n", x);
(gdb) p x
$1 = -2.8638903918474961e+250
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/holmes/test/a.out
Breakpoint 1, main () at test.c:8
8 x = f();
(gdb) c
Continuing.
Breakpoint 2, main () at test.c:10
10 printf("x = %lf\n", x);
(gdb) p x
$2 = 1
(gdb) quit
The program is running. Quit anyway (and kill it)? (y or n) y
nancy 268> exit
Script done on Tue May 13 19:55:42 1997
---------------------------------------
Richard Holmes
Los Alamos National Laboratory
Group XTA
MS B220
Los Alamos, NM 87545
(505) 667-3598
holmes@lanl.gov
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: gdb problem
@ 1997-05-14 18:58 Richard Holmes
1997-05-14 19:09 ` Kortelainen Mika
0 siblings, 1 reply; 3+ messages in thread
From: Richard Holmes @ 1997-05-14 18:58 UTC (permalink / raw)
Michal,
Michal Jaegermann wrote:
>Richard Holmes wrote:
>>
>> I compiled the following code with gcc 2.7.2.1 using the -g flag
>Contrary to what you may be told in other places it is very often
>to your advantage to use a mild optimization (like -O) with gcc
>when using -g. It cleans up a generated code.
I'll keep this in mind. It may help somewhere down the line, although it
shouldn't make a difference here.
>>
>>
>> #include <stdio.h>
>> double f();
>>
>> int main(void)
>> {
>> double x;
>>
>> x = f();
>>
>> printf("x = %lf\n", x);
>> }
>>
>> double f()
>> {
>> return 1.0;
>> }
>>
>>
>> It will print the proper value , 1.0000, if I run it. If, however,
>> I use gdb I can get some strange behavior.
>Looks like a bug in a debugger. Debuggers usually work by inserting
>a special code in breakpoint locations and restoring an old contents
>when a breakpoint is hit. Something is not restored properly. Maybe
>wrong sizes? The problem is likely compounded by the fact that
>'x' above is prime candidate for a removal by a compiler (just a
>location on a stack). Why this hiccup is important?
This is important because I'm debugging a much more complicated program
and gdb reports strange values for certain down in the deepest functions.
I don't know whether to believe gdb or not.
>You may always
>break inside 'f()' and type 'finish' to see a returned value.
This also does not work. gdb tells me the correct return value
when I type finish, but if I step to the 'printf' line and print x
I get garbage.
>> printf("x = %lf\n", x);
>BTW, there is NO "%lf" print specifier in ANSI C. The above is
>technically an error, although most compilers will swallow it (but
>try -Wall, which is always a good idea and on Alpha double that :-).
Wow, I just looked this up in my second edition of K&R and you're right.
I never knew this.
>Michal
Thanks Michal,
Rich
---------------------------------------
Richard Holmes
Los Alamos National Laboratory
Group XTA
MS B220
Los Alamos, NM 87545
(505) 667-3598
holmes@lanl.gov
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: gdb problem
1997-05-14 18:58 gdb problem Richard Holmes
@ 1997-05-14 19:09 ` Kortelainen Mika
0 siblings, 0 replies; 3+ messages in thread
From: Kortelainen Mika @ 1997-05-14 19:09 UTC (permalink / raw)
>
> >BTW, there is NO "%lf" print specifier in ANSI C. The above is
>
> Wow, I just looked this up in my second edition of K&R and you're right.
> I never knew this.
>
But remember to use %lf with scanf when reading data into doubles...
I'm quite sure the difference between %f and %lf is important in
there (otherwise generated code might read too few bytes of data into
a double). I don't have K&R but another book (and my own
experience) second this.
Mika Kortelainen
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1997-05-14 19:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-05-14 18:58 gdb problem Richard Holmes
1997-05-14 19:09 ` Kortelainen Mika
-- strict thread matches above, loose matches on Subject: below --
1997-05-14 2:10 Richard Holmes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox