Created
June 9, 2024 13:07
-
-
Save KOZ60/e856b9b0810b1ed4d90301ad1700646f to your computer and use it in GitHub Desktop.
pdb ファイルからソースコードの位置を読み出す。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using Mono.Cecil; | |
using Mono.Cecil.Pdb; | |
class Program | |
{ | |
/// <summary> | |
/// pdb ファイルからソースの位置を取得する | |
/// </summary> | |
/// <remarks> | |
/// Mono.Cecil を nuget から入手してください。 | |
/// </remarks> | |
static void Main() { | |
var assemblyPath = Assembly.GetExecutingAssembly().Location; | |
var pdbFileName = Path.ChangeExtension(assemblyPath, ".pdb"); | |
var readerParameters = new ReaderParameters { | |
ReadSymbols = true, | |
SymbolReaderProvider = new PdbReaderProvider(), | |
SymbolStream = new FileStream(pdbFileName, FileMode.Open, FileAccess.Read) | |
}; | |
var assembly = AssemblyDefinition.ReadAssembly(assemblyPath, readerParameters); | |
List<string> sourceFiles = new List<string>(); | |
// シンボル情報からソースファイルの位置を取得する | |
foreach (var method in assembly.MainModule.Types.SelectMany(t => t.Methods)) { | |
var sequencePoints = method.DebugInformation.SequencePoints; | |
foreach (var sequencePoint in sequencePoints) { | |
if (sequencePoint.Document != null) { | |
string sourceFilePath = sequencePoint.Document.Url; | |
if (!sourceFiles.Contains(sourceFilePath)) { | |
sourceFiles.Add(sourceFilePath); | |
} | |
} | |
} | |
} | |
// ソースファイルの位置の一覧を表示する | |
Console.WriteLine("Source Files:"); | |
foreach (var sourceFile in sourceFiles) { | |
Console.WriteLine(sourceFile); | |
} | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment