views:

387

answers:

8

In java debugging a hung application is easy. You can take the memory dump of the application and use and use eclipse jvm dump analyser to see the status of the threads and where each threads were blocked?

Does something like this exists for C++?

+1  A: 

Some platforms support pstack.

tster
pstack is definitely one option, I often miss out.Thanks for reminding. :)
Devil Jin
A: 

I have not done this, but I think you can use gdb to generate core of your application at the moment it is hung.

You can try debugging this core using the gdb itself and see for yourself what threads are blocked where?

The above is possible in linux platforms. Not sure, if cygwin on windows can be used for the same purpose.

Devil Jin
A: 

Of course, strategically placed cout statements (or other output alternatives) are always an option but often far from ideal.

If compiling with g++, compile with -g and use gdb. You can attach to a running process (and the source code) or simply run the program in the debugger to start with. Then look at the stack.

In Windows, just pause execution of your program and look at the stack.

Steven
A: 
  1. Figure out the critical sections owned by stuck thread #1
  2. Figure out the critical sections owned by stuck thread #2
  3. Decide the proper order of said critical section acquisition
Paul Betts
You might as well tell him to not write bugs in the first place.
Potatoswatter
Sorry if I was short, but that really all there is to it - all of the players in the deadlock are stuck at the scene of the crime! Now if he was more specific about the platform, I could give better advice on how to do #1-2...
Paul Betts
+2  A: 

You can do the exact same thing with C++; force a core dump and look into it after.

Or, if you're using MSVC, you can simply attach the debugger to the application while it's running. Hit "break all" and poke around through the threads.

Crashworks
shall i say that the answer i gave out was more or less correct. MSVC does provides a very easy interface to debug c++ applications.
Devil Jin
A: 

You can use gdb on Linux Systems to see th threads status

msantamaria
+2  A: 

The magic invocation in gdb is:

thread all apply bt

That runs the bt (backtrace) command for all threads. Unless you have completely stripped your program, you should be able to see the names of each function.

This works both for live and post-mortem (i.e. running gdb against a core) debugging.

R Samuel Klatchko
also known as "t a a bt"
Potatoswatter
+1  A: 
Paul Arnold