Index: lib/msan/msan.h =================================================================== --- lib/msan/msan.h +++ lib/msan/msan.h @@ -111,6 +111,13 @@ GetStackTrace(&stack, common_flags()->malloc_context_size, pc, bp, \ common_flags()->fast_unwind_on_malloc) +#define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \ + StackTrace stack; \ + stack.size = 0; \ + if (msan_inited) \ + GetStackTrace(&stack, kStackTraceMax, pc, bp, \ + common_flags()->fast_unwind_on_fatal) + #define GET_STORE_STACK_TRACE \ GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) Index: lib/msan/msan.cc =================================================================== --- lib/msan/msan.cc +++ lib/msan/msan.cc @@ -211,9 +211,7 @@ ++msan_report_count; - StackTrace stack; - GetStackTrace(&stack, kStackTraceMax, pc, bp, - common_flags()->fast_unwind_on_fatal); + GET_FATAL_STACK_TRACE_PC_BP(pc, bp); u32 report_origin = (__msan_get_track_origins() && OriginIsValid(origin)) ? origin : 0; @@ -422,9 +420,7 @@ } else if (!msan_expected_umr_found) { GET_CALLER_PC_BP_SP; (void)sp; - StackTrace stack; - GetStackTrace(&stack, kStackTraceMax, pc, bp, - common_flags()->fast_unwind_on_fatal); + GET_FATAL_STACK_TRACE_PC_BP(pc, bp); ReportExpectedUMRNotFound(&stack); Die(); } @@ -662,3 +658,11 @@ const char* __msan_default_options() { return ""; } } // extern "C" #endif + +extern "C" { +SANITIZER_INTERFACE_ATTRIBUTE +void __sanitizer_print_stack_trace() { + GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME()); + stack.Print(); +} +} // extern "C" Index: test/msan/print-stack-trace.cc =================================================================== --- /dev/null +++ test/msan/print-stack-trace.cc @@ -0,0 +1,16 @@ +// RUN: %clangxx_msan -O0 %s -o %t && %run %t 2>&1 | FileCheck %s +// RUN: %clangxx_msan -O3 %s -o %t && %run %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}}