How to debug a referenced dll (having pdb) - c#

I have two solutions in my workspace, say A and B.
Solution A is an older project which I finished coding some time ago.
In solution B, I need to use some classes from Solution A. To do so, I add a reference to the dll of one of the projects in solution A.
The problem is when I try to debug. I want to be able to step into A's code as well. Visual studio is not able to load the code for these classes ("There is no source code available for the current location.") and I can only view the disassembly, which is not useful.
The only way I know to debug classes from solution A is by running solution B, detach all processes (in the Debug menu item) and attach the process from solution A.
However, this is very inconvenient and I can only debug A OR B at once.
Is there a way to allow stepping into the code of referenced dlls (for which I do have the source code)?
Solution: My mistake was that I thought that a project can only be part of a single solution. In fact, a project can be part of any number of solutions.
When you need to reference the old project, you should simply add the project to the solution. This is done by right clicking the new solution in the Solution Explorer > Add > Existing Project.
Then, you'll be able to add the project reference. As others wrote, you should probably completely avoid using dll references to your own code (or other code you might need to change and debug).
A very good reference to how solutions should be designed can be found in MSDN.

If you have a project reference, it should work immediately.
If it is a file (dll) reference, you need the debugging symbols (the "pdb" file) to be in the same folder as the dll. Check that your projects are generating debug symbols (project properties => Build => Advanced => Output / Debug Info = full); and if you have copied the dll, put the pdb with it.
You can also load symbols directly in the IDE if you don't want to copy any files, but it is more work.
The easiest option is to use project references!

I had the same issue. He is what I found:
1) make sure all projects are using the same Framework (this is crucial!)
2) in Tools/Options>Debugging>General make sure "Enable Just My Code (Managed Only) is NOT ticked
3) in Tools/Options>Debugging>Symbols clear any cached symbols, untick and delete all folder locations under the "Symbols file (.pdb) locations" listbox except the default "Microsoft Symbol Servers" but still untick it too. Also delete any static paths in the "Cache symbols in this directory" textbox. Click the "Empty Symbols Cache" button. Finally make sure the "Only specified modules" radio button is ticked.
4) in the Build/Configuration Manager menu for all projects make sure the configuration is in Debug mode.

Step 1: Go to Tools-->Option-->Debugging
Step 2: Uncheck Enable Just My Code
Step 3: Uncheck Require source file exactly match with original Version
Step 4: Uncheck Step over Properties and Operators
Step 5: Go to Project properties-->Debug
Step 6: Check Enable native code debugging

Another point to keep in mind, be sure the referenced dlls are not installed in the GAC. After testing, I installed my dlls into the GAC to do system level testing. Later, when I had to debug my code again, I couldn't step into the referenced assemblies until I deleted them from the GAC.

I had the *.pdb files in the same folder and used the options from Arindam, but it still didn't work. Turns out I needed to enable Enable native code debugging which can be found under Project properties > Debug.

When you want to set a breakpoint in source code of a referenced dll, first make sure that you have a pdb file available for it. Then you can just open the related source code file and set a breakpoint over there. The source file does not need to be part of your solution.
As explained in How can I set a breakpoint in referenced code in Visual Studio?
You can review your breakpoints through the breakpoints window, available via Debug -> Windows -> Breakpoints.
This approach has the benefit that you are not required to add an existing project to your solution just for debugging purposes as leaving it out has saved me a lot of build time. Evidently, building a solution with only one project in it is much faster than building a solution with lots of them.

Make sure your DLL is not registered in the GAC. Visual Studio will use the version in the GAC and it will probably have no debugging information.

I don't want to include an external class library project in some of my solutions, so I step into assemblies that I consume in a different way.
My solutions have a "Common Assemblies" directory that contains my own DLLs from other projects. The DLLs that I reference also have their accompanying PDB files for debugging.
In order to debug and set breakpoints, I set a breakpoint in the consuming application's source where I'm calling a method or constructor from the assembly and then step INTO (F11) the method/constructor call.
The debugger will load the assembly's source file in VS and new breakpoints inside of the assembly can be set at that point.
It's not straight forward but works if you don't want to include a new project reference and simply want to reference a shared assembly instead.

The most straigh forward way I found using VisualStudio 2019 to debug an external library to which you are referencing in NuGet, is by taking the following steps:
Tools > Options > Debugging > General > Untick 'Enable Just My Code'
Go to Assembly Explorer > Open from NuGet Packages Cache
Type the NuGet package name you want to debug in the search field & click 'OK'
From the Assembly Explorer, right-click on the assembly imported and select 'Generate
Pdb'
Select a custom path where you want to save the .PDB file and the framework you want
this to be generated for
Copy the .PDB file from the folder generated to your Debug folder and you can now set
breakpoints on this assembly's library code

The following solution worked for me. It involves copy pasting the .dll and .pdb files properly from project A to B:
https://stackoverflow.com/a/16546777/5351410

It must work. I used to debug a .exe file and a dll at the same time !
What I suggest is
1) Include the path of the dll in your B project,
2) Then compile in debug your A project
3) Control that the path points on the A dll and de pdb file....
4)After that you start in debug the B project and if all is ok, you will be able to debug in both projects !

Visual Studio 2022 added a new top-level node: External Sources to solution explorer, which you will find while in debug mode. You can look at all the loaded dlls from there. You can also look at the loaded modules from Debug -> Windows -> Modules in debug mode. From there, right click on your desired dll, and click open file location, and then copy the pdb file to that location. This should allow you to step into any methods of the external dll from the same visual studio window.
Reference: https://devblogs.microsoft.com/visualstudio/debugging-external-sources-with-visual-studio/

Related

Breakpoints set but not yet bound in library project

I'm trying to debug my code in Visual Studio but my Breakpoint is not hit,it's giving me this message "Breakpoints set but not yet bound"
The breakpoint is in a project that is not set as StartUp Project because it is a library project, my controller calls the method by invocation, I also verified that the dll generated by the solution that contains the library is the same as the project added to my solution
I've read various other questions and tried various solutions like closing VS, deleting bin and obj folders, clean and rebuild but I can't reach those brakpoints
You may have a few options to resolve your issue. First and recommended, your dll and exe project should be in the same solution and both in debug mode. If the projects are in separate solutions, set the breakpoint on the method call on the exe project side. Then pressing F11 should let you into your dll code. If you don't own the project or solution exe, you have the option in VS to link your dll to a running process, but you will only be able to link compiled binaries with managed code. For example, your dll could probably link to windows explorer, but any code will run as far as I know.

Can you dynamically load a DLL associated with a currently loaded project then have the visual studio debugger break

I want to be able to have a solution where
Project A does NOT reference project B
I hit build all building both project A and project B
Project A is run and dynamically loads the DLL built in 2. via Assembly.Load
There is a breakpoint in project B in which the IL associated with it gets run
Is this possible?
Sure, this should be possible. You probably need some post-build script to move the projectB-dll and all dependencies to the build folder of project A, or some configuration to specify where the dll should be loaded from.
I'm not sure what you are referring to with your last point, but I would not expect the debugger to have any problems locating the debug symbols or corresponding source code in this case. If you are moving the dlls files you should probably also move the corresponding pdb files to ensure they can be loaded correctly.

Namespace disappears when building RELEASE, but present on DEBUG

The System.Windows.Interactivity namespace vanishes when I swap from DEBUG build to RELEASE build. My project fails to build (naturally), and gives the error and warning visible in the images below. The namespace simply disappears from the Object Browser as soon as I switch build modes, even though the reference is still present in the Solution Explorer.
What's going on here?! And how do I fix it?
The reason can be you have linked to the dll that is placed inside Debug folder. If link shows relative path like ../my.dll when this dll will be not accessible from Release folder. You can replace relative link by full. But better solution will be moving dll above Debug and Release folder in folders hierarchy:
place dll somewhere above Debug and Release folders
remove old dll from References
add new dll to References
Take a look to your build configurations; you can open the .csproj file with some good editor (Vim, Sublime, Notepad++) and take a search the reference you're missing. It is possible to reference different libraries depending on the build configuration, so maybe the reference of this library is not included in the "Build | Release" mode, or it is defined specifically in the "Build | Debug" configuration section instead of the common one.

Breakpoint inside external library that I made

I have 2 C# projects that I am working on. Project A is a library that Project B uses. I have Project A set to automatically copy the .dll into Project B's directory and then Project B uses that copied file for its reference.
Both projects are set to DEBUG and have been cleaned many times. My issue is that if in Project B I go into references -> my external library -> view in assembly explorer and then I try to put a breakpoint somewhere in that code (so I can see exactly when and where a certain part of my external library is being called), the breakpoint always hallows out and says the symbols have not been loaded, even though I can confirm in the Modules window that it says "Symbols Loaded" for the .dll that I am trying to put a breakpoint inside of.
I've set debug info to full inside of Project A and rebuilt it multiple times to try to fix this issue. Is there something I am doing wrong here? All I want to do is put a breakpoint inside of the .dll made in Project A that I can step into during the runtime of Project B (which references that .dll).
You will need to disable the Just My Code option in Visual Studio.
Tools > Options > Debugging > General
Uncheck "Enable Just My Code"

Debugging a dll in c#

While searching for debugging c# DLLs, I came across this article.
http://msdn.microsoft.com/en-us/library/c91k1xcf%28v=VS.100%29.aspx which says
You can start debugging DLL from:
The project used to create the executable that calls the DLL.
or
The project used to create the DLL itself.
I have source code(C#) for the executable project as well as the DLL.
My DLL project is in a different solution.
I want to debug the DLL from my exe project. How do I proceed with this. I have searched goole but without any detailed steps. I also added the DLL project to the exe project solution and added the break points in the source code for library project. But the breakpoint is never hit. How does the debugger know that I have loaded the source code for the DLL. Am I missing anything?
Update:
Following Avitus and Mick's suggestions, I added the DLL project to the exe solution. Also added the reference to the DLL project and the breakpoints were hit.
I also tried to debug it through the DLL project by
project--properties--Debug and setting the exe in the Start external program. In this case the break points were not hit, it kept saying the debug symbols not loaded. I tried all the options here without any success
Configuration was set to Active(Debug).
Debug Info was set to full.
In Tools--options--debugging(Enable just my code[Managed code] was unchecked)
In Tools--options--debugging--symbols, I had all modules,unless excluded selected and the Specify excluded modules list of empty. For some reason this method did not work.
The DLL has to have a debug database, or basically you have to include the DLL's solution in the solution you would debug it in.
Also visual studio has a Create Test feature which generates a bunch of test for methods and functions.

Categories