BlogHeaptrack - Attaching to Running Process Syndicate content

Tue, 12/09/2014 - 23:02

Hello all,

I’m happy to be back so soon with a status update on heaptrack: It is now possible to attach to an already running process!

Thanks to the great help from Celelibi on StackOverflow, I managed to achieve this important goal. Once you know what to do, it is actually extremely simple to patch a running process. I use GDB to attach to the process, then call dlopen to load a special heaptrack library for runtime-injection. Then I call an initialization function which takes the desired output file as a parameter, and then detach GDB. To actually overwrite malloc & friends, one can leverage dl_iterate_phdr and the public ELF API on Linux systems to find dynamic sections that reference one of our target symbols in their global offset table (GOT). This can then be rewritten to point to our custom hooks. Some refactoring later, which stabilized the shutdown sequence to allows multiple heaptrack attach/detach sequences, we can now do this:

  1. heaptrack -p $(pidof <yourapp>)
  2. # wait
  3. ^C
  4. heaptrack_print heaptrack.<yourapp>.$$.gz | less

This is a great help when you want to investigate why the memory consumption of your application suddenly rises. No need to restart the app, just attach heaptrack and wait for some, then kill it and heaptrack_print the outputfile.

Please try this new feature and send me bug reports and feedback.

Cheers

Comments

hello Milian, Would you be Tue, 06/14/2016 - 08:58 — Anonymous

hello Milian,

Would you be able to provide the source file (tar.gz) file link to download it ?

Hi Milian. Thanks for the Thu, 12/11/2014 - 10:54 — Anonymous

Hi Milian.

Thanks for the new development. Just a small remark on the build process, I suppose you should check for CMake being at least 3.0 instead of 2.8 as you are using the PRIVATE keyword in the target_link_libraries command which apparently, if I read the doc correctly, appeared in 3.0 (it was called LINK_PRIVATE in previous versions).

Emmanuel

Hey there, I just pushed a Thu, 12/11/2014 - 16:04 — Anonymous

Hey there,

I just pushed a change to use LINK_PRIVATE - can you try again?

Thanks

Hi. Yes, it is working fine Thu, 12/11/2014 - 17:53 — Anonymous

Hi.

Yes, it is working fine now with my old cmake 2.8.10.2. Thanks for the fix.

Post new comment

  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options