0

I need to keep track of source code lines based on which certain instructions are to be injected.

Steps I followed are as below:

  1. Built my project in debug mode which generated DLLs and corresponding PDBs(these were incremental PDBs and did not have sequence point information of all the methods)

  2. Placed the DLL and PDB in the same folder and used it with Mono.cecil to get sequence point information and injected the instructions. This worked fine for the methods that had sequence point info in PDB

  3. Again, built my project in debug mode but this time added below code in csproj file

    <PropertyGroup>
       <DebugType>Full</DebugType>
    <PropertyGroup/>
    
    

This generated a full PDB file along with DLLs

  1. Upon using the DLL and full PDB in the same filepath, this time Mono.cecil throws below error

    Mono.Cecil.Cil.SymbolNotFoundException: 'No symbol found for file my_file_path\abc.dll"

How do I get Mono.cecil work with full PDBs instead of incremental PDBs.

Thanks in advance.

1 Answers1

0
  1. What version of Mono.Cecil are you using?
  2. How are you setting up the PDB reader?

At least with latest (0.11.4) the following code works:

using Mono.Cecil;
using Mono.Cecil.Rocks;

class Program
{
    static void Main(string[] args)
    {
        var p = new ReaderParameters();
        p.ReadSymbols = true;
            
        using var x = AssemblyDefinition.ReadAssembly(typeof(Program).Assembly.Location, p);
        foreach(var m in x.MainModule.GetAllTypes().SelectMany(t => t.Methods).Where(m => m.DebugInformation != null && m.DebugInformation.HasSequencePoints))
        {
            foreach(var d in m.DebugInformation.GetSequencePointMapping())
            {
                System.Console.WriteLine($"{d.Key} => {d.Value.Document.Url} ({d.Value.StartLine},{d.Value.StartColumn})");
            }
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
      <DebugType>Full</DebugType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Mono.Cecil" Version="0.11.4" />
  </ItemGroup>
</Project>
Vagaus
  • 4,174
  • 20
  • 31
  • I am using version 0.11.4. The project DLL is completely independent of the EXE that uses Mono.cecil so I provide the file path like this: var readParams = new ReadParameters(){ ReadSymbols = true }; var assembDef = AssemblyDefinition.ReadAssembly("local_folder_path\abc.dll", readParams); The full PDB (abc.pdb) is placed in that same path along with DLL – yesha thakrar Jul 08 '21 at 16:17
  • @yeshathakrar Processing an external assembly should make no difference. I've tried with an external one and it worked (i.e. the code above wrote the sequence points to the console). How are you building your assemblies? Which .Net version? – Vagaus Jul 09 '21 at 12:30