Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A poor man's solution would be to just log every call <code>malloc</code> and <code>free</code>, then comb through the logs and look for pattern.</p> <p><a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ld" rel="nofollow"><code>ld</code></a> provides an amazing feature that could help here.</p> <blockquote> <p><code>--wrap=symbol</code> </p> <blockquote> <p>Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to "__wrap_symbol". Any undefined reference to "__real_symbol" will be resolved to symbol.</p> <p>This can be used to provide a wrapper for a system function. The wrapper function should be called "__wrap_symbol". If it wishes to call the system function, it should call "__real_symbol".</p> <p>Here is a trivial example:</p> <pre><code>void * __wrap_malloc (size_t c) { printf ("malloc called with %zu\n", c); return __real_malloc (c); } </code></pre> <p>If you link other code with this file using --wrap malloc, then all calls to "malloc" will call the function "__wrap_malloc" instead. The call to "__real_malloc" in "__wrap_malloc" will call the real "malloc" function.</p> <p>You may wish to provide a "__real_malloc" function as well, so that links without the --wrap option will succeed. If you do this, you should not put the definition of "__real_malloc" in the same file as "__wrap_malloc"; if you do, the assembler may resolve the call before the linker has a chance to wrap it to "malloc".</p> </blockquote> </blockquote> <hr> <h3>Update</h3> <p>Just to be clear on how this is useful.</p> <ul> <li>Add a custom file to Upstart's build.</li> </ul> <p>Like this:</p> <pre><code>void*__wrap_malloc( size_t c ) { void *malloced = __real_malloc(c); /* log malloced with its associated backtrace*/ /* something like: &lt;malloced&gt;: &lt;bt-symbol-1&gt;, &lt;bt-symbol-2&gt;, .. */ return malloced } void __wrap_free( void* addr ) { /* log addr with its associated backtrace*/ /* something like: &lt;addr&gt;: &lt;bt-symbol-1&gt;, &lt;bt-symbol-2&gt;, .. */ __real_free(addr); } </code></pre> <ul> <li><p>Recompile upstart with debug symbols (<code>-g</code>) so you can get some nice backtraces. You can still optimize (<code>-O2/-O3</code>) the code if you wish.</p></li> <li><p>Link Upstart with the extra <code>LD_FLAGS</code> <code>--wrap=malloc</code>, <code>--wrap=free</code>.<br> Now anywhere Upstart calls <code>malloc</code> the symbol will be magically resolved to your new symbol <code>__wrap_malloc</code>. Beautifully this is all transparent to the compiled code as it happens at link time.<br> It's like <a href="http://en.wikipedia.org/wiki/Shim_%28computing%29" rel="nofollow">shimming</a> or <a href="http://en.wikipedia.org/wiki/Instrumentation_%28computer_programming%29" rel="nofollow">instrumenting</a> with out any of the mess.</p></li> <li><p>Run the recompiled Upstart as usual until you're sure the leak has occured.</p></li> <li><p>Look through the logs for mismatch <code>malloced</code>s and <code>addr</code>s.</p></li> </ul> <p>A couple of notes:</p> <ul> <li>The <code>--wrap=symbol</code> feature does not work with function names that are actually macros. So watch out for <code>#define malloc nih_malloc</code>. The this is what libnih does you'd need to use <code>--wrap=nih_malloc</code> and <code>__wrap_nih_malloc</code> instead.</li> <li>Use gcc's <a href="http://www.gnu.org/s/libc/manual/html_node/Backtraces.html" rel="nofollow">builtin backtracing</a> features.</li> <li>All of these changes only affect the recompiled Upstart executable. </li> <li>You could dump the logs to an sqlite DB instead with may make it easier to find mismatch mallocs and frees. </li> <li>you can make you log format an SQL insert statement then just insert them into a database post-mortem for further analysis. </li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload