2007/02/03

Force a core on exit() without modifying the applications code

A nice option to have when using libumem, is the option to core the process right before it exits. Here is how to interpose your own exit() on your application without modifying any of the application's code.

Test application (test.cc):
#include 
#include
using namespace std;
int main(){
char* s = new char[1000];
printf("Done\n");
}

Compile test.cc:
$ cc test.cc -o test

The shared library containing our exit function (exit_interposer.c):
#include 
#include
void exit(int status) {
abort();
}

Compile exit_interposer.c:
cc -o exit_interposer.so -G -Kpic exit_interposer.c

Now run our test program with our new exit function:
$ UMEM_DEBUG=default UMEM_LOGGING=transaction
LD_PRELOAD=libumem.so.1:`pwd`/exit_interposer.so ./test
Done
Abort (core dumped)

Now we can see the leak with libumem:
icing:shrek> mdb core.test.11049.icing
Loading modules: [ libumem.so.1 libc.so.1 ld.so.1 ]
> ::findleaks
CACHE LEAKED BUFCTL CALLER
08082610 1 080af078 libCrun.so.1`__1c2n6FI_pv_+0x33
----------------------------------------
Total 1 buffer, 1152 bytes

This should also work on a daemon which forks processes as long as LD_PRELOAD is set correctly.