Friday, November 4, 2011

Using Google's CoreDumper library

The Google team has published a library called CoreDumper for generating process core dumps programatically. This can be useful for post-analysis in environments where core files are not or cannot be generated/saved by the system (ulimit restrictions, etc).

At my work we've incorporated the CoreDumper library into our Production code, and are using it in conjunction with our exception handling to generate core files when conditions warrant.

One nice feature of this library is the ability to generate a compressed core file, thus significantly reducing disk space consumed by the core.

An example of using this feature follows.


#include <limits>
#include "google/coredumper.h"

void generateCore( const std::string &p_filenameFullPath )
{
// Be sure to use a mutex for concurrency (not shown).
// Reasons are discussed here:
// http://code.google.com/p/google-coredumper/wiki/WriteCoreDump#ERRORS

// The following will create a core file using the specified filename
// and compressed with the gzip compression algorithm.
// Here we're not enforcing limits on the core file size, so if using C++,
// get the limit of size_t as specified. Else replace with SIZE_MAX.
const int iResult =
WriteCompressedCoreDump( p_filenameFullPath.c_str(),
std::numeric_limits<std::size_t>::max(),
COREDUMPER_GZIP_COMPRESSED,
NULL
);

if( 0 == iResult )
{
// replace this call to std::out with a call to your logging system.
std::cout << "generated core: "
<< p_filenameFullPath.c_str()
<< COREDUMPER_GZIP_COMPRESSED->suffix
<< std::endl;
}
else
{
const unsigned int errLen = 128;
char error[ errLen ] = { '\0' };
strerror_r( errno, error, errLen );

// replace this call to std::cerr with a call to your logging system.
std::cerr << "failed to generate core: "
<< error
<< std::endl;
}
}

2 comments:

short said...

Thanks! This blog post really helped me out. Kudos

Vince said...

when this function will be called?