Debugging
Introduction
Debugging can be extremely useful when making your exefs mods. It will allow you to view the asm code as it's running, view register values at certain points, and add break points to step through your code.
For debugging, you'll need the help of Ghidra (I'm pretty sure you can set up IDA in a similar way), GDB (The GNU Project Debugger) and a GDB Stub (Yuzu or Atmosphere). This tutorial assumes you're using Ghidra and Windows with WSL.
How To
Set up your GDB Stub. Once you enable the GDB stub your games will hang at startup waiting for a connection.
In Yuzu
- Emulation -> Configure -> General -> Debug -> Debugger -> Enable GDB Stub
- Take note of the port number here (or change it)
In Atmosphere
- Ensure your version of Atmosphere is at least version 1.2.4 (System Settings → System → "Current version: ... | AMS X.Y.Z | ..." under the 'System Update' button)
- Modify sd:/atmosphere/config/system_settings.ini (or create it if it doesn't exist) and ensure the following values are set accordingly:
[atmosphere]
enable_htc = u8!0x0
enable_standalone_gdbstub = u8!0x1- Reboot your Switch
Set up GDB
- Set up WSL on your PC
- Enable SSH into your WSL VM
- Install
gdb-multiarch
to your WSL VM
Set up Ghidra
- Open the BDSP Main file with debugger (Right click -> Open With -> Debugger)
- Make sure the base address of main is
0x8004000
(this is where yuzu loads the file to)- Window -> Memory Map -> House Icon At Top Right ->
8004000
- This may be different for atmosphere
- Window -> Memory Map -> House Icon At Top Right ->
- Create a new Debugger Target
- In the Targets window click the green/yellow socket at the top right
- Choose
GNU gdb via SSH
and enter your details (I had to modify the following)- GDB launch command:
usr/bin/gdb-multiarch
- SSH username:
<WSL Username>
- GDB launch command:
- You may need to edit more or less depending on how you set up your SSH
- Click Connect and confirm any SSH details (password, keys etc.)
Connect and Debug
- Run BDSP in Yuzu (It will hang at launch when connecting to GDB)
- In the Ghidra Interpreter window enter
target extended-remote <YourWindowsIP>:<YuzuGDBPort>
- In the Ghidra Modules window click the two way red arrow (This links your main program with the program loaded in memory)
- Set any breakpoints you need in the code (Right click -> Toggle Breakpoint)
- In the Ghidra Objects window, continue execution and step through code at breakpoints
- In the Ghidra Registers window, view the value of any registers at the current execution location