C# 如何使用MDbg以编程方式枚举正在运行的.NET进程中的类型?
我想打印在运行的.NET进程中加载的所有不同类型的列表。我的计划是最终在此基础上构建一个GUI应用程序,因此我希望通过我的代码来实现这一点,而不是使用第三方工具。我认为我的最佳选择是使用MDbgCore连接到正在运行的进程,然后使用MDbgProcess.AppDomains获取CorAppDomain对象,并尝试向下遍历对象模型 但是,我不能停止另一个进程并查看任何AppDomains。我一直在使用下面这样的代码(我是基于 这张照片是:C# 如何使用MDbg以编程方式枚举正在运行的.NET进程中的类型?,c#,debugging,reflection,mdbg,C#,Debugging,Reflection,Mdbg,我想打印在运行的.NET进程中加载的所有不同类型的列表。我的计划是最终在此基础上构建一个GUI应用程序,因此我希望通过我的代码来实现这一点,而不是使用第三方工具。我认为我的最佳选择是使用MDbgCore连接到正在运行的进程,然后使用MDbgProcess.AppDomains获取CorAppDomain对象,并尝试向下遍历对象模型 但是,我不能停止另一个进程并查看任何AppDomains。我一直在使用下面这样的代码(我是基于 这张照片是: MDbgPlayground.exe 0 Done!
MDbgPlayground.exe
0
Done!
我尝试过各种调试器。选项。停止*。我曾想过迭代所有方法并在所有方法上设置断点,但我也不能迭代模块列表。我已经尝试过debugger.Options.Trace,但这与使用TraceListener跟踪MDbg的执行有关,而不是跟踪目标应用程序
我正在以发布模式运行我的noddy调试器应用程序,目标程序处于调试模式。我正在使用Visual C#2010,我已经束手无策了。有人能解释一下吗?只是做一些修补,尝试一下这样的方法(您可能需要根据需要进行修改)
这并不容易。我没有针对这个问题的特定源代码,但我会像这样对其进行伪编码:
- 停止Mdbg进程
- 设置一个新的AppDomain(混合应用程序可以有.NET2和.NET4程序集,因此在当前AppDomain中对它们进行反射可能会引发异常!!) System.AppDomain tempDomain=System.AppDomain.CreateDomain(“ReflectionOnly”)
- 迭代MDbgProcess.Modules,并仅使用Assembly.ReflectionOnlyLoad将其加载到appdomain中进行反射(请参阅)
- 遍历tempDomain中的程序集
- 对于每个程序集,获取类型列表
- 报告类型。名称
- 卸载临时域
- 调用MDbgProcess.Go
调试器.Processs.Active
而不是proc
变量。
您还应该在AsyncStop
之前调用debugger.Go()
。那么最后的代码呢
[MTAThread] // MDbg is MTA threaded
static void Main(string[] args)
{
MDbgEngine debugger = new MDbgEngine();
debugger.Options.StopOnModuleLoad = true;
// Launch the debuggee.
int pid = Process.GetProcessesByName("VS2010Playground")[0].Id;
MDbgProcess proc = debugger.Attach(pid);
proc.Go();
if (proc.IsAlive)
{
proc.AsyncStop().WaitOne();
Console.WriteLine(debugger.Process.Active.AppDomains.Count);
if ((debugger.Process.Active.AppDomains.Count > 0)
{
Console.WriteLine((debugger.Process.Active.AppDomains[0].CorAppDomain);
}
}
Console.WriteLine("Done!");
}
这是行不通的——它将只显示在该进程的主程序集中定义的类型,而不是在运行时加载到该进程中的类型。
foreach (Process process in Process.GetProcesses())
{
try
{
Assembly test = Assembly.LoadFrom(process.MainModule.FileName);
Console.WriteLine(test.FullName);
foreach (Type type in test.GetTypes())
Console.WriteLine(type.FullName);
}
catch
{
continue;
}
}
[MTAThread] // MDbg is MTA threaded
static void Main(string[] args)
{
MDbgEngine debugger = new MDbgEngine();
debugger.Options.StopOnModuleLoad = true;
// Launch the debuggee.
int pid = Process.GetProcessesByName("VS2010Playground")[0].Id;
MDbgProcess proc = debugger.Attach(pid);
proc.Go();
if (proc.IsAlive)
{
proc.AsyncStop().WaitOne();
Console.WriteLine(debugger.Process.Active.AppDomains.Count);
if ((debugger.Process.Active.AppDomains.Count > 0)
{
Console.WriteLine((debugger.Process.Active.AppDomains[0].CorAppDomain);
}
}
Console.WriteLine("Done!");
}