* Doubt regarding a tail optimized code under ‘gdb’
@ 2009-05-18 16:47 Amit Saha
2009-05-18 17:43 ` Paul Pluzhnikov
0 siblings, 1 reply; 3+ messages in thread
From: Amit Saha @ 2009-05-18 16:47 UTC (permalink / raw)
To: gdb
Hello all,
Out of sheer curiosity, I tried to run a tail optimised code under
'gdb'. Consider a tail recursive factorial implementation in C:
<code>
#include <stdio.h>
unsigned long long factorial(unsigned long long fact_so_far, unsigned
long long count, unsigned long long max_count){
if (max_count==0 || max_count==1 || count >= max_count)
return fact_so_far;
else
{
printf("%llu %p \n", count, &factorial);
return factorial(fact_so_far * count, ++count, max_count);
}
}
int main(int argc, char **argv)
{
unsigned long long n;
scanf("%llu", &n);
printf("\n Factorial %llu \n",factorial(1,0,n));
return 0;
}
</code>
I place a breakpoint in 'factorial' and I run the above under 'gdb'.
The breakpoint is never hit. Assuming that its tail call optimised (I
have compiled it using gcc -O2), it should hit the breakpoint, at
least once. I get the final result without hitting any breakpoint. For
eg,
(gdb) b factorial
Breakpoint 1 at 0x8048429: file factorial-tail.c, line 3.
(gdb) run
Starting program: /home/amit/quest/codes/factorial-tail
5
0 0x8048420
1 0x8048420
2 0x8048420
3 0x8048420
4 0x8048420
Factorial 120
Program exited normally.
(gdb)
Where am I going wrong? Any pointers would be appreciated? I am using
'gdb-6.8-debian' and gcc-4.3.3.
Best Regards,
Amit
--
Journal: http://amitksaha.wordpress.com
IRC: cornucopic on #scheme, #lisp, #math, #linux
"Recursion is the basic iteration mechanism in Scheme"
--- http://c2.com/cgi/wiki?TailRecursion
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: Doubt regarding a tail optimized code under ‘gdb’
2009-05-18 16:47 Doubt regarding a tail optimized code under ‘gdb’ Amit Saha
@ 2009-05-18 17:43 ` Paul Pluzhnikov
2009-05-19 2:48 ` Amit Saha
0 siblings, 1 reply; 3+ messages in thread
From: Paul Pluzhnikov @ 2009-05-18 17:43 UTC (permalink / raw)
To: Amit Saha; +Cc: gdb
On Mon, May 18, 2009 at 9:46 AM, Amit Saha <amitsaha.in@gmail.com> wrote:
> Out of sheer curiosity, I tried to run a tail optimised code under
> 'gdb'. Consider a tail recursive factorial implementation in C:
Debugging optimized code is tricky; you need to know what you are doing
and know how to read disassembly :-)
> I place a breakpoint in 'factorial' and I run the above under 'gdb'.
> The breakpoint is never hit. Assuming that its tail call optimised (I
> have compiled it using gcc -O2), it should hit the breakpoint, at
> least once. I get the final result without hitting any breakpoint.
I can reproduce this with gcc-4.3.1 on i386, but not with gcc-4.4.0.
This doesn't have anything to do with tail-recursion though: gcc-4.3.1
simply inlines factorial() into main: looking at disassembly, the only
CALLs from main are to scanf and printf. Compiling with -fno-inline makes
it work as expected under gcc-4.3.1 as well.
Cheers,
--
Paul Pluzhnikov
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Doubt regarding a tail optimized code under ‘gdb’
2009-05-18 17:43 ` Paul Pluzhnikov
@ 2009-05-19 2:48 ` Amit Saha
0 siblings, 0 replies; 3+ messages in thread
From: Amit Saha @ 2009-05-19 2:48 UTC (permalink / raw)
To: Paul Pluzhnikov; +Cc: gdb
Hello Paul,
On Mon, May 18, 2009 at 11:13 PM, Paul Pluzhnikov
<ppluzhnikov@google.com> wrote:
> On Mon, May 18, 2009 at 9:46 AM, Amit Saha <amitsaha.in@gmail.com> wrote:
>
>> Out of sheer curiosity, I tried to run a tail optimised code under
>> 'gdb'. Consider a tail recursive factorial implementation in C:
>
> Debugging optimized code is tricky; you need to know what you are doing
> and know how to read disassembly :-)
>
>> I place a breakpoint in 'factorial' and I run the above under 'gdb'.
>> The breakpoint is never hit. Assuming that its tail call optimised (I
>> have compiled it using gcc -O2), it should hit the breakpoint, at
>> least once. I get the final result without hitting any breakpoint.
>
> I can reproduce this with gcc-4.3.1 on i386, but not with gcc-4.4.0.
> This doesn't have anything to do with tail-recursion though: gcc-4.3.1
> simply inlines factorial() into main: looking at disassembly, the only
> CALLs from main are to scanf and printf. Compiling with -fno-inline makes
> it work as expected under gcc-4.3.1 as well.
Yes. Yes. Now I remember. In the assembly code (I obtained by gcc
--S), I remember the CALL to only printf and scanf. So that explains
the behavior. Thanks!
Now, I will look into why the inlining takes place :)
Thanks a ton!
Best,
Amit
>
> Cheers,
> --
> Paul Pluzhnikov
>
--
Journal: http://amitksaha.wordpress.com
IRC: cornucopic on #scheme, #lisp, #math, #linux
"Recursion is the basic iteration mechanism in Scheme"
--- http://c2.com/cgi/wiki?TailRecursion
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-05-19 2:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-18 16:47 Doubt regarding a tail optimized code under ‘gdb’ Amit Saha
2009-05-18 17:43 ` Paul Pluzhnikov
2009-05-19 2:48 ` Amit Saha
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox