{"id":15049213,"url":"https://github.com/jdeokkim/jmprof","last_synced_at":"2025-04-10T02:06:52.672Z","repository":{"id":247536474,"uuid":"819726060","full_name":"jdeokkim/jmprof","owner":"jdeokkim","description":"A tiny, experimental heap profiler for GNU/Linux. (@dgist-datalab)","archived":false,"fork":false,"pushed_at":"2024-07-25T13:15:26.000Z","size":182,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-02T17:42:42.888Z","etag":null,"topics":["c","c99","heap","heap-profiler","hooking","ld-preload","malloc-hook","memory-allocation","memory-leak","memory-profiler","memory-profiling","pebs","pmu","void-linux"],"latest_commit_sha":null,"homepage":"https://github.com/jdeokkim/jmprof","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jdeokkim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-25T04:55:43.000Z","updated_at":"2025-01-01T00:44:11.000Z","dependencies_parsed_at":"2024-07-22T04:50:36.599Z","dependency_job_id":null,"html_url":"https://github.com/jdeokkim/jmprof","commit_stats":null,"previous_names":["jdeokkim/jmprof"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeokkim%2Fjmprof","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeokkim%2Fjmprof/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeokkim%2Fjmprof/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeokkim%2Fjmprof/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdeokkim","download_url":"https://codeload.github.com/jdeokkim/jmprof/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239114227,"owners_count":19583985,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","c99","heap","heap-profiler","hooking","ld-preload","malloc-hook","memory-allocation","memory-leak","memory-profiler","memory-profiling","pebs","pmu","void-linux"],"created_at":"2024-09-24T21:19:07.635Z","updated_at":"2025-02-16T09:31:46.536Z","avatar_url":"https://github.com/jdeokkim.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jmprof\n\nA tiny, experimental heap profiler for GNU/Linux.\n\n## Prerequisites\n\n- GCC version 11.4.0+\n- Git version 2.34.0+\n- GNU coreutils version 8.3+\n- GNU Make version 4.2+\n- elfutils-dev(el) version 0.190+\n- libpfm4-dev(el) version 4.13.0+\n- libunwind-dev(el) version 1.8.1+\n\n\u003c!-- - libsanitizer-devel 13.2.0+ --\u003e\n\n### Void Linux\n\n```console\n$ sudo xbps-install base-devel elfutils-devel libpfm4-devel libunwind-devel\n```\n\n## Building\n\n```console\n$ make \u0026\u0026 sudo make install\n```\n\n## Example\n\n```\n$ jmprof uname -a\njmprof: info: creating a named pipe '/tmp/jmprof-fifo.7086'\njmprof: info: intercepting `*alloc()` calls via /usr/lib/libjmprof.so\n\n\u003c==============================================================================\n\nLinux void-dgist 6.6.40_1 #1 SMP PREEMPT_DYNAMIC Mon Jul 15 20:29:49 UTC 2024 x86_64 GNU/Linux\n\n==============================================================================\u003e\n\njmprof v0.0.6-dev by Jaedeok Kim (jdeokkim@protonmail.com)\n\n\u003e /usr/bin/uname\n\nSUMMARY: \n  30 allocs, 2 frees (8592 bytes alloc-ed)\n\n  ~ alloc #2 (! 519242 ms) -\u003e [120 bytes @ 0x55b6c00102c0]: \n    @ 0x7fd55d7517a6: malloc (src/preload.c:147:9)\n      (in /usr/lib/libjmprof.so)\n    @ 0x7fd55d58d9c4: _nl_load_locale_from_archive (./locale/loadarchive.c:460:9)\n      (in /usr/lib/libc.so.6)\n    @ 0x7fd55d58cf47: _nl_find_locale (./locale/findlocale.c:153:10)\n      (in /usr/lib/libc.so.6)\n    @ 0x7fd55d58f8d7: setlocale (./locale/setlocale.c:337:24)\n      (in /usr/lib/libc.so.6)\n    @ 0x55b6bf36c38d: ?? (:0:0)\n      (in /usr/bin/uname)\n    @ 0x7fd55d581c4c: __libc_start_call_main (../sysdeps/nptl/libc_start_call_main.h:74:3)\n      (in /usr/lib/libc.so.6)\n    @ 0x7fd55d581d05: __libc_start_main@@GLIBC_2.34 (../csu/libc-start.c:128:20)\n      (in /usr/lib/libc.so.6)\n    @ 0x55b6bf36c751: ?? (:0:0)\n      (in /usr/bin/uname)\n\n(...)\n\n  ~ alloc #29 (! 851615 ms) -\u003e [24 bytes @ 0x55b6c00114d0]: \n    @ 0x7fd55d7517a6: malloc (src/preload.c:147:9)\n      (in /usr/lib/libjmprof.so)\n    @ 0x7fd55d5fa7fa: __strdup (./string/strdup.c:44:6)\n      (in /usr/lib/libc.so.6)\n    @ 0x7fd55d594488: textdomain (./intl/textdomain.c:94:20)\n      (in /usr/lib/libc.so.6)\n    @ 0x55b6bf36c3a4: ?? (:0:0)\n      (in /usr/bin/uname)\n    @ 0x7fd55d581c4c: __libc_start_call_main (../sysdeps/nptl/libc_start_call_main.h:74:3)\n      (in /usr/lib/libc.so.6)\n    @ 0x7fd55d581d05: __libc_start_main@@GLIBC_2.34 (../csu/libc-start.c:128:20)\n      (in /usr/lib/libc.so.6)\n    @ 0x55b6bf36c751: ?? (:0:0)\n      (in /usr/bin/uname)\n\njmprof: info: cleaning up\n```\n\n## Summary\n\n### Preloading\n\n- The dynamic linker (`ld.so`, `ld-linux.so*`) find and load the shared objects (shared libraries) needed by a program, prepare the program to run, and then run it. \n- There is an important environment variable for the dynamic linker called `LD_PRELOAD`, which is a list of additional, user-specified, shared libraries **to be loaded before all others.**\n- We can leverage `LD_PRELOAD` to inject custom library code into any applications, allowing us to intercept (or override) the `*libc` function calls.\n- In GNU C Library (glibc), `dlsym()` internally calls `calloc()`, which will lead to an infinite recursion if we try to retrieve the address of `calloc()` with it. Therefore, we need to use `__libc_calloc()` as the address of `calloc()`.\n- Infinite recursion can also occur whenever we use external library functions that call `*alloc()`. In order to prevent this from happening, we can use a thread-local handle guard (a `pthread_key_t` variable) for each `*libc` memory allocation function.\n\n### Stack Unwinding\n\n- We need to **unwind** the stack to get a backtrace.\n- In order to unwind the stack from within a running program (**local** unwinding), we can use `libunwind` with the macro `UNW_LOCAL_ONLY` defined.\n\n### Symbol Resolution\n\n- Each row in `/proc/$PID/maps` (`/fs/proc/base.c` in the GNU/Linux kernel source) describes a region of contiguous virtual memory in a process or thread.\n- By finding the first address for `/proc/$PID/maps` that has mapping which includes the instruction pointer address, we can resolve the symbol that corresponds to the mapped address.\n- The `libdwfl` library from `elfutils` can read DWARF, find and interpret debug information (the `.debug_info` section of an ELF), allowing us to perform symbol resolution in an easier way.\n\n## References\n\n- [Bakhvalov, Denis. “Advanced profiling topics. PEBS and LBR.” easyperf.net. June 08, 2018.](https://easyperf.net/blog/2018/06/08/Advanced-profiling-topics-PEBS-and-LBR)\n- [Brais, Hadi. “An Introduction to the Cache Hit and Miss Performance Monitoring Events.” hadibrais.wordpress.com. March 26, 2019.](https://hadibrais.wordpress.com/2019/03/26/an-introduction-to-the-cache-hit-and-miss-performance-monitoring-events/)\n- [Conrod, Jay. “Understanding Linux `/proc/pid/maps` or `/proc/self/maps`.” stackoverflow.com. September 09, 2009.](https://stackoverflow.com/a/1401595)\n- [Nethercote, Nicholas \u0026 Seward, Julian. “Valgrind: A Framework for Heavyweight Dynamic Binary Instrumentation.” ACM SIGPLAN 2007 Conference on Programming Language Design and Implementation, San Diego, California, USA. June 13-15, 2007.](https://valgrind.org/docs/valgrind2007.pdf)\n- [Picard, Romain. “`LD_PRELOAD` for real world heap access tracking.” blog.oakbits.com. April 11, 2012.](https://blog.oakbits.com/ld_preload-for-real-world-heap-access-tracking.html)\n- [Wolff, Milian. “How to Write a Heap Profiler.” CppCon 2019, Aurora, Colorado, USA. September 15-20, 2019.](https://github.com/milianw/how-to-write-a-memory-profiler)\n\n## License\n\nMIT License\n\n\u003e Copyright (c) 2024 Jaedeok Kim \u003cjdeokkim@protonmail.com\u003e\n\u003e \n\u003e Permission is hereby granted, free of charge, to any person obtaining a copy\n\u003e of this software and associated documentation files (the \"Software\"), to deal\n\u003e in the Software without restriction, including without limitation the rights\n\u003e to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n\u003e copies of the Software, and to permit persons to whom the Software is\n\u003e furnished to do so, subject to the following conditions:\n\u003e \n\u003e The above copyright notice and this permission notice shall be included in all\n\u003e copies or substantial portions of the Software.\n\u003e \n\u003e THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\u003e IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\u003e FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\u003e AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\u003e LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\u003e OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\u003e SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdeokkim%2Fjmprof","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdeokkim%2Fjmprof","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdeokkim%2Fjmprof/lists"}