2016-12-02 94 views
1

我想使用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)); 
      } 
     } 
    } 
} 

回答

0

發現,相比於生產過程的名字在我的開發環境不同。

更正過程的名稱可修復錯誤。