Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sharsnik · Dec 02, 2012 at 07:28 AM · debuggingthreading

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharsnik · Dec 03, 2012 at 03:17 AM 0
Share

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 :/

avatar image whydoidoit · Dec 03, 2012 at 08:32 AM 0
Share

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!

avatar image sharsnik · Dec 03, 2012 at 08:35 AM 0
Share

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!

avatar image whydoidoit · Dec 03, 2012 at 08:47 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges