Track all ObjC method calls?

后端 未结 5 1385
一整个雨季
一整个雨季 2020-11-28 11:31

Sometimes when looking at someone else\'s large Objective-C program, it is hard to know where to begin.

In such situations, I think it would be helpful to log every

相关标签:
5条回答
  • 2020-11-28 12:01

    That's an interesting question. The answer would be more interesting if the solution supported multiple execution threads and there were some sort of call timeline that could report the activity over time (maybe especially with user events plotted in somehow).

    I usually fire up the debugger, set a breakpoint at the main entry point (e.g. - applicationDidFinishLaunching:withOptions:) and walk it in the debugger.

    On OSX, there are also some command-line tools (e.g. sample and heap) that can provide some insight.

    It seems like some kind of integration with instruments could be really cool, but I am not aware of something that does exactly what you're wanting (and I want it now too after thinking about it).

    If one were to log a thread number, and call address, and some frame details, it seems like the pieces would be there to plot the call timeline. The logic for figuring out the appropriate library (Apple-provided or third party) should exist in Apple's symbolicatecrash script.

    0 讨论(0)
  • 2020-11-28 12:03

    You can set the environment variable NSObjCMessageLoggingEnabled to YES. This will write a log of all message sends in the folder /tmp/msgSends-xxx.

    0 讨论(0)
  • 2020-11-28 12:06

    You could add a symbolic breakpoint to objc_msgSend(), and have it log the second parameter without stopping.

    How to do it for your own methods only though is a toucher task. Maybe if you could inspect the class name being called and do some magic to have a conditional breakpoint for only calls where the class' prefix matches your own?

    0 讨论(0)
  • 2020-11-28 12:11

    I don't think logging every single call is practical enough to be useful, but here's a suggestion in that direction.

    On a side note, if it's a large program, it better have some kind of documentation or an intro comment for people to get started with the code.

    In any case, every Cocoa application has an applicationDidFinishLaunching... method. It's a good place to start from. Some apps also have their principal (or 'main window') class defined in the Info.plist file. Both these things might give you a hint as to what classes (specifically, view controllers) are the most prominent ones and what methods are likely to have long stack-traces while the program is running. Like a game-loop in a game engine, or some other frequently called method. By placing a breakpoint inside such a method and looking at the stack-trace in the debugger, you can get a general idea of what's going on.

    If it's a UI-heavy app, looking at its NIB files and classes used in them may also help identify parts of app's functionality you might be looking for.

    Another option is to fire up the Time Profiler instrument and check both Hide missing symbols and Hide system libraries checkboxes. This will give you not only a bird's eye view on the methods being called inside the program, but also will pin-point the most often called ones.

    By interacting with your program with the Time Profiler recording on, you could also identify different parts of the program's functionality and correlate them with your actions pretty easily.

    0 讨论(0)
  • 2020-11-28 12:13

    Instruments allows you to build your own "instruments", which are really just DTrace scripts in disguise. Use the menu option Instrument >> Build New Instrument and select options like which library you'd like to trace, what you'd like to record when you hit particular functions, etc. Go wild!

    0 讨论(0)
提交回复
热议问题