1

I am trying to get the call stack, for some reason the following code returns a wrong stack pointer:

 unsigned int stack_pointer = 0; __asm("la $26, %[spAddr]\n\t" "or $27, $0, $sp\n\t" "sw $27, 0($26)\n\t" "nop"::[spAddr] "m" (stack_pointer)); return stack_pointer; 

What am I missing here?

1
  • You aren't declaring any output parameters (declared after the 1st colon). Where are you expecting things to get returned? See the docs. Commented Aug 20, 2017 at 9:04

1 Answer 1

3

To get the stack pointer use the proper output constraint like so:

register unsigned sp asm("29"); asm("" : "=r" (sp)); 

Note that mips uses a register for the return address, but of course non-leaf functions might store it on the stack.

To implement a backtrace, you can however use the builtins __builtin_return_address and __builtin_extract_return_addr as described in the gcc manual.

Also, if glibc is available, it already has backtrace function, see man backtrace.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.