Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Show Threads in Source - Windbg Way

, 26 Sep 2012
Rate this:
Please Sign up or sign in to vote.
How to show threads in the source - Windbg way

Introduction

A couple of months ago, I wrote a tip on The Code Project about Show Thread in Source feature in Visual Studio. This trick can be found here. In this tip today, I will demonstrate how the similar information could be achieved when either doing a live debugging session or dump file analysis using native debuggers like WinDbg. If you are new to this type of debugging, please visit my blog or watch one of my two videos about it here and here (expand Ottawa or Montreal).

The sample code is the same as the one I used in that previous tip.

class Program
    {
        static object sync1 = new object();
        static object sync2 = new object();
 
        static void Main(string[] args)
        {
 
            Thread[] workers = new Thread[10];
            for (int i = 0; i < 10; i++)
            {
                if (i % 2 == 0)
                {
                    workers[i] = new Thread(new ThreadStart(Method1));
                    workers[i].Start();
                }
                else
                {
                    workers[i] = new Thread(new ThreadStart(Method2));
                    workers[i].Start();
                }
            }
 
            Console.WriteLine("Thread Creation Completed.");
            for (int i = 0; i < 10; i++)
            {
                workers[i].Join();
            }
 
            Console.WriteLine("Done");
 
        }
 
        private static void Method1()
        {
            Console.WriteLine("Method1 called by 
            	ThreadId = {0}", Thread.CurrentThread.ManagedThreadId);
            lock (sync1)
            {
                Thread.Sleep(10000);
            }
        }
 
        private static void Method2()
        {
            Console.WriteLine("Method2 called by 
            	ThreadId = {0}", Thread.CurrentThread.ManagedThreadId);
            lock (sync2)
            {
                Thread.Sleep(30000);
            }
        }
    }

The first step is load sos. Since I am using .NET 4.0, I used load by proxy using clr DLL. Next, let's run !threads command to see managed threads in the process. As you can see from command output shown , Managed thread Ids 4 and 5 own a lock.

You can also verify owning threads info by running !syncblk command as shown below. However this command output does not display the threads that are waiting for these two threads.

Windbg Extension Psscor4 comes to help here. Let's load it first and run the !syncblk command now. As you can see in the figure below, the output of !syncblk command now includes thread Ids for threads that are waiting on these locks. 

Of course, you can always view threads call-stacks to inspects what those threads are doing.

Click to enlarge image

I hope you find this tip helpful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kamran Bilgrami
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.
 
He blogs regularly at http://WindowsDebugging.Wordpress.com
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 27 Sep 2012
Article Copyright 2012 by Kamran Bilgrami
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid