将自定义 assert() 与 AddressSanitizer 集成

Integrate custom assert() with AddressSanitizer

我有一个类似 assert() 的自定义宏,它在失败时调用 abort()。使用 AddressSanitizer 时,我更愿意在断言失败时打印出通常漂亮的堆栈跟踪。如何实现?

您可以使用 __sanitizer_print_stack_trace 来自 sanitizer/common_interface_defs.h:

$ cat tmp.cc
#include <sanitizer/common_interface_defs.h>

int main() {
  __sanitizer_print_stack_trace();
  return 0;
}

$ g++ tmp.cc -fsanitize=address
$ ./a.out 
    #0 0x7fe00b381e58 in __sanitizer_print_stack_trace (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xece58)
    #1 0x55a059f7f802 in main (/home/yugr/a.out+0x802)
    #2 0x7fe00aec5b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x55a059f7f719 in _start (/home/yugr/a.out+0x719)