Can any body please tell me where function name is stored in stack frame . I know return address is stored in link register (lr) . But my requirement is to implement backtrace which print function name I don't want to print function address .
Asked
Active
Viewed 192 times
2
-
3I'm pretty sure function name is never stored on stack. – Andreas Apr 18 '16 at 05:36
-
[This question and answer](http://stackoverflow.com/questions/15752188/arm-link-register-and-frame-pointer) gives details on how to do this manually; this is **NOT** a canned solution so it sounds like it is not for you. You can also use `/proc/self/stack` if it exists to see kernel call stack for the task. This depends on a kernel `.config` option. – artless noise Apr 18 '16 at 15:21
-
4Possible duplicate of [How to retrieve function name from function address using link register (like backtrace\_symbol) in linux](http://stackoverflow.com/questions/36685976/how-to-retrieve-function-name-from-function-address-using-link-register-like-ba) and also [How to unwind...](http://stackoverflow.com/questions/29559347/how-to-unwind-the-stack-to-get-backtrace-for-the-specified-stack-pointer-sp) – artless noise Apr 18 '16 at 15:22
3 Answers
5
There are two API backtrace() and backtrace_symbols() you can use to print backtrace information. please use man backtrace for more details about these two APIs.
As for function name, I do not think it is saved in stack frame, you need refer to map information/symbol table to find it with address.
gzh
- 3,507
- 2
- 19
- 23
-
Yes I am doing on my x86 architecture it working with `backtrace()` and `backtrace_symbols()` but I want it for ARM architecture . inbuild library is not supporting that. – Bhavith C Acharya Apr 18 '16 at 05:25
-
-
@BhavithCAcharya If you use binutil tools such as `readelf -a`, for ARM, it should be something like `arm-poky-linux-gnueabi-readelf`, you will find function name are saved in .symtab section, with value, maybe it will give you some clue about how to implement. – gzh Apr 18 '16 at 07:07
1
In C and C++ function name is not stored in stack frame.
Aivars
- 93
- 7
-
Then how can I get function name using stack frame information like `backtrace_symbols()` – Bhavith C Acharya Apr 18 '16 at 06:29
0
You could use the Preprocessor symbols:
__FILE__ and __LINE__
Somthing like:printf("File: " __FILE__ "in Line: " __LINE__);
This returns the current file and the line the function was called. Note that the string is stored in memory. So don't use this to often.
Schafwolle
- 501
- 3
- 15