Recently with the help of Moinak and Kishore I wrote a wrapper library which dumps memory leaks.
Thought of talking about it here.
You can use libumem to identify memory leaks.
I feel libumem is a powerful tool to identify memory leaks.
The tool we developed is a simple one and just only done for learning stuff.
Idea was to write a wrapper library which we can just preload it to the app.
What the idea was to track all memory allocations which are not freed.
atexit, registered reportalloc (function which reports allocation), so on program termination
we get the dump of all memory allocations which are not freed. Not only I report
at the exit, we can also get a dump of all allocations when a particular signal is sent to
the program. You can set an environment variable SIGNAL_USED to a signal which is unused
in the program (How do you find which signals don't have handlers...use psig on the process).
wrapper library will handle that signal, so when ever you send a signal $SIGNAL_USED
to the process it dumps all allocations at that instance.
We wrote wrappers for malloc, calloc, realloc, valloc and free.
How we got stack trace? We got the stack frames and extracted return addresses and
using dladdr to find symbolic information.
Some codesnips of the library is below.
Only writng malloc here as other are almost similar except calloc which has an extra argument.
void *malloc(size_t size) {
void *buf;
/* dlinit will initialize global function pointers which will
* point to the original libc functions.
*/
dlinit();
/* mptr is a function pointer to libc malloc */
buf = (*mptr)(size);
/* We add the data to a hashed list
* what each node in the hashed chain contains
* is the address returned, stack trace, bytes allocated
*/
addnode(buf, size);
return (buf);
}
void dlinit(void) {
/* lock it so that multiple threads in application might not simultaniously
* do a dlopen
*/
pthread_mutex_lock(&mt);
if (dlh == NULL) {
if ( (dlh = dlopen("libc.so", RTLD_NOW|RTLD_PARENT))
== NULL) {
perror("dlopen: ");
pthread_mutex_unlock(&mt);
exit(1);
}
/* get pointers for malloc, calloc,... etc */
mptr = (void *(*)(size_t))dlsym(dlh, "malloc");
cptr = (void *(*)(size_t, size_t))dlsym(dlh, "calloc");
rptr = (void *(*)(void *, size_t))dlsym(dlh, "realloc");
vptr = (void *(*)(size_t))dlsym(dlh, "valloc");
malignptr = (void *(*)(size_t, size_t))dlsym(dlh, "memalign");
fptr = (void (*)(void *))dlsym(dlh, "free");
/* register signal handler*/
sigact.sa_handler = sighandler;
if (sigaction(SIGNAL_USED, &sigact, NULL)) {
perror("failed in registering signal");
}
atexit(report);
}
pthread_mutex_unlock(&mt);
}
...
...
...
void sighandler(void) {
pthread_t thr;
/* create a new thread and dump current allocations which are not freed. */
pthread_create(&thr, NULL, reportallocs, NULL);
pthread_detach(thr);
}
If any institution is interested, in any courses in Unix or want to here about Solaris,
let us know about it. We can volunteer to take classes. Ofcource it depends on how busy we are
Along with Raju Alluri and others we formed a team to teach Advanced Unix Programming.
Our first one was a 3 day workshop on Advanced Unix Programming at GRIET, Hyderabad, India.
I was surprised to see the crowd that registered for the workshop. Most of them were faculty
members from different colleges in and around Hyderabad. But the pity thing is they know a little
about Unix and Solaris. I am sure this is the case with most of the colleges except IITs and NITs.
Hi
Let me introduce myself. I am Sriram and I work for Solaris Sustaining.
I graduated from IIT-Guwahati, India.
I am passionate about Solaris from my college days onwards.
I personally feel Solaris is the best OS on this planet and feel proud to be working in a group which
fixes issues in Solaris.
I like to play a lot. From the day I was born I was watching my father play and
then watch cricket on TV, probably the reason why I love cricket.
I play Badminton. No reason why,but I like playing it. I represented my school twice in badminton.
I also like playing chess which I used to play long hours with my grandpa.
When I get bored I play loosing game in chess.
Do you know what? you have to loose all your pieces on board before your opponent.
I like drawing, Now I am much more interested in cryptic drawings. Will share what I have meant, once
I know what it is :) just kidding. When I have time will blog it.
Recent books I read:
The Chess Mysteries of Sherlock Holmes, I feel this book is a master piece written by Raymond Smullyan.
This book deals with Retrograte Analysis.
I thought of buying all the books of this Author, but I couldn't get them in
any of the bookstores in India :(
DaVinci Code, nice one. It involves lots of puzzles and interesting twists.
Currently reading The music of the primes: The Author is a mathematician,
who talks about discoveries in primes. The book focuses mainly on Reimann Hypothesis.
If you read it, I am sure you fall in love with primes. Not too much of maths involved.
So easy to understand for anyone.