aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorjohannst <johannes.stoelp@gmail.com>2020-03-19 21:49:11 +0100
committerjohannst <johannes.stoelp@gmail.com>2020-03-19 21:49:11 +0100
commit72236e27a609df9b434ba14b213e8e44be8fd7fa (patch)
tree9788e0418ba5be5373dc6b57440a4ab5d1f63108
parentd5b5504d4a5b046bb54a99e271375a7a7493eb4c (diff)
downloadnotes-72236e27a609df9b434ba14b213e8e44be8fd7fa.tar.gz
notes-72236e27a609df9b434ba14b213e8e44be8fd7fa.zip
added glibc section
-rw-r--r--src/SUMMARY.md1
-rw-r--r--src/glibc.md47
2 files changed, 48 insertions, 0 deletions
diff --git a/src/SUMMARY.md b/src/SUMMARY.md
index 8859b11..45c07b5 100644
--- a/src/SUMMARY.md
+++ b/src/SUMMARY.md
@@ -23,3 +23,4 @@
- [nm](./nm.md)
- [c++filt](./c++filt.md)
- [c++](./c++.md)
+- [glibc](./glibc.md)
diff --git a/src/glibc.md b/src/glibc.md
new file mode 100644
index 0000000..2b2ab34
--- /dev/null
+++ b/src/glibc.md
@@ -0,0 +1,47 @@
+# glibc
+
+## malloc tracer [`mtrace(3)`][mtrace]
+Trace memory allocation and de-allocation to detect memory leaks.
+Need to call `mtrace(3)` to install the tracing hooks.
+
+If we can't modify the binary to call `mtrace` we can create a small shared
+library and pre-load it.
+```c
+// libmtrace.c
+#include <mcheck.h>
+__attribute__((constructor)) static void init_mtrace() { mtrace(); }
+```
+
+Compile as:
+```bash
+gcc -shared -fPIC -o libmtrace.so libmtrace.c
+```
+
+To generate the trace file run:
+```bash
+export MALLOC_TRACE=<file>
+LD_PRELOAD=./libmtrace.so <binary>
+```
+**Note**: If `MALLOC_TRACE` is not set `mtrace` won't install tracing hooks.
+
+To get the results of the trace file:
+```bash
+mtrace <binary> $MALLOC_TRACE
+```
+
+## malloc check [`mallopt(3)`][mallopt]
+Configure action when glibc detects memory error.
+
+```bash
+export MALLOC_CHECK_=<N>
+```
+
+Useful values:
+```markdown
+1 print detailed error & continue
+3 print detailed error + stack trace + memory mappings & abort
+7 print simple error message + stack trace + memory mappings & abort
+```
+
+[mtrace]: http://man7.org/linux/man-pages/man3/mtrace.3.html
+[mallopt]: http://man7.org/linux/man-pages/man3/mallopt.3.html