我正在尝试调试clang与asan检测到的内存错误,但是错过了valgrind.但我不能让我的clang内置二进制文件给我任何有用的调试信息.我可以用一个简短的测试程序证明这一点:
#include <stdlib.h> #include <string.h> int main(void) { char *a = malloc(8); memset(a,9); free(a); return 0; }
(显然这个错误将由valgrind接收,它纯粹是为了显示clang的问题.)
我用Clang 3.4-1ubuntu1编译它,如下所示:
clang -fsanitize=address -fno-sanitize-recover -o test -O0 -g test.c
果然,./ test中止,我看到一些调试信息:
==3309==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000eff8 at pc 0x43e950 bp 0x7fff168724f0 sp 0x7fff168724e8 WRITE of size 9 at 0x60200000eff8 thread T0 #0 0x43e94f (/home/jason/Code/astest/test+0x43e94f) #1 0x7faa43c47de4 (/lib/x86_64-linux-gnu/libc.so.6+0x21de4) #2 0x43e6ac (/home/jason/Code/astest/test+0x43e6ac) 0x60200000eff8 is located 0 bytes to the right of 8-byte region [0x60200000eff0,0x60200000eff8) allocated by thread T0 here: #0 0x42cc25 (/home/jason/Code/astest/test+0x42cc25) #1 0x43e874 (/home/jason/Code/astest/test+0x43e874) #2 0x7faa43c47de4 (/lib/x86_64-linux-gnu/libc.so.6+0x21de4)
但我真正想知道的是发生错误的行号以及分配内存的位置.
如何从clang asan获取此信息?
解决方法
如果我们看一下铿锵声
AddressSanitizer documentation,它会说:
To make AddressSanitizer symbolize its output you need to set the
ASAN_SYMBOLIZER_PATH environment variable to point to the
llvm-symbolizer binary (or make sure llvm-symbolizer is in your
$PATH):
并显示以下示例:
ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer ./a.out
正如OP所指出的,安装位置可能会有所不同,但是一旦你知道llvm-symbolizer的位置,步骤就是一样的.