View A Backtrace For All Threads With GDB

Debugging multi-threaded programs can be really tricky. GDB however, will always do its best to rescue you from whatever horrible bug you’re currently looking at. Today I wanted to show you a really nice command for viewing all the threads in your program.

This particular command is especially helpful if you ever need to diagnose a deadlock, because it will give you a complete overview of the entire system and allow you to see which locks are currently being requested.

I’m going to use the C++11 threads program from my thread tutorial to illustrate, with one minor modification – I’ve added a sleep command in the thread subroutine. The only reason I’ve done this is because it keeps my threads alive for long enough to show them all to you in the debugger. On a commercial multi-threaded system, your threads will be doing something most of the time, so my sleep command is essentially simulating my threads going off and doing important jobs 😉

Read More »

Log GDB Output To A File

Sometimes, especially when you’re dealing with a long stack trace, or a multi-threaded stack trace, trying to view the debug output from GDB in a terminal window can be a little awkward.

Did you know that you can log specific output (or even the entire session) from within GDB to a text file?

All you have to do is type:

set logging on

at the GDB prompt.

GDB saves all output from this point in a text file called gdb.txt that resides in the directory in which you are running GDB (usually the same directory as your executable).

When you’re done you can type:

set logging off

And GDB will stop recording output.

Note that you can turn logging on and off several times and GDB will concatenate output to the gdb.txt file, so you don’t have to worry about overwriting what you’ve already got there.

If you want to record multiple files in a session you can change the filename with:

set logging file myfile.txt

Here’s a little screenshot:


C++11 Auto Keyword

This is just a short post to illustrate the use of the auto keyword. It’s something really simple from the latest standard that you can use in your programs and it will save you typing time :-) .

So, the auto keyword has always been around, but it was pretty much redundant. ‘auto’ was the default storage duration specifier of any variable that you declared, even if you didn’t state it as such. That means that it would be allocated space at the start of the enclosing block of code, and deallocated space at the end.

To put it into context, if you didn’t use auto, you’d use extern or static. It’s a way to tell the compiler how long you want the variable available.

Read More »

C++11 Threads Don’t Work in Eclipse (Luna)

If you want to use Eclipse to write programs that use the C++11 thread class, you need to make some adjustments to enable everything to run correctly.

Without these, you’ll run into all sorts of strange errors.

There are three steps to getting your program to run perfectly and here’s what you need to do:

Read More »