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 )
Thursday, December 11, 2008
Debugging Cocoa with DTrace Talk Tonight at Cocoaheads
Posted by
Colin Wheeler
at
7:46 AM
1 comments
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 } 22The 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.
Posted by
Colin Wheeler
at
6:33 PM
4
comments
Labels: libxml2, XML Parsing
Wednesday, October 01, 2008
Thank Goodness the F'ing iPhone NDA is being lifted
Apple FINALLY did the right thing today and publicly recognized what pretty much all iPhone developers and the public that have been paying attention to the news have known for a long time now, that the iPhone NDA was doing much more harm than good. From their page ( http://developer.apple.com/iphone/program/ ) "To Our Developers We have decided to drop the non-disclosure agreement (NDA) for released iPhone software. We put the NDA in place because the iPhone OS includes many Apple inventions and innovations that we would like to protect, so that others don’t steal our work. It has happened before. While we have filed for hundreds of patents on iPhone technology, the NDA added yet another level of protection. We put it in place as one more way to help protect the iPhone from being ripped off by others. However, the NDA has created too much of a burden on developers, authors and others interested in helping further the iPhone’s success, so we are dropping it for released software. Developers will receive a new agreement without an NDA covering released software within a week or so. Please note that unreleased software and features will remain under NDA until they are released. Thanks to everyone who provided us constructive feedback on this matter." Personally I am of the opinion that it's far better for people to eventually recognize that they made mistakes and try to correct it than people to live in blissful ignorance and just believe they did the right thing, so in this regard I am glad that Apple has finally publicly come around and recognize that the iPhone NDA, while it helped Apple "protect" some things on the iPhone, it was really doing a net damage to the platform that hurt it and made developers afraid to even touch the iPhone SDK. I was really looking forward to Bill Dudney's Core Animation book, I really originally just wanted to use it on the Mac, but because it just contained 1 chapter on the iPhone Core Animation differences the Pragmatic Programmers couldn't publish it. Thats 3-4 months that I couldn't simply hold a book in my hands and learn something that contributes back to Apples platform because 1 small part contains something on the iPhone (Yes I have the PDF, but I find it hard to sit down at a computer and read a whole book that way, I really only read book PDF's on my Mac as a reference looking up one small piece of information I want.) Now I hope they rescind what they said in their recent email saying they would just publish it without the chapter and finally publish the whole book in its entirety. And it's not just that the Core Animation book was probably one of the most publicly known books initially that people knew was being held up by the NDA, Amazon shows many other books on iPhone Development that are not yet available due to the NDA. In the end I really hope Apple has learned a lot from this. Now I hope that iPhone App quality will go up as a result of developers and sites soon being able to finally share code and information between each other. I know I have a couple iPhone SDK articles that are in development, but I haven't given them the time they deserve because I just didn't know if Apple was ever going to lift the NDA anytime soon. Apple we love you, we love the Mac and the iPhone, and we are even willing to put up with some things that developers of other platforms would scoff at because we like this platform that much. But when you get a ton of negative press and tons of developers are very publicly and loudly criticizing you all with the same opinion, it's not to give you grief, it's because we care about this that much and you're royally screwing up on something. If we really wanted to hurt you we'd be silent and say nothing. Also where is my iPhone Dev Key Apple?
Posted by
Colin Wheeler
at
12:01 PM
0
comments
Monday, September 22, 2008
Announcing the First NSCoder Night in Ames
I am starting NSCoder night in Ames, IA. The first meeting will be at The Stomping Grounds at 7pm on Tuesday the 23rd. If people want the location can change, just email me or tell me in person at the first NSCoder Night in Ames. Come and bring your Cocoa Projects and I'll see you then. Here is a link to the NSCoder Night site explaining what NSCoder Night is about: http://nscodernight.com/
Posted by
Colin Wheeler
at
1:24 PM
2
comments
Labels: NSCoder Night
Tuesday, August 19, 2008
Xcode Shortcuts: Original Documents now Creative Commons Licensed
I am hardly one to hold something back from the Mac Developer Community when I think I have something that will benefit everybody. As such I am finally doing something I've wanted to do for a while. My Xcode Shortcuts guide has a ton of downloads and is one of the most popular articles of all time on my site, and I think many people would like to use the content in various different formats to suit their needs so I don't want to hold you all back. As such I am releasing the Xcode Shortcuts guide under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License. In essence you can share and modify the work as you please as long as you attribute me as the author somewhere in the work, don't use it for Commercial Purposes and share the modified works under a similar License. The Xcode Shortcuts guide was created with Pages '08 and thus you need it to modify the document, though I have included a copy of the document exported to Rich Text format for some compatibility though the document looks like one big mess when I open it in Text Edit. The only thing I've done from the Guide I released is to remove the Xcode Icon just for legal reasons. If you want it's easy to paste it back in there. Although it's not required, if you modify and use the Xcode Shortcuts Guide leave a comment here or send me a quick email letting me know. Thanks! Have Fun! Right Click on the link and select "Save Linked File to 'Downloads'" Xcode Shortcuts Guide CC Licensed
Posted by
Colin Wheeler
at
10:15 AM
3
comments
Labels: Keyboard Shortcuts, Xcode
Sunday, August 03, 2008
I Agree with Gus: VMware Mac OS X Virtualization rocks!
Recently Gus Mueller posted about how VMware with a virtualized instance of Mac OS X is a dream come true for him. It's also a dream come true for me. The ability to run Mac OS X on a virtual machine instance has been something that's been on my wish-list for a while now. Backing up all my data and reinstalling all my apps so that I can install the current version of Mac OS X and then create another partition for the developer seed of Mac OS X is a pain in the butt and overall wiping my HD and reinstalling Mac OS X is something I only like to do when a new major revision of Mac OS X comes out like 10.5, 10.6, etc to avoid any problems and clear out the clutter. Also if I have to reboot to boot into a different version of OS X I feel like it requires a lot of planning to decide when I should boot into the other version of OS X, how long I should spend in it and what exactly I am working on when in the new version of OS X (not to mention you don't have your regular apps,etc), however with virtualization I can multitask and work on 2 things at once or switch between the 2 versions of Mac OS X more flexibly. I'd like to use this as a means to install say Snow Leopard and develop on that which can fit into my workflow very easily as it could fit in its own space and thus I can keep developing on Leopard and then switch to a space running Snow Leopard. I can only imagine the possibilities if I became a Mac Indie of how VMware with virtualized instances of Mac OS X could fit into my workflow with features like snapshots. This also takes VMware from being that app you have to run in order to run Windows XP/Vista so you can do your homework or just work on it because there's not a Mac version of that app or your client/teacher has to run your work on XP/Vista to a true Mac app. It's no longer a "this thing runs Windows" app it's a "this thing runs another instance of Mac OS X on Mac OS X!" which sounds a 1000x more exciting. I do also agree with Gus that Apple should allow the Mac OS X Client Versions to be virtualized as well and not just the Mac OS X Server version, but that is up to Apple legal. When I was flying back from WWDC, I got on my plane and a woman came to me frantically saying that she had a ticket for another better seat and wanted to trade seats with me so she could be with her children. I agreed and got bumped up to Economy Plus with plenty of leg room and more importantly sat right next to some guy working for VMware. To my surprise he was running Mac OS X Leopard Server in VMware, however I was tired and at the time I think I remember hearing something about how Apple was allowing this, so I thought "huh that's nice." He asked me if I had my snow leopard DVD on me so he could try that out, but I believe I put it in my checked luggage so I didn't get to see that, which is a shame because I think I'll try that soon and see if it works. After I got back and had time to rest I soon realized just how significant seeing Mac OS X virtualized was. All in all the future for Mac Developers working on 2 versions of Mac OS X is just looking better and better thanks to developments like this. By the way, a big Thank you goes out to all the twitterers who suggested that I go with VMware, i've been very much enjoying it ever since I switched from Parallels. Update: Heres a Blog Entry from VMware about Leopard Server virtualization and here is a YouTube Video showing it in Action
Posted by
Colin Wheeler
at
12:30 PM
5
comments
Labels: VMware
Thursday, June 19, 2008
One GIT Build Script to Rule them all
There seem to be at least 2 camps of GIT Users on OS X, those who installed git with the Mac OS X Package installer and those who installed it from Mac Ports. On my new project Gitty (a Git Repo inspector/manager just beginning development), I didn't want to discriminate, but at the same time I didn't know perl well enough to modify Marcus Zarra's Build Script beyond changing /opt/local/bin/git to /usr/local/git/bin/git. However one of my followers on Twitter was kind enough to modify the perl build script that sticks part of the GIT Hash in the about box to search for both locations and use which ever one you have installed so that it is now GIT Location Agnostic. It looks for the standard install location first and then if It can't find that searches for the MacPorts git install location and uses that. This works great for me and so i thought i'd share... enjoy. Thanks
# Xcode auto-versioning script for Subversion by Axel Andersson
# Updated for git by Marcus S. Zarra and Matt Long
# Updated to use git in the Standard Install Location or the MacPorts
# install location by Patrick Burleson
use strict;
# Get the current git commit hash and use it to set the CFBundleVersion value
my $REV = "";
if( -e "/usr/local/git/bin/git" )
{
$REV = `/usr/local/git/bin/git show --abbrev-commit | grep "^commit"`;
}
elsif ( -e "/opt/local/bin/git" )
{
$REV = `/opt/local/bin/git show --abbrev-commit | grep "^commit"`;
}
else
{
die "Git not found";
}
my $INFO = "$ENV{BUILT_PRODUCTS_DIR}/$ENV{WRAPPER_NAME}/Contents/Info.plist";
my $version = $REV;
if( $version =~ /^commit\s+([^.]+)\.\.\.$/ )
{
$version = $1;
}
else
{
$version = undef;
}
die "$0: No Git revision found" unless $version;
open(FH, "$INFO") or die "$0: $INFO: $!";
my $info = join("", <FH>);
close(FH);
$info =~ s/([\t ]+<key>CFBundleVersion<\/key>\n[\t ]+<string>).*?(<\/string>)/$1$version$2/;
open(FH, ">$INFO") or die "$0: $INFO: $!";
print FH $info;
close(FH);
Posted by
Colin Wheeler
at
11:30 AM
4
comments

