Capturing Stack Crawls in Application Emulation under Win32

I have always freely admitted that Win32 development is not my thing. What started as a religious conviction (UNIX being what it was when I was a lad…) and later became first a lack of opportunity and exposure and most recently a conscious choice to keep my skills marketable in niche markets, I’ve just done very little work on the platform. Sure, during the heyday of Windows CE (now Windows Mobile) I learned enough to talk with skilled Win32 developers transitioning to Windows CE, and successfully managed WinCE-based development projects, but that’s not engineering. More recently, I wrote a book on developing applications for eBay that included examples in C#, but that’s different. C# is managed code, and anyway, writing still isn’t really engineering.

Nonetheless, much of my career has been spent working within the confines of the Win32 platform. Many mobile platforms such as Qualcomm BREW stage their development environments on top of Win32, providing emulators in which the application you’re writing is linked in as a Win32 DLL. While this has its limitations—emulation isn’t simulation, etc—it’s definitely a good way to go for platform vendors, because the Win32 tool chain is well-understood by countless developers, even if the intricacies of a specific mobile platform isn’t.

At the day job, I recently came across a need to track down a number of memory leaks in a C-based application for Qualcomm BREW. The code had already been written, so it wasn’t feasible to go back and implement a wrapper to the memory management utilities that tracked allocation and free operations using the __FILE__ and __LINE__ macros; even if I could, I also needed to track down a few BREW component instance leaks, which would be considerably more complex to track using wrappers. Because of the need to determine component instance leaks and how the BREW simulator manages its own heap (providing leak reporting, at least), tools like Purify weren’t really an option either.

By hand-editing the include files that define BREW’s object creation and memory allocation routines (AEEShell.h and StdLib.h) I could directly hook the memory allocations, giving me an opportunity to determine the source of the leaks…if only I knew what portions of the code were performing the allocations. Using a dictionary matching pointers to memory or components and information about how those regions re allocated isn’t my idea; it’s hardly new, and frankly, I don’t know where it originated. It is a good idea, though, because it makes the work go a lot faster. By instrumenting allocations to track where in the code the allocation is being made (typically inserting that information in a dictionary keyed by the pointer to the region or component being allocated) and instrumenting the deallocation to release those records, an examination of the resulting dictionary after a run gives you a list of points in your code that may be allocating resources that aren’t freed later. The technique isn’t perfect, of course, especially if you pass references to memory or components around to other systems that you can’t instrument, but it’s a good start. I had a dictionary implementation that I could use to store information about each allocation referenced by pointer; if I could get stack frame data at the points where I hooked the allocator and deallocators, the work would become much easier.

I knew that I should be able to programmatically get stack trace information running under the simulator on Win32, although I had no idea how. Oddly, this doesn’t seem to be common knowledge, either, after pinging a few colleagues on the subject. However, after more reading of MSDN articles and googling than I care to admit, I came across Jochen Kalmbach’s StackWalker class, a C++ class that provides an utterly simple interface to obtain a stack crawl anywhere in your Win32 application. Great stuff, and I’m glad I found it! Using it was far simpler for me than trying to wrap my head (and some new code) around the debugger API that Microsoft provides.

I was quickly able to modify it to meet our needs to run within the Qualcomm BREW simulator and provide stack dumps to a log file; after those changes and a bit of truly shameless hacking on private copies of AEEStdlib.h and AEEShell.h, leak tracking became much easier. At some point I plan on providing a simple BREW component for simulator-only use that incorporates his code and provides much of the same functionality; it may make a good article or blog post in the future. In the mean time, I hope that simply describing the process here helps someone.

This experience provides a situation supporting my belief that a general interest in computing and computing platforms is key to being a good software engineer; if I’d thought too narrowly about the problem as being inherent to an application running on Qualcomm BREW, I might never have decided to add a bit of simulation-specific code to automate the bookkeeping behind my debugging.

Using Common Lisp on Mac OS X

I’ve been a Lisp nut for years. I was first exposed to Lisp on my TRS-80, when I was about thirteen; Randy Beer had written a Lisp introduction in the March ’83 issue of 80 Micro, and I remember reading it again and again. I’ve used various flavors of Lisp for exploratory prototyping programming over the years, and have enjoyed using both Scheme and Common Lisp.

Getting a dialect of Common Lisp running on Mac OS X isn’t hard — in fact, if you’re just setting out, an excellent choice is LispWorks Personal Edition. I’ve recently gotten to the point, however, where I’ve wanted to do some work with the Common Lisp Interface Manager, which isn’t supported in LispWorks Personal Edition under Mac OS X, and set out to configure a Common Lisp installation on my MacBook from the ground up.

It wasn’t difficult, although it took some Googling to get all the pieces together; shortly after I finished, someone on the Bay Area Functional Programmer‘s mailing list asked about running Common Lisp on Mac OS X, and I put together my notes on the topic and replied. The list is fairly small — less than 300 members — and I promised them and myself that I’d organize the notes and provide them in a more accessible location.

What follows is a step-by-step installation guide to installing Steel Bank Common Lisp (SBCL) and McCLIM on a PowerBook G4; I undertook this from the initial notes I took configuring the MacBook. I chose SBCL because it’s well-supported by the open source community and works well with McCLIM; I have nothing against other Common Lisp implementations, including Clozure. 

A word of warning is in order: I’m comfortable with Common Lisp, but relatively unfamiliar with how Common Lisp programmers handle package distribution. I get the basic idea behind ASDF, of course, but I’m not seasoned in its use. Consequently, I had a get-things-working-and-clean-up-later mentality when I undertook my first SBCL-McCLIM configuration, and this largely remained when I wrote what follows. Other Lisp developers may have better ways to organize their working environments, and I’d welcome positive comments (drop me an email, and we’ll figure out how to incorporate your feedback).

Starting out, you should have a relatively up-to-date Mac OS X box (I’ve done this on Mac OS X 10.4.x and 10.5.x systems). When you’re done, you will have installed Carbon Emacs, SLIME, SBCL, and McCLIM configured to work with the X Windows server Mac OS X provides, along with darcs. 

Continue reading Using Common Lisp on Mac OS X

The obligatory first post…

Installing and configuring WordPress has been on my personal to-do list for an absurdly long amount of time. Back in February 2002, I started a personal blog on LiveJournal, which I kept up-to-date on an intermittent basis. I’d always planned on using it both as a professional soapbox and a personal journal, although it quickly became more of the latter than the former. In recent years, it’s largely lain fallow, a victim of benign neglect as I tackle professional writing and engineering commitments.

With the imminent publication of my latest book, Beginning Javaâ„¢ ME Platform (Beginning from Novice to Professional), it’s again apparent that I should put more effort into reaching out to both those I know and those who read my work. Not only is this an important part of publicity for me as an author, but it’s also a service to you, my readers, because it gives me an opportunity to keep you up to date. I only hope that I am more successful in keeping you up to date than I am self-publicity, something I have generally neglected. 

Hence, this blog.

I’m not certain what all I will post here, or how frequently I will post. You can certainly expect any updates or feedback on my books, of course, but I’m also planning on using this as a working notebook for other technical musings. I don’t have any plans to become a career blogger and make this a central part of my professional activities; to be frank I think there’s too much of that on the Internet already! Instead, I think this is going to fill a very narrow nice, with me posting the occasional update to a previous (probably paper) publication, various tips and tricks I’ve picked up in my engineering career, and possibly some engineering notes about things I’ve found difficult in the hopes that capturing it here will help others.

Thanks for stopping by!