如何在.NET中进行线程转储? (一个JVM线程转储)

我发现无法在.NET中的所有线程上转储堆栈。 既不发送信号到进程也不编程访问所有线程。 我只能通过Thread.CurrentThread访问当前线程。

任何诡计?

如果您在进程已经运行时尝试获取堆栈转储(la jstack),则有两种方法,如下所述:

使用托管堆栈资源管理器

有一个鲜为人知但有效的工具叫做Managed Stack Explorer 。 虽然它具有基本的GUI,但如果你添加到路径,它可以有效地成为jstack的.NET等价物; 然后这只是一个打字的问题:

  mse / s / p  

使用windbg

  1. 下载并安装适用于您的体系结构的适用于Windows版本的调试工具(x86 / x64 / Itanium)
  2. 如果您需要有关Windows函数调用的信息(例如,您要跟踪内核调用),请下载并安装相应的符号。 如果您只想要自己的代码的线程转储,这不是绝对必要的。
  3. 如果您需要行号或任何其他详细信息,请确保将程序集的PDB文件放在调试器可以找到它们的位置(通常只是将它们放在实际程序集旁边)。
  4. 开始 – >程序 – > Windows调试工具[x64] – > windbg
  5. 使用菜单将调试器附加到正在运行的进程
  6. 为.NET 2.0加载带有“.loadby sos mscorwks”的SOS扩展(对于.NET 1.0 / 1.1,加载“.load sos”)
  7. 使用“!eestack”进行线程转储
  8. 使用“.detach”分离

我发现有必要采取生产线程转储,这对我有用。 希望能帮助到你 :-)

只是为了节省其他人的麻烦,这里的上面的端口为c#:

static void WriteThreadInfo(StringBuilder sw, IEnumerable threads) { foreach(Thread thread in threads) { if(!thread.IsAlive) continue; sw.Append(String.Concat("THREAD NAME: ", thread.Name)); sw.Append(GetStackTrace(thread)); sw.AppendLine(); sw.AppendLine(); } } static String GetStackTrace(Thread t) { t.Suspend(); var trace1 = new StackTrace(t, true); t.Resume(); String text1 = System.Environment.NewLine; var builder1 = new StringBuilder(255); for (Int32 num1 = 0; (num1 < trace1.FrameCount); num1++) { StackFrame frame1 = trace1.GetFrame(num1); builder1.Append(" at "); System.Reflection.MethodBase base1 = frame1.GetMethod(); Type type1 = base1.DeclaringType; if (type1 != null) { String text2 = type1.Namespace; if (text2 != null) { builder1.Append(text2); builder1.Append("."); } builder1.Append(type1.Name); builder1.Append("."); } builder1.Append(base1.Name); builder1.Append("("); System.Reflection.ParameterInfo [] infoArray1 = base1.GetParameters(); for (Int32 num2 = 0; (num2 < infoArray1.Length); num2++) { String text3 = ""; if (infoArray1[num2].ParameterType != null) { text3 = infoArray1[num2].ParameterType.Name; } builder1.Append(String.Concat(((num2 != 0) ? ", " : ""), text3, " ", infoArray1[num2].Name)); } builder1.Append(")"); if (frame1.GetILOffset() != -1) { String text4 = null; try { text4 = frame1.GetFileName(); } catch (System.Security.SecurityException) { } if (text4 != null) { builder1.Append(String.Concat(" in ", text4, ":line ", frame1.GetFileLineNumber().ToString())); } } if (num1 != (trace1.FrameCount - 1)) { builder1.Append(text1); } } return builder1.ToString(); } 

我没有找到一种方法来获取C#中所有托管线程的列表(只有ProcessThreads),所以看起来你需要维护自己感兴趣的线程列表。

此外,我发现我无法在正在运行的线程上调用新的Stacktrace(t,true),因此添加了暂停和恢复。 显然,你需要考虑这是否会导致问题,如果您要转储生产应用程序

顺便说一下,我们已将这个调用放在我们的应用程序wcf rest界面上,这样很容易做到。

我在这一点上看到为.NET CLR生成线程转储的最佳工具是DebugDiag。 此工具将生成活动CLR线程的非常详细的报告(使用Crash / Hang分析器)以及建议。

我建议查看以下.NET DebugDiag教程,因为它显示了生产问题后的分析过程。 步骤如下:

  • 创建受影响的w3wp进程的转储文件
  • 启动调试诊断工具,选择并启动Crash / Hang分析器。
  • 打开并分析报告分析概述。
  • 最后,查看阻止的线程摘要并执行更深入的潜水分析。

我为过去的一个项目写了一个翻车机:

 void CrashHandler::WriteThreadInfo(StringWriter* sw, ArrayList* threads, String* type) { sw->WriteLine(type); IEnumerator* ie = threads->GetEnumerator(); while(ie->MoveNext()) { botNETThread* bnt = static_cast(ie->Current); if(!bnt->IsAlive) continue; sw->WriteLine(String::Concat(S"ORIGIN ASSEMBLY: ", bnt->Assembly->FullName)); sw->WriteLine(String::Concat(S"THREAD NAME: ", (bnt->Name && bnt->Name->Length)?bnt->Name:S"Unnamed thread")); sw->Write(GetStackTrace(bnt->_thread)); sw->WriteLine(); sw->WriteLine(); } } String* CrashHandler::GetStackTrace(Thread* t) { System::Diagnostics::StackTrace __gc * trace1 = __gc new System::Diagnostics::StackTrace(t, true); System::String __gc * text1 = System::Environment::NewLine; System::Text::StringBuilder __gc * builder1 = __gc new System::Text::StringBuilder(255); for (System::Int32 num1 = 0; (num1 < trace1->FrameCount); num1++) { System::Diagnostics::StackFrame __gc * frame1 = trace1->GetFrame(num1); builder1->Append(S" at "); System::Reflection::MethodBase __gc * base1 = frame1->GetMethod(); System::Type __gc * type1 = base1->DeclaringType; if (type1 != 0) { System::String __gc * text2 = type1->Namespace; if (text2 != 0) { builder1->Append(text2); if (builder1 != 0) { builder1->Append(S"."); } } builder1->Append(type1->Name); builder1->Append(S"."); } builder1->Append(base1->Name); builder1->Append(S"("); System::Reflection::ParameterInfo __gc * infoArray1 __gc [] = base1->GetParameters(); for (System::Int32 num2 = 0; (num2 < infoArray1->Length); num2++) { System::String __gc * text3 = S""; if (infoArray1[num2]->ParameterType != 0) { text3 = infoArray1[num2]->ParameterType->Name; } builder1->Append(System::String::Concat(((num2 != 0) ? S", " : S""), text3, S" ", infoArray1[num2]->Name)); } builder1->Append(S")"); if (frame1->GetILOffset() != -1) { System::String __gc * text4 = 0; try { text4 = frame1->GetFileName(); } catch (System::Security::SecurityException*) { } if (text4 != 0) { builder1->Append(System::String::Concat(S" in ", text4, S":line ", frame1->GetFileLineNumber().ToString())); } } if (num1 != (trace1->FrameCount - 1)) { builder1->Append(text1); } } return builder1->ToString(); } 

您可以使用Process.GetCurrentProcess()。线程来获取线程

而且我知道我使用托管C ++,但它很容易遵循。 我采用线程的arraylist,因为为了我的目的,我已经将我的线程分类了。 是的,我使用以前编写的堆栈帧代码,因为我当时是MC ++的新手:)

整个文件都在这里 。 这是我前段时间写的暗黑破坏神II僵尸引擎 。

System.Diagnostics中有各种方便的类可以帮助您调试和收集各种跟踪信息,即StackTrace。

有一个不稳定的Process类可用于获取执行线程的数量,但很少有细节。 使用以下代码段:

 Using System.Diagnostics; var threads = Process.GetCurrentProcess().Threads; 

好吧,看了一下之后,似乎捕获所有当前堆栈的最简单方法是通过迷你转储和SOS之类的工具,或者如果你正在运行vista。

祝好运。

如果您需要以编程方式执行此操作(可能在CI过程中需要自动转储),则可以使用此答案中的信息来解决其他问题。

基本上,使用CLR MD附加到您自己的进程:

 using Microsoft.Diagnostics.Runtime; using (DataTarget target = DataTarget.AttachToProcess( Process.GetCurrentProcess().Id, 5000, AttachFlag.Passive)) { ClrRuntime runtime = target.ClrVersions.First().CreateRuntime(); foreach (ClrThread thread in runtime.Threads) { IList stackFrames = thread.StackTrace; PrintStackTrace(stackFrames); } } 

这里PrintStackTrace留给读者练习。