Keeping track of thread creation in a process

In some cases, one of our daemons will end up with thousands of threads. Clearly a bug somewhere, but I can't see it in the code. (Admittedly, it's C++ which is a bit alien to me still. Anyway.)

If I wanted to just be notified each time a thread was created in the process, what are some ways to do that? I assume dtrace and lldb have ways to od it, but I'm not quite sure what.

Are these your threads? That is, do the backtraces lead into your code? Or are they created by the system, with the backtraces just system library code (most likely Dispatch)?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Created by us, using the 3rd party libevent library, as well as a few others.

OK. In that case the ‘obvious’ answer here is to set a breakpoint on the thread creation code and watch as threads are getting created. However, I suspect that’s too obvious, in that something is preventing you from doing it. Care to elaborate?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yes, I'm not sure where the thread creation code is.

Traditionally, I might use dtrace to look at fork and vfork, would bsdthread_create be sufficient here? I could possibly set a breakpoint & log command in pthread_create? There doesn't seem to be an Instrument for checking when thread creation and exit happens.

I'm not sure where the thread creation code is.

OK, then how about setting a symbolic breakpoint on pthread_create? It’s likely that’ll catch the ones you care about, and you can use a backtrace to find the caller.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

That's what I was wondering, glad is sounds sane.

Keeping track of thread creation in a process
 
 
Q