- Home /
Getting a stack trace of a specific thread
Hello, I'm trying to figure out a way to debug infinite loops. What I have so far is an independent thread which uses some Native code to get Keyboard input, and then would - in theory - interject into the main thread and get a stack trace.
Here's the code:
public class InfiniteLoopDebugger : MonoBehaviour
{
bool mRunning = false;
static Thread mMainThread = null;
bool mKeyState = false;
[DllImport("user32.dll")]
static extern ushort GetAsyncKeyState(int vKey);
public static bool IsKeyPushedDown(int keyCode) //Uses Windows.From.Keys
{
return 0 != (GetAsyncKeyState(keyCode) & 0x8000);
}
StackTrace GetStackTrace(Thread targetThread)
{
StackTrace stackTrace = null;
var ready = new ManualResetEvent(false);
new Thread(() =>
{
// Backstop to release thread in case of deadlock:
ready.Set();
Thread.Sleep(200);
try { targetThread.Resume(); }
catch { }
}).Start();
ready.WaitOne(10000);
targetThread.Suspend();
try { stackTrace = new StackTrace(targetThread, true); }
catch (System.Exception e) { UnityEngine.Debug.Log(e); }
finally
{
try { targetThread.Resume(); }
catch { stackTrace = null; /* Deadlock */ }
}
return stackTrace;
}
void startDebugging()
{
mRunning = true;
while (mRunning)
{
if (!mKeyState && IsKeyPushedDown(19)) //Pause / Break
{
UnityEngine.Debug.Log("Key pressed");
if (mMainThread != null)
{
StackTrace stackTrack = GetStackTrace(mMainThread);
if (stackTrack == null)
{
UnityEngine.Debug.LogError("Attempt to get stacktrace deadlocked. Please try again.");
}
else
{
UnityEngine.Debug.Log(stackTrack.ToString());
}
}
else
{
UnityEngine.Debug.LogError("Main thread not set.");
}
}
mKeyState = IsKeyPushedDown(19);
Thread.Sleep(10);
}
}
public void endDebugger()
{
mRunning = false;
}
public void Start()
{
mMainThread = Thread.CurrentThread;
Thread executionThread = new Thread(startDebugging);
executionThread.Start();
}
}
The GetStackTrace method I used comes from this thread: http://stackoverflow.com/questions/285031/how-to-get-non-current-threads-stacktrace
The issue, however, is that this method apparently isn't implemented in Mono. I get a not implemented error: System.NotImplementedException: The requested feature is not implemented. at System.Diagnostics.StackTrace..ctor (System.Threading.Thread targetThread, Boolean needFileInfo) [0x00000] in :0
Does anyone have any ideas for a work around? The goal here is to be able to see where the main thread is in it's execution when you hit Break.
Things I've tried but failed at: Forcing mono to use the .NET version of StackTrace Using Thread.Abort to kill the main thread, and then catch the exception to get a stack trace (Thread abort does kill the main thread, however)
Answer by whydoidoit · Dec 02, 2012 at 11:44 AM
Well I find my infinite loops using MonoDevelop and just hit Pause as described in this Unity Gems article.
Hey, thanks for your suggestion. Unfortunately, time when I get an infinite loop situation, the $$anonymous$$ono debugger automatically detaches from the game process... I don't know if this is common, or something wonky about my particular game.. but regardless it renders the $$anonymous$$ono debugger ineffective :/
Interesting - as I say in that article - that's what I do to find them and I've never had it fail.
$$anonymous$$onoDevelop detaching sounds like something very ter$$anonymous$$al!
In fact I can ATTACH at that point!
Alright, well, that's good to know!
There must be something about my code that makes $$anonymous$$onoDevelop hate it. Next time I have an issue, I'll look into it!
Thanks for the info!
Yeah that does happen too actually - that or it locks everything up on stepping the code - especially with 4. So it locks in an "unknown" place - hmmm. Hard to work out how to debug that :S Sounds like you are going to be adding a lot of Log statements, what a nightmare.
Your answer
Follow this Question
Related Questions
How could I optimize this terrain generation? 1 Answer
MonoDevelop unable to attach to Android device 0 Answers
The requested item has been unloaded 9 Answers
Access to Networking Bandwidth Statistics 0 Answers
What are Asserts for? 1 Answer