2

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?

Sergey Zenchenko
  • 854
  • 1
  • 12
  • 31

3 Answers3

8

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
  • 34,103
  • 3
  • 70
  • 88
  • 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 Oct 06 '10 at 19:43
  • 1
    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 Oct 06 '10 at 19:45
2

You can have each and every Objective-C message sent in your application logged by setting the NSObjCMessageLoggingEnabled environment variable to YES. Select the executable in Xcode, press Cmd-I to show the inspector and add the environment variable there (Arguments tab). Obj-C messages are logged in /tmp/msgSends-<pid>. Remember to switch the setting off when done because the log file may be huge.

Costique
  • 23,712
  • 4
  • 76
  • 79
  • 1
    You can also duplicate your current scheme and create a new one that enables msgSends. Under `Run` -> `Arguments` -> `Environment variables`. I recommend making the scheme shared so other people on your team can use it too. A "Zombies" scheme can be similarly useful. – funroll Aug 13 '13 at 13:36
0

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
  • 620
  • 1
  • 5
  • 8