TweetFollow Us on Twitter

Debugging PPC
Volume Number:10
Issue Number:8
Column Tag:Powering Up

Debugging

A slight detour into the subterranean areas of the PowerMac

By Richard Clark, General Magic & Jordan Mattson, Apple

Since January, we have been demonstrating that programming a Power Macintosh is very similar to programming a 68K-based Macintosh. This month we’ll show you that debugging on a Power Macintosh is quite similar to debugging on a 68K Macintosh, but with a few changes due to Mixed Mode and the new runtime architecture. (Astute readers might notice that we promised to talk about Human Interface programming this month. We’ve had to take a slight detour away from this plan, due to technical problems with our Human Interface demo code and a new job, but we’ll be back on track next month.)

We’ll begin by examining the available tools, then review the most common new problems a Power Macintosh programmer faces.

Choose Your Tool

We’ll begin with some good news - debugging an emulated application on the Power Macintosh is the same as debugging that application on a 68K Macintosh. You can use MacsBug, SADE, Jasik’s “The Debugger,” TMON, the THINK C debugger, and anything else you use normally. In fact, MacsBug just thinks it’s running on a 68020.

However, since you’re reading this column you probably want to debug a PowerPC application on your Power Macintosh. If you like using source-code debuggers, you’ll need to use a different set of tools: either Apple’s “Macintosh Debugger for PowerPC”, or Metrowerks’ “MW Debug.” Three tools support assembly-level debugging on PowerPC: Apple’s “Macintosh Debugger”, Jasik’s “The Debugger”, and MacsBug.

Looking At The New Debuggers

The new Power Macintosh source-level debuggers come in two parts - the debugger application, and a low-level “nub” which does most of the work of handling breakpoints and reading/writing memory. (Apple supplies the nubs for both its own debugger and the Metrowerks debugger.) You could think of the nub as a version of Macsbug which depends on an application to provide the interface.

Like Macsbug, the nub used with Apple’s “Macintosh Debugger” installs itself at the lowest level of the operating system and completely takes over the machine when active. (The nub actually disables interrupts while doing its work.) Since the machine is completely frozen when the nub has control, Apple’s debugger requires two machines: a Power Macintosh with the nub and the program being tested, and a 68K Macintosh or Power Macintosh running the debugging application. These two machines are connected by a serial cable which carries commands and data to the nub, and data back from the nub.

The two-machine mode provides some real advantages. If something happens to the machine running the debugging application, you can restart it and continue as if nothing happened. Also, using two machines for debugging keeps the user interface that you’re testing separate from the interface on the test tools. (Anybody’s who’s tried to track down redrawing problems with a single-machine source level debugger can appreciate that!)

Metrowerks’ debugger uses a different version of Apple’s nub which doesn’t take over the machine completely and which doesn’t use the serial port directly. This version of the nub communicates with the debugging application through the Program-to-Program Communication toolbox, which allows the nub and the application to be on the same machine or connected via the Macintosh’s built-in networking.

This nub has both advantages and disadvantages. The first disadvantage: since the nub is built on the PPC toolbox, much of the system has to be functioning correctly for the nub to communicate with the debugging application, so a crash could also kill your debugging tools. Disadvantage 2: the Process Manager gets involved when you send a message via the PPC toolbox, which limits the top speed of the connection. The final disadvantage is this: since this nub isn’t installed automatically as an extension, so you have remember to start it running before you run the debugger application, or place it in your startup folder. In this nub’s favor, you can debug with one machine, which saves money, desk space, and keeps you from trying to remember which mouse does which!

Both of these debuggers work with .SYM/.xSYM files - known generically as “symbol files” - which are generated by the PowerPC linker. (The only difference between a .SYM file and an .xSYM file is the name - Apple’s engineers adopted the convention of ending PowerPC symbol files with .xSYM so that developers who build “fat binaries” could have symbol files for both the PowerPC and 68K code at the same time.) Symbol files match the start of each line of source code to the corresponding location in the compiled program. Symbol files also list each of the variables in the application, and where each variable is located. Using this information, debuggers can determine which line of source the current program counter points to, let you set breakpoints at the beginning of a current line (and single-step through the source code), and examine and modify variables.

Without a symbol file, the debuggers can only show you machine-language instructions, and let you modify locations in memory directly.

Teaching An Old Debugger New Tricks

The other two PowerPC debuggers - MacsBug and Steve Jasik’s “The Debugger” are 68K debuggers with some additional commands for the Power Macintosh.

MacsBug is still a 68K debugger at its heart: it is completely emulated and all of the register displays, breakpoints, stack crawls, and code displays operate as if running on a 68020. The largest single change to MacsBug is that it now knows about the “Modern” Memory Manager introduced with the Power Macintosh. The new memory manager uses different algorithms and different structures internally than the “classic” memory manager; this causes older versions of MacsBug to claim that the heap is corrupted when using the new memory manager. (MacsBug 6.5 contains the changes to work with the new memory manager.)

If MacsBug is still a 68K debugger, how can you debug PowerPC code with it? Apple has released an unsupported set of debugger extensions (known as dcmds, pronounced dee-commands) for PowerPC debugging. These commands include a disassembler, register display, stack crawl, and breakpoint setter for PowerPC code. (Setting breakpoints requires that you have one of the debugging nubs installed, so the breakpoint will be handled in the source-level debugger.)

The new MacsBug and the dcmds were shipped with the March, 1994 issue of develop magazine (issue 17), on the CD-ROM which accompanied the magazine. The dcmds don’t appear to be available anywhere else. However, the new MacsBug is available on the Internet via anonymous ftp. Connect to ftp.apple.com, look in the /dts/mac/tools/macsbug directory, and download macsbug-6-5d6.hqx. (If there’s a later version in the directory, take that instead.)

On the other hand, Steve Jasik’s “The Debugger” has been completely revised to include PowerPC debugging support. It supports source-level and assembly-level debugging, including breakpoints, stack traces, and disassembling PowerPC code. The Debugger isn’t a complete solution for PowerPC debugging yet, since single-stepping through source code, A-Trap breaks, and the “step spy” command don’t work for PowerPC code. (They should be working by the time you read this column.)

One interesting feature of The Debugger is the way it annotates PowerPC disassemblies. Every line is labeled with the name of the instruction, which helps if you’re learning PowerPC assembly language.


/* 1 */
;#  void MainLoop()
;#  {
;#    EventRecordtheEvent;
;#  
                     .MainLoop
 0:mfsprr0,LR  ; Move From Special Purpose Reg
 4:stmw r28,-16(SP); Store Multiple Word
 8:stw  r0,8(SP) ; Store Word
 C:stwu SP,-96(SP) ; Store Word with Update
 10:    lar29,gDone(RTOC) ; Load Address
;#    while (!gDone) {
 14:    jplad_2  ; Branch
;#      WaitNextEvent(everyEvent, &theEvent, 1L, NULL);
;#  
;#      // The external code didn't handle the event, so we will
 18:  lad_1 li r3,-1 ; Load Immed
 1C:    lar4,theEvent(SP) ; Load Address
 20:    lir5,1 ; Load Immed
 24:    lir6,0 ; Load Immed
 28:    bl.WaitNextEvent  ; Branch, set LR
 2C:    lwz RTOC,20(SP) ; Load Word and Zero

The Debugger is completely self-contained, i.e. it doesn’t require a separate nub, and requires a symbol file to enable its source-level debugging abilities.

Are There Any New Bugs On The Power Mac?

The first thing to remember when debugging a Power Macintosh program is that you’re still using a Macintosh. The same toolbox managers are present on the Power Macintosh, and much of the Toolbox and Operating System code is emulated. This means that most crashes into MacsBug (or another debugger) happen for the same reasons as on a 68K Macintosh: the program either passed a bad value to the toolbox, or corrupted a block in the heap, or grew the stack into the heap, and so on.

Still, the Power Macintosh can crash in some unique ways, due to the presence of Mixed Mode and the limited memory protection afforded by the new runtime architecture.

Using Mixed Mode incorrectly can cause crashes into 68K code. The most common problem in all newly ported PowerPC code comes from forgetting to pass a Universal Procedure Pointer:


/* 2 */
// Oops! We’re passing the address of a procedure directly
partCode = TrackControl(myControl, startPoint, myActionProc);

This causes a crash into the debugger with an “illegal instruction error” on the 68K side. You can diagnose this error in several different ways:

• Use the PowerPC disassembler (or the dis dcmd in MacsBug) to look at the address where the crash occurred. If you see reasonable PowerPC instructions, you probably forgot to pass a Universal Procedure Pointer.

• Think about what your code is doing. Did you click on a control, is a dialog being drawn, or is an Apple event being handled? These all involve callback procedures, so you may have forgotten a UPP.

• Set a break on the Mixed Mode A-trap. (This only works in MacsBug.) Using the ATVB (A-Trap Vector Break) dcmd, you can set a break on _MixedModeDispatch (0xAAFE). (The normal A-Trap Break command won’t work because the Power Macintosh doesn’t run all traps through the same trap dispatcher as the 68K Macintosh.) This lets you look at every mixed mode switch

The Power Macintosh’s limited memory protection can uncover bugs you didn’t know you had. If Virtual Memory is turned on, your code is loaded into a read-only area, so attempting to overwrite your code will result in an Access Violation. Even if VM is off, attempts to write to location 0 usually trigger access violations, as do all attempts to overwrite the ROM.

Data structure alignment presents some of the nastiest bugs. Remember that the PowerPC prefers to have all 2-byte values begin on even addresses, and 4-byte values on addresses which are multiples of 4. The compiler will help maintain this alignment in data structures by inserting “padding” in the appropriate places. Naturally, if you weren’t expecting this padding, your program might behave in unexpected ways.

Diagnosing this bug is easy: try re-compiling your code with the “use 68K alignment” option to your compiler, and see if the problem goes away. If it does, you probably forgot to install alignment directives on one or more of your structures.

Next Month In Powering Up

Next month, we’ll return with an “all-code” column exploring some Human Interface techniques you can apply on the Power Macintosh, and talk about a few nifty PowerPC hacks.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »

Price Scanner via MacPrices.net

Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more
May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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.