From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25322 invoked by alias); 20 Oct 2006 12:41:13 -0000 Received: (qmail 25314 invoked by uid 22791); 20 Oct 2006 12:41:12 -0000 X-Spam-Check-By: sourceware.org Received: from wx-out-0506.google.com (HELO wx-out-0506.google.com) (66.249.82.229) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 20 Oct 2006 12:41:09 +0000 Received: by wx-out-0506.google.com with SMTP id i31so914317wxd for ; Fri, 20 Oct 2006 05:41:08 -0700 (PDT) Received: by 10.90.78.1 with SMTP id a1mr744406agb; Fri, 20 Oct 2006 05:41:07 -0700 (PDT) Received: by 10.90.100.17 with HTTP; Fri, 20 Oct 2006 05:41:07 -0700 (PDT) Message-ID: <3ef5826d0610200541g4804ec15sc2b3167b455c735e@mail.gmail.com> Date: Fri, 20 Oct 2006 12:41:00 -0000 From: "Ashwin Bharambe" To: gdb@sourceware.org Subject: Re: Programmatic access to stack traces in C or C++ programs In-Reply-To: <20061020031303.GA11223@nevyn.them.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <3ef5826d0610191927n590c416fx238aa355a378d57c@mail.gmail.com> <20061020031303.GA11223@nevyn.them.org> 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/msg00156.txt.bz2 Sweet. Thanks Daniel for pointing me to libunwind.. Now, given the instruction pointer and stack pointer values, do you think I can use some parts of gdb code to dump out a more readable version of the stackframe? It would be _really_ nice if I could dump out argument values as well, but I suspect that might be much harder. Ashwin On 10/19/06, Daniel Jacobowitz wrote: > On Thu, Oct 19, 2006 at 10:27:08PM -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..) > > > > 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. 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. > > Your best bet for reliability is to work with libunwind, if you can > restrict yourself to code with unwind information - which is more and > more practical nowadays. > > > 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 would appreciate any pointers regarding how I can make gdb believe > > the current process is the one it should use, without really > > ptrace()ing it... > > You can not readily do this. It will be easier and faster to stick > with forking another copy of GDB. > > -- > Daniel Jacobowitz > CodeSourcery > -- Ashwin Bharambe, Ph.D. Candidate, Carnegie Mellon University. Office: 412-268-7555 Web: http://www.cs.cmu.edu/~ashu