title: Unable to track free calls for printf
body: <p>I'm writing are small memory profiler which is using the LD_PRELOAD trick. Overall it works good for malloc and free. Unfortunately I'm not able to track IO-Operations such as printf. Those functions get tracked correctly at the malloc functions but it didn't seam that they got passed to free. Here are code snippets:</p>
  <pre class="lang-c prettyprint-override"><code>void heaptracker_init(void) 
  {
      ignore = false;
  
      // get original symbols
      libc_hooks.malloc = dlsym(RTLD_NEXT, &quot;malloc&quot;);
      check_error_dlfcn(libc_hooks.malloc);
  
      // for the other primitives as well ...
  
      // init list for storing information
      if ((alloc_list = list_init()) == NULL)
          return;
      
  // does write results from list
      if (atexit(heaptracker_destroy))
          return;
  
      initialized = true;
      
      stopped = true;
  
      // for concurrent access on list ...
      if ((errno = pthread_mutex_init(&amp;mutex, NULL)) != 0) {
          return;
      }
  
      stopped = false;
  }
  
  </code></pre>
  <p>The malloc routine does store the allocations in a list and performs the actual allocation.</p>
  <pre class="lang-c prettyprint-override"><code>void *malloc(size_t size) 
  {
     if (!initialized)
          heaptracker_init();
  
      void * (*libc_malloc)(size_t) = libc_hooks.malloc;
  
      void *p = libc_malloc(size);
  
      list_append(alloc_list, infos, p, size, stacktrace_length);
      return p;
  }
  </code></pre>
  <p>The free routine looks like this:</p>
  <pre class="lang-c prettyprint-override"><code>void free(void *ptr)
  {
  
  if (!initialized)
          heaptracker_init();
  
  
      if (pthread_mutex_lock(&amp;mutex) != 0) 
          return;
  
      // does remove
      list_remove(alloc_list, ptr);    
      
      errno = pthread_mutex_unlock(&amp;mutex);
      libc_hooks.free(ptr);
  }
  
  </code></pre>
  <p>For a small test programm like this one:</p>
  <pre class="lang-c prettyprint-override"><code>int main()
  {
      printf(&quot;Hello World&quot;);
      char *buffer = malloc(100);
      return 0;
  }
  
  </code></pre>
  <p>The expected output should be:</p>
  <pre class="lang-json prettyprint-override"><code>{
  &quot;type&quot;: &quot;leak&quot;,
  &quot;bytes&quot;: 100,
  &quot;stacktrace&quot;: [&quot;main&quot;]
  }
  </code></pre>
  <p>While the actual output is:</p>
  <pre class="lang-json prettyprint-override"><code>[
     {
      &quot;type&quot;: &quot;leak&quot;,
      &quot;bytes&quot;: 100,
      &quot;stacktrace&quot;: [&quot;main&quot;]
      },
      {
       &quot;type&quot;: &quot;leak&quot;,
       &quot;bytes&quot;: 1024,
       &quot;stacktrace&quot;: [&quot;main&quot;, &quot;_IO_printf&quot;, ...,  &quot;_IO_file_doallocate&quot;]
       }
  ]
  </code></pre>