How to find out who called a method?

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?

Leave a Comment