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

A C++11 Threads Tutorial

Last week we created a simple program using the pthreads library. This week I’ve “translated” that program into the C++11 threads version, so you can see how it compares.

I say compares, but on linux, using GCC, the C++11 thread library is basically a wrapper for pthreads anyway, so although you are using native C++ commands, they are effectively calling the pthread commands that we saw last week.

So, the first thing to remember, when you are using the new C++11 thread library, is that you have to add a couple of flags to your build in order for it to run properly.

Read more