如何找出谁叫方法? [英] How to find out who called a method?
问题描述
示例:当我的方法-fooBar被调用时,我想要它登录控制台其他类调用它的其他方法。
只知道如何记录fooBar本身和它的类的方法名称:
_cmd
$ b $这是可能弄清楚的吗? div class =h2_lin>解决方案
在完全优化的代码中,没有100%肯定的方式来确定某个方法的调用者。编译器可以使用尾调用优化,而编译器有效地为被调用者重新使用调用者的栈帧。
要查看一个示例,请在任何给定的方法使用gdb并查看backtrace。注意,在每个方法调用之前你都没有看到objc_msgSend()。这是因为objc_msgSend()对每个方法的实现进行了一次尾调用。
虽然可以编译未优化的应用程序,系统库避免了这一个问题。
这只是一个问题;实际上,你问的是如何重新创建CrashTracer或gdb?。一个非常困难的问题,在其上作出的职业。除非你想要调试工具成为你的职业生涯,否则我建议你不要走这条路。
你真的想回答什么问题?
Example: When my method -fooBar gets called, I want it to log in the console which other method of which other class called it.
Right now, I only know how to log the method name of fooBar itself and it's class, with this:
_cmd
[self class]
Is this possible to figure out?
In fully optimized code, there is no 100% surefire way to determine the caller to a certain method. The compiler may employ a tail call optimization whereas the compiler effectively re-uses the caller's stack frame for the callee.
To see an example of this, set a breakpoint on any given method using gdb and look at the backtrace. Note that you don't see objc_msgSend() before every method call. That is because objc_msgSend() does a tail call to each method's implementation.
While you could compile your application non-optimized, you would need non-optimized versions of all of the system libraries to avoid just this one problem.
And this is just but one problem; in effect, you are asking "how do I re-invent CrashTracer or gdb?". A very hard problem upon which careers are made. Unless you want "debugging tools" to be your career, I would recommend against going down this road.
What question are you really trying to answer?
这篇关于如何找出谁叫方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!