How do i find address?

How can I find the address (the return address is sufficient) of the caller of a function ?

  • I'm trying to debug some code and I want to pull the return address of the stack. I'm running this on a MIPS core. I tried putting a local variable at the bottom of the local variable list, a ULONG, (I thought that would the last local variable just above the return address). I then took the address and added 1 to it (assuming that adding one to ULONG will add 4 to the address). However, I didn't get an address that mapped anywhere in the symbol table. If someone knows how I can get this. If you can give me an assembly snippet or whatever that'll be great.

  • Answer:

    I'm not familiar with the MIPS architecture, but if you are using gcc, there is a gcc builtin for this purpose: void * __builtin_return_address (unsigned int level); The gcc builtin documentation (http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html) says the following: This function returns the return address of the current function, or of one of its callers. The level argument is number of frames to scan up the call stack. A value of 0 yields the return address of the current function, a value of 1 yields the return address of the caller of the current function, and so forth So, you can do something like this: #include <stdio.h> void foo() { printf("%p\n", __builtin_return_address(1)); } int main (int argc, const char* argv[]) { foo(); }

Vibhav Sreekanti at Quora Visit the source

Was this solution helpful to you?

Other answers

I do not know of any standard way of doing this in C, but it is trivial in assembly. I have never done any assembly programming in MIPS, but as I understand it something like lw $r1 0($sp) should load the return address into register 1. If you need this in C, you could read up on how your compiler implements inline assembly, and do something like int myfunction() { int *foo; __asm__ { lw $r1 0($sp) sw $r1 foo } printf("%p",foo); } depending on how inline assembly and pointers work in your compiler. To then get the calling functions address would then require you to decrement the address until you hit a match in the symbol table. P.S. If you add your debug print to the top of the function, you should be able to use the $ra register, which contains the most recent return address, but this could be from a function that has already returned.

Lars Johan Olof Lidström

Jyotiswarup Pai Raiturkar

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.