From owner-freebsd-hackers@FreeBSD.ORG Mon Feb 11 15:23:46 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B525016A417 for ; Mon, 11 Feb 2008 15:23:46 +0000 (UTC) (envelope-from redcrash@gmail.com) Received: from rn-out-0910.google.com (rn-out-0910.google.com [64.233.170.187]) by mx1.freebsd.org (Postfix) with ESMTP id 429C213C46A for ; Mon, 11 Feb 2008 15:23:46 +0000 (UTC) (envelope-from redcrash@gmail.com) Received: by rn-out-0910.google.com with SMTP id s42so1844065rnb.13 for ; Mon, 11 Feb 2008 07:23:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; bh=/+/qoSYGmbhDBsBlGTuV3tvcDZO0XMSvahTXH0HI3ms=; b=cpmrJYu+/hUWGm69aiLvgd6EOvErBvWfikhrieD3eNiQoJXFCPJwqYX+/6BNJLoNosnSt8qwgySqhhW1AOFNg9EI2vr7jm5A3fzQttLVfMkoc2n/TPl+pJqxaAFQtaV7wpdGij3jS2z9RW7kFBhRYBXKyr3TrlFkXi6PC4KG6So= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=DpyMfPWlsDKmO5h/ikODmj4f4CaFxiJjnQf0lvFPLLyrLEjpXwsb1gARXqvxIr4LPlk3lEh9jmfSqXF2WIFWAEwcUxS+oSBnCOyLk9x6jEt4ydfiuumNJ93cGjZHF2Wpn38VFhq22bHaTxWhgTfKW3ksAUru2oXjJrapA1FAKn8= Received: by 10.142.165.9 with SMTP id n9mr55806wfe.93.1202741886068; Mon, 11 Feb 2008 06:58:06 -0800 (PST) Received: by 10.142.143.9 with HTTP; Mon, 11 Feb 2008 06:58:06 -0800 (PST) Message-ID: Date: Mon, 11 Feb 2008 15:58:06 +0100 From: "Harald Servat" To: freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Subject: backtrace call comparison X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Feb 2008 15:23:46 -0000 Hello list, I'm developing a tool that gathers the callstack information of an application at certain points. In order to do that, I use the "backtrace" (and its relative backtrace_symbols) call and I'm mailing you to share some results, thoughts & possible patches. When I compare the behaviour of the backtrace call on a FreeBSD 6.2 box and on Linux boxes I see that two levels of the callstack are missing. Using the example that can be found in http://www.gnu.org/software/libc/manual/html_node/Backtraces.html I see the following ( I present three commands here, compilation, run and translation of addresses using addr2line). FreeBSD output: ** #~/tests/seq/backtrace>gcc bt.c -o bt -rdynamic -g -I/usr/local/include -L/usr/local/lib/ -lexecinfo #~/tests/seq/backtrace>./bt Obtained 3 stack frames. 0x80486b3 at ./bt 0x80486d9 at ./bt 0x804856a <_start+118> at ./bt #~/tests/seq/backtrace>addr2line -e ./bt -f 0x80486b3 dummy_function /home/harald/tests/seq/backtrace/bt.c:30 0x80486d9 main /home/harald/tests/seq/backtrace/bt.c:36 0x804856a _start ??:0 Linux output: ** #>gcc -g -rdynamic ./bt.c -o bt #>./bt Obtained 5 stack frames. ./bt(print_trace+0x14) [0x8048668] ./bt(dummy_function+0xb) [0x80486e9] ./bt(main+0x15) [0x8048700] /lib/tls/libc.so.6(__libc_start_main+0xe4) [0x5d4ad4] ./bt [0x80485c5] #>addr2line -e ./bt -f 0x8048668 print_trace /home/des/harald/bt.c:14 0x80486e9 dummy_function /home/des/harald/bt.c:30 0x8048700 main /home/des/harald/bt.c:36 On the linux side we can see that there're two additional routines in the callstack. They're located on 0x8048668 and 0x80485c5. The latter seem to be related to the "trampoline" to run the application (i.e., to invoke the main) so I think this can be safely skipped, however the former it's the print_trace call which in fact is on the callstack but on the FreeBSD implementation it does not appear on the result. Thinking a bit more on this,... A programmer usually knows the routine that is running when he or she codes the application (unless he or she starts playing with the callstack manually with jumps and so), so in fact there's no real need to know the code is on print_trace. To emulate the Linux behaviour just add on level to the callstack that is the very same routine (in this case "print_trace"). So a possible patch for the example could be ... array[0] = (void*) print_trace; size = backtrace (&array[1], 10-1); size++; ... This does not give the same output because each entry of the array buffer points to the returning address of the callstack whereas array[0] is the first address of "print_trace" and not the returning address of backtrace itself. However it's enough for me. Regards, -- _________________________________________________________________ Empty your memory, with a free()... like a pointer! If you cast a pointer to an integer, it becomes an integer, if you cast a pointer to a struct, it becomes a struct. The pointer can crash..., and can overflow. Be a pointer my friend...