捕获所有Objective-C消息并获取Cocoa运行时中的对象列表 [英] Catch all Objective-C messages and get list of objects in Cocoa runtime
问题描述
我需要捕获并记录Cocoa应用程序中对象发送的所有消息。还需要在运行时对象实例列表。
I need to catch and log all messages sended by objects in Cocoa app. And also I need list of object instances in runtime. It's is posible?
推荐答案
使用 dtrace
到系统。请参阅MacTech的这篇精彩的介绍性文章。
Use dtrace
, it's already built-in to the system. See this great introductory article at MacTech.
Dtrace
是一个系统级标准机制,因此您可以记录活动。各种系统API通知内核,即每个系统调用,每个 objc_msgSend
等生成可跟踪点,您可以将dtrace脚本传递到内核以记录这些活动。非常强大。
Dtrace
is a system-wide standard mechanism so that you can log activities. Various system APIs notify the kernel, i.e. every system call, every objc_msgSend
, etc generates a traceable point, and you can pass dtrace script to the kernel to log these activities. It's very powerful.
作为练习,请将以下内容放入一个名为 objc.d
的文件中:
As an exercise, please put the following into a file called objc.d
:
objc$target:::entry
{
printf("[%s %s]\n", probemod,probefunc);
}
然后从命令行运行
$ sudo dtrace -q -s objc.d -p 3333
其中3333应该是一些Cocoa应用程序的pid。你将得到发送给任何对象的每条消息的日志! Woo-hoo!
where 3333 should be the pid of some Cocoa app. You'll get a log of every message sent to any object! Woo-hoo!
这篇关于捕获所有Objective-C消息并获取Cocoa运行时中的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!