-
Notifications
You must be signed in to change notification settings - Fork 124
Advanced debugging with dnSpy
How to debug PB (programmable block), mod and even game exceptions.
dnSpy has a debugger that can attach to the game process and catch exceptions (even if they're already caught by the code) and pause the code so you can go through it step by step, look at variables, etc.
dnSpy is also a decompiler which you can look at the game's code and even at compiled PB/mod scripts in order to see how the game's compiler alters them. For this in particular see Looking at the compiled code.
-
Get it from https://github.com/0xd4d/dnSpy/releases (the .NET Framework one, not NET Core!)
-
Extract anywhere (e.g.
C:\Program Files\dnSpy\
). -
Run
dnSpy.exe
for the 64bit version.
The basic steps for attaching dnSpy to the game and to monitor for an exception.
-
Start the game and dnSpy.
-
dnSpy:
Debug -> Attach to process (Ctrl+Alt+P)
and double click SpaceEngineers.exe from that list.If you cannot see SE there and you have the NET framework dnSpy and ran the 64bit exe, then it might require running dnSpy as admin.
-
dnSpy:
Debug -> Windows -> Exception settings (Ctrl+Alt+E)
and pick what exceptions you want to catch.For example: search
null
and checkNullReferenceException
, the most common exception.If you're looking for the cause of "Script too complex", add
VRage.Compiler.ScriptOutOfRangeException
(with the green plus button). -
In-game: Recompile the script (if it's a mod, reload the world to recompile).
(This is only needed once after you attach. Details at Seeing local variables when code is paused)
-
In-game: Run the script until it throws errors (exceptions).
-
Once the error occurs dnSpy will pause code execution, now proceed to Navigating paused code.
When dnSpy is attached to the game and you're monitoring for an exception or breakpoint (advanced topic: Breakpoints), then the game's code is paused and self-navigates to the exact line the exception or breakpoint occured.
Some things you can do here (in dnSpy):
-
The top-center toolbar has some buttons with arrows and stuff: step-into, step-over or step-out to navigate the code flow.
-
Debug -> Windows -> Locals (Alt+4)
can show values from variables and fields from the current pause context.(Advanced topic: Seeing local variables when code is paused)
-
Debug -> Windows -> Call Stack (Ctrl+Alt+C)
shows the exception stacktrace which you can double-click in to navigate to the mentioned code. -
Use the
Continue
button to resume the game (or exit the game if it's an unhandled exception, dnSpy only shows them to you, it doesn't prevent them).
Since dnSpy is also a decompiler, you can look at the game code.
This doesn't require the game running.
-
File -> Open
navigate to<Steam>/steamapps/common/SpaceEngineers/Bin64
-
Select all .dll and .exe files and click
Open
. -
Edit -> Search Assemblies (Ctrl+Shift+K)
and search away!
After that:
-
What to search really depends on what you're looking for, if you want to know what properties an API interface has, then simply type that interface's name, maybe also set
Search for:
toType
to be more specific. -
Right click anything -> Analyze
to see where it's used, implemented, raised, etc. -
For multiple projects (e.g. also having Medieval Engineers' libraries), see
File -> Open List
A big thank you goes to Digi for writing this tutorial.
Do you have questions, comments, suggestions for improvements? Is there something I can do better? Did I make a mistake? Please add an issue here, and prefix your issue title with Wiki. Thank you, your help will be very appreciated!