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.cc =================================================================== --- lib/asan/asan_stack.cc +++ lib/asan/asan_stack.cc @@ -45,3 +45,11 @@ return false; } #endif + +using namespace __asan; // NOLINT + +SANITIZER_INTERFACE_ATTRIBUTE +extern "C" void __sanitizer_print_stack_trace() { + GET_STACK_TRACE_FATAL_HERE; + PrintStack(&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,18 @@ +// RUN: %clangxx_asan -O0 %s -o %t && %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O1 %s -o %t && %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O2 %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:9}} +// CHECK: {{ #2 0x.* in main .*print-stack-trace.cc:13}}