【问题标题】:How do I programmatically enumerate types in a running .NET process using MDbg?如何使用 MDbg 以编程方式枚举正在运行的 .NET 进程中的类型?
【发布时间】:2010-04-28 20:48:13
【问题描述】:

我想打印出在运行的 .NET 进程中加载​​的所有不同类型的列表。我的计划是最终基于此构建一个 GUI 应用程序,因此我想通过我的代码而不是第三方工具来实现这一点。我认为我最好的选择是使用 MDbgCore 附加到正在运行的进程,然后使用 MDbgProcess.AppDomains 获取 CorAppDomain 对象,并尝试将对象模型向下走。

但是,我无法终生停止其他进程并查看任何 AppDomain。我一直在使用如下代码(我基于来自Mike Stall's blog 的代码)

    [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);
        if (proc.IsAlive)
        {
            proc.AsyncStop().WaitOne();

            Console.WriteLine(proc.AppDomains.Count);
            if (proc.AppDomains.Count > 0)
            {
                Console.WriteLine(proc.AppDomains[0].CorAppDomain);
            }
        }

        Console.WriteLine("Done!");
    } 

打印出来:

MDbgPlayground.exe
0
Done!

我尝试了各种风格的 debugger.Options.Stop*。我考虑过迭代所有方法并在所有方法上设置断点,但我也无法迭代 Modules 列表。我试过 debugger.Options.Trace,但这与使用 TraceListeners 跟踪 MDbg 的执行有关,而不是跟踪目标应用程序。

我在发布模式下运行我的 noddy 调试器应用程序,而在调试模式下运行目标。我正在使用 Visual C# 2010,但我束手无策。任何人都可以对此有所了解吗?

【问题讨论】:

    标签: c# debugging reflection mdbg


    【解决方案1】:

    只是做一些修补,尝试这样的事情(您可能需要根据需要进行修改)...

            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;
                }
            }
    

    【讨论】:

    • 这行不通——它只会显示在该进程的主程序集中定义的类型,而不是在运行时加载到该进程中的类型。
    【解决方案2】:

    这不会那么容易。我没有针对这个问题的特定源代码,但我会像这样对其进行伪编码:

    • 停止 Mdbg 进程
    • 设置一个新的 AppDomain(混合应用可以有 .NET2 和 .NET4 程序集,因此在当前 appdomain 中反映它们可能会引发异常!!)

      System.AppDomain tempDomain=System.AppDomain.CreateDomain("ReflectionOnly");

    • 遍历 MDbgProcess.Modules 并使用 Assembly.ReflectionOnlyLoad 将它们加载到应用程序域中进行仅反射(请参阅 How to load a .NET assembly for reflection operations and subsequently unload it?

    • 遍历 tempDomain 中的程序集
    • 对于每个程序集,获取类型列表
    • 报告类型.名称​​
    • 卸载 tempDomain
    • 调用 MDbgProcess.Go

    【讨论】:

      【解决方案3】:

      由于某些原因,您应该使用 debugger.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!");
      } 
      

      【讨论】:

        猜你喜欢
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 2014-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        相关资源
        最近更新 更多