From 09b0dbfaf931453272630a909093e853b00d9f37 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 17 Dec 2012 16:28:15 +0000 Subject: [PATCH] tsan: say what thread had created a thread in reports llvm-svn: 170346 --- compiler-rt/lib/tsan/lit_tests/race_on_heap.cc | 2 +- .../tsan/lit_tests/race_with_finished_thread.cc | 2 +- compiler-rt/lib/tsan/lit_tests/simple_stack.c | 4 +-- compiler-rt/lib/tsan/rtl/tsan_report.cc | 34 +++++++++++++--------- compiler-rt/lib/tsan/rtl/tsan_report.h | 1 + compiler-rt/lib/tsan/rtl/tsan_rtl.h | 1 + compiler-rt/lib/tsan/rtl/tsan_rtl_report.cc | 1 + compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc | 1 + 8 files changed, 28 insertions(+), 18 deletions(-) diff --git a/compiler-rt/lib/tsan/lit_tests/race_on_heap.cc b/compiler-rt/lib/tsan/lit_tests/race_on_heap.cc index 62987bf..dc679e8 100644 --- a/compiler-rt/lib/tsan/lit_tests/race_on_heap.cc +++ b/compiler-rt/lib/tsan/lit_tests/race_on_heap.cc @@ -42,6 +42,6 @@ int main() { // CHECK: #1 alloc // CHECK: #2 AllocThread // ... -// CHECK: Thread T1 (tid={{.*}}, finished) created at: +// CHECK: Thread T1 (tid={{.*}}, finished) created by main thread at: // CHECK: #0 pthread_create // CHECK: #1 main diff --git a/compiler-rt/lib/tsan/lit_tests/race_with_finished_thread.cc b/compiler-rt/lib/tsan/lit_tests/race_with_finished_thread.cc index cc7834a..a267290 100644 --- a/compiler-rt/lib/tsan/lit_tests/race_with_finished_thread.cc +++ b/compiler-rt/lib/tsan/lit_tests/race_with_finished_thread.cc @@ -38,6 +38,6 @@ int main() { // CHECK: Previous write of size 4 at {{.*}} by thread T1: // CHECK: #0 foobar // CHECK: #1 Thread1 -// CHECK: Thread T1 (tid={{.*}}, finished) created at: +// CHECK: Thread T1 (tid={{.*}}, finished) created by main thread at: // CHECK: #0 pthread_create // CHECK: #1 main diff --git a/compiler-rt/lib/tsan/lit_tests/simple_stack.c b/compiler-rt/lib/tsan/lit_tests/simple_stack.c index 6de20cb..4539cb7 100644 --- a/compiler-rt/lib/tsan/lit_tests/simple_stack.c +++ b/compiler-rt/lib/tsan/lit_tests/simple_stack.c @@ -56,11 +56,11 @@ int main() { // CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack.c:18{{(:26)?}} ({{.*}}) // CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack.c:23{{(:3)?}} ({{.*}}) // CHECK-NEXT: #2 Thread2{{.*}} {{.*}}simple_stack.c:33{{(:3)?}} ({{.*}}) -// CHECK: Thread T1 (tid={{.*}}, running) created at: +// CHECK: Thread T1 (tid={{.*}}, running) created by main thread at: // CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}}) // CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}}) // CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:43{{(:3)?}} ({{.*}}) -// CHECK: Thread T2 ({{.*}}) created at: +// CHECK: Thread T2 ({{.*}}) created by main thread at: // CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}}) // CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}}) // CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:44{{(:3)?}} ({{.*}}) diff --git a/compiler-rt/lib/tsan/rtl/tsan_report.cc b/compiler-rt/lib/tsan/rtl/tsan_report.cc index af8235a..d70a142 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_report.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_report.cc @@ -35,6 +35,14 @@ ReportDesc::~ReportDesc() { #ifndef TSAN_GO +const int kThreadBufSize = 32; +const char *thread_name(char *buf, int tid) { + if (tid == 0) + return "main thread"; + internal_snprintf(buf, kThreadBufSize, "thread T%d", tid); + return buf; +} + static void PrintHeader(ReportType typ) { Printf("WARNING: ThreadSanitizer: "); @@ -82,14 +90,12 @@ static void PrintMutexSet(Vector const& mset) { } static void PrintMop(const ReportMop *mop, bool first) { - Printf(" %s of size %d at %p", + char thrbuf[kThreadBufSize]; + Printf(" %s of size %d at %p by %s", (first ? (mop->write ? "Write" : "Read") : (mop->write ? "Previous write" : "Previous read")), - mop->size, (void*)mop->addr); - if (mop->tid == 0) - Printf(" by main thread"); - else - Printf(" by thread T%d", mop->tid); + mop->size, (void*)mop->addr, + thread_name(thrbuf, mop->tid)); PrintMutexSet(mop->mset); Printf(":\n"); PrintStack(mop->stack); @@ -101,12 +107,9 @@ static void PrintLocation(const ReportLocation *loc) { loc->name, loc->size, loc->addr, loc->file, loc->line, loc->module, loc->offset); } else if (loc->type == ReportLocationHeap) { - Printf(" Location is heap block of size %zu at %p allocated", - loc->size, loc->addr); - if (loc->tid == 0) - Printf(" by main thread:\n"); - else - Printf(" by thread T%d:\n", loc->tid); + char thrbuf[kThreadBufSize]; + Printf(" Location is heap block of size %zu at %p allocated by %s:\n", + loc->size, loc->addr, thread_name(thrbuf, loc->tid)); PrintStack(loc->stack); } else if (loc->type == ReportLocationStack) { Printf(" Location is stack of thread T%d:\n\n", loc->tid); @@ -128,9 +131,12 @@ static void PrintThread(const ReportThread *rt) { Printf(" Thread T%d", rt->id); if (rt->name) Printf(" '%s'", rt->name); - Printf(" (tid=%zu, %s)", rt->pid, rt->running ? "running" : "finished"); + char thrbuf[kThreadBufSize]; + Printf(" (tid=%zu, %s) created by %s", + rt->pid, rt->running ? "running" : "finished", + thread_name(thrbuf, rt->parent_tid)); if (rt->stack) - Printf(" created at:"); + Printf(" at:"); Printf("\n"); PrintStack(rt->stack); } diff --git a/compiler-rt/lib/tsan/rtl/tsan_report.h b/compiler-rt/lib/tsan/rtl/tsan_report.h index 2c3667e..67545b3 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_report.h +++ b/compiler-rt/lib/tsan/rtl/tsan_report.h @@ -78,6 +78,7 @@ struct ReportThread { uptr pid; bool running; char *name; + int parent_tid; ReportStack *stack; }; diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.h b/compiler-rt/lib/tsan/rtl/tsan_rtl.h index b14c99e..0713a3f 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_rtl.h +++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.h @@ -377,6 +377,7 @@ struct ThreadContext { u64 epoch0; u64 epoch1; StackTrace creation_stack; + int creation_tid; ThreadDeadInfo *dead_info; ThreadContext *dead_next; // In dead thread list. char *name; // As annotated by user. diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cc b/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cc index c4dcdfb..b1556d8 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cc @@ -180,6 +180,7 @@ void ScopedReport::AddThread(const ThreadContext *tctx) { rt->pid = tctx->os_id; rt->running = (tctx->status == ThreadStatusRunning); rt->name = tctx->name ? internal_strdup(tctx->name) : 0; + rt->parent_tid = tctx->creation_tid; rt->stack = SymbolizeStack(tctx->creation_stack); } diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc b/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc index 2277a08..3597759 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc @@ -156,6 +156,7 @@ int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) { thr->clock.release(&tctx->sync); StatInc(thr, StatSyncRelease); tctx->creation_stack.ObtainCurrent(thr, pc); + tctx->creation_tid = thr->tid; } return tid; } -- 2.7.4