17

I'm curious why __builtin_return_address() doesn't supports other arguments than 0 in ARM ? It's a problem that somehow you can't deduce calling function address from the stack of ARM ? Or something else ?

Thanks

2 Answers 2

12

According to this post <http://codingrelic.geekhold.com/2009/05/pre-mortem-backtracing.html>,

Also on some architectures, including my beloved MIPS, only __builtin_return_address(0) works. MIPS has no frame pointer, making it difficult to walk back up the stack. Frame 0 can use the return address register directly. If ARM also does not have a frame pointer, this would explain the limitation.

See also http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html.

Sign up to request clarification or add additional context in comments.

3 Comments

On the ARM, the return address is passed in register R14, and it is the duty of the callee to save it when calling another function. So even with a frame pointer, there is no guarantee that the return address is ever stored on the stack.
Indeed, when the return address is callee-saved rather than saved on the stack by the call instruction, it's impossible in general to find it. There should be a way using dwarf2 unwind/debugging data, but that would require __builtin_return_address to be a call to a heavy weight unwind library call rather than a trivial builtin...
BTW, I solved this stack tracing problem on ARM by using -finstrument-functions which are called on every function entry/exit. There is overhead of course, but it is acceptable for me. (And there is no_instrument_function attribute where max speed of call is required...)
4

Backtrace on ARM is hard. The Glibc backtrace function does work these days, but you need an up to date compiler/glibc, and you need to have built everything with -funwind-tables. GDB also has trouble without debug info.

1 Comment

Thanks for mentioning -funwind-tables! My backtraces on ARM were always depth 1 until I enabled this compiler flag. Using GCC 4.3.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.