Platform SDK: Win32 API |
Debugging flat thunks is difficult not only because the mechanism itself is complex, but also because the necessary debugging tools are difficult to master. The flat-thunk mechanism is complex because it must transform function calls in 32-bit compiled code into calls compatible with 16-bit code and vice versa. Because 32-bit code uses data types and CPU register sets that are different than those used with 16-bit code, the flat-thunk mechanism must translate function parameters, switch stacks, and translate return values. The flat-thunk mechanism is optimized for speed, yet must allow preemptive 32-bit code to call nonpreemptive 16-bit code. The thunk compiler makes creating flat thunks much easier than manually creating them, but it is not foolproof.
To debug flat thunks, you must use a system-level debugger, rather than the application-level debugger you would generally use. Application-level debuggers such as the Microsoft Visual C++ debugger and WinDBG cannot trace through thunks because they consist of both 32-bit and 16-bit code and cause the system to claim or release the Win16Mutex. To trace through a thunk, you need to use a system-level debugger such as WDEB386.EXE. Using WDEB386.EXE is more complicated than using an application-level debugger, because you must know Intel x86 assembly language, know how Intel x86 microprocessors work, and remember many debugger commands.