How to find out who is the caller of a method or function? [duplicate]

左心房为你撑大大i 提交于 2019-12-21 06:16:14

问题


I want to write a debug function or method that will help print useful information. When it is called, I need:

  • the memory address of the calling object (if called by an object)
  • the method signature of the caller (or the name of the method), or the name of the function
  • the class name that owns that method or function

Is it possible to get this information without passing a whole bunch of parameters?

I want to make something like:

debug();

which then goes into every method and function, and helps printing out useful informations about what's going on.


回答1:


My first instinct would be to suggest using gdb and breakpoints, since most of that information is available in a stack trace. However, if you really want to see it printed, there are ways to approximate what you're talking about

The preprocessor recognizes the __PRETTY_FUNCTION__ macro for printing the function/method name, and it works well for Objective-C methods. If you print the method name and the value of self at each method of interest, you've pretty much generated a poor man's stack trace.

Try including a #define like this in a header that every file that wants it includes:

#define METHOD() printf("%s\t0x%x\n", __PRETTY_FUNCTION__, (unsigned int)self)

Then just include this line whenever you want to print that information:

METHOD();

The output will look something like this:

-[MyClass initWithFoo:bar:] 0x12345678

As I mentioned, this type of approach is likely to produce huge amounts of output, and gdb is probably a more pragmatic option.




回答2:


  1. Set a symbolic breakpoint on the methods you are interested in debugging.
  2. If you need to move back up in the stack (to see where the method call came from) you can either use the Xcode Debugger, or if you want to automated it, use backtrace n to move back up in the stack n number of frames.



回答3:


I use Karl Kraft's DebugLog




回答4:


Sorry I don't have a full answer for you, just some relevant info.

NSThread defines a method that gets you an unsymbolicated backtrace, callStackReturnAddresses. In 10.6, it also will give you strings satisfying your second and third requests callStackSymbols.

Getting address of the calling object is interesting, but not totally simple. It's going to involve walking up the stack and picking out where the receiver object is usually stored if there is a usual place it's stored on ARM. For this you (or someone) would need to understand the calling conventions for ARM, which are probably documented in an ARM ABI (application binary interface) somewhere. I don't know ARM. This would be doable on i386, and not really on ppc.




回答5:


You can use the backtrace(3) API to find out what method or function called you. Getting the calling object, if there was one, is much, much harder, though.



来源:https://stackoverflow.com/questions/1373991/how-to-find-out-who-is-the-caller-of-a-method-or-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!