ClrMD无法附加到生产服务器上的进程

问题描述:

我想使用ClrMD转储在特定进程中运行的所有线程的堆栈跟踪。该代码在我的开发环境中正常工作,但不在生产服务器上。ClrMD无法附加到生产服务器上的进程

服务器运行的是:在Windows Server 2012 R2标准

我收到的错误是:

无法附加到进程。错误0.

This post问如何将ClrMD附加到另一个用户进程,这正是我想要做的。我终止了进程(作为Windows服务运行),并以与我试图执行ClrMD的用户相同的方式启动它。我仍然得到错误。

尝试给用户调试privlidges,但是这也没有帮助。

我敢打赌,问题与如何配置生产服务器有关。我有管理员权限。

关于下一步做什么的建议?

enter image description here

代码:

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using Microsoft.Diagnostics.Runtime; 

namespace ConsoleApplication4 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int pid = 0; 
      var result = new Dictionary<int, string[]>(); 
      var targetProcessName = "Dist.TingbogScraper.Business.TingbogScraperService.vshost"; 
       // Change this to the process you are looking for 
      var outputPath = "C:\\temp\\ClrMDresult.txt"; 
      var exceptionOutput = "C:\\temp\\ClrMDdump.txt"; 

      var processes = Process.GetProcesses(); 

      foreach (var process in processes) 
      { 
       if (process.ProcessName.Contains(targetProcessName)) 
       { 
        pid = process.Id; 
       } 
      } 

      try 
      { 
       using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Passive)) 
       { 
        ClrRuntime runtime = dataTarget.ClrVersions.First().CreateRuntime(); 

        foreach (var t in runtime.Threads) 
        { 
         try 
         { 
          if (t.StackTrace != null) 
          { 
           result.Add(
            t.ManagedThreadId, 
            t.StackTrace.Select(f => 
            { 
             if (f.Method != null) 
             { 
              return f.Method.Type.Name + "." + f.Method.Name; 
             } 

             return null; 
            }).ToArray() 
           ); 
          } 
         } 
         catch (Exception ex) 
         { 

         } 
        } 
       } 

       foreach (var kvp in result) 
       { 
        var value = kvp.Value; 
        foreach (var stacktrace in value) 
        { 
         System.IO.File.AppendAllText(outputPath, 
          string.Format("{0} {1} {2}", kvp.Key, stacktrace, Environment.NewLine)); 
        } 
       } 
      } 
      catch (ClrDiagnosticsException ex) 
      { 
       System.IO.File.AppendAllText(outputPath, 
          string.Format("{0} {1} {2}", ex.Message, ex.StackTrace, ex.Source)); 
      } 
     } 
    } 
} 

发现,相比于生产过程的名字在我的开发环境不同。

更正过程的名称可修复错误。