I hack my Mac

Yes, I hack my Mac! Here is a little list of "hacks" and/or "tricks" I have discovered while using Mac OS X.

How to build fat binaries

Right now there are two architectures in Apple hardware: PowerPC and Intel. But, it is possible to compile a fat binary (also known as universal binary), which is an executable file that contains binary code for both PowerPC and Intel.
You first need to have installed the following packages from XCode: To compile a fat binary:
[pbn@ibook] /Users/pbn> gcc -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk hello.c
And you see the result is a fat binary:
[pbn@ibook] /Users/pbn> file a.out 
a.out: Mach-O fat file with 2 architectures
a.out (for architecture i386):  Mach-O executable i386
a.out (for architecture ppc):   Mach-O executable ppc

The heap command

As its name implies, heap will show you the heap of a running process. You can call it by giving it the PID of the process:
[pbn@ibook] /Users/pbn> heap 144
To find the PID of a process, type for instance:
[pbn@ibook] /Users/pbn> ps ux
or
[pbn@ibook] /Users/pbn> ps aux
You can also call the heap command by giving it the name of process. For instance, let's look at the heap of the well-known process, the Dock:
[pbn@ibook] /Users/pbn> heap Dock
Process 142: 1 zone
Zone DefaultMallocZone_0x300000: Overall size: 9352KB; 5303 nodes malloced for 554KB (5% of capacity); largest unused: [0x01839e00-7992KB]

Zone DefaultMallocZone_0x300000: 5303 nodes - Sizes: 24KB[1] 20KB[2] 16KB[1] 11KB[1] 8KB[2] 5KB[1] 5KB[1] 4KB[11] 3072[5] 2560[9] 2048[6] 1536[14] 1024[48] 512[11] 480[4] 464[2] 448[5] 432[4] 400[2] 384[1] 352[2] 336[6] 320[4] 304[2] 288[13] 272[17] 256[12] 240[196] 224[24] 208[14] 192[38] 176[21] 160[7] 144[46] 128[47] 112[58] 96[190] 80[221] 64[212] 48[1137] 32[837] 16[2068] 

Found 510 ObjC classes in process 142

-----------------------------------------------------------------------
Zone DefaultMallocZone_0x300000: 5303 nodes (566912 bytes) 

                                     CLASS_NAME     COUNT     BYTES       AVG
                                     ==========     =====     =====       ===
                                   <non-object>      2171    404016     186.1
                                     NSCFNumber      1213     19408      16.0
                                     NSCFString       721     27072      37.5
                                 NSCFDictionary       630     85824     136.2
                                       NSCFType       243     17296      71.2
                                      NSCFArray       162      4896      30.2
                                          NSURL        62      1984      32.0
                                        NSCFSet        35      1696      48.5
                                     NSMachPort        27      1728      64.0
                                       NSCFData        25      2544     101.8
                           NSCFAttributedString        10       160      16.0
                                      NSCFTimer         2       160      80.0
                           NSNotificationCenter         1        64      64.0
                                       NSThread         1        64      64.0

You'll notice all classes start with NS. NS means NextStep.
(As you may know, Mac OS X descends from NextStep)

The leaks command

The leaks will tell you about a process' leaked memory. Basically, it's memory that the application has allocated, but that has been lost and cannot be freed. You can call leaks with the pid or the name of a process.

How do you write NextStep ?

Yes, Mac OS X descends from NextStep.
They should write on the Mac OS X package: "May contain trace quantitites of NextStep".

But, how exactly do you write NextStep ?
Well, even the NextStep developers didn't agree about this... It can be written NextStep, NeXTstep, NeXTStep, NeXTSTEP, or NEXTSTEP. You saw in the above example that the heap command give us hints about NextStep. But, there are traces of NextStep elsewhere in Mac OS X. Let us look at the man pages for two commands, open and defaults:
If you type:

[pbn@ibook] /Users/pbn> man open
... you can see:
HISTORY
     First appeared in NextStep.

If you type:
[pbn@ibook] /Users/pbn> man defaults
... you can see:
HISTORY
     First appeared in NeXTStep.

... even between two different man pages in MacOSX, NextStep is not capitalized the same way !

Here's a little script I wrote:

MacOSX "tellmeafortune.pl" is a script written in Perl and AppleScript. It will pick a random fortune and tell it using Mac OS X's speech synthesizer. Click here to download it.

Did you know that Mac OS X is the work of Evil ?

You can also look at my section about Mac OS X Internals

Recommended books

Running Mac OS X Tiger
Jason Deraleau, James Duncan Davidson
O'Reilly

If you are ready to dig deep into your Mac, this book expertly guides you to the core of Mac OS X. It helps you understand the inner workings of the operating system so you can know how to get the most out of it. And it gives you countless ideas--and step-by-step instruction--for customizing and revving up Tiger to your specific needs and your liking.

Learning Unix for Mac OS X Tiger
Dave Taylor
O'Reilly

If you're a Mac OS X user and you don't know much about Unix then this book is for you. You will start using the Terminal.App in Mac OS X and slowly discover the power of Unix.

Mac OS X Tiger for Unix Geeks
Brian Jepson, Ernest Rothman
O'Reilly

Welcome to the next level ! If you've read and understood the previous book, you can now jump to this one. You'll learn about Unix on Mac OS X. This book is recommended for anyone switching from Unix, Linux, FreeBSD, NetBSD, OpenBSD or DragonFlyBSD to Mac OS X.

Valid HTML 4.0 Transitional Powered by Debian