-2

I am trying to add instrumentation into my code that will print out something like

'Thread 1 forks Thread 2'

Any suggestions on how I can achieve this?

arunxls
  • 124
  • 1
  • 9
  • 1
    It's questionable whether it shows enough research effort but it's quite clear what's being asked and answerable. – P.P Nov 19 '14 at 00:20
  • Are you doing it in LLVM IR? – coder hacker Nov 19 '14 at 01:03
  • Give part of code where you are trying to print it? – coder hacker Nov 19 '14 at 01:12
  • I want to generate a complete trace of the program execution. I intercepted calls to pthread_create in a opt phase that I wrote. Basically, I just go through all the instructions and instrument lock/unlock, load/store etc – arunxls Nov 19 '14 at 01:23

1 Answers1

3

Terminology correction: one thread may create another thread, not fork, which is usually used to mention one process forking another.

No, a thread has no way to get another thread's identifier. On Linux, you can check if gettid() == getpid() to find if it's the main thread. Solaris has thr_main() to identify if the caller is main thread or not. FreeBSD has pthread_main_np() for the same purpose.

But there's no way to identify parent-child relationship between any threads. Any thread can create more threads. You'll have to use pass the thread identifiers around when creating threads or use global data structure to maintain this information.

P.P
  • 117,907
  • 20
  • 175
  • 238