开发者

How to get a caller method name?

Frequently, the same method X gets called from different other methods A, B, C. Is it possible to get a name of the caller method (A, B, C) from inside method X? Most preferably would be GDB console command (during debug) but stuff with NSLo开发者_JAVA技巧g would also be sufficient.


Typing bt while inside the called function will help. This prints the backtrace of the called functions; the function just below the called function in the call hierarchy is the one that called it.

(gdb) bt
#0  factorial (n=10) at recursive.c:13
#1  0x0040135e in main () at recursive.c:9

Observe, here, that main called factorial.


You can use the command backtrace in gdb to see the call stack.


If you're at a breakpoint inside of method X you can use where to print out the stack, you will be able to see where the call to X originated.


Typedef NSLog to print the function name and add it at start and end of the method:

#define CustomLogEnter(fmt, ...) NSLog((@"Function entered %s  " fmt), __PRETTY_FUNCTION__, ##__VA_ARGS__);

Since you want an alternative to gdb and without using breakpoints, you can try the above, Also you can log the line number using __LINE__.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜