OpenSim/Framework/Util.cs
/// <summary>
/// Return the stack trace of a different thread.
/// </summary>
/// <remarks>
/// This is complicated because the thread needs to be paused in order to get its stack
/// trace. And pausing another thread can cause a deadlock. This method attempts to
/// avoid deadlock by using a short timeout (200ms), after which it gives up and
/// returns 'null' instead of the stack trace.
/// 
/// Take from: http://stackoverflow.com/a/14935378
/// 
/// WARNING: this doesn't work in Mono. See https://bugzilla.novell.com/show_bug.cgi?id=571691
/// 
/// </remarks>
/// <returns>The stack trace, or null if failed to get it</returns>
Return the stack trace of a different thread.
Return the stack trace of a different thread .
private static StackTrace GetStackTrace(Thread targetThread)
        {
            if (IsPlatformMono)
            {
                return null;
            }
            ManualResetEventSlim fallbackThreadReady = new ManualResetEventSlim();
            ManualResetEventSlim exitedSafely = new ManualResetEventSlim();
            try
            {
                new Thread(delegate()
                {
                    fallbackThreadReady.Set();
                    while (!exitedSafely.Wait(200))
                    {
                        try
                        {
                            targetThread.Resume();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }).Start();
                fallbackThreadReady.Wait();
                targetThread.Suspend();
                StackTrace trace = null;
                try
                {
                    trace = new StackTrace(targetThread, true);
                }
                catch (ThreadStateException)
                {
                }
                try
                {
                    targetThread.Resume();
                }
                catch (ThreadStateException)
                {
                }
                return trace;
            }
            finally
            {
                exitedSafely.Set();
            }
        }
private static StackTrace GetStackTrace ( Thread targetThread ) { if ( IsPlatformMono ) { return null ; } ManualResetEventSlim fallbackThreadReady = new ManualResetEventSlim ( ) ; ManualResetEventSlim exitedSafely = new ManualResetEventSlim ( ) ; try { new Thread ( delegate ( ) { fallbackThreadReady . Set ( ) ; while ( ! exitedSafely . Wait ( 200 ) ) { try { targetThread . Resume ( ) ; } catch ( Exception ) { } } } ) . Start ( ) ; fallbackThreadReady . Wait ( ) ; targetThread . Suspend ( ) ; StackTrace trace = null ; try { trace = new StackTrace ( targetThread , true ) ; } catch ( ThreadStateException ) { } try { targetThread . Resume ( ) ; } catch ( ThreadStateException ) { } return trace ; } finally { exitedSafely . Set ( ) ; } }
Return the stack trace of a different thread.
Return the stack trace of a different thread .
// This doesn't work in Mono // Whatever happens, do never stop to resume the main-thread regularly until the main-thread has exited safely. // From here, you have about 200ms to get the stack-trace //failed to get stack trace, since the fallback-thread resumed the thread //possible reasons: //1.) This thread was just too slow //2.) A deadlock ocurred //Automatic retry seems too risky here, so just return null. // Thread is running again already // Signal the fallack-thread to stop
The stack trace, or null if failed to get it
The stack trace or null if failed to get it
targetThread
Thread
remarks
This is complicated because the thread needs to be paused in order to get its stack
trace. And pausing another thread can cause a deadlock. This method attempts to
avoid deadlock by using a short timeout (200ms), after which it gives up and
returns 'null' instead of the stack trace.



this doesn't work in Mono.
This is complicated because the thread needs to be paused in order to get its stack trace . And pausing another thread can cause a deadlock . This method attempts to avoid deadlock by using a short timeout ( 200ms ) after which it gives up and returns ' null ' instead of the stack trace . this doesn ' t work in Mono .