* Programmatic access to stack traces in C or C++ programs
@ 2006-10-20 2:27 Ashwin Bharambe
2006-10-20 3:10 ` Neo Jia
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Ashwin Bharambe @ 2006-10-20 2:27 UTC (permalink / raw)
To: gdb
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.
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...
Thanks very much for reading the long message!
Ashwin
--
Ashwin Bharambe, Ph.D. Candidate, Carnegie Mellon University.
Web: http://www.cs.cmu.edu/~ashu
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Programmatic access to stack traces in C or C++ programs
2006-10-20 2:27 Programmatic access to stack traces in C or C++ programs Ashwin Bharambe
@ 2006-10-20 3:10 ` Neo Jia
2006-10-20 3:13 ` Daniel Jacobowitz
2006-10-20 20:18 ` Michael Snyder
2 siblings, 0 replies; 7+ messages in thread
From: Neo Jia @ 2006-10-20 3:10 UTC (permalink / raw)
To: Ashwin Bharambe; +Cc: gdb
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.
I think the stack backtrace should be arch. dependent. So, your lib
should be also arch. 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.
>
> 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.
My suggestion is to read the document of each arch. especially the ABI
for each arch. Then you might know
where is the stack frame pointer then you can quite easy to dump it
back. You might need to pay attention to those
code compiled with omit frame pointer option.
Another interesting thing for you, google has a project called
"coredumper". http://goog-coredumper.sourceforge.net/
Thanks,
Neo
>
> 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...
>
> Thanks very much for reading the long message!
> Ashwin
>
--
I would remember that if researchers were not ambitious
probably today we haven't the technology we are using!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Programmatic access to stack traces in C or C++ programs
2006-10-20 2:27 Programmatic access to stack traces in C or C++ programs Ashwin Bharambe
2006-10-20 3:10 ` Neo Jia
@ 2006-10-20 3:13 ` Daniel Jacobowitz
2006-10-20 12:41 ` Ashwin Bharambe
2006-10-20 20:18 ` Michael Snyder
2 siblings, 1 reply; 7+ messages in thread
From: Daniel Jacobowitz @ 2006-10-20 3:13 UTC (permalink / raw)
To: Ashwin Bharambe; +Cc: gdb
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Programmatic access to stack traces in C or C++ programs
2006-10-20 3:13 ` Daniel Jacobowitz
@ 2006-10-20 12:41 ` Ashwin Bharambe
2006-10-20 14:27 ` Daniel Jacobowitz
0 siblings, 1 reply; 7+ messages in thread
From: Ashwin Bharambe @ 2006-10-20 12:41 UTC (permalink / raw)
To: gdb
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 <drow@false.org> 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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Programmatic access to stack traces in C or C++ programs
2006-10-20 12:41 ` Ashwin Bharambe
@ 2006-10-20 14:27 ` Daniel Jacobowitz
0 siblings, 0 replies; 7+ messages in thread
From: Daniel Jacobowitz @ 2006-10-20 14:27 UTC (permalink / raw)
To: Ashwin Bharambe; +Cc: gdb
On Fri, Oct 20, 2006 at 08:41:07AM -0400, Ashwin Bharambe wrote:
> 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.
GDB is really not set up to reuse this way.
--
Daniel Jacobowitz
CodeSourcery
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Programmatic access to stack traces in C or C++ programs
2006-10-20 2:27 Programmatic access to stack traces in C or C++ programs Ashwin Bharambe
2006-10-20 3:10 ` Neo Jia
2006-10-20 3:13 ` Daniel Jacobowitz
@ 2006-10-20 20:18 ` Michael Snyder
2006-10-20 21:47 ` David Carlton
2 siblings, 1 reply; 7+ messages in thread
From: Michael Snyder @ 2006-10-20 20:18 UTC (permalink / raw)
To: Ashwin Bharambe; +Cc: gdb
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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Programmatic access to stack traces in C or C++ programs
2006-10-20 20:18 ` Michael Snyder
@ 2006-10-20 21:47 ` David Carlton
0 siblings, 0 replies; 7+ messages in thread
From: David Carlton @ 2006-10-20 21:47 UTC (permalink / raw)
To: Michael Snyder; +Cc: Ashwin Bharambe, gdb
On Fri, 20 Oct 2006 12:41:57 -0700, Michael Snyder <Michael.Snyder@palmsource.com> said:
> On Thu, 2006-10-19 at 22:27 -0400, Ashwin Bharambe wrote:
>> 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 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.
I've had the same wish as Ashwin. The reason why I want it is for
unit testing - jUnit prints out a backtrace when an assertion fails,
and I sometimes want that functionality when running C++ unit tests.
And I'm not about to run GDB every single time that I run a unit test.
libunwind sounds like it might be what I'm looking for, though.
David Carlton
david.carlton@sun.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2006-10-20 21:47 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-20 2:27 Programmatic access to stack traces in C or C++ programs Ashwin Bharambe
2006-10-20 3:10 ` Neo Jia
2006-10-20 3:13 ` Daniel Jacobowitz
2006-10-20 12:41 ` Ashwin Bharambe
2006-10-20 14:27 ` Daniel Jacobowitz
2006-10-20 20:18 ` Michael Snyder
2006-10-20 21:47 ` David Carlton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox