Solaris Sustaining Engineer
Sriram Popuri's Weblog
Archives
May 2005 »
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  
       
Today
XML
Search

Links
OpenSolaris User Group OpenSolaris: Love at First Boot
Referrers

Today's Page Hits: 31

Main | Next month (Jun 2005) »
20050616 Thursday June 16, 2005
Wrapper library to track memory leaks
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); } </plaintext> </div><br> <div class="entryfooter"> Jun 16 2005, 06:24:17 PM IST <a href="http://blogs.sun.com/popuri/entry/wrapper_library_to_track_memory" title="Permanent link to this weblog entry" class="entrypermalink">Permalink</a> <a href="http://blogs.sun.com/popuri/entry/wrapper_library_to_track_memory#comments" class="entrycommentslink">Comments [1]</a> </div> <br> <a name="want_to_know_solaris_let" id="want_to_know_solaris_let"></a> <span class="entrytitle">Want to know Solaris..let us know</span><br> <div class="body"> <p> 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 </p> <p> 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. </p> </div><br> <div class="entryfooter"> Jun 16 2005, 06:13:06 PM IST <a href="http://blogs.sun.com/popuri/entry/want_to_know_solaris_let" title="Permanent link to this weblog entry" class="entrypermalink">Permalink</a> <a href="http://blogs.sun.com/popuri/entry/want_to_know_solaris_let#comments" class="entrycommentslink">Comments [0]</a> </div> <br> <a name="you_can_came_to_sriram" id="you_can_came_to_sriram"></a> <span class="entrytitle">You came to Sriram Popuri's blog</span><br> <div class="body"> <html> <body> Hi </br> <p> Let me introduce myself. I am Sriram and I work for Solaris Sustaining.<br\> I graduated from IIT-Guwahati, India.<br\> I am passionate about Solaris from my college days onwards.<br\> 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.<br\> </p> <p> I like to play a lot. From the day I was born I was watching my father play and<br\> then watch cricket on TV, probably the reason why I love cricket.<br\> I play Badminton. No reason why,but I like playing it. I represented my school twice in badminton.<br\> I also like playing chess which I used to play long hours with my grandpa.<br\> When I get bored I play loosing game in chess.<br\> Do you know what? you have to loose all your pieces on board before your opponent.<br\> I like drawing, Now I am much more interested in cryptic drawings. Will share what I have meant, once<br\> I know what it is :) just kidding. When I have time will blog it. </p> <b>Recent books I read:</b><br\> The Chess Mysteries of Sherlock Holmes, I feel this book is a master piece written by Raymond Smullyan. This book deals with Retrograte Analysis.<br\> I thought of buying all the books of this Author, but I couldn't get them in any of the bookstores in India :( <br\><br\> DaVinci Code, nice one. It involves lots of puzzles and interesting twists. <br\><br\> Currently reading The music of the primes: The Author is a mathematician,<br\> who talks about discoveries in primes. The book focuses mainly on Reimann Hypothesis.<br\> If you read it, I am sure you fall in love with primes. Not too much of maths involved.<br\> So easy to understand for anyone.<br\> </body> </html> </div><br> <div class="entryfooter"> Jun 16 2005, 05:32:09 PM IST <a href="http://blogs.sun.com/popuri/entry/you_can_came_to_sriram" title="Permanent link to this weblog entry" class="entrypermalink">Permalink</a> <a href="http://blogs.sun.com/popuri/entry/you_can_came_to_sriram#comments" class="entrycommentslink">Comments [1]</a> </div> <br> <!-- Begin SiteCatalyst code version: G.5. --> <script type="text/javascript" language="JavaScript"> <!-- if(typeof s_channel=='undefined'){var s_channel="popuri";} //--></script> <script type="text/javascript" language="JavaScript" src="/omniture/s_code_remote.js"></script> <!-- End SiteCatalyst code version: G.5. --> </body> </html>