Can I get a backtrace from an explicitly allocated stack

86 views Asked by At

I have a pool of threads that have explicitly allocated stacks (i.e., using pthread_attr_setstack).

Is it possible to use something like gnulib's backtrace & backtrace_symbols on this stack?

Thanks.

1

There are 1 answers

0
Ben Jackson On

backtrace() returns a backtrace for the calling program, in the array pointed to by buffer.

Therefore yes, it does not care where the stack was allocated. You simply call it from the target thread and the stack information is implicit in the current stack pointer.

If you want to backtrace another thread (other than the one that's running) that's trickier. For one thing, it may be running, which would make all attempts to backtrace subject to races. But for another, I don't think there's any standard way for the current stack pointer (which is where the trace should start) to be made visible to another thread. Without that information you can't start the trace, because the base of the stack isn't really enough information.

(ptrace can get the current register values. This is the system call used by debuggers. It will be quite disruptive, though)