Archive for the ‘Programming’ Category
Essentials 2008
Continuing from last year’s edition, here’s the software that I use on a day-to-day basis. Because of my traitorous switch to Mac, this list looks quite a bit different than it did earlier. As before, this is cribbing from Mark Pilgrim’s series - his 2008 edition is also full of good recommendations.
- Mac OS X 10.5 - after using a Mac as my primary machine for over a year now, I’m on the fence as to whether I’d ever go back to Linux or not. On one hand, the UI design and software support is great, but there’s just enough proprietary bullshit to make me reconsider. For the time being though, it’s the best there is, because my other favorite OS Ubuntu and Linux in general seems to be on a steady decline to crap city. If anyone takes offense to this assertion, I’ll be glad to write up a detailed response as to why it’s a mess.
- F-Spot, delicious, Vim - still continue to kick ass. Because of the wedding, I ended up effectively doubling my picture library, mostly with U’s 2MB pictures from her SLR camera - F-Spot handles it like a champ.
- Git and GitHub - Git is so mind-blowingly useful to anyone who is a developer, power-user, or anyone who works with text files, that I can’t possibly leave it off this list. This program continues to help me out just about every time I program; at work, I use it to manage multiple in-flight hotfixes to certain train-wreck components who shall remain nameless (but not linkless), as well as whenever I have to do any large change to Windows source code. At home, GitHub is a great way to manage my developer tools as well as my personal projects.
Seriously, if you’re any sort of programmer whatsoever, learn Git.
- Quicksilver - it’s hard to describe what QS actually is, the term “app launcher” betrays its real utility, but it’s by-far one of the best reasons to use Mac OS X. Basically, it’s a GUI version of a fast command-line interface, one that learns which commands you use most often and shortens the number of keystrokes you need to use them. Taking some time to learn everything that QS can do pays off quite a bit for your productivity.
- Firefox - continues to be the browser of choice, with its fantastic plugins (the “It’s All Text!” plugin being one of my favorite, lets me use GVim to type Emails or this blog entry for example). Great developer tools like Web Developer Toolbar and Firebug make it way better than Safari for most things. Speed and platform-integration are two of the things I do miss though…
- Live Mesh - file synchronization that just works. Works great with both Windows and Mac, and its remote desktop feature while somewhat anemic, is beautifully simple to use. If you don’t have a backup solution (and if you don’t, you will lose your stuff - storing everything on a USB stick does not count), this is a fantastic way to do it with almost zero work
- VMware Fusion - solid virtualization software for Mac, great integration with the guest without the evil hacks that Parallels uses (trust me as a Windows developer when I say this, you are much safer with VMware than with Parallels). These days however, I prefer more to use a dedicated VM running on another machine that I can remote desktop into rather than a local VM.
- Cygwin - without this, work would be way more painful. A Windows machine without Cygwin is nearly worthless to me.
- iTunes - …and I f’ing hate it. Please, someone write a music player for OS X that doesn’t epically suck. Amarok 2.0 and Songbird don’t count - Amarok went from the best music player on the planet without question, to a giant pile of gray crap. Trolltech systematically destroyed every decent KDE piece of software by releasing Qt4 and causing everyone to decide to make massive rewrites of their software, but I digress.
Anyways, some of the few things that iTunes got very, very right are, how it remembers exactly where I am in a podcast and syncs it with the iPhone, its podcast support in general, and its great sync support with devices (letting me choose whether to auto/manual sync music, making automatic backups of my phone, etc). The iTunes store would also be a gigantic win if it wasn’t so DRM-encumbered, and I would spend way more money there, instead of at Amazon MP3, which is also a fantastic service.
- XBMC - I’ve been using this on the XBox for years, and now that it’s a 100% cross-platform app, it’s even more awesome. Playing back video on your TV with this is fantastic, I stream movies and TV from my desktop machine over wireless and it works near-flawlessly, and understands just about any format. Setting up a cheap box with XBMC on it is the best way to get your music and movies onto your TV, hands-down. There’s also versions for hacked AppleTVs, which turns an AppleTV to me, from “complete trash” to “very compelling”. If the AppleTV had decent audio/video outputs, it would’ve been my new media box.
Stuff I don’t use anymore
- Tomboy - only runs on Linux until recently, and is local-only. The original developer also annoys me by coming up with fantastic ideas then abandoning them (I could also point that right towards myself, but anyways…)
- sshfs - I still use this occasionally when I have to traverse firewalls, but for everything local, Samba is faster and a bit less of a pain on OS X
- Unison - since I do much less work on my desktop than I used to, having two-way sync isn’t as useful to me; I just rsync from laptop -> desktop.
- Ubuntu - too many things broken on MacBook Pros, most the fault of Apple’s strange hardware, but it doesn’t change the fact that it’s broken.
How 64-bit Windows works - cause no one seems to get it
64-bit is the future
Despite everyone dragging their heels as much as possible, eventually developers and users will start moving soon to 64-bit; in WinSE, we predict that this is going to accelerate quite a bit because computer manufacturers are starting to sell machines with more than 4GB of RAM, and customers start complaining when they pay for 4GB and only get 3ish or so
However, even among experienced developers, I hear a lot of confusion about how 64-bit works, and how I can run 32-bit programs on 64-bit. Listening to the .NET Rocks episode on the subject shows that even fairly knowledgeable guys can be pretty confused about how all of this works. Hopefully, this blog post can set the record straight for some of you - if anything seems like it makes no sense, leave a comment or send me an Email and I’ll try to update the entry!
The one thing to remember
Whether you’re on Windows, Linux, or OS X, there’s one fundamental problem that underlies all of the complexity; if you remember this one fact, everything else in this article will make sense. Here it is:
You can not load a 32-bit library in a 64-bit process.
Repeat that out loud a few times, it’ll help. This is a fundamental aspect of 64-bit computing and 32-bit compatibility mode on the amd64 and IA64 architecture, it’s not specific to Windows. This rule shows up in a lot of places that you wouldn’t expect, like Explorer shell extensions, browser plugins like Flash, and multimedia codecs in Linux.
In an “all 64-bit” world, there are no problems - everything is simple and works pretty much just like 32-bit. However, one of the advantages of the amd64 architecture is that it allows 32-bit code to be run in compatibility mode, with almost no loss in performance. Being able to run both 64-bit and 32-bit apps in the same OS is huge for application compatibility, and AMD was very smart to make it possible (unlike Itanium, which pretty much threw x86 compatibility out the window, and felt the much-deserved consequences). However, to make this actually work is most-definitely not a trivial affair - there’s a lot of things to fix up to make everything as seamless as 32-bit.
How does Windows deal with it?
The core infrastructure in Windows NT to handle this is called WOW64 - this code manages emulating the 32-bit architecture (either by only setting the processor mode in amd64, or actually performing emulation in IA64). As a result of the rule I’ve described above, there are actually two copies of almost every API in Windows - one for native binaries and one for 32-bit binaries.
For every system DLL, there’s two versions!
When a 32-bit app gets launched on x64, all of the DLLs being used are from the \Windows\SysWOW64 directory; usually these follow an identical codepath to the 32-bit version of the DLL, though certain DLLs like user32.dll actually operate differently in WOW64 mode. There’s a lot of stuff behind the scenes to make this happen, such as Filesystem Redirection (almost all apps don’t need to know anything about WOW64!) However, there’s still one problem - for 100% user-mode DLLs, this solution works, but the kernel is 64-bit, and only 64-bit. We surely can’t load a separate 32-bit kernel running at the same time!
When it comes down to it, you need to get the data to the kernel in a different way, some sort of IPC mechanism - in Windows this involves the RPC/LPC mechanism, or more commonly, via a syscall. When a 32-bit app calls CreateFileW, it calls NtCreateFile, which pushes the appropriate parameters into registers, and invokes the SYSENTER instruction to switch back into 64-bit mode. This mechanism works because there is only the kernel to worry about transitioning to, and it can have special knowledge as to whether the calling process was 32-bit or 64-bit.
What about .NET?
.NET is an interesting case; when you install the 64-bit .NET framework, two versions of the CLR are installed as well as the GAC (knowing that there are two GAC’s can avoid a major Gotcha!). However, most .NET binaries are MSIL - since the binary is in a processor-agnostic instruction set, which one does it choose?
The answer is, “usually 64-bit, except for when it doesn’t.” .NET assemblies can be marked as “only run on x86/x64″ via the compiler or via the Corflags tool,and there’s one very important reason to do so, if you use P/Invoke to a library that only has a x86 version. Unfortunately, the check for x86-only will only extend so far down the dependency chain, so if you have a library who uses a library who uses a native library, the CLR will end up throwing a BadImageFormatException.
Debugging using WinDbg
There’s one special trick to debugging an x86 binary on 64-bit with WinDbg, via this blog entry, that if you’re having trouble getting a call stack, you need to load the wow64exts debugger extension and use the “!sw” command to switch between 64-bit and 32-bit mode. VS is luckily smart enough to handle this for you (though I haven’t tried it recently!)
Great Programming Books: Fowler’s “Refactoring”
As a part of my goal of learning as much as possible about programming, I’ve set out to read all of the “classics” of Computer Science / Engineering books. And since everyone has their own definition of what a classic is, I’ll share my own $0.02 on the subject.
Of course, if I stuck to the traditional knowledge of the subject, I’d have the same list that everyone else has, so I’ll try to be a little bit more unique through the series and put a few books on there that might not be typical. These are books that improve your perspective on how to design and write software, as well as specific technology books that I find to be really well-written and useful, even if they don’t teach anything specifically about software design.
However, this first book isn’t atypical at all, but it’s something that changed how I approach programming, especially when working with others’ code. And the fact of the matter is, after you’re done learning how to program and start working professionally in the field, you’ll most likely be working frequently with other people’s code. Even if you’re a one-man shop, learning to refactor properly will make adding features to your code easier and cleaner, and helps you to write elegant, maintainable code.
One of the best things that Fowler discusses in the book is, not only what refactoring is or how to do it, but when to do it. his cheat sheet in the back of the book identifies what he calls “code smell” - common code mistakes that result in lousy code, and what to do about them.
If you take away only one thing from this book, it’s this: if you go to add a feature to existing code and it’s not trivially easy to add in, refactor the code until it is. Hacking in code all over the place to make something happen will work for awhile, but you’ll eventually turn the code into an unmaintainable mess.
And if you have room for two things, here’s the 2nd takeaway: unit tests are very important, they let you refactor the code without wondering if you broke anything. If you spend time debugging an issue, it helps you exactly one time. You could spend an equal amount of time writing a unit test that catches the issue, and it helps you over and over.
Running your iPhone SDK App on iPhone without the App Store
Writing your own iPhone apps is fun and all, but it’s completely useless to me if Apple’s going to make me pay $100 and wait for months to get it onto my phone - one of the flat-out stupidest things that mar an amazing product; well luckily, the hackers are stepping up and turning Apple’s cathedral into a bazaar by Jailbreaking out of Apple’s code-signing prison, and creating an awesome alternative to App Store called Cydia.
Using some elbow grease, you can write an app using Apple’s official SDK, test it in the simulator, then actually put it onto the phone to see how it works. If it works great, either host your own F/OSS iPhone repository, or pony up the cash and get it into the App Store.
Here’s the steps to get an SDK app onto your phone:
- Since Apple enforces codesigning on the iPhone, you need to Jailbreak your phone - otherwise we have to have a dev key.
- Build your app using the “2.0 Device - Release” profile (*not* Simulator)
- Using Cydia, install OpenSSH, and use the network preferences pane to figure out your IP address
- Open up a terminal, and get your app onto the phone:
cd /path/to/my/app/build/Release-iphoneos
tar -czpvf MyAppName.tar.gz MyAppName.app
scp MyAppName.tar.gz root@192.168.xxx.xxx:/Applications/ # Replace with your iPhone’s IP
ssh root@192.168.xxx.xxx
cd /Applications
tar -xzpvf MyAppName.tar.gz
rm MyAppName.tar.gz - We need to “fake sign” our binary, so keep typing in the terminal…
apt-get install ldid
cd MyAppName.app
ldid -S MyAppName
exit - Now, restart your iPhone - you should see the app in your Springboard
I make no guarantees about this method, but it at least worked with the AccelerometerGraph.app sample that Apple ships (and that ironically is completely useless unless it’s on an actual phone).
Update: Getting your apps into the Cydia list is as easy as going to this website and submitting a description / package - just like App Store, only way easier and Free’er!
Dear Lazyweb: best solution for forums?
In my push to make Yikes! a real open-source app, one thing that the project needs is a good forum. Either hosted, or I can put it on my own server, as long as it’s easy to use. I’d also prefer that it supports OpenID. Beast looks pretty good (PS: you do *not* want to Google for that), but hosting >1 Rails site on my Linode may cause it to explode. Anyone had good/bad experiences?
Yikes! for nerds: how to get the code
I ran out of time yesterday, but as promised, here’s how to build and run Yikes!
Quick Start
cd yikes
rake && rake ffmpeg
ruby lib/main.rb -l /path/to/videos -t /path/to/ipod/output -r 1800 -b
Getting the code
By far, the best way to get the code is via Git; this lets you view the entire commit history, as well as send me changes. If you don’t have Git, you can download precompiled source code trees for Linux or Mac OS X 10.5. The Git clone URL via Github is git://github.com/xpaulbettsx/yikes.git
Building (”Huh? Building? On Ruby?”)
(If you downloaded the precompiled version, skip this part!) Even though the application is in Ruby, we need to build ffmpeg and its associated libraries from source, so you need to have the XCode tools installed, and you probably need MacPorts as well. While building this takes forever, it’s fairly easy:
Running the app
Right now, you have to run Yikes! from the command line, but the syntax is pretty easy. Here’s a sample:
# The long version
ruby lib/main.rb –library /path/to/videos –target /path/to/ipod/output –rate 1800 –background
# or if you want the short version
ruby lib/main.rb -l /path/to/videos -t /path/to/ipod/output -r 1800 -b
# If you want to run it on the sample files for development, there’s an easier way
rake run
Yikes! It’s your videos on your iPod!
For awhile, I’ve been working on a project that is pretty cool, and I’m finally getting near the “first 90% done” software development mark; now I’ve got the 2nd 90% to get it to production-quality, and the 3rd 90% will make it actually good. Here’s the screenshot:

Yes, the UI is rough-draft, I’ve got to go to town on it in CSSEdit
What’s it do?
In its simple mode, Yikes! will take a directory and convert all the movies to iPod/iPhone format (H.264 MPEG-4’s, so compatible with most players), and it will skip files it’s already converted. This isn’t too far off from what you could do with Handbrake and some clever bash scripts.
However, you can also run the program in background mode, and this is where it gets really useful. You give the program a folder of videos, and a place to put the iPod videos, and it will start a web site that you can go to on another computer, where you can see the converted videos, download them, or (and here’s the clever part), add it to iTunes as a video podcast, which will copy all the videos to your iPod automagically.
Where’s the code?
Github! http://github.com/xpaulbettsx/yikes
Update: Changed URL from earlier, merged webif-ramaze into master
Later today once I’m back at home I’ll put up a “how to get/build the code”, as it’s a little tricky. I’m working on official releases for Mac and Linux, and a Windows port is in the future; while I haven’t been coding towards it, I also have made sure to not choose anything that’s completely impossible for Win32.
Thoughts? Ideas? Comments? Want to help?
Since I’m always busy with work, it’s taken me quite a while to get to this point, and I’m definitely open to accepting contributions and making this a real open-source project; so far, I’ve set up Github and a bug tracker (but no mailing list, forums, documentation, etc). If you’re not handy with coding, websites, or art/design, I would even just appreciate suggestions or ideas for cool features. My Email address is paul at paulbetts dot org, let me know!
Debugging .NET exceptions *without* VS installed using WinDbg
If your .NET app is crashing on a machine that doesn’t have Visual Studio, here’s the easy way to get the managed exception - people have written way better articles on SOS and WinDbg, but here’s a 10sec overview; even if you have no idea how WinDbg works, often just having the managed exception will give you enough good info, and WinDbg is way easier to install and lightweight than VS.
Install the Debugging Tool for Windows, then spin up WinDbg (or cdb, the command-line version if you’re even more metal). Either attach to a running instance of your program, or start a new one. Run these commands to get started once you get the prompt (if you don’t get the prompt, and you’ve attached, you need to break-in by hitting Ctrl-C):
.sympath+ C:\path\to\your\binary\directory
.reload -f
g
Ok, your app is running - one thing that will totally fool you if you’re used to using VS is, if you start the program with WinDbg, your program will break-in immediately once you start it, before any code in your app has run (even the .NET startup code). Now, crash your app; you should’ve been kicked back into the debugger now and back at the prompt, with something like:
"The given key was not present in the dictionary."
eax=001fe078 ebx=e0434f4d ecx=00000001 edx=00000000 esi=001fe100 edi=005461d8
eip=76c5f35f esp=001fe078 ebp=001fe0c8 iopl=0 nv up ei pl nz ac pe nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00000216
KERNEL32!RaiseException+0×58:
76c5f35f c9 leave
0:000>
Now, here’s where we get to the real work - to debug .NET framework apps, we need the SOS extension, so we’ll pull that in, then use one of the functions that SOS defines (to see them all, run !help):
0:000> !printexception
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll -
PDB symbol for mscorwks.dll not loaded
Exception object: 22e0580c
Exception type: System.Collections.Generic.KeyNotFoundException
Message: The given key was not present in the dictionary.
InnerException: [none]
StackTrace (generated):
SP IP Function
001FE1F4 6BFB480D mscorlib_ni!System.ThrowHelper.ThrowKeyNotFoundException()+0×1d
001FE1FC 06DF1E20 mscorlib_ni!System.Collections.Generic.Dictionary`2[[LogDebugger.EventGroupingKeys, LogDebugger.Engine],[System.__Canon, mscorlib]].get_Item(LogDebugger.EventGroupingKeys)+0×38
001FE208 06494B36 LogDebuggerUI!LogDebugger.Ui.FilterEnvironment.[buildhighlightbrushmap]b__e(LogDebugger.LDEvent)+0xe
[ [ rest of stack trace… ] ]
There’s way more you can do with WinDbg and SOS (it’s the way we debug Windows programs at Microsoft), but this should get anyone who wants a quick way to see an exception the way to do it. Using Mono, you can actually get the exception trace even easier (but you can’t do any debugging), as any app you run via the command line (i.e. via mono ./path/to/app.exe) will print the same information to STDERR.
My vimrc now on Github
Awhile ago, I posted my .vimrc, but now I’ve officially moved it to Github since it’s so much easier to find / read there, via its Github web page. So now, the new way to check it out is:
Some highlights:
- My generate_ctags script that (through the “rebuild_tags” wrapper) will go through an entire directory and generate appropriate tag databases
- git2cl, which takes your git commit log and writes out CHANGELOG
- My dotfiles (.vimrc, .zshrc, .zshrc_complete, .screenrc, .irbrc)
- A lot of cool Vim scripts I’ve collected over the years, like the Git commit editor and Tagexplorer
My ad-hoc Tivo
What I’ve been up to
Work is far too busy, and it doesn’t give me anything to talk about - my job all day is to put out fires in Windows Vista, and there’s definitely no shortage of work in that department (though we’re definitely making progress!)
My Yahoo! Pipe
However, since I haven’t got any juicy Microsoft secrets to spill, instead I will share my clever Yahoo! Pipes rig that works like TiVo only free’er. Here’s a screenshot of its PVC goodness, click it to be taken to the page:

Yahoo! Pipes + Awesome = Very Yes
But what does this do?
What does this actually do for us though? Well, the output of this pipe is an RSS feed of Bittorrent files - put these into your favorite RSS-enabled torrent program, and it will watch the output of this pipe, and add all of the torrents to be downloaded! My favorite one is KTorrent:

Auto-downloaded goodness
How it all works
Here’s what some of these boxes do - keep in mind, our goal is to end up with a list of torrent files to download of our favorite new shows, with no duplicates or files we already have. I didn’t really arrange the pipes diagram very well - the flow of data is from right to left, and ending up in the center. So, starting at the right, we see that we have a lot of Fetch Feed blocks. These blocks serve as our data source, and fetch the results of searches from tvrss.net. All of these go into a Union operator, which just mixes all of our sources into one list. So after we do that, we end up with data that looks like:
South Park 12×7 [DSRIP - 0TV]
South Park 12×6 [DSRIP - 0TV]
South Park 12×5 [DSRIP - REPACK - 0TV]
South Park 12×5 [DSRIP - 0TV]
South Park 12×4 [DSRIP - 0TV]
South Park 12×3 [DSRIP - 0TV]
South Park 12×2 [DSRIP - 0TV]
As you can see, we’ve got some problems: first, we don’t really want to treat the 0TV and the REPACK versions as separate, so let’s filter out the release group from the title with a Regex block; it’s like ’search and replace’ but with a vague ’search’ part. We replace ‘[anything]‘ with nothing - in code it looks like “\[.*?\]“.
After that, I only want certain seasons of shows, so we only permit items if they contain certain text, like “Scrubs 7x”. Once we’ve done that, we’re almost finished; our only problem is that we’ve got a lot of duplicate entries, and they’re in random order. To fix that, we’ll use the Unique and Sort blocks to put them in chronological order.
The results
The Colbert Report 2008-04-24
The Daily Show 2008-04-24
Scrubs 7×9
30 Rock 2×13
Top Chef 4×7
The Daily Show 2008-04-23
The Colbert Report 2008-04-23

