GDB Setup
Disclaimer
This guide will only cover GDB will for the switch console itself! If you want to set up GDB using an emulator, you can follow this guide on Luminescent Platinum documentation.
Setup
Requirements
- Debian WSL (Windows)
- Custom .gdbinit
Setting up on the Switch side
- Make sure you have the latest Atmosphere set up and running!
- Edit
sd:/atmosphere/config/system_settings.ini(or create it if it's not there) and paste the following in (reboot the switch after you save the file):
[atmosphere]
enable_htc = u8!0x0
enable_standalone_gdbstub = u8!0x1
Setting up WSL (Windows Only)
- Download
Debianfrom the Microsoft Store - Once it's done, run the
Debianapplication - If you run into
The Windows Subsystem for Linux optional component is not enabled, then search forWindows Features, then scroll down and enableWindows Subsystem for Linux - Set a password and confirm
Setting up GDB
- Run
sudo apt-get -y update && sudo apt-get -y install gdb-multiarch && sudo apt-get -y install wget(this will update your repos, install gdb-multiarch, and install wget) - Run
wget https://raw.githubusercontent.com/Coolsonickirby/smash-ultimate-research-setup/main/gdbinit/.gdbinit -O ~/.gdbinit && wget https://raw.githubusercontent.com/Coolsonickirby/smash-ultimate-research-setup/main/gdbinit/attach.py -O ~/attach.py && wget https://raw.githubusercontent.com/Coolsonickirby/smash-ultimate-research-setup/main/gdbinit/.gdbinit.switch -O ~/.gdbinit.switch && wget https://raw.githubusercontent.com/Coolsonickirby/smash-ultimate-research-setup/main/gdbinit/print_addr_setup.py -O ~/print_addr_setup.py(this will download and set up the custom .gdbinit, .gdbinit.switch, attach.py, and print_addr_setup.py files for convenience) - Run
nano ~/.gdbinit.switchand scroll down until you get to thetarget extended-remoteline - Edit the IP in the file to be your switch's IP (you can find the switch's IP by going to
System Settings -> Internet -> IP Address) - Hit
Ctrl + X -> Y -> Enter - You're done, GDB is now set up with auto-attach + a handful of useful functions!
Using GDB
- Open up
Debianif you're on Windows - Run
gdb-multiarch - Run
Super Smash Bros. Ultimateon the switch once it says to "Launch your game" - Let it auto-attach and set $main automatically
- Set up any breakpoints, no_ops, stubs, etc...
- Type
cand hit enter
GDB Functions
The .gdbinit file we set up earlier has these handful of functions that will be pretty useful in your reversing endeavors!
my_bt-> Prints the backtrace as absolute addresses. Often misses the first address on the backtrace but you can just p/x $lr for that.my_bt2-> Prints the backtrace with offsets relative to the base of main.no_op <offset>-> Takes an offset into main and NOPs the instruction at that addressstub <offset>-> Takes an offset into main and stubs the function at that addressreplace <offset> <new_instruction>-> Replaces the instruction at an offset with the new instructionget_pc-> Gets the PC as an offset relative to the base of mainbreak_at <offset>-> Sets a breakpoint at an offset relative to the base of mainlocalize <register/address>-> Converts the value in the register (or the passed address) to an offset relative to the base of mainxxd <address> <size>-> Print a xxd dump of the addressprint_trace-> Runsget_pc,localize $lr, &my_bt2to get the current offset, the calling offset, and the backtrace in one command instead of three