TweetFollow Us on Twitter

Feb 97 Factory Floor

Volume Number: 13 (1997)
Issue Number: 2
Column Tag: From The Factory Floor

Mike Lockwood, Debugger

By Dave Mark, ©1996 by Metrowerks, Inc., all rights reserved.

One of the most important, yet least understood part of the Mac development process is the debugger. Sure, everyone knows how to use the debugger to step through your source code, but I've always wanted a peek beneath the hood, a chance to see how a debugger really works. This month's interview is with Mike Lockwood, debugger extraordinaire.

Dave: You worked at Apple four years before you came to Metrowerks. What did you do there?

Mike: I first started working on development tools on my own, before I joined Apple. I wrote my own MacApp debugger as a side project, and when I got it working I sold it to Apple. Apple contracted me to finish it, and it shipped on E.T.O. as SourceBug. Soon after that, I joined Apple as an employee and worked for a year on the debugger for a new integrated development environment that was to replace MPW.

After that project was canceled, I transferred to the System Software group and worked on the Finder team. At the time there was no source level debugger that could debug Finder extensions, so I kept working on the debugger from my previous project - so I could use it myself. I added support for debugging threads and shared libraries, and it was soon being used by a number of different groups within Apple. Since it was an underground, guerrilla programming effort, I decided to call it VoodooMonkey. Then when no one was looking, someone snuck it on one of the Developer CDs and VoodooMonkey shipped outside of the company. Since then, it became somewhat of a cult classic.

After working on the Finder team for awhile, I became interested in working on developer tools again and wanted to move back to the East coast. So I transferred to the Cambridge office to work on the Dylan project. There, I wrote the application framework for Apple Dylan, and served as the main guinea pig for the Dylan compiler and development environment. After that project got canceled, there was an opportunity for me at Metrowerks, and I joined them the day after I left Apple.

Dave: How did you hook up with Metrowerks?

Mike: Metrowerks first contacted me in the Fall of 1993. They needed a 68K debugger nub for CodeWarrior, Apple only had a PowerPC version of DebugServices up and running. Greg Galanos called me to see if he could use DebuggerINIT, which was the debugger nub I wrote for VoodooMonkey. At the time, I didn't know much at all about Metrowerks, other than they were working on a C/C++ compiler for PowerPC. I wanted to help them out, but I also wanted to avoid some of the political problems I ran into after VoodooMonkey shipped. So, I directed Greg to go through the proper channels in Cupertino.

I knew Greg would never be able to cut through all of the red tape, and I wasn't sure I would ever hear from him again. However, he called me two days later and said "Hey, I just licensed the sources to DebuggerINIT from Apple." Now I knew I was dealing with a force to be reckoned with.

I continued to talk to Greg and other people at Metrowerks, and provided some informal tech support for DebuggerINIT. When it was clear that the Dylan project was going to be shut down, I talked more seriously with Metrowerks about joining the company. Dan Podwall, the original author of the Metrowerks debugger, was busy working on the browser and they needed someone to take over the debugger.

Dave: What exactly is a debugger nub?

Mike: A debugger nub is a small program that controls a program that is being debugged, and is responsible for things like setting breakpoints and single stepping through code. It provides a layer between the main debugger application and the low level system stuff. The distinction is important in the case of two machine debugging, because the debugger nub runs on the machine being debugged and the debugger application runs on a different machine. MetroNub is our debugger nub for single machine MacOS and Java debugging. We have other debugger nubs for debugging code running on other platforms.

Dave: How does MetroNub interface with the Metrowerks debugger?

Mike: MetroNub is a system extension that provides an API to the Metrowerks debugger that supports debugging functionality like single stepping. MetroNub exports a structure full of mixed mode function pointers via a Gestalt selector that the debugger application can call. MetroNub also interfaces with various parts of the OS. For example, it patches the Resource Manager to track where code resources are loaded in memory, and uses an undocumented Process Manager API to suspend and resume a process that is being debugged. It also patches the 68K exception vectors and registers callback functions with the PowerPC Exception Manager and the MP kernel so it can be notified when exceptions occur.

Now let's examine what happens when you single step through a line of code. At this point, the program is already stopped, meaning that MetroNub had already told the Process Manager to suspend the program's process and make it ineligible for scheduling. The debugger application then calls the MetroNub "Step" command. When it makes this call, it would specify to MetroNub whether to step into or step over function calls. It would also specify a range of addresses for the program counter so MetroNub will not stop execution again until the PC is outside of this range. When stepping though source code, this range would correspond to a single line in the source code.

MetroNub would record the PC range specified, and would then tell the Process Manager to resume the process. The process is then eligible to be scheduled again, but will not actually resume until the debugger application calls WaitNextEvent(). After it is rescheduled, some of MetroNub's code that was left on the call chain of the program being debugged will resume execution. Before MetroNub returns control to the program being debugged it restores all the registers and sets the trace bit in the processor's status register. The trace bit tells the processor to cause a trace exception after each instruction is executed. MetroNub receives these trace exceptions, and processes them until the program counter falls out of the specified range. Then MetroNub tells the Process Manager to do an immediate context switch from the program being debugged to the debugger application. This leaves some of MetroNub's exception handling code on the call chain of the program being debugged, which will resume after the program being debugged is rescheduled by the Process Manager.

Dave: What language is MetroNub written in?

Mike: MetroNub is written almost entirely in C++, with just a small amount of 68K assembler. The assembler is used only in a few places where it is necessary to access registers directly or execute some uncommon 68K instructions. For example, the 68K exception vector patches and some of the Resource Manager patches are written in assembler. MetroNub is mostly 68K code, and only contains a small amount of PowerPC glue code for some of the callbacks it registers with the OS.

Dave: But how does the link between the source code MetroNub happen?

Mike: MetroNub doesn't know anything about the source code. The debugger application handles the mapping from source code to object code offsets, based on information in the sym file, which is generated by the compiler and linker. Suppose you want to set a breakpoint at a certain line of source code. The sym file contains a table that maps the offset in the source file to an offset in the object code for the function containing that line of code. A different table in the sym file is then used to translate the offset in the function's object code to an offset into a CFM code fragment or a code resource. The debugger then tells MetroNub to set a breakpoint at the given offset within the code fragment or code resource.

The debugger application does not have to know where the code is loaded in memory or if it is even in memory at the time the user sets the breakpoint. If it is loaded, the debugger will convert the offset to an absolute address and insert a trap instruction at the address where the breakpoint should be set. If the code is not loaded yet, MetroNub remembers the breakpoint and installs the trap instruction after the code is loaded but before the program is able to execute the code.

Dave: What changes did you have to make to your design to deal with Java?

Mike: Fortunately, no major design changes were needed to support Java. That was good news for us, because time-to-market was important. Our debugger could already debug two different processor architectures (68K and PowerPC) simultaneously, so adding a third architecture was not difficult. We just treated Java byte codes as if they were assembler instructions and the Java VM as if it was a microprocessor, and the debugger architecture we already had in place worked fine. I had to write a new implementation of the symbolics code so it could read symbolic information from Java class files and Zip files instead of a sym file. I also had to add an API to the Metrowerks Java VM so MetroNub could communicate with the Java VM and control the execution of Java code. This approach had the nice side effect that it is possible to debug both 68K and PowerPC C/C++ code and Java code simultaneously with a single debugger.

Dave: A lot of companies have said that they will no longer support the 68K. Do you think learning to debug 68K code is a worthwhile investment?

Mike: 68K is becoming less important, but a good understanding of 68K code is valuable when debugging on System 7, even if you are writing PowerPC native code. Since much of the operating system is still emulated 68K code, it is not uncommon to have to debug 68K code on a PowerMac.

Dave: What's your feeling on how people should approach debugging? Should they learn MacsBug? Should they buy a copy of Jasik's Debugger? Learn PowerPC assembler? Got any good book recommendations? Are there utilities people should definitely have on their hard drive?

Mike: The Metrowerks debugger is strictly a high level debugger. You also need to have a low level debugger like MacsBug or Jasik's debugger installed when debugging with the Metrowerks debugger. Jasik's debugger is actually both a low level and high level debugger, so if you like Jasik's debugger, you don't even need a copy of the Metrowerks debugger! Jasik's debugger is very powerful and has a lot of features that no other debugger has. But many people find the Metrowerks debugger easier to use.

Nowadays, it isn't critical that you learn PowerPC assembler, unless you need to write very low level or very optimized code. But for debugging purposes, it is often useful to have basic reading knowledge of assembler, even if you do not write it yourself (I have written a lot of 68K assembler over the years, but haven't actually had a need to write any PowerPC assembler yet). But understanding how to read and debug at the assembler level can be very useful at times.

There are a number of utilities available that can help you with debugging. You can use the Debugging Memory Manager from Apple to help catch memory problems in your application heap. Similarly, you can use the DebugNew library that comes with CodeWarrior to track down memory problems in the built-in C/C++ memory allocater. Zone Ranger, which also comes with CodeWarrior, can be helpful monitoring memory usage and watching for memory leaks. There are a number of extensions, like EvenBetterBusError, DoubleDispose, etc. that are available from Apple that watch out for common programming errors when using the Memory Manager or Resource Manager. Onyx Technology has a tool called QC that monitors all Memory Manager calls and checks for these programming errors. A demo version of QC is available on the CodeWarrior Reference CD.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.