This is a nice little trick you might not know.
Lets say you have an application build in release
mode (/debug:pdbonly /optimize+) and you want to attach the debugger directly to a running instance.
To keep it simple, this is our app:
public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { Cal(100); } int Cal(int x) { return x * x; } }
We set a break point in this line and attach to the process:
return x * x;
But the debugger will never break in our code!
The reason is that the JIT compiler has inlined the method call (remember that will compiled with /optimize+)
This is indeed a good thing. In most cases, we want the JIT compiler to have the option to inline our production code.
We can see that the module is optimized in the modules window in VS:
Disable JIT Optimization
For debugging purposes, we can actually disable JIT Optimization without rebuilding the code.
Put an ini file (yes ini) in the same folder as the exe file. The filename should match the exe filename, just with ini as extension. In this case WpfApplication1.exe and WpfApplication1.ini.
The ini file should contain these 3 lines:
[.NET Framework Debugging Control] GenerateTrackingInfo=1 AllowOptimize=0
This will tell the JIT compiler not to optimized the code, so the method-call won’t be inlined.
We can now see that the loaded module is not optimized:
Remember to disable “Enter Just My Code” in VS.NET (Tools –> Options –> Debugger)