Hello All,
I am trying to get the address of the caller function. Basically i am calling a noreturn function (lets say A) from main. In "A" i get the address of the caller using __builtin_return_address function. After getting the address i call another function "B" and in "B" i print the caller address.
void main() {
A();
return;
}
__noreturn void A(){
void *caller = __builtin_return_address(0);
B(caller);
}
void B(void *caller) {
printf ("The caller address is %p",caller);
}
However, I am not able to get the correct caller address. My aim is to use addr2line utility and print the correct file name and the exact location from where "A" was invoked.
If i call the noreturn function A() from a wrapper. It gives me correct result.
void main() {
wrapper();
return;
}
void wrapper() {
A();
}
__noreturn void A(){
void *caller = __builtin_return_address(0);
B(caller);
}
void B(void *caller) {
printf ("The caller address is %p",caller);
}
Any help is appreciated.
Thanks
What I have tried:
Dumped the object code to debug and find the address which is printed. I see that the address which "__builtin_return_address" is some random address.
Tried calling __builtin_return_address with 1. However i get an error saying the output will be unpredictable.