简体   繁体   中英

How NOT to debug a referenced dll?

I'm using a Release version of a dll which is referenced in other project. This dll has tampering detection when debugger is running (dll is also mine, I don't plan to do something illegal) which gives false positive results when debugging.

Is there an option in Visual Studio (2017-1019) not to "debug" referenced dll, but still be able to debug code which is being developed? "Debug" means that I don't want to Step-In in this specific dll, but just to get the data from the method the same way I could get it in Release.

Eg:

//Release dll
int Sum (int a, int b)
{
bool TamperingDetected = CheckForTampering();
if (TamperingDetected)
  return 0;
else
  return a + b;
}

//Other project
MessageBox.Show(dll.Sum(1, 1));

If I run this part of the code in Release I can easily get the correct value. No tampering will be detected, if someone would like to reverse engineer this method, it's very easy to do it in Release "legally". If I run in Debug, I will always get 0. As said before, I don't want to Step-In in Sum method, so no debugging should be done in this dll, but everything else should work like in Debug.

Just My Code option makes no difference.

The background is that one team creates this dll with tampering detection and the other is using it as a NuGet. Now they cannot debug their part of the code since our is detecting tampering and returning false values. It would be too much for the other team to have the source code, since we then risk that they do something wrong during the build on that part of the code which they don't know.

One option would be to give them Debug and Release dll's and they could switch between them like explained here: https://stackoverflow.com/a/7284102 , but then we lose the simplicity of sharing a NuGet and we still risk that Debug dll is copied instead of Release dll, thus losing tampering detection in the final product. NuGet is then always in Release.

Switching NuGets automatically with switching the Debug and Release configuration is not possible, or I still didn't find a way to it, but this would also be a good option. Only if some completely other project is used just to download both Debug and Release NuGets and then using the above mentioned answer, but I don't like it either.

Since there is a very small chance that it's possible to do what I first wanted, second best solution is to automatically switch between NuGets and this works with Choose-When-Otherwise. Condition in PackageReference or in ItemGroup doesn't work (at least not in VisualStudio, according to this ). So I'll create two NuGets, one will be Release with tampering detection and the other will also be Release but without tampering detection ("-debug" suffix is added to this version). NuGet is then not installed through NuGet Manager but only the dllVersion is updated in.csproj:

<PropertyGroup>
  <dllVersion>1.2.3</dllVersion>
</PropertyGroup>
<Choose>
  <When Condition="'$(Configuration)'=='Debug'">
    <ItemGroup>
      <PackageReference Include="dllNuGet" Version=$(dllVersion)-debug/>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <PackageReference Include="dllNuGet" Version=$(dllVersion)/>
    </ItemGroup>
  </Otherwise>
</Choose>

It seems that you want to decide whether to debug the dll according to a certain switch under Debug mode.

Obviously , the method you gave is problematic. After all, it is based on the current configuration of the project. Once the release mode is enabled, you cannot debug the code of your main project. Essentially, you still want to debug the code.

However , that disable Enable Just My Code option under Tools --> Options --> Debugging --> General will make you debug code under the Release mode. But it's not a perfect Debug mode and sometimes I cannot hit the breakpoint and cannot debug further, it is not recommended.

I have two solutions:

=====================================================

Tip One) Use Assembly dll directly rather than nuget

1) create a new Configuration called Debug_NotDLL which inheritances Debug mode. 在此处输入图像描述

Also , make two dlls of Debug and Release mode.

2) Add these on the csproj file:

 <Reference Include="test" Condition="'$(Configuration)'=='Debug'">
      <HintPath>..\test\bin\Debug\xxx\xxx.dll</HintPath>
    </Reference>
    <Reference Include="test" Condition="'$(Configuration)'=='Debug_NotDLL'">
      <HintPath>..\test\bin\Release\xxx\xxx.dll</HintPath>
 </Reference>

Then , you can switch Debug and Debug_NotDLL configuration to get what you want.

====================================================

Tip two) Use nuget

create new package, in this situation, I recommend that you should use net standard class library project.

I suggest you could create two nuget package of your dll.

You would better rename the dll of Release mode into xxx_Release.dll and xxx_Debug.dll for Debug mode.

To create a release nuget package , you should directly right-click on the lib project under Release mode--> click Pack . To rename the nuget package, you could use PackageId msbuild property to set its name to xxx_Release under csproj file. Check this document .

Note that if you create the nuget package under the local machine, you can always enter the nuget source code because there is a cache of the project source code on the current machine. It is a special siuation. So you should test the nuget package on another machine.

To create a debug nuget package , the dll and pdb file is not enough, you should also add the source files into the nuget package.

Then , pack such nuget package called xxx.Debug.xxxnupkg under Debug mode. This is one link and two link about the steps of creating a debug nuget package.

When you finish it, you can install these two nuget packages, and add these under tha main project,

<ItemGroup>
    <PackageReference Include="xxx.Debug" Version="1.0.0" Condition="'$(Configuration)'=='Debug'" />
  <PackageReference Include="xxx.Release" Version="1.0.0" Condition="'$(Configuration)'=='Debug_NotDLL'" />
 </ItemGroup>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM