Archive for the 'Technology' Category

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 »

Silly Hack of the Day: Mount the Obj-C Runtime as a Filesystem

Thursday, January 10th, 2008

Update: CocoaHacks was quite a bit of fun. Not only did I demonstrate the rather silly little hack, but I ran it fully garbage collected. With one simple change (that should be in the next release), the MacFUSE framework works just fine with Garbage Collection enabled!

The best compliment came from Amit Singh; something along the lines of “That is absolutely the strangest filesystem I have seen.”

In any case, my little hack repository has been updated to be GC only. retain/release/autorelease/dealloc are dead to me (in this project, anyway).

If you want to build MacFUSE with GC enabled, simply set Objective-C Garbage Collection to “Supported” in Xcode in the MacFUSE framework project’s build settings.

Browsing Classes_tn.png

At left, is a screenshot of the Finder browsing the Objective-C classes active in the runtime of a little Cocoa app that I wrote this evening.

The little app is called RuntimeFS and it contains a simple bit of code that traipses through the Objective-C runtime and collects information about the Objective-C classes encountered. This information is then barfed up by the elegantly simple delegate like API required by MacFuse to create a filesystem.

Let me restate that: MacFuse kicks ass. The Objective-C API is trivially easy to use. Trivially easy. I implemented this little hack in less than two hours, not having looked at the MacFuse API before. Nor did I read the docs; just looked at this example and one header file. I have implemented filesystems in a couple of different languages. Filesystems are hard. Or was. Not anymore.

I dropped the source code in the “Silly” directory of my public SVN repository.

Because, really, this is quite a silly hack. And hack it is — it doesn’t crash, but that is about all the quality assurance analysis I have done.

Free as in “MIT License” free. Have fun. I’m accepting patches, of course.

Future Stuff

If I were to go anywhere with this, the first thing I would do would be to move the subclasses into a subdirectory and then add other subdirectories to contain additional data.

Specifically, I would add directories like -1- Instance Variables, -2- Class Methods, -3- Instance Methods, -4- Subclasses, -5- Documentation (or something), etc…

The naming convention serves two purposes. First, it sorts nice like. Secondly, the names are invalid as class names and, thus, it makes handling the metadata vs. class directories trivially easy while also eliminating potential namespace conflicts.

Posted in Hacks, Humor, Mac OS X, Software, Technology | 13 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 »

HD DVD: End of Week 1

Sunday, December 30th, 2007

It has now been nearly a week since we added an HD DVD player to the home entertainment system.

Some impressions.

We watched Serenity this evening. It is visually stunning. I’m sure the audio is pretty amazing, too, but I don’t currently have the 5.1 pre-amp / speakers hooked up.

By “visually stunning”, I mean: It looks better than it did in the theater. As an added bonus, I make better popcorn, have vastly superior beverages for far less money, and can watch a visually stunning movie while sitting in front of a fire.

No wonder the theaters are running scared. Hell — we paid $19 for Serenity on HD DVD which, accounting for the evening’s expense, is about 1/3rd to 1/5th the cost of actually going to a theater (depending on babysitting expenses).

Anyway — HD DVD really delivers in terms of the visuals when paired with a decent TV; 46″ 1080p Sony LCD, in my case.

The Planet Earth really drives it home. I have watched it on DVD, via Satellite, and on HD DVD. At 1080p, The Planet Earth is an awesome — a moving — tour of the awesome breadth of life on this planet.

As well, we watched the remastered HD DVD version of Blazing Saddles. The difference between it and the DVD is quite noticeable, but mostly in that HD DVD so clearly displays the noise and imperfections found in the original production process.

And, of course, if the discs do so, the extras on HD DVD can be considerably richer and more deeply integrated with the primary content than regular DVDs. Speaking of regular DVDs, the player does an awesome job of upscaling legacy content (though, honestly, I have no idea how it compares to the various $30 to $70 upscaling DVD players that are commonplace these days).

And that is pretty much where the happiness ends. Click on through for a bit of a rant on the vasty stupidity that is next generation media….
Read the rest of this entry »

Posted in Entertainment, Rants, Technology | 19 Comments »

Peggle.

Thursday, December 27th, 2007

Apparently, Peggle is the top selling game at PopCap games.

I gave the demo a try — I dig puzzle games.

As far as I can tell, it is basicaly a really well presented simulation of pachinko with about an order of magnitude more control over how the ball is put in play.

Completing any of the levels beyond the initial set basically boils down to luck.

The brilliant bit is that the game is implemented such that luck and skill are nearly indistinguishable. Failure begets success by simply repeating non-stupid shots and the luck of the board layout. On any given shot, skill lasts about two bounces or so and then luck takes over.

It is really quite the brilliant bit of balance that anyone who pays attention to either user experience or industrial design (in the HCI sense) should pay attention to.

Regardless of the delicious implementation and even at 50% off, I still can’t see this game being worth $10 ($20 normally).

Of course, I don’t find slot machines at all entertaining either.

In the comments, Robert added a link to the Zero Punctuation review of Peggle. Spot on and brilliant, as well.

Posted in Entertainment, Software | 4 Comments »

HD DVD

Saturday, December 22nd, 2007

After watching the Blu-Ray vs. HD-DVD competition for a bit, I decided it was time to pull the trigger and add an HD disc player to the home entertainment system.

I chose the Toshiba HD-A30 1080p HD DVD player as it was relatively cheap at $232, in stock (the A35 was not), it can do 1080p, and the reviews are solid. Coincidentally, like my TV, the player came with a copy of the GPL license and is running Linux (can’t find the download link, though).

Overall, HD DVD is a cheaper, simpler, format and the spec is actually complete. Blu-ray seems to be mired in incompleteness and disagreements amongst the consortium behind it. And the players are more expensive ($100 price different between entry level HD DVD and BluRay players).

At this point, it seems unlikely that Blu-Ray will “win”. Even if it does, there is enough critical mass of content on HD DVD, that I won’t run out of content any time soon. As well, I’m certain that the lifespan of this generation of high-def optical discs is limited, to be replaced in the next five years by a shift to online delivery of high definition content. Discs are inconvenient and fragile.

I also picked up The Planet Earth [HD DVD] along with the player (Sorry, Ann).

It is just flat out stunning. It is like watching a nature documentary composed of individual frames shot by a 2 megapixel digital camera. Just gorgeous.

As well, regular DVDs look amazing. The player does an excellent job of upsampling.

Setup was relatively easy in comparison to other similar devices, but catasptrophically stupid in the grand scheme of things. It is inexcusable that a digital device talking over a digital interface (HDMI) to another digital device while being plugged into a wired LAN with DHCP requires any manual setup at all.

The player still hasn’t quite figured out how to talk to the internet. But, unlike Blu-Ray (from what the review’s indicate), the player came out of the box running the full HD-DVD spec and, thus, has no problem playing the limited number of discs I dropped into it.

We also grabbed a copy of Serenity [HD DVD] (impulse buy at Target while obtaining an HDMI cable). Should be amazing. And today, I stumbled upon The Big Lebowski [HD DVD]. SCORE!

I also upgraded our netflix account to automatically deliver HD DVD discs, when available. This, of course, led to a browsing of the HD DVD selection and I was surprised by the quantity of remastered classics available on HD DVD; Blazing Saddles, Trading Places, Animal House, Caddyshack, Being John Malkovich, The Breakfast Club, Army of Darkness, Dune, Happy Gilmore, etc…

(Yes, my notion of “classic” is a bit different than many.)

I’m also looking forward to watching Transformers, What Dreams May Come, and a Scanner Darkly on HD DVD. All three should be flat out visually stunning.

Posted in Entertainment, Technology | 9 Comments »