如何使用MONO进行线程转储?

时间:2010-01-14 20:33:09

标签: .net mono stack-trace thread-dump

如何在使用MONO运行的挂起应用程序中显示线程(stacktraces)?

我知道我可以使用Managed Stack Explorer(MSE)在.NET中完成它。因为应用程序仅与MONO挂起,我需要使用MONO。

或者还有其他想法如何找到悬挂的地方?

2 个答案:

答案 0 :(得分:22)

假设您使用的是Linux / Unix,而不是Windows,请向您的程序发送SIGQUIT信号。这可以通过

完成
kill -QUIT $PID

其中$ PID是您程序的pid。 Mono然后将所有线程的堆栈跟踪转储到stdout。请注意,虽然此后进程仍在运行,但您不应期望它保持可用/稳定。

有关背景信息,请参阅http://en.wikipedia.org/wiki/SIGQUIT

注意:在运行kill命令的终端窗口中,线程转储将打印出来。它将出现在单声道进程的stderr中。

答案 1 :(得分:1)

It is also possible to quickly grab a managed stack trace using GDB. Execute gdb; use sudo if you're not root or debugging a process owned by your user.

Execute this script which I got from the debugging Mono page on mono-project.org:

handle SIGXCPU SIG33 SIG35 SIGPWR nostop noprint

define mono_stack
 set $mono_thread = mono_thread_current ()
 if ($mono_thread == 0x00)
   printf "No mono thread associated with this thread\n"
 else
   set $ucp = malloc (sizeof (ucontext_t))
   call (void) getcontext ($ucp)
   call (void) mono_print_thread_dump ($ucp)
   call (void) free ($ucp)
 end
end

If you like you can drop these commands in your ~/.gdbinit so you don't have to copy and paste all the time.

Now attach to your PID:

attach 12345

Note that the whole process is now paused so if you're doing this in production it is advisable to script this so it's as fast as possible.

To get your stack trace, execute mono_stack as defined above. Note that you won't see the output in gdb but in stdout. If you run your process with upstart you can just edit the upstart job to use console log to log it to /var/log/upstart.

You may be interested in another thread than your main thread however. To do so, execute info threads to get your thread list and thread 2 to switch to thread #2. For more information on thread debugging, see debugging programs with multiple threads in the GDB docs.

Once you're done, execute quit, and your program will continue working.

相关问题