1

This is quite easy to do with a low-level debugger, like x64dbg (for instance). Say, if I have a running native process I can attach to it, set a breakpoint, and then step through native code with it.

Is there a similar debugger but for .NET, that would allow me to do what I listed above?

1
  • You can also try JetBrains Rider (it has a trial). It allows to attach to running process, decompile it and set breakpoints as well. It uses the same logic as their dotPeek tool though it might be not userfriendly to navigate through code in that way. Otherway is to decompile app through dotPeek as source project Sep 20, 2022 at 20:29

2 Answers 2

2

Try using https://github.com/dnSpyEx/dnSpy. It has NET decompiler, debugger and you can make changes directly in C# code, compile it and save changes into existing dll or exe module.

5
  • Thanks. I tried it. Although debugging optimized .NET code is kinda tricky. The variables don't show in most cases, and the currently displayed active line in C# code is 2 or 3 lines behind where the actual execution is. Worse still there's no option to display Assembly instructions between C# lines of code to know the actual values of variables. Otherwise all you get are their names. Do you have any idea how to resolve these issues?
    – c00000fd
    Sep 19, 2022 at 9:39
  • For dealing with issues like "cannot see this variable value because it has been optimized away", I have used this switch, stackoverflow.com/a/279593/1001395 (in a .ini file of the same name like the assembly you are debugging, ie System.Data.dll.ini) Sep 21, 2022 at 22:39
  • @FernandoGonzalezSanchez hmm, that is really interesting. Although I am not sure if it would work. The process I am trying to debug is linked against *.ni.dll assemblies (native images), as in C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.Df7c88f51#\7f6d8ef6224b79741cbfa39fe91b6702\Microsoft.Data.Mashup.ProviderCommon.ni.dll. Where would I apply that registry setting then?
    – c00000fd
    Sep 22, 2022 at 14:18
  • Interesting, haven't tried with NGEN images, but this doc suggest it can work too learn.microsoft.com/en-us/dotnet/framework/debug-trace-profile/… Sep 23, 2022 at 0:07
  • Also provided more details on options to disassemble on the fly in my answer reverseengineering.stackexchange.com/a/30908/11551 (I used a lot RedGate Reflector in that past that way). Sep 23, 2022 at 0:10
0

In Visual Studio (not Visual Studio Code), go to menu Debug -> "Attach to Process", pick up the process from the list. Now you can press "pause" / "Break All" in Visual Studio to suspend all process threads and start debugging them.

This works regardless of if the process is written in C++ or .NET.

Visual Studio will show the stack trace and even source code if available. (In .NET even if source code is not available if you have installed a plugin for Visual Studio like Redgate Reflector Pro or https://github.com/icsharpcode/ILSpy).

For debugging a service process, you have to wait until the execution enters your program, you cannot attach a debugger during the SCM execution. The simplest way to do this is to add an blind loop to your code and once attached, exit the loop with the debugger (by changing the value of the sentinel variable) like

   int i = 0;
   while (i == 0) {
     i++;
     i--;
   }

For dealing with issues like "cannot see this variable value because it has been optimized away" you can disable CLR optimizations, I have used this switch (AllowOptimize), see stackoverflow.com/a/279593/1001395 (in a .ini file of the same name like the assembly you are debugging, ie System.Data.dll.ini) <= this works in .NET classic.

[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.