Tuesday, May 27, 2008

DTrace for Cocoa Developers

Update: Sorry people I didn't know the default Viddler downloading permissions. If you logged in and tried to download the video and couldn't before, login again and you should be able to download it now.

So in this Screencast Im going to show you how to use DTrace and how to ultimately turn that knowledge into a custom DTrace Instrument for Instruments. Honestly I would login and download the full quality version of this from Viddler. I apologize for the hisses in the s's that I make, I was slightly tired and trying to control that. As always if I said something wrong and made a mistake please let me know right away and i'll make a correction here.

DTrace Examples in the movie

Example #1

colinw$ sudo dtrace -n 'syscall:::/execname == "Safari"/{@num[probefunc] = count();}' -q ^C

lstat 2 sigaltstack 2 sigprocmask 2 stat 2 getuid 16 mmap 32 munmap 36 geteuid 94 gettimeofday 128 stat64 248

Example #2

colinw$ sudo dtrace -n 'syscall::*open*:entry{printf("%s %s",execname,copyinstr(arg0));}' dtrace: description 'syscall::*open*:entry' matched 7 probes CPU ID FUNCTION:NAME 0 17604 open:entry mds . 1 17604 open:entry nmbd /private/var/samba/browse.dat. 0 17604 open:entry Safari /.vol/234881026/1644952 1 17604 open:entry mdworker /private/var/samba/browse.dat 1 17604 open:entry mds . 1 17604 open:entry Safari /.vol/234881026/230745/QuickTime Preferences ^C

Example #3

colinw$ sudo dtrace -n 'syscall:::/execname == "Safari"/{@num[ustack()] = count();}' -q Password: ^C

libSystem.B.dylib`sendto$UNIX2003+0xa CoreFoundation`__CFSocketEnableCallBacks+0x255 CoreFoundation`CFSocketEnableCallBacks+0x4f CFNetwork`_SocketStreamRead+0x5e5 CoreFoundation`CFReadStreamRead+0x1dd CFNetwork`httpRdFilterRead+0x5ef CoreFoundation`CFReadStreamRead+0x1dd CFNetwork`httpStreamRead+0x2aa CoreFoundation`CFReadStreamRead+0x1dd CFNetwork`httpReadStreamCB+0x94 CoreFoundation`_CFStreamSignalEventSynch+0x89 CoreFoundation`CFRunLoopRunSpecific+0xca8 CoreFoundation`CFRunLoopRunInMode+0x58 Foundation`+[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:]+0x140 Foundation`-[NSThread main]+0x2d Foundation`__NSThread__main__+0x134 libSystem.B.dylib`_pthread_start+0x141 libSystem.B.dylib`thread_start+0x22 2

Example #4

sh-3.2# dtrace -n 'objc16675:NSMutableArray::entry{@num[ustack()] = count();}' dtrace: description 'objc16675:NSMutableArray::entry' matched 24 probes ^C

CoreFoundation`+[NSMutableArray arrayWithCapacity:] Keynote`0x201ab Keynote`0x35289 Keynote`0x9d981 AppKit`-[NSWindow makeFirstResponder:]+0x12e Keynote`0xa1b29 AppKit`-[NSWindow sendEvent:]+0x1505 Keynote`0x18c7e7 AppKit`-[NSApplication sendEvent:]+0xadc SFApplication`-[SFAppApplication sendEvent:]+0x283 Keynote`0x26d69 AppKit`-[NSApplication run]+0x34f Keynote`0x352f Keynote`0x34a5 Keynote`0x4cc36 Keynote`0x4cb5d 0x2 1

Useful DTrace Links Solaris DTrace Guide (PDF) from Sun Solaris DTrace Guide (HTML) from Sun DTrace Review (Google Tech Talk) MacTech: Exploring Leopard with DTrace


Joe Goh said...

I logged into Viddler but couldn't find the download link though. Has downloading been enabled for the video?

Colin Wheeler said...

joe it should be there now. it's below the playing video view.

Joe Goh said...


Hmm, I must be blind. Where's the download link?

Colin Wheeler said...

Joe: Sorry just found out that Viddler put download restrictions on the video so only I could see it when I logged in. I changed the permissions so you should be able to download it now. Sorry about that, I didn't know Viddler put that kind of restriction on the video by default.

Come to think of it I should go through my old videos and make sure the restrictions are off of them as well.