Archive for the 'Objective-C' Category

AutoZone: The Objective-C Garbage Collector

Tuesday, November 11th, 2008

The source code for AutoZone, the Objective-C Garbage Collector found in Mac OS X Leopard, is now available. It has been released under the Apache v2 license.

This is the same collector that is found in Mac OS X Leopard 10.5.3 (the collector was not updated as a part of 10.5.4 or 10.5.5).

The garbage collector is not limited to Objective-C. It is actually a fairly generic scanning, conservative, generational, multi-threaded, language agnostic, collector. The implementation has certainly been tested and optimized with Mac OS X based applications.

Notably, MacRuby uses AutoZone to offer a common GC implementation across object graphs that span Ruby and Objective-C.

Posted in Apple, Mac OS X, Objective-C, Software, Technology | 11 Comments »

Objective-C: Printing Class Name from Dtrace

Saturday, January 26th, 2008

When analyzing an Objective-C application with Dtrace, one big challenge is how to introspect any objective-c objects that are passed as parameters into the various trace points.

While you can use the Objective-C provider (documented on the dtrace man page) to trace particular methods of specific classes, it doesn’t really help for actually introspecting an instance or class.

By “introspecting”, I mean “printing out the damned class name”, for example.

Full monty on the click through.

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 5 Comments »

Objective-C: Atomic, properties, threading and/or custom setter/getter

Sunday, January 13th, 2008

Jim Correia asked on cocoa-dev:

Can you offer any advice for the times when I must write my own accessor, but want it to have similar behaviors to the autogenerated accessor? In particular, I’m thinking about the default atomic behavior. Is this simply wrapping the work in a @synchronized(self) block? Something else?

First, some context. Jim is referring to Objective-C 2.0’s properties and, more specifically, to the behavior of methods automatically synthesized by the compiler.

By default, synthesized accessors are atomic. Atomic does not mean thread safe.

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 3 Comments »

Objective-C: Using Instruments to trace messages-to-nil

Friday, January 4th, 2008
In Action_tn.png

In Leopard, Apple added a new tool to the developer tool suite called Instruments. It is a timeline based debugging and performance analysis tool. With a full suite of tools to analyze all kinds of dimensions of performance, correlating all data onto a timeline that can be inspected and navigated at will. And it remembers prior runs so you can compare results before and after changes.

And it fully consumes dtrace.

Clicking through the screenshot at right will show a run of Instruments with a single analysis instrument active.

I created a custom instrument that encapsulates the dtrace script I discussed in Objective-C: Using dtrace to trace messages-to-nil.

I ran the resulting instrument against TextEdit.

What can’t be conveyed by that screenshot is exactly how “live” the data can be examined. You can click and drag on the timeline to select a subset of the run, details of the selected instrument are shown in the table in bottom right, and any given row can be selected to show the backtrace active at the time the sample was taken.

I didn’t want to clutter up the screenshot with lots of data and, thus, didn’t demonstrate the awesomeness that is being able to relate data across multiple instruments, correlated by time.

That’ll be in the next Mac OS X / Software related post.

Click on through for an explanation with screenshots as to exactly how to convert the dtrace script to an instrument.

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 4 Comments »

Objective-C: Using dtrace to trace messages-to-nil

Thursday, January 3rd, 2008

Update: There are [at least] two different Objective-C messagers. objc_msgSend() and objc_msgSend_stret(). All of what is written here applies to both however objc_msgSend_stret() is the problematic one when it comes to messages to nil; it is the one used to return structures and, thus, the one that does not necessarily guarantee a zero return.


In my last post on the subject, Adrian Milliner posted a short dtrace script that would log the backtrace for all invocations of objc_msgSend where the first paramater — the target — was nil.

The script is as follows:

pid$1::objc_msgSend:entry
/arg0==0/
{
  ustack();
}

Once saved to a file (in this case objc-nil-trace.d, the trace can be applied to any running Cocoa process via:

sudo dtrace -s objc-nil-trace.d <pid>

The <pid> argument should, obviously, be the process ID of the process to be traced.

It works well enough and is certainly faster than a conditional breakpoint in GDB (likely, orders of magnitude faster), but it is far from a complete solution.

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 6 Comments »

Objective-C: Logging Messages to Nil

Wednesday, January 2nd, 2008

This post will make this post useful.

Jim Correia had asked the lazytwitter how to log all method invocations against nil objects.

Some background:

Objective-C is a “nil eats messages” language. That is, if you invoke a method on an object reference that is nil, the runtime will eat the method. It is mostly invisible, only causing outright failures for certain return types that are larger than a pointer — structs, floats, doubles, etc… — at which point, the return value is undefined. Actually, the return value behavior for message to nil isn’t quite that simple.

Regardless of whether you think “nil eats messages” is the correct behavior, that it exists means that you’ll invariably be caught in a situation where a receiver is surprisingly nil. When this happens, it can be a pain to debug for a number of reasons.

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 7 Comments »

Objective-C: One Hack to Log Methods

Tuesday, January 1st, 2008

Jim Correia recently tweeted a query asking if it were possible to log all attempts to message through nil in Objective-C.

It is, but not through public API. But that isn’t the subject of this particular post (it’ll be the next post).

In the process of writing up a general solution to Jim’s query, it reminded me of a new feature in Objective-C 2.0 that can be a tremendously handy debugging and exploration hack.

Classes in Objective-C 2.0 can dynamically resolve methods by implementing one or both of the following methods:

+ (BOOL)resolveClassMethod:(SEL)sel;
+ (BOOL)resolveInstanceMethod:(SEL)sel;

The intended purpose of said methods is to allow code to provide an implemention of a method on demand. If the resolver methods can provide an implementation, it uses the function…

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types);

… to add an implementation of the requested method to the class. If not, they return NO and the normal forward-or-fail mechanisms kick in.

You can easily create a proxy class that will log a method whenever it is invoked. The simplest form is as follows:

@interface LogEmAsTheyAreInvoked
@end

@implementation LogEmAsTheyAreInvoked
+ (BOOL)resolveInstanceMethod:(SEL)name
{
    NSLog(@"Instance resolving %@", NSStringFromSelector(name));
    return NO;
}

+ (BOOL)resolveClassMethod:(SEL)name
{
    NSLog(@"Class resolving %@", NSStringFromSelector(name));
    return NO;
}
@end

Of course, this is pretty much useless. Without an implementation of…

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;

… the runtime will rapidly decide that all viability is defunct, thus aborting your process. Even the most basic of services, such as allocation, cannot be handled directly. If you need to allocate an instance of the above, use something like:

class_createInstance(objc_getClass("LogEmAsTheyAreInvoked"), 0)

If you want to be particularly tricky, you could supply the proxied class’s instance size instead of the zero and then plug in the method implementations from the original class into the proxied class as each method is invoked. Behavior that tests the class hierarchy may be surprising.

On the slightly less tricky front, you could also create functionality like NSProxy and have the instance of LogEmAsTheyAreInvoked wrap — proxy — some instance of some class. Then it is just a matter of forwarding the method invocations, as desired (which could also directly be used for logging but lacks the nice & automatic “only invoked once” nature of the above methods).

In any case, a neat hack. One of many possible hacks in this vein and not the most general purpose of ‘em. Potentially useful for debugging. Your mileage may vary.

Posted in Mac OS X, Objective-C, Software | 1 Comment »

PyObjC Xcode Templates now in Subversion

Monday, December 3rd, 2007

Ronald and I realized that the Xcode templates that are included in Leopard didn’t make it into the PyObjC repository.

So, I moved ‘em over.

You can find them in the PyObjC subversion repository.

These aren’t just templates, though. See the README.txt.

As it implies, the project-tool.py script is used to effectively convert between Xcode templates and buildable projects. That is, you can create a project that builds/runs like a regular Xcode project, and then easily turn it into a template with the invocation of a simple command line like this:

project-tool.py -k -v --template Cocoa-Python\ Document-based\ Application/CocoaDocApp.xcodeproj/TemplateInfo.plist \
    Cocoa-Python\ Document-based\ Application/ \
    ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/AA\ Testing/Cocoa-Python\ Document-based\ Application

Or, specifically:

./project-tool --help
Usage: project-tool.py [options]  

    Copies tree of templates or projects from  to .
    Before copying, it cleans up  by removing various bits of garbage.
    After copying, it transforms  by replacing strings with their Xcode
    template counterparts.

    The reverse flag can be used to reverse this process; turning an Xcode
    template into a working project.

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -v, --verbose         verbose
  -k, --kill-dest       erase  (no warning)
  -r, --reverse         reverse transformation (template -> editable project)
  -w, --working         try to make destination into a working project
  -n, --nib             rewrite NIB files to 10.5 text-only format
  -t TEMPLATEFILE, --template=TEMPLATEFILE
                        path to TemplateInfo.plist that should be used during
                        conversion

Nothing remotely Python specific about it. And, as some have noticed, the PyObjC Cocoa templates leverage the new Interface Builder file format (xib) such that all kinds of substitutions happen in the templated interfaces, too. As in: No more MyDocument ever again.

Posted in Mac OS X, Objective-C, PyObjC, Software, Xcode | No Comments »

Cocoa Based Smoked Pork

Friday, November 30th, 2007
23 Hour Smoked Boston Butt

As many have tweeted and ‘blogged, there was a bit of Cocoa gathering at Apple this week.

It was mighty cool to hang with so many of the folks that consume our products.

Personally, I was ecstatic to see that so many were embracing garbage collection and finding great success therein.

It is really gratifying to see people run with the tools that we [all of Dev Tech] have pushed out. Damn, you folks are creative!

Anyway, a twitservation (twitter-conversation) — more a half-assed argument– with Wil Shipley combined with the awesomeness of the kitchen led me to cook up 33 lbs of smoked pork for lunch today.

I did it just a bit different this time. Namely, I cooked it slightly longer — 23.5 hours, now that I have looked at the actual wall time — and slightly cooler.

23 Hour Smoked Apples in Pork Fat

I have moved to using a probe thermometer stuck through the gap between halves of the Big Green Egg to monitor temperature. As well, I’m using a large plate setter that coincidentally raises the cooking grid to 1/4″ below the opening of the BGE.

As a result, whatever temperature I set the Stoker too, it will absolutely be the cooking temperature at the interface between fire and food. As a result, this particular pork was cooked at a lower temperature than I have done in the past in that the gradient between cooking grid and top of dome ran as about a 30 degree downward slope (cooking grid @ 230, dome at 200). Previously, the grid probe was typically 1.5″ above grid and, thus, grid temp was probably a good 20 to 30 degrees higher than I intended.

The end result was that the fat and connective tissue was fully rendered, but the cuts of meat still had a slice to them! You could cut it with a fork easily enough, but it still required cutting.

Personally, I found it to be a more pleasing and versatile product than straight up pulled pork.

As an experiment, I halved some apples and placed the halves in a pan under the pork as it cooked with the open face up. No clue what was going to happen.

The end result was a bowl made of apple skin filled with apple stew where the water had been replaced by rendered pork fat.

Universally accepted as delicious. Next time, I’ll make quite a few more and bake them into a pie with little bits of pork fat strewn throughout.

Posted in Food, Mac OS X, Objective-C | 2 Comments »

SWT Moving to Cocoa

Friday, November 30th, 2007

A few weeks ago, the SWT folks came out to Apple to learn a bit about Cocoa & Leopard.

Very early in the week, they discovered the BridgeSupport generated XML metadata that describes almost all Objective-C and C APIs on the system (and is included with user installations of Leopard).

Using that, they whipped up a tool that automatically generates a set of Java classes that mirror the Objective-C classes, with all the necessary glue bits underneath.

End result? SWT apps are now Cocoa apps. The goal is to host Eclipse on top of this version of SWT and they already have some success on that front.

Very cool.

All of this work has been pushed back into the SWT repository and they have now put out a call for help from the community.

Posted in Cocoa, Mac OS X, Objective-C, Software | 1 Comment »