TweetFollow Us on Twitter

Debugging Tips
Volume Number:10
Issue Number:5
Column Tag:PowerPC Series

Related Info: Memory Manager Dialog Manager

Debugging Tips for Mixed Mode

So that’s what I’m looking at when I crash!

By Dave Falkenburg, Apple Computer, Inc.

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

About the author

For having such short legs, Dave runs well - he’s part of the team which just shipped Power Macintosh in record time. As keeper of the secrets of the Process Manager, he’s something of a nexus for the myriad parts of the Macintosh which all seem to come together in that cauldron of code, a veritable witch’s brew of bat wings and furless pets. Among other accomplishments, he’s the guy who gave Ron Avitzur his first PowerPC 601 book.

When writing for Power Macs...

You’ll need several tools. The obvious ones are a Power Macintosh and a development environment. Also handy, and not so obvious, are some bits and pieces. develop Issue #17 contains the dis dcmd. ETO #13 and recent developer CDs have MacsBug 6.5d6 (or later), and it has support for the new Power Macintosh Memory Manager.

When developing native applications for the Power Macintosh family, it’s pretty likely that you will stumble upon bugs which get introduced due to misuse of Mixed Mode Universal Procedure Pointers (UPPs).

Both the calling conventions and instruction set of any function are encoded within a data structure when that function has been “wrapped” by a UPP - two essential details which Mixed Mode needs in order to make your applications and plug-in modules work without knowing the sordid details of the emulator.

In the 68K Macintosh world, parameters to functions generally go on the stack or in registers, or sometimes both. PowerPC code generally puts all parameters in registers. On 68K, Pascal and C also have different calling conventions- each language passes parameters in the reverse order of it’s peer, and Pascal also insists on the callee cleaning up parameters. PowerPC has a single calling convention, which Mixed Mode will happily deal with.

Another important point is that PowerPC ProcPtrs aren’t pointers to code, but in fact, point at a data structure called a transition vector. This essentially makes them “ProcHandles”. We can take advantage of that when we diagnose crashes.

Another essential thing we rely on is the fact that the PowerPC and 680x0 instruction sets aren’t very similar - if you are looking at some sort of code in MacsBug, il <address> or dis <address> will probably tell you what kind of code you are looking at. For example, if we look at 680x0 code with dis, we will see something like the following.

dis 00142028
 00142028
 00142028 **undefined**   | 4e754e56
 0014202c **undefined**   | fffc48e7
 00142030 twi TO_LT|TO_GT,23622098,0x2a2e    | 0f082a2e
 00142034 **undefined**   | 000c286e
 00142038 **undefined**   | 0010382e
 0014203c **undefined**   | 0014200d
 00142040 subfic  r16,r21,0x2a78   | 22152a78
 00142044 **undefined**   | 0b7c2a81
 

The same code as viewed with il looks more familiar:

il 0014202A
 +15DA  0014202A LINKA6,#$FFFC| 4E56 FFFC
 +15DE  0014202E MOVEM.L  D4-D7/A4,-(A7)     | 48E7 0F08
 +15E2  00142032 MOVE.L   $000C(A6),D5 | 2A2E 000C
 +15E6  00142036 MOVEA.L  $0010(A6),A4 | 286E 0010
 +15EA  0014203A MOVE.W   $0014(A6),D4 | 382E 0014
 +15EE  0014203E MOVE.L   A5,D0    | 200D

If you aren’t already familiar with the Mixed Mode Manager or the PowerPC Runtime Architecture, you’ll might want to read more about it in Inside Macintosh: RISC System Software before reading on.

UPP Basics

Emulated code can always call UPPs “blindly”, that is, without needing to use CallUniversalProc.

This was a necessary design principle for the emulator, because older applications, and large portions of the Macintosh Toolbox have no idea they are running on a PowerPC 601 microprocessor most of the time. JSR-ing through a UPP essentially executes a trap instruction which transfers control to Mixed Mode. Mixed Mode converts parameters, calls the PowerPC subroutine, and the returns to the emulated caller after translating the result code.

This may seem like magic, but is actually more mundane than mysterious. A UPP data structure starts with a trap (0xAAFE, a.k.a. _MixedModeMagic). When you’re executing 68K code, and it JSRs through a UPP, the trap gets handled by Mixed Mode. Mixed Mode checks the rest of the data structure to figure out whether it’s 68K code, PowerPC code, or both. Mixed Mode then transfers control to the destination code, moving parameters from one mode to the other. So it might move 68K registers and/or stack arguments into PowerPC registers, or vice versa. What gets moved depends on the contents of the procInfo, a data structure which describes where parameters live, and where they should go.

PowerPC code must always use CallUniversalProc to call code which was passed as a ProcPtr of unknown type. All code which doesn’t look like a UPP is assumed to be 68K code. This allows a PowerPC-based Dialog Manager to automatically call a ModalFilterProc regardless of whether or not the application is 68K code or PowerPC code.

Mixed Mode relies on the procInfo field in order to convert parameters and function result.

The biggest opportunity for making a mistake is in the creation of procInfo values. Much of the work has been done for you by Apple engineers in the Universal Headers. They defined constants and CallXXXProc macros for most of the Toolbox and OS routines which accept a ProcPtr, and it’s far more trouble-free to use them than to create your own.

UPP Symptoms

We’ll discuss the problems you can stumble across by breaking down the three major cases that Mixed Mode must deal with:
68K -> PowerPC, PowerPC -> 68K, and PowerPC -> PowerPC.

In almost all cases, a bad procInfo value is usually fatal, and very difficult to debug. Your program’s stack can be completely munged beyond all repair. It’s far easier to check your procInfos up front than to debug a bad one at run time! Always use the uppXXX, NewXXXProc, and CallXXXProc macros in the Universal Headers to keep you from screwing this up.

Bad procInfo values usually show up as either a crash with the PC pointing two bytes into the RoutineDescriptor data structure, or as mysterious crashes which have unexplained side effects. Remember those Macintosh 128K crashes when you accidentally slammed into the sound/disk pages? You won’t have any easier of a time figuring out how you got there, and you don’t even get to see the technicolor snowcrash or hear the machine-gun sounds.

Here are a variety of situations where something can go wrong. I’ll treat each one separately.

Calling from 68K to PowerPC

Problem: Calling a PowerPC ProcPtr that hasn’t been wrapped in a UPP.

You will see a 68K illegal instruction, and the PC will be somewhere in code where every third longword is a zero.

What’s happening is that Mixed Mode never got the chance to switch modes for you, so you’re still trying to execute this code with the 68K emulator. That’s eventually going to generate an error, because the PowerPC instruction set is so different from the 68K instruction set. You can tell if this is what happened by doing a dis PC^ in MacsBug. You’ll need the dis dcmd that came with develop Issue #17, or future developer CDs. If you see something like “mflr r0” then this is probably what happened.

Problem: Calling a UPP with a bad procInfo

Check your procInfos, because bad ones will almost always get you. I’ve told you this already, and Imay repeat it a time or two. Check out Inside Macintosh: RISC System Software, or <MixedMode.h> for information about creating procInfo values.

Calling from PowerPC to 68K

Problem: Calling 68K code as PowerPC code (i.e. you forgot to use CallUniversalProc)

This will manifest itself as a “spurious interrupt” or “bus error” in older versions of MacsBug. This usually turns up as a random jump into space. Remember that PowerPC ProcPtrs are really ProcHandles and the caller will actually dereference your “LINK #-xx,A6” and try to use it as a pointer.

Take a look at your PC. Are you executing in the 0x4E560000 to 0x4E56FFFF range? If you are, odds are good that you did a jump into what turns out to have been a LINK instruction which was incorrectly treated as an address. LINK’s opcode is 0x4E56.

Problem: Calling a UPP with a bad procInfo

Check your procInfos. An incorrectly-formed procInfo will get you. It is OK to call 68K code directly with CallUniversalProc. That’s because anything that doesn’t begin with the MixedMode trap instruction is assumed to be 68K code, and that particular instruction was carefully chosen to not be a likely value to hit by accident.

Calling from PowerPC to PowerPC

I bet you didn’t think of this case. It arises when a PowerPC-based manager must support both emulated callers, and PowerPC callers.

Problem: Calling a UPP without CallUniversalProc from PowerPC Code

All routine descriptors start with 0xAAFE, which is not only a trap, but also an illegal instruction for the PowerPC microprocessor.

This will manifest itself as either a PowerPC Illegal Instruction (a.k.a. spurious interrupt, for you MacsBug fans) or bus error depending on what is going on inside Mixed Mode at the time. This is similar to the Calling 68K code as PowerPC code above. If you get an illegal instruction, you can try doing a dm PC-2 and see if you find an 0xAAFE hanging around.

Problem: Calling a PowerPC procPtr as if it were a UPP

This case is pretty easy to spot - you will die in MacsBug with a 68K illegal instruction error. If you do a dis PC (using those wonderful PowerPC dcmds), you can see that the emulator is running, but for some reason the PC is pointing at “mflr r0” and something like “lwz r3,10(r1)”, two things a 680x0 emulator doesn’t know how to do!

Problem: Calling a routine descriptor with a bad procInfo

Check your procInfos. Got the idea?

More tips and tricks

When developing native applications for Power Macintosh, try to do the following things to avoid getting into the trouble described above.

• Source level debugging and single stepping are your friends - become familiar with one of the PowerPC-capable debuggers: CodeWarrior, Macintosh Debugger for PowerPC, or Jasik’s Debugger. Keep your eyes open for others, as well.

• Try to understand everywhere you use ProcPtrs in your code. This is where all those seemingly useless naming conventions we all use really pay off, like naming call function pointers XXXProcPtr, or all global variables gXXX.

• Use the macros provided by in the Universal Headers to avoid botching procInfos. You can also make your code “universal” by using these macros. That means that you can compile for 68K or PowerPC without any further modification of your source code.

Remember, you don’t need to use UPPs internally within your application if the code type is always known - UPPs are only needed when code can be called from either side of the mixed mode boundary. Supporting both emulated and PowerPC plug in modules is a good example of when to use a UPP.

Happy debugging!

For more information on items mentioned above:

CodeWarrior - a development environment from Metrowerks for both 68K and PowerPC, and it runs native on either platform. Available from the Mail Order Store. See page 89 for details, or call 310/575-4343.

Macintosh Debugger for PowerPC - a two-machine debugger from Apple. Requires one Power Macintosh (to debug) and any other Macintosh to do the driving.

Jasik’s Debugger - a two-headed debugger from Steve Jasik, famous for his personal support. Available from the Mail Order Store. See page 89 for details, or call 310/575-4343. Jasik Designs may be reached at 415/322-1386.

dis dcmd - develop Issue #17, available from Apple.

Debugging On PowerPC, by Dave Falkenburg and Brian Topping - develop Issue #17, available from Apple.

MacsBug 6.5d6 - ETO #13 and recent developer CDs.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Fantastical 3.8.2 - Create calendar even...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
iShowU Instant 1.4.14 - Full-featured sc...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more
Geekbench 6.2.0 - Measure processor and...
Geekbench provides a comprehensive set of benchmarks engineered to quickly and accurately measure processor and memory performance. Designed to make benchmarks easy to run and easy to understand,... Read more
Quicken 7.2.3 - Complete personal financ...
Quicken makes managing your money easier than ever. Whether paying bills, upgrading from Windows, enjoying more reliable downloads, or getting expert product help, Quicken's new and improved features... Read more
EtreCheckPro 6.8.2 - For troubleshooting...
EtreCheck is an app that displays the important details of your system configuration and allow you to copy that information to the Clipboard. It is meant to be used with Apple Support Communities to... Read more
iMazing 2.17.7 - Complete iOS device man...
iMazing is the world’s favourite iOS device manager for Mac and PC. Millions of users every year leverage its powerful capabilities to make the most of their personal or business iPhone and iPad.... Read more

Latest Forum Discussions

See All

‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »
Motorsport legends NASCAR announce an up...
NASCAR often gets a bad reputation outside of America, but there is a certain charm to it with its close side-by-side action and its focus on pure speed, but it never managed to really massively break out internationally. Now, there's a chance... | Read more »
Skullgirls Mobile Version 6.0 Update Rel...
I’ve been covering Marie’s upcoming release from Hidden Variable in Skullgirls Mobile (Free) for a while now across the announcement, gameplay | Read more »
Amanita Design Is Hosting a 20th Anniver...
Amanita Design is celebrating its 20th anniversary (wow I’m old!) with a massive discount across its catalogue on iOS, Android, and Steam for two weeks. The announcement mentions up to 85% off on the games, and it looks like the mobile games that... | Read more »
SwitchArcade Round-Up: ‘Operation Wolf R...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 21st, 2023. I got back from the Tokyo Game Show at 8 PM, got to the office here at 9:30 PM, and it is presently 11:30 PM. I’ve done what I can today, and I hope you enjoy... | Read more »
Massive “Dark Rebirth” Update Launches f...
It’s been a couple of months since we last checked in on Diablo Immortal and in that time the game has been doing what it’s been doing since its release in June of last year: Bringing out new seasons with new content and features. | Read more »
‘Samba De Amigo Party-To-Go’ Apple Arcad...
SEGA recently released Samba de Amigo: Party-To-Go () on Apple Arcade and Samba de Amigo: Party Central on Nintendo Switch worldwide as the first new entries in the series in ages. | Read more »
The “Clan of the Eagle” DLC Now Availabl...
Following the last paid DLC and free updates for the game, Playdigious just released a new DLC pack for Northgard ($5.99) on mobile. Today’s new DLC is the “Clan of the Eagle" pack that is available on both iOS and Android for $2.99. | Read more »
Let fly the birds of war as a new Clan d...
Name the most Norse bird you can think of, then give it a twist because Playdigious is introducing not the Raven clan, mostly because they already exist, but the Clan of the Eagle in Northgard’s latest DLC. If you find gathering resources a... | Read more »
Out Now: ‘Ghost Detective’, ‘Thunder Ray...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »

Price Scanner via MacPrices.net

Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more
14″ M1 Pro MacBook Pros still available at Ap...
Apple continues to stock Certified Refurbished standard-configuration 14″ MacBook Pros with M1 Pro CPUs for as much as $570 off original MSRP, with models available starting at $1539. Each model... Read more

Jobs Board

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
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
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
Retail Key Holder- *Apple* Blossom Mall - Ba...
Retail Key Holder- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 03YM1 Job Area: Store: Sales and Support 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.