|Did you know ...||Search Documentation:|
|Debugging and profiling foreign code (valgrind, asan)|
This section is only relevant for Unix users on platforms supported by valgrind. Valgrind is an excellent binary instrumentation platform. Unlike many other instrumentation platforms, valgrind can deal with code loaded through dlopen().
The callgrind tool can be used to profile foreign code loaded
under SWI-Prolog. Compile the foreign library adding -g
option to gcc or swipl-ld. By setting the environment
yes, SWI-Prolog will not
release loaded shared objects using dlclose(). This trick is required to
get source information on the loaded library. Without, valgrind claims
that the shared object has no debugging information.234Tested
using valgrind version 3.2.3 on x64. Here is the complete
sequence using bash as login shell:
% VALGRIND=yes valgrind --tool=callgrind pl <args> <prolog interaction> % kcachegrind callgrind.out.<pid>
Instead of valgrind, you can use AddressSanitizer. Here is a short example for building with asan enabled and then running the resulting binary. When you exit swipl, a message is printed and any leaks are printed. During execution, other messages may be printed out, such as freeing an address twice or using freed or unallocated memory.
% cd build.sanitize % cmake -G Ninja -DCMAKE_BUILD_TYPE=Sanitize .. % ninja % ASAN_OPTIONS=detect_leaks=1 build.sanitize/src/swipl <prolog interaction> % halt Running LSAN memory leak check (reclaim_memory=1) No leaks detected