如何找出谁调用了一个方法? [英] How to find out who called a method?

查看:30
本文介绍了如何找出谁调用了一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:当我的方法 -fooBar 被调用时,我希望它登录控制台,哪个其他类的其他方法调用了它.

Example: When my method -fooBar gets called, I want it to log in the console which other method of which other class called it.

现在,我只知道如何记录 fooBar 本身及其类的方法名称,如下:

Right now, I only know how to log the method name of fooBar itself and it's class, with this:

_cmd

[self class]

这能算出来吗?

推荐答案

在完全优化的代码中,没有 100% 万无一失的方法来确定某个方法的调用者.编译器可能会采用尾调用优化,而编译器有效地为被调用者重用调用者的堆栈帧.

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.

要查看此示例,请使用 gdb 在任何给定方法上设置断点并查看回溯.请注意,您不会在每次方法调用之前看到 objc_msgSend().这是因为 objc_msgSend() 对每个方法的实现进行了尾调用.

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.

这只是一个问题;实际上,您是在问我如何重新发明 CrashTracer 或 gdb?".职业生涯的一个非常困难的问题.除非您希望调试工具"成为您的职业,否则我建议您不要走这条路.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆