How do I make a thread dump with MONO?

后端 未结 2 476
走了就别回头了
走了就别回头了 2021-01-01 15:11

How can I show the threads (stacktraces) in a hanging application that run with MONO?

I know that I can do it in .NET with the Managed Stack Explorer (MSE). Because

相关标签:
2条回答
  • 2021-01-01 15:34

    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.

    0 讨论(0)
  • 2021-01-01 15:38

    Assuming you're on Linux/Unix, not Windows, send a SIGQUIT signal to your program. This can be done with

    kill -QUIT $PID
    

    where $PID is the pid of your program. Mono will then dump stack traces of all threads to stdout. Note that although the process stays running after this, you should not expect it to remain usable/stable.

    See http://en.wikipedia.org/wiki/SIGQUIT for some background.

    Note: The thread dump will not print out in the terminal window where you ran the kill command. It will appear in the stderr of the mono process.

    0 讨论(0)
提交回复
热议问题