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

Thread Debugging with the Thread Information Block

, 5 Jun 2002
Rate this:
Please Sign up or sign in to vote.
How to use the Thread Information Block to store descriptive information about the thread

Introduction

I have found one of most difficult things about thread programming is debugging, because you must constantly keep track of which thread is active, and what thread is supposed to be doing. While I was looking in MSDN for something else, I came across an article by John Robbins. Toward very end of article, Robbins shows how to store descriptive text in Thread Information Block (TIB) (which Matt Pietrek has described), and then how to display this information in VC debugger using a watch expression.

IMPORTANT NOTE:   I am not claiming authorship for idea of using TIB to store thread description. Please see citations listed below for original articles by Pietrek and Robbins. I have merely repackaged TIB routines into handy header file, and would like to bring this to attention of others here at CodeProject, because I believe this technique is so obscure that few may be aware of it.

Thread Information Block

Matt Pietrek documented fields of TIB in his May 1996 article in MSJ. There is TIB for each thread running in Win32 system, and in all Intel-based Win32 implementations, FS register points to TIB. This leads to following function to obtain TIB address of active thread:
static tagXTIB * GetTIB()
{
	tagXTIB * pTib;

	__asm
	{
		MOV  EAX , FS:[18h]
		MOV  pTib , EAX
	}
	return pTib;
}
Among TIB structure fields that are common to all Win32 implementations, there is one at 14H called pvArbitrary, which is available for applications to use however they want. This is field that XTib will use to store pointer to descriptive string.

How To Use

First thing to do is to include header file XTib.h. Next, call SetThreadName() from thread function, with string that will uniquely identify thread. This string can be generated from thread function's lpParameter (thread function argument) - for example, lpParameter could be some integer value, or pointer to some string. Final step is to set watch expression in debugger. To do this, set breakpoint (for example, somewhere in thread function). When debugger breaks, enter this expression in watch window:

(char*)(DW(@TIB+0x14))
Now debugger will automatically display string pointed to by pvArbitrary field in TIB. This saves you from having to go to Debug | Threads to find out what thread you are in.

For applications running under NT, another useful watch expression is

DW(@tib+0x24)
This will display the thread ID, which is stored at 24H in the TIB on NT 4, Win2000, and XP.

XTib Demo

Demo project provides sample app that creates five threads and lets you see what is displayed in watch window. Here is what watch window will display for thread 1:

      XTib demo screenshot

Acknowledgments

XTib code is based on:

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

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

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
NewsThis may cause trouble on X64 machines. Pinmemberalunw30-Sep-09 6:57 
GeneralRe: This may cause trouble on X64 machines. PinmemberMaciej Dziurosz20-Aug-10 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 6 Jun 2002
Article Copyright 2002 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid