Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++
Article

Thread Debugging with the Thread Information Block

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
5 Jun 2002CPOL3 min read 93.5K   1K   35   9
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)


Written By
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. Pin
alunw30-Sep-09 6:57
alunw30-Sep-09 6:57 
GeneralRe: This may cause trouble on X64 machines. Pin
Maciej Dziurosz20-Aug-10 2:19
Maciej Dziurosz20-Aug-10 2:19 
GeneralWe’re sorry, but there is no Microsoft.com Web page that matches your entry... Pin
Indivara1-Mar-07 18:23
professionalIndivara1-Mar-07 18:23 
The links in the acknowledgments seem to have changed. New links are -
* "Under The Hood[^]", May 1996 Microsoft Systems Journal, by Matt Pietrek.
* "Bugslayer[^]", January 2000 Microsoft Systems Journal, by John Robbins.


Rose | [Rose]



My Favorite VS Add-on - ResOrg[^]

AnswerRe: We’re sorry, but there is no Microsoft.com Web page that matches your entry... Pin
Hans Dietrich19-Apr-11 23:50
mentorHans Dietrich19-Apr-11 23:50 
QuestionCan it work on win95? Pin
tigra_woo25-Feb-04 20:48
tigra_woo25-Feb-04 20:48 
GeneralSetThreadName... Pin
Jochen Kalmbach [MVP VC++]7-Nov-02 2:36
Jochen Kalmbach [MVP VC++]7-Nov-02 2:36 
GeneralRe: Clickety! Pin
Daniel Turini7-Nov-02 2:44
Daniel Turini7-Nov-02 2:44 
General#pragma pack() Pin
WREY22-Jun-02 10:18
WREY22-Jun-02 10:18 
GeneralRe: #pragma pack() Pin
Hans Dietrich23-Jun-02 17:41
mentorHans Dietrich23-Jun-02 17:41 

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

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