From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28895 invoked by alias); 20 Oct 2006 20:18:45 -0000 Received: (qmail 28878 invoked by uid 22791); 20 Oct 2006 20:18:44 -0000 X-Spam-Check-By: sourceware.org Received: from mx2.palmsource.com (HELO mx2.palmsource.com) (12.7.175.14) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 20 Oct 2006 20:18:33 +0000 Received: from localhost (localhost [127.0.0.1]) by localhost.domain.tld (Postfix) with ESMTP id 62FA9225AB; Fri, 20 Oct 2006 13:18:32 -0700 (PDT) Received: from mx2.palmsource.com ([127.0.0.1]) by localhost (mx2.palmsource.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 30895-01-2; Fri, 20 Oct 2006 13:18:29 -0700 (PDT) Received: from ussunex01.palmsource.com (unknown [192.168.101.9]) by mx2.palmsource.com (Postfix) with ESMTP id F0E0627B44; Fri, 20 Oct 2006 12:41:58 -0700 (PDT) Received: from 192.168.92.75 ([192.168.92.75]) by ussunex01.palmsource.com ([192.168.101.9]) via Exchange Front-End Server owa.palmsource.com ([10.0.20.17]) with Microsoft Exchange Server HTTP-DAV ; Fri, 20 Oct 2006 19:41:58 +0000 Received: from svmsnyderlnx by owa.palmsource.com; 20 Oct 2006 12:41:57 -0700 Subject: Re: Programmatic access to stack traces in C or C++ programs From: Michael Snyder To: Ashwin Bharambe Cc: gdb@sourceware.org In-Reply-To: <3ef5826d0610191927n590c416fx238aa355a378d57c@mail.gmail.com> References: <3ef5826d0610191927n590c416fx238aa355a378d57c@mail.gmail.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Date: Fri, 20 Oct 2006 20:18:00 -0000 Message-Id: <1161373317.9942.113.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 X-IsSubscribed: yes Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2006-10/txt/msg00162.txt.bz2 On Thu, 2006-10-19 at 22:27 -0400, Ashwin Bharambe wrote: > Hi all, > > I wanted to create a "stacktrace library" which would provide a > routine to obtain the stacktrace of the program from any point > _programmatically_ (like Java's stacktraces, for example..) Can't be done in C (at least not without major hackery) -- you would have to write in assembler. > I was aware of libc's non-standard stacktrace API but it did not quite > work in many cases failing to resolve addresses, etc. It seems like > stacktrace functionality is quite implementation and > architecture-dependent. Whatever you do would absolutely be architecture-dependent, and ABI dependent (as it is in gdb). > So, I was wondering if I could use portions of > gdb's code to create such a library. Currently, to print a stacktrace, > I utilize a piece of code (not mine, it's off the net) which fork()s a > gdb sub-process, makes it ptrace the parent and run the command > "backtrace". However this is quite time-consuming and sort of ugly. That would surely work - but as you say, it is sort of ugly. > My question, therefore, is: are there pieces of the code I can steal > from libgdb to make this happen programmatically. I tried some naive > ways of performing gdb_init() and then having it execute the > 'backtrace' command (by invoking backtrace_command directly, for > example), however gdb says there's no stack. This seems to be the case > because it does not initialize its data structures without starting a > process. I think that's pretty hopeless. But why don't you do it the easy way? Just launch your program under gdb from the start, then use gdb to put breakpoints at all the functions you are interested in. You could write a script for that part. Then, at each breakpoint, have gdb do this: silent backtrace continue Voila, you've got all your backtraces. And no overhead for forking.