Debugging 3rd Party .NET Code without symbols in Visual Studio – Michael’s Coding Spot

It ’ mho not to test rare or sometimes to see eldritch issues occur in Third occasion library code. Name some {method} performing and also you ’ ve bought an odd exception. Or an defective habits or perhaps a course of crash. Is it a microbe ? Are you making baby one thing improper ? Is it a configuration downside ? It necessarily could be good to debug a few of these points .
On this article we ’ rhenium going to do barely that – We ’ ra going to see Depreciation to debug Third occasion library code in ocular Studio. It doesn ’ t must be Third occasion code although. It is perhaps rampart firm code with out symbols, your personal manufacturing code, or code from the .NET framework itself. We ’ ll see Depreciation to debug any .NET discussion board that has no symbols or supply code .

Breaking down on Exceptions from Third-party code

There are various instances when there ’ s an exception within the Third-party code that you just wish to debug. hera are some examples :

  • When one thing unusual is happening and you may’t perceive why. Term, a request to your server returns a 500 error code for no obvious tiny. There’s a posthumous probability there was an exception in some middleware for which you don’t have symbols and supply code. The exception was swallowed in a squeeze clause and you might be left to guesswork.
  • It occurs as soon as and last time that you just’re calling Third occasion library code and it returns surprising re-launch. There’s a posthumous probability an exception was thrown after which dealt with. Suppose you knew the exception particulars, it may need helped to unravel the problem.
  • Suppose you’re calling Third occasion library code and it throws an exception. You will note the exception object, {but} solely partial name stack frames. And no native variables from the body the place the exception was thrown. Certain, the exception object comprises a few of the particulars, {but} that’s not the entire image.

There are a few issues you must do to have the ability to debug these exceptions.

1. Consumer Code and non-user code

ocular Studio rag code as “ user-code ” and “ non-user code ”. Suppose an meeting has symbols and it ’ s not optimized, then it ’ randomness thought of “ user-code ”. in any other case, it ’ brimstone thought of “ non-user code ”. Any Third-party code goes to be “ non-user code ” as a result of it ’ s usually with out symbols and about absolutely optimized .
To interrupt on exceptions from non-user code, you ’ ll want to show off the “ Allow Simply My Code ” setting in VS choices :

2. Optimized code

If you construct a challenge in “ {Release} ” temper, it ’ south set to “ optimize code ” by default possibility. That is performed for efficiency causes as a result of optimize code is far sooner. Code optimization adjustments many issues like rearranges code and inlines strategies. For efficiency causes and to stop reverse-engineering, Third occasion code is about {always} optimized .
If you construct in “ Debug ” mode, the default rig is to not optimize code. That ’ s as a result of when code is optimized, lots of the debugger ’ brimstone options cease working. This contains seeing Native Variables, seeing the presently executed line and stopping on breakpoints .
The factor is that the IL code produced from Debug and {Release} builds is the lapp as a result of code optimization is completed in runtime. The lone dispute is a masthead that instructs the JIT compiler to optimize or to not optimize. By default, while you do break on the exception, you received ’ metric ton perceive Locals as a result of the code will probably be optimized. {But} suppose you test “ Suppress JIT optimization on module load ” ( like within the picture above ), then VS will inform the JIT Compiler, to not optimized code .

Annotation that code is optimized when a module masses. To test suppose you connect Visible Studio when the module was already loaded, it should keep optimized, even suppose the “Suppress… ” setting is on. That’s why it’s {always} finest to celebration the method with Visible Studio debugger as an alternative of attaching. You may see which modules are optimized within the Modules window.

right here ’ s an instance of an exception in Third-party code :

1 2 3 4 5 6

public

override

void

Celebration

(

)

{

    

var

serialized

=

“{“Nadsafsdfds”

;

    

Individual

deserialized

=

JsonConvert

.

DeserializeObject

<

Individual

>

(

serialized

)

;

}

When checking “ Suppress JIT optimization ” you ’ ll see Locals and also you ’ ll see a prosperous birdcall push-down storage :

nonetheless, suppose “ Suppress JIT optimization ” was not checked and the code was optimized, the re-launch are a lot less oi informative :

The Locals cannot be evaluated and the decision push-down storage doesn ’ triiodothyronine really say something from throughout the non-user code .

Newtonsoft.Json has SourceLink enabled. This implies the symbols comprise a hyperlink to the proper git commit with the supply code. Suppose including with NuGet, Visible Studio will have the ability to routinely load the supply code and every little thing will work by itself. As an alternative of including a NuGet package deal, I deleted the symbols and manually referenced Newtonsoft.Json.dll. To test we’re now coping with an meeting with out symbols or supply code.

Place Breakpoints anyplace

It ’ s potential to put breakpoints in ocular Studio into strategies for which you don ’ t have the supply code .
Earlier than beginning, make certain to uncheck “ Allow Simply My Code ”, in any other case the breakpoint received ’ deoxythymidine monophosphate be hit. And to test “ Suppress JIT optimization on school burden ” to test that you could see Locals when the breakpoint does hit .
Earlier than inserting the breakpoint in Third occasion code, you ’ ll want to seek out the {method} the place you wish to place the breakpoint. To try this, use your {favorite} decompiler to open the fabrication and discover the strategies ( like ILSpy, dotPeek, or JustDecompile ). Within the case of Newtonosoft.Json, let ’ s say I wish to place a breakpoint within the personal {method} SerializeObjectInternal. You ’ ll want the absolutely certified listing right here : Newtonsoft.Json.JsonConvert.SerializeObjectInternal the place Newtonsoft.Json is the namespace, JsonConvert is the lower arm political name, and SerializeObjectInternal is the {method} .
now go to Debug | Home windows | Breakpoints and {click} on New | Operate Breakpoint…

Within the dialogue that opens, enter the fully-qualified diagnose : Newtonsoft.Json.JsonConvert.SerializeObjectInternal .
That ’ s it, run the code and the breakpoint will probably be hit .

You ’ ll have the ability to see the prosperous name stack and the Locals. Fairly courteous for no symbols and no reservoir code. You may journey within the handle push-down storage and early threads. You ’ ll see Locals for description frames, together with these with out symbols .
discover that VS has a little bit oddity when inserting these breakpoints. It doesn ’ triiodothyronine appear to work suppose you place the breakpoint earlier than the incumbent debug college time period. to test suppose you restart the method and celebration debugging last time, your beforehand set breakpoints received ’ triiodothyronine work. You consistently must celebration debug and set contemporary breakpoints within the stream debug seance .

TIP: Typically, you wish to debug Third-party code, {but} don’t know which strategies are hit and the place to put the breakpoint. An effective way to seek out out which strategies had been invoked in a state of affairs is to make use of a efficiency profiler like dotTrace. Report the snapshot you wish to debug and have a look at description the strategies that had been known as. It’s finest to make use of Tracing mode, which exhibits description strategies that had been hit, even suppose they took all ink little bit date and time.

Prosperous debugging expertise with dotPeek’s image server

Inserting breakpoints is dainty and might get you someplace, {but} there ’ second nothing like a prosperous debug expertise the place you may step over code with F10, step into strategies with F11, and {watch} variables. It ’ second fairly potential to do in Visible Studio with any managed discussion board, even with out symbols and starting code .
regardless that this know is a snatch first rate with dnSpy, you may obtain the identical with ocular Studio and dotPeek ’ mho Image Server functionality. The definition is fairly easy : dotPeek decompiles the meeting and extracts symbols that time to the decompiled code. It then acts as a logo server. ocular Studio simply wants so as to add the image server ’ mho placement and debug simply works .
To do that, celebration dotPeek with the proper bitness – dotPeek32.exe suppose you ’ re debugging a 32-bit process and dotPeek64.exe suppose you ’ re debugging a 64-bit process. Open the fabrication that you just wish to debug in dotPeek with File | Open. You ’ ll see it seem within the Meeting Explorer. now {click} the “ Celebration image server ” press button :

dotPeek will let you know the server ’ randomness placement. One thing like http://localhost:33417 .
In Visible Studio, go to Instruments | Choices | Debugging | Symbols. Hit the New Location press button on prime and add the URL from dotPeek. now you ’ re description deliberate to celebration debugging. You ’ ll have the ability to step into ( F11 ) the Third occasion library and debug freely with supply code and description different VS options accessible :

Like with description different strategies on this article, when utilizing this, you must disable the “ Allow Simply My Code ” possibility and allow the “ Suppress JIT optimization on school load ” possibility .
Suppose you don ’ thyroxine wish to step into the Third-party library code with F11, and wish to place a breakpoint alternatively, there are a match of how to try this :

  • Use the New Breakpoint performance in ocular Studio ’ brimstone Breakpoints window that we talked about earlier than .
  • Use ReSharper ’ s Navigation to Sources characteristic of speech. On this modality, R # overrides the default Visible Studio habits when clicking F12 ( Go to Definition ). alternatively of going to meeting Metadata, R # can take you to decompiled sources. And it integrates completely with dotPeek ’ south image server by preferring image waiter sources first. Go to Extensions | ReSharper | Choices | Instruments | However Sources and choose the final possibility Navigation to Sources :

    now you may navigate to the library code with F12 and {position} breakpoints freely .

Prosperous debugging expertise with simply ReSharper

You may obtain a prosperous debug expertise with nothing {but} ReSharper. That’s, with out dotPeek at description. The antic is to go to the Modules window after the college you wish to debug was loaded, and select Load Symbols with ReSharper Decompiler.
ReSharper load symbols for modules without PDBsReSharper load symbols for modules without PDBs
now you ’ ll have the ability to step into code with F11, set breakpoints, and so forth. Like with the opposite methods, bear in mind to disable the “ Allow Simply My Code ” possibility and allow the “ Suppress JIT optimization on school lode ” selection .
I skilled some quirks with this {method} performing that I didn ’ t have with dotPeek. For some information the decompiled souce code cost was not discovered. When this occurs VS presents a dialogue asking for the situation of that file. It did exist on the new disk, R # decompiled it accurately. My workaround was to make use of Every little thing to seek out that file on disk and set the prosperous path within the dialogue. After this, every little thing continued to work effectively .

Because of Cezaty Piątek for introducing me to this {method}. He wrote an awesome weblog lesson describing intimately on Depreciation to do that with simply ReSharper

Abstract

There are a number of methods that we noticed to debug .NET assemblies with out symbols or reference code. These strategies is usually a lifeguard when operating into international demeanor. Suppose there ’ s one factor to recollect from this text, it ’ s this : At any time when one thing unusual is happening, uncheck “ Allow Simply My Code ” and run the state of affairs last time. It ’ brimstone similar potential that some exception occurred and also you didn ’ deoxythymidine monophosphate even discover .
As for the improbable know with dotPeek ’ s image server – american samoa joke as it’s, dnSpy is even higher. And it ’ mho way more change into to debug manufacturing machines than ocular Studio. Simply copy the power booklet and run the possible. {But} that ’ s stuff for an additional net log lesson. glad debug .

contribution : Benefit from the weblog ? I’d release you to subscribe !

efficiency Optimizations in C # : 10 Greatest Practices ( unique article )



Wish to change into an skilled downside downside solver ? Take a look at a chapter from my bible Sensible Debugging for .NET Builders