[compiler-rt] r221469 - [Sanitizer] Introduce "stack_trace_format" runtime flag.

Alexey Samsonov vonosmas at gmail.com
Thu Nov 6 10:43:46 PST 2014


Author: samsonov
Date: Thu Nov  6 12:43:45 2014
New Revision: 221469

URL: http://llvm.org/viewvc/llvm-project?rev=221469&view=rev
Log:
[Sanitizer] Introduce "stack_trace_format" runtime flag.

This flag can be used to specify the format of stack frames - user
can now provide a string with placeholders, which should be printed
for each stack frame with placeholders replaced with actual data.
For example "%p" will be replaced by PC, "%s" will be replaced by
the source file name etc.

"DEFAULT" value enforces default stack trace format currently used in
all the sanitizers except TSan.

This change also implements __sanitizer_print_stack_trace interface
function in TSan.

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_stacktrace_libcdep.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/print-stack-trace.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc Thu Nov  6 12:43:45 2014
@@ -68,6 +68,7 @@ void SetCommonFlagsDefaults(CommonFlags
   f->print_suppressions = true;
   f->disable_coredump = (SANITIZER_WORDSIZE == 64);
   f->symbolize_inline_frames = true;
+  f->stack_trace_format = "DEFAULT";
 }
 
 void ParseCommonFlagsFromString(CommonFlags *f, const char *str) {
@@ -161,6 +162,10 @@ void ParseCommonFlagsFromString(CommonFl
       "default and for sanitizers that don't reserve lots of virtual memory.");
   ParseFlag(str, &f->symbolize_inline_frames, "symbolize_inline_frames",
             "Print inlined frames in stacktraces. Defaults to true.");
+  ParseFlag(str, &f->stack_trace_format, "stack_trace_format",
+            "Format string used to render stack frames. "
+            "See sanitizer_stacktrace_printer.h for the format description. "
+            "Use DEFAULT to get default format.");
 
   // Do a sanity check for certain flags.
   if (f->malloc_context_size < 1)

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h Thu Nov  6 12:43:45 2014
@@ -62,6 +62,7 @@ struct CommonFlags {
   bool print_suppressions;
   bool disable_coredump;
   bool symbolize_inline_frames;
+  const char *stack_trace_format;
 };
 
 inline CommonFlags *common_flags() {

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stacktrace_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stacktrace_libcdep.cc?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stacktrace_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stacktrace_libcdep.cc Thu Nov  6 12:43:45 2014
@@ -43,8 +43,8 @@ void StackTrace::Print() const {
     for (uptr j = 0; j < addr_frames_num; j++) {
       AddressInfo &info = addr_frames[j];
       frame_desc.clear();
-      RenderFrame(&frame_desc, "DEFAULT", frame_num++, info,
-                  common_flags()->strip_path_prefix);
+      RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
+                  info, common_flags()->strip_path_prefix);
       Printf("%s\n", frame_desc.data());
       info.Clear();
     }

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc Thu Nov  6 12:43:45 2014
@@ -99,6 +99,7 @@ void InitializeFlags(Flags *f, const cha
   cf->allow_addr2line = true;
   cf->detect_deadlocks = true;
   cf->print_suppressions = false;
+  cf->stack_trace_format = "    #%n %f %S %M";
 
   // Let a frontend override.
   ParseFlags(f, __tsan_default_options());

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc Thu Nov  6 12:43:45 2014
@@ -118,9 +118,9 @@ void PrintStack(const ReportStack *ent)
     Printf("    [failed to restore the stack]\n\n");
     return;
   }
-  for (int i = 0; ent; ent = ent->next, i++) {
+  for (int i = 0; ent && ent->info.address; ent = ent->next, i++) {
     InternalScopedString res(2 * GetPageSizeCached());
-    RenderFrame(&res, "    #%n %f %S %M", i, ent->info,
+    RenderFrame(&res, common_flags()->stack_trace_format, i, ent->info,
                 common_flags()->strip_path_prefix, "__interceptor_");
     Printf("%s\n", res.data());
   }

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h Thu Nov  6 12:43:45 2014
@@ -586,7 +586,7 @@ ReportStack *SkipTsanInternalFrames(Repo
 u32 CurrentStackId(ThreadState *thr, uptr pc);
 ReportStack *SymbolizeStackId(u32 stack_id);
 void PrintCurrentStack(ThreadState *thr, uptr pc);
-void PrintCurrentStackSlow();  // uses libunwind
+void PrintCurrentStackSlow(uptr pc);  // uses libunwind
 
 void Initialize(ThreadState *thr);
 int Finalize(ThreadState *thr);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc Thu Nov  6 12:43:45 2014
@@ -41,7 +41,7 @@ void TsanCheckFailed(const char *file, i
   Printf("FATAL: ThreadSanitizer CHECK failed: "
          "%s:%d \"%s\" (0x%zx, 0x%zx)\n",
          file, line, cond, (uptr)v1, (uptr)v2);
-  PrintCurrentStackSlow();
+  PrintCurrentStackSlow(StackTrace::GetCurrentPc());
   Die();
 }
 
@@ -668,12 +668,12 @@ void PrintCurrentStack(ThreadState *thr,
   PrintStack(SymbolizeStack(trace));
 }
 
-void PrintCurrentStackSlow() {
+void PrintCurrentStackSlow(uptr pc) {
 #ifndef TSAN_GO
   BufferedStackTrace *ptrace =
       new(internal_alloc(MBlockStackTrace, sizeof(BufferedStackTrace)))
           BufferedStackTrace();
-  ptrace->Unwind(kStackTraceMax, StackTrace::GetCurrentPc(), 0, 0, 0, 0, false);
+  ptrace->Unwind(kStackTraceMax, pc, 0, 0, 0, 0, false);
   for (uptr i = 0; i < ptrace->size / 2; i++) {
     uptr tmp = ptrace->trace_buffer[i];
     ptrace->trace_buffer[i] = ptrace->trace_buffer[ptrace->size - i - 1];
@@ -684,3 +684,12 @@ void PrintCurrentStackSlow() {
 }
 
 }  // namespace __tsan
+
+using namespace __tsan;
+
+extern "C" {
+SANITIZER_INTERFACE_ATTRIBUTE
+void __sanitizer_print_stack_trace() {
+  PrintCurrentStackSlow(StackTrace::GetCurrentPc());
+}
+}  // extern "C"

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/print-stack-trace.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/print-stack-trace.cc?rev=221469&r1=221468&r2=221469&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/print-stack-trace.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/print-stack-trace.cc Thu Nov  6 12:43:45 2014
@@ -1,10 +1,7 @@
-// RUN: %clangxx -O0 %s -o %t && %run %t 2>&1 | FileCheck %s
-// RUN: %clangxx -O3 %s -o %t && %run %t 2>&1 | FileCheck %s
-// RUN: %tool_options=symbolize_inline_frames=false %run %t 2>&1 | FileCheck %s --check-prefix=NOINLINE
-
-// Not yet implemented for TSan.
-// https://code.google.com/p/address-sanitizer/issues/detail?id=243
-// XFAIL: tsan
+// RUN: %clangxx -O0 %s -o %t && %tool_options=stack_trace_format=DEFAULT %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx -O3 %s -o %t && %tool_options=stack_trace_format=DEFAULT %run %t 2>&1 | FileCheck %s
+// RUN: %tool_options='stack_trace_format="frame:%n lineno:%l"' %run %t 2>&1 | FileCheck %s --check-prefix=CUSTOM
+// RUN: %tool_options=symbolize_inline_frames=false:stack_trace_format=DEFAULT %run %t 2>&1 | FileCheck %s --check-prefix=NOINLINE
 
 #include <sanitizer/common_interface_defs.h>
 
@@ -17,8 +14,11 @@ int main() {
   return 0;
 }
 // CHECK: {{    #0 0x.* in __sanitizer_print_stack_trace}}
-// CHECK: {{    #1 0x.* in FooBarBaz(\(\))? .*print-stack-trace.cc:12}}
-// CHECK: {{    #2 0x.* in main.*print-stack-trace.cc:16}}
+// CHECK: {{    #1 0x.* in FooBarBaz(\(\))? .*print-stack-trace.cc:9}}
+// CHECK: {{    #2 0x.* in main.*print-stack-trace.cc:13}}
+
+// CUSTOM: frame:1 lineno:9
+// CUSTOM: frame:2 lineno:13
 
 // NOINLINE: #0 0x{{.*}} in __sanitizer_print_stack_trace
-// NOINLINE: #1 0x{{.*}} in main{{.*}}print-stack-trace.cc:12
+// NOINLINE: #1 0x{{.*}} in main{{.*}}print-stack-trace.cc:9





More information about the llvm-commits mailing list