
Debugging normally assumes you have the source code, a matching build and symbol files. dnSpy removes those assumptions. It lets you set breakpoints on decompiled C#, inspect variables and step through a program you only have as a compiled assembly. This guide walks through a complete debugging session, from starting the target to catching an exception, and explains the settings that make the difference between a smooth session and breakpoints that never trigger.
Scope: debug software you own, maintain or are authorised to analyse. Debuggers can change program state and memory; use test data and a non-production environment wherever possible.
How dnSpy debugs without source
When dnSpy decompiles a method, it records which IL instructions each C# statement came from. A breakpoint you place on a line of decompiled code is translated into a breakpoint at the corresponding IL offset. The .NET runtime then reports when execution reaches that offset, and dnSpy maps the location back to the line you see. Local variables are read from the runtime’s debugging interfaces and displayed with the names the decompiler chose, or with the original names if a PDB happens to be available.
This means you never need to recompile anything. It also explains the main limitation: if the JIT compiler heavily optimises a method, some IL offsets and locals no longer map neatly to machine code, and the debugger has less to work with.
Step 1: pick the right dnSpy and prepare
- Use
dnSpy.exefor 64-bit targets anddnSpy-x86.exefor 32-bit ones. The bitness guide shows how to check. - If the target runs as administrator, start dnSpy as administrator too.
- Copy the application to a working folder if you plan to experiment with edits later, and keep the original untouched.
- Open the main executable and its own assemblies in dnSpy so you can set breakpoints before the program starts.
Step 2: reduce JIT optimisation
Release builds are optimised, and the JIT optimises them further at runtime. Variables disappear, short methods are inlined into their callers, and breakpoints may be skipped. dnSpy’s debugger options (Debug › Options › Debugger) include settings that ask the runtime to suppress JIT optimisation for modules as they load. Enabling them makes locals visible and breakpoints dependable.
Two things to remember:
- The setting applies when a module is loaded. Starting the program from dnSpy guarantees that; attaching to an already running process does not affect code that has already been compiled.
- Code runs a little slower with optimisation suppressed, which can matter for timing-sensitive bugs.
Step 3: start the program under the debugger
Choose Debug › Start Debugging or press F5. A dialog asks which debug engine to use and which executable to start:
- .NET Framework for classic desktop and server applications.
- .NET (formerly .NET Core) for modern applications, either an apphost executable or a
dotnet MyApp.dlllaunch. - Unity for games using a debug-enabled Mono runtime.
Fill in command-line arguments and the working directory if the program needs them. A useful option is to break at the entry point, which pauses the program before any of its own code runs so you can place breakpoints calmly.
Step 4: set breakpoints on decompiled code
Navigate to the method you are interested in and click in the left margin or press F9 on a line. A red dot appears. Because breakpoints map to IL offsets, they only exist on lines that correspond to real instructions; dnSpy moves a breakpoint to the nearest valid line if necessary.
Right-click a breakpoint to make it smarter:
- Conditions stop only when an expression is true, for example
amount > available. - Hit counts stop on the nth time a line is reached, ideal for loops.
- Filters limit a breakpoint to a specific process or thread.
- Tracepoints log a message to the Output window instead of stopping, which keeps timing closer to normal.
The Breakpoints window lists every breakpoint across all assemblies, so you can disable groups without deleting them.
Step 5: inspect state when execution stops
When a breakpoint hits, the line is highlighted and a yellow arrow shows the next statement. The tool windows now show live data:
| Window | What it shows |
|---|---|
| Locals | Parameters and local variables of the current method, expandable into object fields |
| Autos | Variables used on the current and previous lines |
| Watch | Expressions you add yourself, re-evaluated at every stop |
| Call Stack | The chain of calls that led here; double-click a frame to inspect it |
| Threads | All managed threads, useful for deadlocks and race conditions |
| Modules | Every loaded assembly, including dynamic and in-memory ones |
| Memory | Raw bytes at an address, for low-level analysis |
You can edit values in the Locals and Watch windows to test a hypothesis, for example to see whether a method succeeds when a flag is true, without changing any code.
Step 6: step through the code
Use the familiar commands: F10 steps over a call, F11 steps into it, and Shift+F11 steps out of the current method. Stepping into a framework method works too, because dnSpy decompiles framework assemblies just like your own. Set Next Statement lets you move the execution point within a method to retry a block, a powerful but risky trick that can leave objects in an inconsistent state.
Step 7: catch exceptions where they happen
Many bugs show up as an exception that is caught and logged far from its cause. Open Debug › Windows › Exception Settings and tick the exception types you care about, such as System.NullReferenceException or System.InvalidOperationException. dnSpy will then break as soon as the exception is thrown, a first-chance exception, before any catch block runs. The Locals window shows exactly which value was null.
This single technique often solves in minutes problems that took days of log reading.
Attaching to a running process
Sometimes the problem only appears after hours of running, or the program is started by a service host. Use Debug › Attach to Process, pick the process from the list, which shows its runtime and architecture, and click Attach. Breakpoints in modules that are already loaded become active immediately. Keep in mind that JIT optimisation settings do not apply retroactively, so some locals may be unavailable.
For ASP.NET applications hosted in IIS, attach to the worker process w3wp.exe; for self-hosted services, attach to the service’s own executable.
Debugging dynamically loaded code
Plug-in systems, scripting engines and some protection schemes load assemblies from memory instead of files. dnSpy lists them in the Modules window, can decompile them while the process is paused, and can save them to disk with the module dump feature. Enabling “break when a module is loaded” in the debugger options helps you stop exactly when such code appears.
Debugging Unity games built with Mono
Unity’s standard runtime does not expose a debugger interface that dnSpy can use. The dnSpy project published modified, debug-enabled Mono runtime builds for many Unity versions; replacing the game’s Mono library with the matching build enables dnSpy’s Unity debug engine. Pick the build that matches the game’s Unity version and architecture exactly, keep a backup of the original file, and only do this with games you are permitted to modify. IL2CPP builds cannot be debugged this way because they contain no IL.
Common problems and fixes
Breakpoint shows as hollow or is never hit
The module may not be loaded yet, the code may be optimised, or you set it in a different copy of the assembly than the one the process loaded. Check the path in the Modules window.
Locals show “cannot evaluate” messages
Optimised code. Restart the program from dnSpy with optimisation suppressed.
Stepping jumps around unexpectedly
Compiler-generated state machines for async methods and iterators split one method into several. Turn on the decompiler options that reconstruct async and iterator methods, or set breakpoints after each await.
The process exits as soon as the debugger attaches
Some programs detect debuggers deliberately. If it is your own software, remove the check in a test build; if it is not, respect that the author has chosen to prevent analysis.
Summary
Debugging without source in dnSpy follows the same rhythm as any debugger: prepare, break, inspect, step and repeat. The differences are what make it powerful. Breakpoints go straight onto decompiled code, framework internals are as visible as your own methods, and first-chance exception breaks show the real cause of a failure. Start the target from dnSpy with JIT optimisation suppressed, match the bitness, and you can understand the behaviour of almost any .NET program you are permitted to analyse.