Hi Peoples, 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?
A:
Yes it is possible, objective-c is highly dynamic and you can get a lot of information at runtime. Have a closer look at the Objective-C Runtime Reference
mgratzer
2010-10-06 13:39:50
You haven't answered his question. Which of the Objective-C runtime calls will allow the logging of every message sent to every object?
JeremyP
2010-10-06 14:24:32
Yeah, I think what only way to catch all messages it's substitute objc_msgSend method with my custom method.
Sergey Zenchenko
2010-10-06 14:47:04
To be fair it was an answer matching the question
Mike Abdullah
2010-10-06 15:29:21
+4
A:
Use dtrace
, it's already built-in to the system. See this great introductory article at MacTech.
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.
As an exercise, please put the following into a file called objc.d
:
objc$target:::entry
{
printf("[%s %s]\n", probemod,probefunc);
}
Then run from the command line
$ sudo dtrace -q -s objc.d -p 3333
where 3333 should be the pid of some Cocoa app. You'll get a log of every message sent to any object! Woo-hoo!
Yuji
2010-10-06 16:24:18
You really should redirect output to a file for later analysis. This will obviously produce a truly voluminous quantity of data rapidly. Something like `sudo dtrace -q -s objc.d -p 3333 > /tmp/allmethodcalls.txt`
bbum
2010-10-06 19:43:37
Note also that the above won't necessarily catch every single method call. The output rate is simply too fast for dtrace to handle and there will be thousands dropped during active computation.
bbum
2010-10-06 19:45:14