audk/EmulatorPkg/Win/Host
Nate DeSimone cbfae3e8a9 EmulatorPkg: Fix Source Level Debug on Windows
The Visual Studio Windows debugger will only load symbols for PE/COFF images
that Windows is aware of. Therefore, to enable source level debugging, all
PEI/DXE modules must be loaded via LoadLibrary() or LoadLibraryEx() and the
the instance in memory created by LoadLibrary() must be the one that is
actually executed.

The current source level debug implementation in EmulatorPkg for Windows is
inherited from the old Nt32Pkg. This implementation makes the assumption that
all PEI/DXE modules have a DLL export tables with a symbol named
InitializeDriver. Therefore, this source level debug implementation requires
all modules to be linked in a non-PI spec defined manner. Support for adding
the InitializeDriver symbol was removed in EmulatorPkg, which broke source
level debugging.

To fix this, the source level debugging implementation has been modified to
use the PE/COFF entry point directly. This brings the implementation into
compliance with the PI spec and should work with any PEIM/DXE driver.
Implementing this requires parsing the in-memory instance of the PE/COFF image
created by Windows to find the entrypoint and since PEIMs/DXE drivers are not
garunteed to have 4KB aligned sections, it also requires explicit configuration
of the page table using VirtualProtect().

With this fix, the debugging experience is now so good it is unprecedented!
In Visual Studio Code, add the following to launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "EmulatorPkg Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/<path_to_build>/Build/EmulatorX64/DEBUG_<tool_chain>/X64/WinHost",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}/<path_to_build>/Build/EmulatorX64/DEBUG_<tool_chain>/X64/",
      "environment": [],
      "console": false,
    }
  ]
}

Make modifications to the above template as nessesary and build EmulatorPkg.
Now, just add breakpoints directly in Visual Studio Code the way you would with
any other software project. When you start the debugger, it will halt at the
breakpoint automatically without any extra configuration required.

Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Andrew Fish <afish@apple.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Chasel Chiu <chasel.chiu@intel.com>
Signed-off-by: Nate DeSimone <nathaniel.l.desimone@intel.com>
2025-01-22 00:27:46 +00:00
..
WinBlockIo.c EmulatorPkg: spurious failure in WriteBlocks on X64 2024-12-17 19:01:48 +00:00
WinFileSystem.c EmulatorPkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
WinGop.h EmulatorPkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
WinGopInput.c EmulatorPkg: Add Signature to Graphics Structure 2024-10-08 00:31:58 +00:00
WinGopScreen.c EmulatorPkg: Add Signature to Graphics Structure 2024-10-08 00:31:58 +00:00
WinHost.c EmulatorPkg: Fix Source Level Debug on Windows 2025-01-22 00:27:46 +00:00
WinHost.h EmulatorPkg/WinHost: Add Reset2 PPI 2022-12-05 05:35:41 +00:00
WinHost.inf EmulatorPkg: VS2022 Support on WinHost. 2024-08-13 13:04:46 +08:00
WinInclude.h EmulatorPkg/Win/Host: Fix RUNTIME_FUNCTION redefinition error 2023-07-24 03:57:52 +00:00
WinMemoryAllocationLib.c EmulatorPkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
WinPacketFilter.c EmulatorPkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
WinThunk.c EmulatorPkg WinThunk: Use Win32 API to get Performance Frequency and Count 2024-11-14 02:15:43 +00:00