Gayan Pathirage
Gayan Pathirage

Reputation: 2089

How to log errors thrown from Google AddressSanitizer to a log file

AddressSanitizer by default throws all errors to shell itself, hence I tried running my ASAN build with following command;

>MCTester_ASAN>asan.log

==15619==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61400000f9d0 at pc 0x46cff2 bp 0x7fffc062cb90 sp 0x7fffc062cb88
    #0 0x46cff1 in heapOutOfBoundWrite() /home/MemTest/main.cpp:49
    #1 0x46d68f in main /home/MemTest/main.cpp:116
    #2 0x7fbd3365bc35 in __libc_start_main (/lib64/libc.so.6+0x1ec35)
    #3 0x40a0f8 (/x01/exd10/bin/MCTester_ASAN+0x40a0f8)

ASAN:SIGSEGV
==15619==ERROR: AddressSanitizer: SEGV on unknown address 0x00000044ff97 (pc 0x00000046cff2 sp 0x7fffc062cba0 bp 0x7fffc062cbb0 T0)
    #0 0x46cff1 in heapOutOfBoundWrite() /home/MemTest/main.cpp:49
    #1 0x46d68f in main /home/MemTest/main.cpp:116
    #2 0x7fbd3365bc35 in __libc_start_main (/lib64/libc.so.6+0x1ec35)
    #3 0x40a0f8 (/x01/exd10/bin/MCTester_ASAN+0x40a0f8)

AddressSanitizer can not provide additional info.
Segmentation fault

But I still get the output to shell itself and not to the log file.

How to capture the output to a log file?

Upvotes: 10

Views: 8843

Answers (2)

Hi-Angel
Hi-Angel

Reputation: 5639

To make sanitizer write report to a file you can pass log_path=log_file as part of ASAN_OPTIONS. It may be handy in case you want to see asan messages separated from any other output. Note: the filename will not be exactly log_file but rather a log_file.$pid.

Example:

$ cat test.cpp
int main() {
    *((char*)0) = 0;
}
$ g++ test.cpp -o a -fsanitize=address -g3
$ ASAN_OPTIONS="log_path=asan.log" ./a
AddressSanitizer:DEADLYSIGNAL
$ cat asan.log.436962
=================================================================
==436962==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5626d3a50196 bp 0x7ffd0b2c2b00 sp 0x7ffd0b2c2b00 T0)
==436962==The signal is caused by a WRITE memory access.
==436962==Hint: address points to the zero page.
    #0 0x5626d3a50196 in main /tmp/test.cpp:2
    #1 0x7f79c3a78151 in __libc_start_main (/usr/lib/libc.so.6+0x28151)
    #2 0x5626d3a5008d in _start (/tmp/a+0x108d)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /tmp/test.cpp:2 in main
==436962==ABORTING

Upvotes: 8

Gluttton
Gluttton

Reputation: 5988

But I still get the output to shell itself and not to the log file.

This is because AddressSanitizer put errors in the stderr, but not in stdout. There are a lot of answers for your question.

For example:

yourcommand &>filename

Upvotes: 3

Related Questions