Find the parent thread of a child thread

Problem: The issue is I have to find the parent of a thread which has sometimes paniced.

Description:
When our application starts 70 threads gets created which is as per the requirement.
Later on as per the needs these 70 threads can create more threads.
So apart from these 70 threads a large number of other threads gets created(from these 70 threads) and destroyed randomly.
So imagine there is a tree of threads.
There is an issue coming up in one such randomly created thread.
On debugging the code we saw that the issue will be due to a value set by its parent thread.
So we have to find the parent thread of this erroneus thread.

We are not able to get the parent thread id of this thread?

Will anybody let us know how to find the parent of such randomly created threads using especially windbg/visual studio?

I don’t *think* there’s anything that tracks this (in kernel mode at
least…And I don’t see anything in the TEB that looks promising). Can’t you
just change the app to record this information someplace so that it can be
extracted later?

-scott


Scott Noone
Consulting Associate and Chief System Problem Analyst
OSR Open Systems Resources, Inc.
http://www.osronline.com

Hope to see you at the next OSR kernel debugging class February 14th in
Columbia, MD!

wrote in message news:xxxxx@windbg…

Problem: The issue is I have to find the parent of a thread which has
sometimes paniced.

Description:
When our application starts 70 threads gets created which is as per the
requirement.
Later on as per the needs these 70 threads can create more threads.
So apart from these 70 threads a large number of other threads gets
created(from these 70 threads) and destroyed randomly.
So imagine there is a tree of threads.
There is an issue coming up in one such randomly created thread.
On debugging the code we saw that the issue will be due to a value set by
its parent thread.
So we have to find the parent thread of this erroneus thread.

We are not able to get the parent thread id of this thread?

Will anybody let us know how to find the parent of such randomly created
threads using especially windbg/visual studio?

Thanks Scott for the reply

The Platform is Windows.
Lanuage is C++.
Since its the client code and so we have to debug and fix it as of now and not to make any changes in the design of client code.

xxxxx@gmail.com wrote:

The Platform is Windows.
Lanuage is C++.
Since its the client code and so we have to debug and fix it as of now and not to make any changes in the design of client code.

How do you possibly plan to “fix it” without modifying the code?

As you have specified the problem, there is no solution. The system
doesn’t track the ancestry of threads. Hence, if you need to know the
ancestry, you need to track it yourself.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Thanks Tim for the inputs.
So I understand that I will have to make my own provision of saving the child thread id either in parent or passing parent id to child as parameters.

But is not there any way using debuggers to find the parent like we use in process using ppid.

xxxxx@gmail.com wrote:

But is not there any way using debuggers to find the parent like we use in process using ppid.

Two of us have now told you that the operating system does not keep
track of this information. There is no place to look it up. The system
does not KNOW who the parent was, because the parent/child relationship
for threads is not interesting.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Thanks Tim

When I teach multithreaded programming, I often refer to “parent” and
“child” threads, and point out that this is a concept that a *programmer*
has about the relationship of threads. But the OS has no such concept. The
concept is no more meaningful than thinking that 65 means ‘A’ or 0x07 means
“SOMESTATUS | OTHERSTATUS | MORESTATUS”. We may think in terms of the
symbolic names; the machine doesn’t. We may think of the concept of
“parent” and “child” threads, but the OS does not.
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Tim Roberts
Sent: Thursday, January 06, 2011 4:20 PM
To: Kernel Debugging Interest List
Subject: Re: [windbg] Find the parent thread of a child thread

xxxxx@gmail.com wrote:

But is not there any way using debuggers to find the parent like we use in
process using ppid.

Two of us have now told you that the operating system does not keep track of
this information. There is no place to look it up. The system does not
KNOW who the parent was, because the parent/child relationship for threads
is not interesting.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.


WINDBG is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Whilel this is neither driver nor debugger-related, I feel I have to jump in
here.

First, are you programming the apps in C or C++/MFC? Note that I strongly
advise against writing native-code GUI apps in raw C code as being not
cost-effective (you spend too much time solving trivia that MFC already
handles cleanly).

Second, it is not clear what value the thread ID has. I regularly tell
people that “The only useful thing I have ever found for thread ID is
printing it in debug trace messages”. Yes, you need it to do
PostThreadMessage, but you can’t reliably do PostThreadMessage to the main
GUI thread [RTFM]. In MFC, I’d use the CWinThread* object when I needed
thread information at all; more likely, I’d create an architecture for my
program that avoided ever needed a thread ID. So one of the first things I
question when one needs to implement parent/child relationships is why a
thread ID is required at all (I just had a long discussion on another NG
with someone who felt compelled to keep an array of thread IDs; I’ve never
found a reason to keep such a collection of such information in decades of
multithreaded Windows programming).
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Thursday, January 06, 2011 4:02 PM
To: Kernel Debugging Interest List
Subject: RE:[windbg] Find the parent thread of a child thread

Thanks Tim for the inputs.
So I understand that I will have to make my own provision of saving the
child thread id either in parent or passing parent id to child as
parameters.


WINDBG is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.

I don’t see how you could possibly “fix” it without making changes since
there is no possible way to get this information. Code this bad not only
has to be changed, it MUST be changed.
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Thursday, January 06, 2011 3:21 PM
To: Kernel Debugging Interest List
Subject: RE:[windbg] Find the parent thread of a child thread

The Platform is Windows.
Lanuage is C++.
Since its the client code and so we have to debug and fix it as of now and
not to make any changes in the design of client code.


WINDBG is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.

See below…

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Thursday, January 06, 2011 2:38 PM
To: Kernel Debugging Interest List
Subject: [windbg] Find the parent thread of a child thread

Problem: The issue is I have to find the parent of a thread which has
sometimes paniced.

*****
I was not aware that a thread can panic, but it sounds more like it is need
of psychological counseling than programming help. There are drugs that
deal with panic attacks, but I don’t know how to administer them to threads.

And why do you have to find the parent? And do you need to do this just
while you are debugging, or do you need to do it in code executing in the
thread? And why should the parent matter?
*****

Description:
When our application starts 70 threads gets created which is as per the
requirement.
Later on as per the needs these 70 threads can create more threads.
So apart from these 70 threads a large number of other threads gets
created(from these 70 threads) and destroyed randomly.
So imagine there is a tree of threads.

****
No, you have a *concept* of a tree of threads, but no such entity exists.
Any notion of a tree is like thinking of a tree as a data structure; there
are only bits, nothing more, and if I want to call some of those bits “left
pointer” and “right pointer” I’m free to do so, but the computer has no idea
that these bits represent a tree structure.
****

There is an issue coming up in one such randomly created thread.

****
This is too vague to be meaningful. I could not tell you how to respond to
“an issue”.
****

On debugging the code we saw that the issue will be due to a value set by
its parent thread.
So we have to find the parent thread of this erroneus thread.

We are not able to get the parent thread id of this thread?

****
That’s because the concept does not exist, and unless you program it in, it
will never exist.

It is the responsibility of each thread that might care to know its logical
parent, and this is a requirement of the programmer to provide this
information to the thread. This information is not generally available.
But it is not clear to me why a thread should care about its logical parent

If you are programming in C++, and using worker threads, you would pass in a
base structure to every worker thread, and passing parameters into other
threads would require using classes derived from this base class, so every
thread could find the “parent” thread information by casting that parameter
to this base parameter class. If you are using MFC and UI threads, it is
only moderately more complex.

This app sounds thread-heavy and I question the basic design.

Also, I have no idea what problem you are trying to solve! You are asking a
typical low-level “how do I find out X?” style question instead of saying
why discovering X has any meaning.

There are much deeper questions about what constitutes failure of a thread
and what you expect to do about it.
****

Will anybody let us know how to find the parent of such randomly created
threads using especially windbg/visual studio?

****
It will be whereever you put it, and using a debugger, you would look
whereever you put it. Using my suggestion of using derived classes, you
would look at the thread parameter to the top-level thread function, which
is a void*, cast it to the base type of your parameter block, and look at
the parent ID field you set in it.

class ThreadParam {
public:
DWORD ThreadID;
};

class FirstLevelThreadParam : public ThreadParam {
public:
int something;
DWORD somethingElse;
LPCTSTR somestring;
};

class SecondLevelThreadParam : public ThreadParam {
public:
double value;
int * retval;
};

Etc. Note that if you cast the parameter to ThreadParam, you will always
see the ThreadID field.

But I seriously doubt that thread IDs have much value.
****


WINDBG is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.