Thursday, January 22, 2009

Late Night Cocoa: DTrace

Last week I recorded an episode of Late Night Cocoa with Scotty in which I discuss DTrace (why it was created & how it works) and reveal the origin of the name "Cocoa Samurai", you can listen to it here: http://www.mac-developer-network.com/podcasts/lnc/lnc038/ I realize listing to my conversation I left 1 big thing hanging. Mainly I mentioned that it'd be great if the OmniGroup had DTrace probes in their apps (they do, not USDT's as far as I know, just standard ones) and if I could trace that, what I meant is that its great from a pro-user/consumer perspective if say OmniFocus acts up on me one day and I can use DTrace to trace OmniFocus and give them tracing information to help them fix the bug, I am against using ptrace() on an app because it stops me from helping you debug your apps with DTrace, especially when it's not too much trouble for me to gather this information and send it to you to help you. Also for those of you asking... Yes I am considering writing a DTrace/Cocoa book to give this subject the justice is deserves. I've got some ideas, and as soon as I get settled into my last school term (for real this time) I'll think about this in more detail, but for the moment I am fairly busy.

Wednesday, January 21, 2009

Debug Cocoa with DTrace Guide & Embedding DTrace into Xcode Video

Debugging Cocoa with DTrace Guide

dbg_guide_tmb.png

Finally today I can finally reveal my Debugging Cocoa with DTrace guide to you. I had originally meant that this be a quick article on this site, but I kept adding and adding to it and finally by the time I knew what I had gotten myself into it was 40+ pages long. This guide isn't as comprehensive as I would like even at the length that it is. It covers many things regarding the nature of tracing Cocoa apps with DTrace and some various useful techniques. If you don't know DTrace this guide has links to this site and Suns sites that introduce you to DTrace. Again many thanks to the reviewers who helped me in recognizing the things that could be improved or the finer points I missed. Thanks for your help! You can download it from here (right click & save as): http://www.1729.us/cocoasamurai/Debugging%20Cocoa%20with%20DTrace.pdf Embedding DTrace in your Xcode/Cocoa Project Video In this video I explain how to embed DTrace probes into your application which you can then use to get a lot of useful information about the state of your application. Watch it below or you should be able to watch and download it at viddler: http://www.viddler.com/explore/Machx/videos/8/ Update Viddler isn't allowing downloads at the moment. I don't know what is happening here as the downloads usually work fast, but if they don't work in a while I'll try and find out what's going on with Viddler. All I know is downloading/embedding is set to everyone so Viddler should be allowing downloads. Update 2: Viddler Downloads are working now :)

Thursday, January 08, 2009

Announcing the DTrace TextMate Bundle Beta 1 (Updated)

UPDATE: The DTrace TextMate Bundle is now on Beta 3 and I am no longer hosting it, it is on GitHub and will be updated there. You should also go to GitHub to get the latest version and fixes for the bundle. I've been working for a little while on a DTrace Bundle for TextMate to make life writing DTrace Scripts a lot easier and now you can download Beta 1 of the DTrace TextMate Bundle. Now I should warn you it's very rough, but the bulk of the functionality is there. So if you are okay with an evolving bundle for TextMate go ahead and try it. Heres a video showing it off You can download it here: http://github.com/Machx/dtrace-textmate-bundle/tree/master This bundle is open source and is hosted at github: http://github.com/Machx/dtrace-textmate-bundle/tree/master. So go get git and you can keep it in sync with the latest fixes. Here are the things that work: Syntax Highlighting (mostly) D Language Code Folding All Tab Triggers All Included Scripts (Core Data, Garbage Collection, Cocoa Events, etc.) Bugs/What Doesn’t work After you do a tab trigger if you immediately try to do another tab trigger TextMate won’t let you (don’t know if this is my bug or a TextMate bug) Syntax Highlighting only highlights with one color (this will be corrected in the future) Run in Terminal Command doesn’t work right now There is a menu item “More Mac OS X D Scripts” that doesn’t contain anything at the moment, in time several useful DTrace scripts will be added, but for now it’s empty

Tuesday, January 06, 2009

Coming Soon... DTrace Bundle for TextMate

I've been working on this for a little while and it's still a relatively early work in progress, but the idea it to make scripts that you intend to run from Terminal much easier and even write scripts that mirror what's available in Instruments so you can make your own custom modifications if you want to. Stay tuned for more word on when this will be available....

Im a guest on Mac Developer Rountable 15: Getting Started with Cocoa

A couple weeks ago I was graciously invited on to be a guest for the Mac Developer Roundtable where the topic was "Getting Started with Cocoa" and I had a good discussion with Danny, Scotty, Brent, Mich and Justin about what it took to get started in Cocoa and what we wish we had paid attention to when we were starting out. I haven't listened to it yet, but if scotty can clean up my performance from the first time I was on this one should sound great. Listen to it now: http://www.mac-developer-network.com/podcasts/mdr/mdr015/

Thursday, December 11, 2008

Debugging Cocoa with DTrace Talk Tonight at Cocoaheads

Tonight at the Des Moines Cocoaheads I will be doing a talk in which I will describe how DTrace can be the perfect compliment to your debugging session and help you find bugs quicker regardless of if you are using DTrace through the Terminal or Instruments. Topics that will be covered are * Brief review of DTrace & how it works * What DTrace can do for you * DTrace and the Objective-C Provider * Tasks DTrace can make much easier for you that would otherwise be mundane and consume time * Getting the most out of Custom DTrace Probes in Instruments * useful DTrace snippets & functions to know * and more... If you are in the central Iowa area come and see this and more at DM Cocoaheads tonight ( http://www.cocoaheads.org:80/us/DesMoinesIowa/index.html. ) I look forward to seeing you there! After this talk is over with all presentation materials will be posted here with a in depth article with more information than I could present in a reasonable amount of time tonight. Also this is kinda a Intermediate/Advanced Level Talk it assumes some basic knowledge of how DTrace works (though like I said there will be a very brief review of DTrace and how it works.) If you don't know DTrace check out my earlier article and the accomanying links from DTrace for Cocoa Developers ( http://is.gd/mIq )

Friday, October 17, 2008

Getting Some XML Love with libXML2

A while back Marcus Zarra did a good article on working with libXML2 and xmlTextReader to parse XML Data without loading the whole thing into memory. However the tutorial failed my needs only for 1 reason... he used a file on Disk. In his call to xmlReaderForMemory() he passes a path to the resource on disk (yes I know it makes for an easy self contained project that's easy to demonstrate, but bear with me here), I made the assumption that this made the method unusable for objects residing only in memory. In fact just about every method in libxml2 seems to have an argument for a path to a resource on disk and initially thought I couldn't really do strict in memory xml parsing. How wrong I was as you'll see later on. What I am going to show you today is how to create a valid xmlTextReader object from libXML2 with only the assumption that you are downloading the xml data from a server somewhere. Marcus showed a great intro into XML parsing with libXML2 and using a file on disk, but in my code I am getting Data back in memory from calls to web services on the internet and so I went to find out what it'd take to do XML Parsing with libXML2 without using a reference to a file on disk. Basically I wanted to assume that I just have a valid NSData object that has XML in it. When I got done with this I found out there are 2 ways to do this 1 very insanely easy way which isn't obvious from looking at the libXML2 method names alone and 1 harder way that accomplishes the same thing. The hard way

    1 NSData *xmlData; /* for this example assume xmlData has some valid xml data */
    2 
    3 xmlParserInputBufferPtr inpt = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_UTF8);
    4 
    5 if(!inpt) {
    6     NSLog(@"Failed to create XML Input Buffer");
    7     return;
    8 }
    9 
   10 NSString *XML_STRING = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
   11 
   12 xmlBufferPtr xmlBuffr = xmlBufferCreateStatic((void *)[XML_STRING UTF8String] ,strlen([XML_STRING UTF8String]));
   13 
   14 inpt->buffer = xmlBuffr;
   15 
   16 xmlTextReaderPtr reader = xmlNewTextReader(inpt, NULL);
   17 
   18 if (!reader) {
   19     NSLog(@"Failed to create xmlTextReader");
   20     return;
   21 }
   22 
The point in this example is to create a xmlParserInputBufferPrt object that will be passed into xmlNewTextReader() so we don't read anything off of the disk. On line 3 we create this xmlParserInputBufferPtr by using the alloc method and passing in that we are going to use UTF8 encoding. And of course on line 5 we check for the existence of the xmlParserInputBufferPrt and if it doesn't exist there is really no point in going any further. Then I create a NSString object from the NSData object (line 10) which will allow us to get the UTF8String from the NSData object and (again) signify that we are using UTF8 Encoding. Then the big thing we need to create (line 12) is the xmlBufferPrt object. This creates a static buffer with the entire contents of the XML from the NSData object passing in a pointer to the string contents itself and the length of the string. Then (line14) in the xmlParserInputBufferPtr we point the data buffer pointer to the xmlBufferPrt object we created on line 12. After that it's just a matter of creating a xmlTextReaderPtr (line 16) with xmlNewTextReader and pointing the input buffer to the xmlParserInputBufferPointer which now has all the XML in it and pass NULL to the path of the XML. Now you have a xmlTextReader which you can use to parse xml with. The Easy Way Now I started going down this path because of Peter Hosey's suggestion of using xmlParserInputBufferPtr which logically seemed like the best solution to my problem of wanting to use strictly in memory objects and do no reading off of disk. If I could just pass NULL for the path in xmlNewTextReader() could I do the same with xmlTextReaderForMemory and it'll still work? As it turns out... YES... yes it does work.
    1 NSData *xmlData; /* for our purposes here assume xmlData has valid xmlData in it */
    2     
    3 xmlTextReaderPtr reader = xmlReaderForMemory([xmlData bytes], 
    4                                                                                          [xmlData length], 
    5                                                                                          NULL, NULL, 
    6     (XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA | XML_PARSE_NOERROR | XML_PARSE_NOWARNING));
    7     
    8 if (!reader) {
    9     NSLog(@"Failed to create xmlTextReader");
   10     return;
   11 }
In fact all you need to do is set the 3rd and 4th parameters to NULL in xmlReaderForMemory() and it works fine with in memory objects like if you have a NSData object you got back from API's like say NSURLConnection sendSynchronousRequest. The only thing I wish is that there was a lot better documentation on libxml2, maybe there is a great rescource I just don't know about, but from my googling it was hard to find anything and I had to do a lot of trial by error. Update: Im aware of the documentation at xmlsoft.org, most of it however pretty much just shows you a list of method names and describes the arguments you pass in to methods with a couple decent doc's/tutorials. This isn't great documentation for me as it doesn't describe which arguments are necessary, if a argument is optional you should make that crystal clear and the documentation doesn't make it clear that a lot of arguments are in fact optional in libxml2 methods. Specifically one page im referring to is at http://xmlsoft.org/html/libxml-tree.html#xmlParserInputBufferPtr.

 
...