2

Is there any way I can make gdb halt every time when the pthread being executed changes?

Say I have two POSIX threads (thread A and thread B) running and I want gdb to halt when thread A is being suspended and thread B continues execution (and the other way around).

MBober
  • 1,095
  • 9
  • 25

1 Answers1

4

I want gdb to halt when thread A is being suspended and thread B continues execution

Your question assumes a single-core computer, which are exceedingly rare nowadays.

If you have a multi-core CPU, then both A and B will execute simultaneously, and your question makes no sense.

Even assuming that you are executing on a single-core CPU, the feature you are asking for would (I expect) be mostly useless: a typical multi-threaded program would switch between two threads for tens of thousands of times before reaching any "interesting" code. Surely you don't want to continue 10,000 times?

When debugging multi-threaded code, usually you want to assert that various invariants hold throughout the program execution. Setting breakpoints and single-stepping rarely helps in multi-threaded debugging.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • I use the QNX operating system on a single processor in an embedded system. I might be wrong here, but aren't different threads of the same process run consecutively on the same processor - even on a multi-core CPU? – MBober Nov 03 '11 at 14:36
  • Yes, you are wrong. The whole point of multithreading is to make it possible for a single application use more than one core in a multi-core machine *simultaneously*. – Employed Russian Nov 03 '11 at 14:50
  • Problem is that my program crashes the whole OS so that I cannot be sure if all debug messages are delivered before the crash. Everything works fine when I step through the program. This is why I guess this is a timing issue. So I need to figure out which Thread runs when to trace the problem down. I noticed that gdb stops on SIG32 which is send to a thread when its priority has been changed. I was hoping that there is an equivalent signal on resuming a thread on which gdb may halt. – MBober Nov 03 '11 at 15:33