From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24230 invoked by alias); 18 May 2009 16:47:14 -0000 Received: (qmail 24222 invoked by uid 22791); 18 May 2009 16:47:13 -0000 X-SWARE-Spam-Status: No, hits=-1.4 required=5.0 tests=BAYES_00,J_CHICKENPOX_43,SARE_MSGID_LONG40,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mail-gx0-f157.google.com (HELO mail-gx0-f157.google.com) (209.85.217.157) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 18 May 2009 16:47:08 +0000 Received: by gxk1 with SMTP id 1so6529474gxk.0 for ; Mon, 18 May 2009 09:47:06 -0700 (PDT) MIME-Version: 1.0 Received: by 10.150.151.7 with SMTP id y7mr12898326ybd.329.1242665226075; Mon, 18 May 2009 09:47:06 -0700 (PDT) From: Amit Saha Date: Mon, 18 May 2009 16:47:00 -0000 Message-ID: <547db2260905180946u3c7dbec5s830511b49e15947e@mail.gmail.com> Subject: =?windows-1252?Q?Doubt_regarding_a_tail_optimized_code_under_=91gdb?= =?windows-1252?Q?=92?= To: gdb@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2009-05/txt/msg00120.txt.bz2 Hello all, Out of sheer curiosity, I tried to run a tail optimised code under 'gdb'. Consider a tail recursive factorial implementation in C: #include 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; } 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