Using log-malloc2 for unit testing memory allocations

Latest version of log-malloc2 library has (IMHO) an unique little feature, that makes it well suited for unit testing memory allocations. It provides simple API for inquiring actual memory usage at runtime. This way, it is possible to compare usage before entering and after leaving some function, to ensure that there are no memory leaks inside of it.

Continue reading

Printing backtrace on SIGSEGV

New version of log-malloc2 provides new helpful functions and scripts that make backtrace printing and analyzing easy and convenient.

log-malloc2_util.h provides few fully inlined functions:

1. log_malloc_backtrace_init(void)

  • Pre-initializes backtrace() function, to avoid any later memory allocations. Use of this function is optional, but it’s good to use it on program start if you want to generate backtrace in SIGSEGV signal handler (memory allocations in SIGSEGV should be avoided if possible).

2. ssize_t log_malloc_backtrace(int fd)

  • Prints current backtrace to given file descriptor, including process memory map (/proc/self/maps) to make backtrace symbol conversion easier (this is needed because of ASLR).
  • Generated output can be directly pasted to backtrace2line script, that will convert it to human readable stack trace (ASLR is supported).

Because both functions are inlined, it is not needed to link program against log-malloc2 library, and this makes it also bit easier to use it in segfault (SIGSEGV) signal handler.