2

I wonder how to debug multi-threaded programs effectively.

What I've done so far:

  1. I read some gdb reference, but all of them talk little about multi-thread debuging.
  2. I used gdb to debug my c++ programs.
  3. linux thread reference

What's your tricks to share?

Skills

  1. 1> Understand the code structure well. 2> Debug thread by thread. 3> In terms of exact time-stamps implemented.

PS: The approch still cannot solve my problem.

luoluo
  • 5,353
  • 3
  • 30
  • 41
  • 4
    [This](http://www.youtube.com/watch?v=xTmAknUbpB0&feature=share&list=PLckFgM6dUP2hc4iy-IdKFtqR9TeZWMPjm&index=11) may be of help to you. – Borgleader Feb 26 '14 at 05:30
  • @Borgleader Can't open the url.. Thank u on the same. – luoluo Feb 26 '14 at 05:33
  • Avoid the problem completely by doing it right in the first place. I haven't had a multi-threading bug in decades. – user207421 Feb 26 '14 at 07:33
  • @EJP I debug the program to see what in hand is going on step by step. I want to make a clear picture of the thread modle. – luoluo Feb 26 '14 at 07:45
  • @luoluo I only debug programs that have bugs in them. If you don't understand your own code, write code that you do understand. – user207421 Feb 26 '14 at 08:51
  • I understand it, but something going wrong. Then step by step maybe is a good way to figure out what's goign wrong. – luoluo Feb 26 '14 at 08:57
  • Do you expect timestamps to provide absolute truths? Hint: things may become visible to different CPUs at different times. – ninjalj Feb 26 '14 at 10:07
  • @ninjalj We don't need the same truth. But on certain cpu, we can solve the problem. which is the main point to me. – luoluo Feb 26 '14 at 10:12
  • Possibly related: http://stackoverflow.com/questions/981011/c-programming-debugging-with-pthreads – Brendan Feb 27 '14 at 02:17
  • @Brendan helps a lot. thx. – luoluo Feb 27 '14 at 02:33

2 Answers2

0
  1. Disable watchdog
  2. Assign each thread a unique id/name. This way you may get thread id inside any function and know for sure what thread executes it.
  3. Learn how to use Threads Window in Visual Studio: http://msdn.microsoft.com/en-us/library/w15yf86f.aspx
Evgeny Sobolev
  • 515
  • 4
  • 13
0

Using the debugger to understand a program might work well for single threaded systems.

It definitly doesn't work (well) for problems involving more then one thread. This is per design, as human nature is single threaded.

So to get into a multithreaded system:

  1. Identify all threads and how they depend on another by reading and understanding the sources.
  2. Debug each thread on its own. This might imply disabling or synchronising other threads.
  3. Add detailed logging in terms of exact time-stamps implemented to not add synchronisation to the threads

This approach follows the paradigm of doing one thing at a time.

alk
  • 69,737
  • 10
  • 105
  • 255