Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get function return address within function?

I want to print return value in my tracer, there are two questions

  1. How to get return address ?
  2. The return position is updated before OR after ~Tracer() ?

Need text here so Stackoverflow formats the code:

struct Tracer
{
  int* _retval;
  ~Tracer() 
  { printf("return value is %d", *_retval); }
};


int foo()
{
  Tracer __tracter = { __Question_1_how_to_get_return_address_here__ };

  if(cond) {
     return 0;
  } else {
     return 99;
  }

  //Question-2: 
  // return postion is updated before OR after ~Tracer() called ???
}
like image 529
Raymond Avatar asked Jan 15 '23 23:01

Raymond


1 Answers

I found some hints for Question-1, checking Vc code now

For gcc, __builtin_return_address http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html

For Visual C++, _ReturnAddress

like image 154
Raymond Avatar answered Jan 28 '23 16:01

Raymond