From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14826 invoked by alias); 19 May 2009 02:48:32 -0000 Received: (qmail 14817 invoked by uid 22791); 19 May 2009 02:48:30 -0000 X-SWARE-Spam-Status: No, hits=0.0 required=5.0 tests=AWL,BAYES_00,SARE_MSGID_LONG40,SPF_PASS X-Spam-Check-By: sourceware.org Received: from yx-out-1718.google.com (HELO yx-out-1718.google.com) (74.125.44.156) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 19 May 2009 02:48:25 +0000 Received: by yx-out-1718.google.com with SMTP id 4so2114538yxp.48 for ; Mon, 18 May 2009 19:48:23 -0700 (PDT) MIME-Version: 1.0 Received: by 10.150.11.20 with SMTP id 20mr13745352ybk.348.1242701303052; Mon, 18 May 2009 19:48:23 -0700 (PDT) In-Reply-To: <8ac60eac0905181043i46a817d6nbb9a31f7c22d497d@mail.gmail.com> References: <547db2260905180946u3c7dbec5s830511b49e15947e@mail.gmail.com> <8ac60eac0905181043i46a817d6nbb9a31f7c22d497d@mail.gmail.com> From: Amit Saha Date: Tue, 19 May 2009 02:48:00 -0000 Message-ID: <547db2260905181948l123e42c2v4c63fa6ee589a5b4@mail.gmail.com> Subject: =?windows-1252?Q?Re=3A_Doubt_regarding_a_tail_optimized_code_under_=91g?= =?windows-1252?Q?db=92?= To: Paul Pluzhnikov Cc: 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/msg00126.txt.bz2 Hello Paul, On Mon, May 18, 2009 at 11:13 PM, Paul Pluzhnikov wrote: > On Mon, May 18, 2009 at 9:46 AM, Amit Saha 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