Index: include/sanitizer/common_interface_defs.h =================================================================== --- include/sanitizer/common_interface_defs.h +++ include/sanitizer/common_interface_defs.h @@ -85,6 +85,9 @@ const void *old_mid, const void *new_mid); + // Print the stack trace leading to this call. Useful for debugging user code. + void __sanitizer_print_stack_trace(); + #ifdef __cplusplus } // extern "C" #endif Index: lib/asan/asan_stack.h =================================================================== --- lib/asan/asan_stack.h +++ lib/asan/asan_stack.h @@ -77,11 +77,10 @@ #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC -#define PRINT_CURRENT_STACK() \ - { \ - GET_STACK_TRACE(kStackTraceMax, \ - common_flags()->fast_unwind_on_fatal); \ - PrintStack(&stack); \ +#define PRINT_CURRENT_STACK() \ + { \ + GET_STACK_TRACE_FATAL_HERE; \ + PrintStack(&stack); \ } #endif // ASAN_STACK_H Index: lib/asan/asan_stack.cc =================================================================== --- lib/asan/asan_stack.cc +++ lib/asan/asan_stack.cc @@ -45,3 +45,9 @@ return false; } #endif + +SANITIZER_INTERFACE_ATTRIBUTE +extern "C" void __sanitizer_print_stack_trace() { + using namespace __asan; + PRINT_CURRENT_STACK(); +} Index: lib/asan/lit_tests/TestCases/print-stack-trace.cc =================================================================== --- /dev/null +++ lib/asan/lit_tests/TestCases/print-stack-trace.cc @@ -0,0 +1,16 @@ +// RUN: %clangxx_asan -O0 %s -o %t && %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O3 %s -o %t && %t 2>&1 | FileCheck %s + +#include + +void FooBarBaz() { + __sanitizer_print_stack_trace(); +} + +int main() { + FooBarBaz(); + return 0; +} +// CHECK: {{ #0 0x.* in __sanitizer_print_stack_trace}} +// CHECK: {{ #1 0x.* in FooBarBaz\(\) .*print-stack-trace.cc:7}} +// CHECK: {{ #2 0x.* in main .*print-stack-trace.cc:11}}