Blogs

Blogs
Browse in : All > Blogs

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: SetThreadName for Windows native programs

Author: Roger M Orr

Date: 21 April 2007 21:23:33 +01:00 or Sat, 21 April 2007 21:23:33 +01:00

Summary: [21-04-2007] How to name your threads when debugging C++ code on Windows

Body: When I gave my presentation on debugging at ACCU I had several people asking me about how to name threads when debugging under Windows.

I originally found the technique described in a presentation about Visual Studio but it has since surfaced on MSDN as well -
http://msdn2.microsoft.com/en-us/library/xcb2z8hs(vs.71).aspx

The method is to generate a special runtime exception which allows the program to pass the name for the thread to the debugger.

Note that this only works with some windows debuggers, and obviously won't work if you attach the debugger to the program after the threads have started up.

I find this very useful when debugging programs with a lot of threads - I hope you will too!
Does anyone know similar techniques for other systems - gdb on Linux for example?


/**
 * SetThreadName
 *
 * Provide a text name for threads in the debugger.
 * Usage: SetThreadName (-1, "MainThread");
 */
void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName )
{
   typedef struct tagTHREADNAME_INFO
   {
      DWORD dwType; // must be 0x1000
      LPCSTR szName; // pointer to name (in user addr space)
      DWORD dwThreadID; // thread ID (-1=caller thread)
      DWORD dwFlags; // reserved for future use, must be zero
   } THREADNAME_INFO;

   THREADNAME_INFO info;
   info.dwType = 0x1000;
   info.szName = szThreadName;
   info.dwThreadID = dwThreadID;
   info.dwFlags = 0;

   __try
   {
      RaiseException( 0x406D1388, 0,
                      sizeof(info)/sizeof(DWORD),
                      (DWORD*)&info );
   }
   __except(EXCEPTION_CONTINUE_EXECUTION)
   {
   }
}


Notes: 

More fields may be available via dynamicdata ..