1 //===-- ThreadSanitizerRuntime.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ThreadSanitizerRuntime.h"
12 #include "Plugins/Process/Utility/HistoryThread.h"
13 #include "lldb/Breakpoint/StoppointCallbackContext.h"
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginInterface.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/RegularExpression.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Expression/UserExpression.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Symbol/Symbol.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Symbol/VariableList.h"
28 #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
29 #include "lldb/Target/SectionLoadList.h"
30 #include "lldb/Target/StopInfo.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
35 using namespace lldb_private;
37 lldb::InstrumentationRuntimeSP
38 ThreadSanitizerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) {
39 return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp));
42 void ThreadSanitizerRuntime::Initialize() {
43 PluginManager::RegisterPlugin(
44 GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.",
45 CreateInstance, GetTypeStatic);
48 void ThreadSanitizerRuntime::Terminate() {
49 PluginManager::UnregisterPlugin(CreateInstance);
52 lldb_private::ConstString ThreadSanitizerRuntime::GetPluginNameStatic() {
53 return ConstString("ThreadSanitizer");
56 lldb::InstrumentationRuntimeType ThreadSanitizerRuntime::GetTypeStatic() {
57 return eInstrumentationRuntimeTypeThreadSanitizer;
60 ThreadSanitizerRuntime::~ThreadSanitizerRuntime() { Deactivate(); }
62 static constexpr std::chrono::seconds g_retrieve_data_function_timeout(2);
64 const char *thread_sanitizer_retrieve_report_data_prefix = R"(
67 void *__tsan_get_current_report();
68 int __tsan_get_report_data(void *report, const char **description, int *count,
69 int *stack_count, int *mop_count, int *loc_count,
70 int *mutex_count, int *thread_count,
71 int *unique_tid_count, void **sleep_trace,
72 unsigned long trace_size);
73 int __tsan_get_report_stack(void *report, unsigned long idx, void **trace,
74 unsigned long trace_size);
75 int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr,
76 int *size, int *write, int *atomic, void **trace,
77 unsigned long trace_size);
78 int __tsan_get_report_loc(void *report, unsigned long idx, const char **type,
79 void **addr, unsigned long *start, unsigned long *size, int *tid,
80 int *fd, int *suppressable, void **trace,
81 unsigned long trace_size);
82 int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr,
83 int *destroyed, void **trace, unsigned long trace_size);
84 int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id,
85 int *running, const char **name, int *parent_tid,
86 void **trace, unsigned long trace_size);
87 int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid);
90 const int REPORT_TRACE_SIZE = 128;
91 const int REPORT_ARRAY_SIZE = 4;
95 const char *description;
98 void *sleep_trace[REPORT_TRACE_SIZE];
103 void *trace[REPORT_TRACE_SIZE];
104 } stacks[REPORT_ARRAY_SIZE];
114 void *trace[REPORT_TRACE_SIZE];
115 } mops[REPORT_ARRAY_SIZE];
127 void *trace[REPORT_TRACE_SIZE];
128 } locs[REPORT_ARRAY_SIZE];
133 unsigned long mutex_id;
136 void *trace[REPORT_TRACE_SIZE];
137 } mutexes[REPORT_ARRAY_SIZE];
147 void *trace[REPORT_TRACE_SIZE];
148 } threads[REPORT_ARRAY_SIZE];
150 int unique_tid_count;
154 } unique_tids[REPORT_ARRAY_SIZE];
158 const char *thread_sanitizer_retrieve_report_data_command = R"(
161 t.report = __tsan_get_current_report();
162 __tsan_get_report_data(t.report, &t.description, &t.report_count, &t.stack_count, &t.mop_count, &t.loc_count, &t.mutex_count, &t.thread_count, &t.unique_tid_count, t.sleep_trace, REPORT_TRACE_SIZE);
164 if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
165 for (int i = 0; i < t.stack_count; i++) {
167 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
170 if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
171 for (int i = 0; i < t.mop_count; i++) {
173 __tsan_get_report_mop(t.report, i, &t.mops[i].tid, &t.mops[i].addr, &t.mops[i].size, &t.mops[i].write, &t.mops[i].atomic, t.mops[i].trace, REPORT_TRACE_SIZE);
176 if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
177 for (int i = 0; i < t.loc_count; i++) {
179 __tsan_get_report_loc(t.report, i, &t.locs[i].type, &t.locs[i].addr, &t.locs[i].start, &t.locs[i].size, &t.locs[i].tid, &t.locs[i].fd, &t.locs[i].suppressable, t.locs[i].trace, REPORT_TRACE_SIZE);
182 if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
183 for (int i = 0; i < t.mutex_count; i++) {
184 t.mutexes[i].idx = i;
185 __tsan_get_report_mutex(t.report, i, &t.mutexes[i].mutex_id, &t.mutexes[i].addr, &t.mutexes[i].destroyed, t.mutexes[i].trace, REPORT_TRACE_SIZE);
188 if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
189 for (int i = 0; i < t.thread_count; i++) {
190 t.threads[i].idx = i;
191 __tsan_get_report_thread(t.report, i, &t.threads[i].tid, &t.threads[i].os_id, &t.threads[i].running, &t.threads[i].name, &t.threads[i].parent_tid, t.threads[i].trace, REPORT_TRACE_SIZE);
194 if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
195 for (int i = 0; i < t.unique_tid_count; i++) {
196 t.unique_tids[i].idx = i;
197 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
203 static StructuredData::Array *
204 CreateStackTrace(ValueObjectSP o,
205 const std::string &trace_item_name = ".trace") {
206 StructuredData::Array *trace = new StructuredData::Array();
207 ValueObjectSP trace_value_object =
208 o->GetValueForExpressionPath(trace_item_name.c_str());
209 size_t count = trace_value_object->GetNumChildren();
210 for (size_t j = 0; j < count; j++) {
212 trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
216 StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
221 static StructuredData::Array *ConvertToStructuredArray(
222 ValueObjectSP return_value_sp, const std::string &items_name,
223 const std::string &count_name,
224 std::function<void(ValueObjectSP o, StructuredData::Dictionary *dict)> const
226 StructuredData::Array *array = new StructuredData::Array();
228 return_value_sp->GetValueForExpressionPath(count_name.c_str())
229 ->GetValueAsUnsigned(0);
230 ValueObjectSP objects =
231 return_value_sp->GetValueForExpressionPath(items_name.c_str());
232 for (unsigned int i = 0; i < count; i++) {
233 ValueObjectSP o = objects->GetChildAtIndex(i, true);
234 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
238 array->AddItem(StructuredData::ObjectSP(dict));
243 static std::string RetrieveString(ValueObjectSP return_value_sp,
244 ProcessSP process_sp,
245 const std::string &expression_path) {
247 return_value_sp->GetValueForExpressionPath(expression_path.c_str())
248 ->GetValueAsUnsigned(0);
251 process_sp->ReadCStringFromMemory(ptr, str, error);
256 GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data,
257 std::map<uint64_t, user_id_t> &thread_id_map) {
258 ConvertToStructuredArray(
259 data, ".threads", ".thread_count",
260 [process_sp, &thread_id_map](ValueObjectSP o,
261 StructuredData::Dictionary *dict) {
263 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
264 uint64_t thread_os_id =
265 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
266 user_id_t lldb_user_id = 0;
268 bool can_update = true;
269 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(
270 thread_os_id, can_update);
272 lldb_user_id = lldb_thread->GetIndexID();
274 // This isn't a live thread anymore. Ask process to assign a new
275 // Index ID (or return an old one if we've already seen this
277 // It will also make sure that no new threads are assigned this Index
279 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
282 thread_id_map[thread_id] = lldb_user_id;
286 static user_id_t Renumber(uint64_t id,
287 std::map<uint64_t, user_id_t> &thread_id_map) {
288 auto IT = thread_id_map.find(id);
289 if (IT == thread_id_map.end())
295 StructuredData::ObjectSP
296 ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
297 ProcessSP process_sp = GetProcessSP();
299 return StructuredData::ObjectSP();
301 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
302 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
305 return StructuredData::ObjectSP();
307 EvaluateExpressionOptions options;
308 options.SetUnwindOnError(true);
309 options.SetTryAllThreads(true);
310 options.SetStopOthers(true);
311 options.SetIgnoreBreakpoints(true);
312 options.SetTimeout(g_retrieve_data_function_timeout);
313 options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
314 options.SetAutoApplyFixIts(false);
315 options.SetLanguage(eLanguageTypeObjC_plus_plus);
317 ValueObjectSP main_value;
318 ExecutionContext exe_ctx;
320 frame_sp->CalculateExecutionContext(exe_ctx);
321 ExpressionResults result = UserExpression::Evaluate(
322 exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "",
323 main_value, eval_error);
324 if (result != eExpressionCompleted) {
325 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
326 "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n",
327 eval_error.AsCString());
328 return StructuredData::ObjectSP();
331 std::map<uint64_t, user_id_t> thread_id_map;
332 GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
334 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
335 dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
336 dict->AddStringItem("issue_type",
337 RetrieveString(main_value, process_sp, ".description"));
338 dict->AddIntegerItem("report_count",
339 main_value->GetValueForExpressionPath(".report_count")
340 ->GetValueAsUnsigned(0));
341 dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(
342 main_value, ".sleep_trace")));
344 StructuredData::Array *stacks = ConvertToStructuredArray(
345 main_value, ".stacks", ".stack_count",
346 [thread_sp](ValueObjectSP o, StructuredData::Dictionary *dict) {
347 dict->AddIntegerItem(
349 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
350 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
351 // "stacks" happen on the current thread
352 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
354 dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
356 StructuredData::Array *mops = ConvertToStructuredArray(
357 main_value, ".mops", ".mop_count",
358 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
359 dict->AddIntegerItem(
361 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
362 dict->AddIntegerItem(
365 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
367 dict->AddIntegerItem(
369 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
370 dict->AddBooleanItem(
372 o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
373 dict->AddBooleanItem(
375 o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
376 dict->AddIntegerItem(
378 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
379 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
381 dict->AddItem("mops", StructuredData::ObjectSP(mops));
383 StructuredData::Array *locs = ConvertToStructuredArray(
384 main_value, ".locs", ".loc_count",
385 [process_sp, &thread_id_map](ValueObjectSP o,
386 StructuredData::Dictionary *dict) {
387 dict->AddIntegerItem(
389 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
390 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
391 dict->AddIntegerItem(
393 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
394 dict->AddIntegerItem(
396 o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
397 dict->AddIntegerItem(
399 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
400 dict->AddIntegerItem(
403 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
405 dict->AddIntegerItem(
407 o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
408 dict->AddIntegerItem("suppressable",
409 o->GetValueForExpressionPath(".suppressable")
410 ->GetValueAsUnsigned(0));
411 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
413 dict->AddItem("locs", StructuredData::ObjectSP(locs));
415 StructuredData::Array *mutexes = ConvertToStructuredArray(
416 main_value, ".mutexes", ".mutex_count",
417 [](ValueObjectSP o, StructuredData::Dictionary *dict) {
418 dict->AddIntegerItem(
420 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
421 dict->AddIntegerItem(
423 o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
424 dict->AddIntegerItem(
426 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
427 dict->AddIntegerItem(
429 o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
430 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
432 dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
434 StructuredData::Array *threads = ConvertToStructuredArray(
435 main_value, ".threads", ".thread_count",
436 [process_sp, &thread_id_map](ValueObjectSP o,
437 StructuredData::Dictionary *dict) {
438 dict->AddIntegerItem(
440 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
441 dict->AddIntegerItem(
444 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
446 dict->AddIntegerItem(
448 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
449 dict->AddIntegerItem(
451 o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
452 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
453 dict->AddIntegerItem(
455 Renumber(o->GetValueForExpressionPath(".parent_tid")
456 ->GetValueAsUnsigned(0),
458 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
460 dict->AddItem("threads", StructuredData::ObjectSP(threads));
462 StructuredData::Array *unique_tids = ConvertToStructuredArray(
463 main_value, ".unique_tids", ".unique_tid_count",
464 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
465 dict->AddIntegerItem(
467 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
468 dict->AddIntegerItem(
471 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
474 dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
476 return StructuredData::ObjectSP(dict);
480 ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) {
481 std::string description = report->GetAsDictionary()
482 ->GetValueForKey("issue_type")
486 if (description == "data-race") {
488 } else if (description == "data-race-vptr") {
489 return "Data race on C++ virtual pointer";
490 } else if (description == "heap-use-after-free") {
491 return "Use of deallocated memory";
492 } else if (description == "heap-use-after-free-vptr") {
493 return "Use of deallocated C++ virtual pointer";
494 } else if (description == "thread-leak") {
495 return "Thread leak";
496 } else if (description == "locked-mutex-destroy") {
497 return "Destruction of a locked mutex";
498 } else if (description == "mutex-double-lock") {
499 return "Double lock of a mutex";
500 } else if (description == "mutex-invalid-access") {
501 return "Use of an uninitialized or destroyed mutex";
502 } else if (description == "mutex-bad-unlock") {
503 return "Unlock of an unlocked mutex (or by a wrong thread)";
504 } else if (description == "mutex-bad-read-lock") {
505 return "Read lock of a write locked mutex";
506 } else if (description == "mutex-bad-read-unlock") {
507 return "Read unlock of a write locked mutex";
508 } else if (description == "signal-unsafe-call") {
509 return "Signal-unsafe call inside a signal handler";
510 } else if (description == "errno-in-signal-handler") {
511 return "Overwrite of errno in a signal handler";
512 } else if (description == "lock-order-inversion") {
513 return "Lock order inversion (potential deadlock)";
516 // for unknown report codes just show the code
520 static std::string Sprintf(const char *format, ...) {
523 va_start(args, format);
524 s.PrintfVarArg(format, args);
526 return s.GetString();
529 static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) {
530 lldb_private::Address so_addr;
531 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
535 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
539 std::string sym_name = symbol->GetName().GetCString();
543 static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr,
545 lldb_private::Address so_addr;
546 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
550 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
554 ConstString sym_name = symbol->GetMangled().GetName(
555 lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
557 ModuleSP module = symbol->CalculateSymbolContextModule();
561 VariableList var_list;
562 module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list);
563 if (var_list.GetSize() < 1)
566 VariableSP var = var_list.GetVariableAtIndex(0);
567 decl = var->GetDeclaration();
570 addr_t ThreadSanitizerRuntime::GetFirstNonInternalFramePc(
571 StructuredData::ObjectSP trace) {
572 ProcessSP process_sp = GetProcessSP();
573 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
576 trace->GetAsArray()->ForEach([process_sp, runtime_module_sp,
577 &result](StructuredData::Object *o) -> bool {
578 addr_t addr = o->GetIntegerValue();
579 lldb_private::Address so_addr;
580 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
584 if (so_addr.GetModule() == runtime_module_sp)
595 ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) {
596 ProcessSP process_sp = GetProcessSP();
598 std::string summary = report->GetAsDictionary()
599 ->GetValueForKey("description")
603 if (report->GetAsDictionary()
604 ->GetValueForKey("mops")
607 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
608 ->GetValueForKey("mops")
612 ->GetValueForKey("trace"));
614 if (report->GetAsDictionary()
615 ->GetValueForKey("stacks")
618 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
619 ->GetValueForKey("stacks")
623 ->GetValueForKey("trace"));
626 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
629 if (report->GetAsDictionary()
630 ->GetValueForKey("locs")
633 StructuredData::ObjectSP loc = report->GetAsDictionary()
634 ->GetValueForKey("locs")
637 addr_t addr = loc->GetAsDictionary()
638 ->GetValueForKey("address")
642 addr = loc->GetAsDictionary()
643 ->GetValueForKey("start")
648 std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
649 if (!global_name.empty()) {
650 summary = summary + " at " + global_name;
652 summary = summary + " at " + Sprintf("0x%llx", addr);
655 int fd = loc->GetAsDictionary()
656 ->GetValueForKey("file_descriptor")
660 summary = summary + " on file descriptor " + Sprintf("%d", fd);
669 ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) {
670 addr_t result = (addr_t)-1;
672 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
673 [&result](StructuredData::Object *o) -> bool {
675 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
681 return (result == (addr_t)-1) ? 0 : result;
684 std::string ThreadSanitizerRuntime::GetLocationDescription(
685 StructuredData::ObjectSP report, addr_t &global_addr,
686 std::string &global_name, std::string &filename, uint32_t &line) {
687 std::string result = "";
689 ProcessSP process_sp = GetProcessSP();
691 if (report->GetAsDictionary()
692 ->GetValueForKey("locs")
695 StructuredData::ObjectSP loc = report->GetAsDictionary()
696 ->GetValueForKey("locs")
700 loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
701 if (type == "global") {
702 global_addr = loc->GetAsDictionary()
703 ->GetValueForKey("address")
706 global_name = GetSymbolNameFromAddress(process_sp, global_addr);
707 if (!global_name.empty()) {
708 result = Sprintf("'%s' is a global variable (0x%llx)",
709 global_name.c_str(), global_addr);
711 result = Sprintf("0x%llx is a global variable", global_addr);
715 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
716 if (decl.GetFile()) {
717 filename = decl.GetFile().GetPath();
718 line = decl.GetLine();
720 } else if (type == "heap") {
721 addr_t addr = loc->GetAsDictionary()
722 ->GetValueForKey("start")
725 long size = loc->GetAsDictionary()
726 ->GetValueForKey("size")
730 Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
731 } else if (type == "stack") {
732 int tid = loc->GetAsDictionary()
733 ->GetValueForKey("thread_id")
736 result = Sprintf("Location is stack of thread %d", tid);
737 } else if (type == "tls") {
738 int tid = loc->GetAsDictionary()
739 ->GetValueForKey("thread_id")
742 result = Sprintf("Location is TLS of thread %d", tid);
743 } else if (type == "fd") {
744 int fd = loc->GetAsDictionary()
745 ->GetValueForKey("file_descriptor")
748 result = Sprintf("Location is file descriptor %d", fd);
755 bool ThreadSanitizerRuntime::NotifyBreakpointHit(
756 void *baton, StoppointCallbackContext *context, user_id_t break_id,
757 user_id_t break_loc_id) {
758 assert(baton && "null baton");
762 ThreadSanitizerRuntime *const instance =
763 static_cast<ThreadSanitizerRuntime *>(baton);
765 StructuredData::ObjectSP report =
766 instance->RetrieveReportData(context->exe_ctx_ref);
767 std::string stop_reason_description;
769 std::string issue_description = instance->FormatDescription(report);
770 report->GetAsDictionary()->AddStringItem("description", issue_description);
771 stop_reason_description = issue_description + " detected";
772 report->GetAsDictionary()->AddStringItem("stop_description",
773 stop_reason_description);
774 std::string summary = instance->GenerateSummary(report);
775 report->GetAsDictionary()->AddStringItem("summary", summary);
776 addr_t main_address = instance->GetMainRacyAddress(report);
777 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
779 addr_t global_addr = 0;
780 std::string global_name = "";
781 std::string location_filename = "";
782 uint32_t location_line = 0;
783 std::string location_description = instance->GetLocationDescription(
784 report, global_addr, global_name, location_filename, location_line);
785 report->GetAsDictionary()->AddStringItem("location_description",
786 location_description);
787 if (global_addr != 0) {
788 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
790 if (!global_name.empty()) {
791 report->GetAsDictionary()->AddStringItem("global_name", global_name);
793 if (location_filename != "") {
794 report->GetAsDictionary()->AddStringItem("location_filename",
796 report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
799 bool all_addresses_are_same = true;
800 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
801 [&all_addresses_are_same,
802 main_address](StructuredData::Object *o) -> bool {
804 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
805 if (main_address != addr)
806 all_addresses_are_same = false;
809 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
810 all_addresses_are_same);
813 ProcessSP process_sp = instance->GetProcessSP();
814 // Make sure this is the right process
815 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
816 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
818 thread_sp->SetStopInfo(
819 InstrumentationRuntimeStopInfo::
820 CreateStopReasonWithInstrumentationData(
821 *thread_sp, stop_reason_description, report));
823 StreamFileSP stream_sp(
824 process_sp->GetTarget().GetDebugger().GetOutputFile());
826 stream_sp->Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
827 "info -s' to get extended information about the "
830 return true; // Return true to stop the target
832 return false; // Let target run
835 const RegularExpression &ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() {
836 static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
840 bool ThreadSanitizerRuntime::CheckIfRuntimeIsValid(
841 const lldb::ModuleSP module_sp) {
842 static ConstString g_tsan_get_current_report("__tsan_get_current_report");
843 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
844 g_tsan_get_current_report, lldb::eSymbolTypeAny);
845 return symbol != nullptr;
848 void ThreadSanitizerRuntime::Activate() {
852 ProcessSP process_sp = GetProcessSP();
856 ConstString symbol_name("__tsan_on_report");
857 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
858 symbol_name, eSymbolTypeCode);
863 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
866 Target &target = process_sp->GetTarget();
867 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
869 if (symbol_address == LLDB_INVALID_ADDRESS)
872 bool internal = true;
873 bool hardware = false;
874 Breakpoint *breakpoint =
875 process_sp->GetTarget()
876 .CreateBreakpoint(symbol_address, internal, hardware)
878 breakpoint->SetCallback(ThreadSanitizerRuntime::NotifyBreakpointHit, this,
880 breakpoint->SetBreakpointKind("thread-sanitizer-report");
881 SetBreakpointID(breakpoint->GetID());
883 StreamFileSP stream_sp(process_sp->GetTarget().GetDebugger().GetOutputFile());
885 stream_sp->Printf("ThreadSanitizer debugger support is active.\n");
891 void ThreadSanitizerRuntime::Deactivate() {
892 if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
893 ProcessSP process_sp = GetProcessSP();
895 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
896 SetBreakpointID(LLDB_INVALID_BREAK_ID);
901 static std::string GenerateThreadName(const std::string &path,
902 StructuredData::Object *o,
903 StructuredData::ObjectSP main_info) {
904 std::string result = "additional information";
906 if (path == "mops") {
907 int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
909 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
911 o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
913 o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
914 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
916 std::string addr_string = Sprintf(" at 0x%llx", addr);
918 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")
919 ->GetBooleanValue()) {
923 result = Sprintf("%s%s of size %d%s by thread %d",
924 is_atomic ? "atomic " : "", is_write ? "write" : "read",
925 size, addr_string.c_str(), thread_id);
928 if (path == "threads") {
930 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
931 result = Sprintf("Thread %d created", thread_id);
934 if (path == "locs") {
936 o->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
938 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
940 o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
941 if (type == "heap") {
942 result = Sprintf("Heap block allocated by thread %d", thread_id);
943 } else if (type == "fd") {
945 Sprintf("File descriptor %d created by thread %t", fd, thread_id);
949 if (path == "mutexes") {
951 o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
953 result = Sprintf("Mutex M%d created", mutex_id);
956 if (path == "stacks") {
958 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
959 result = Sprintf("Thread %d", thread_id);
962 result[0] = toupper(result[0]);
967 static void AddThreadsForPath(const std::string &path,
968 ThreadCollectionSP threads, ProcessSP process_sp,
969 StructuredData::ObjectSP info) {
970 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach(
971 [process_sp, threads, path, info](StructuredData::Object *o) -> bool {
972 std::vector<lldb::addr_t> pcs;
973 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach(
974 [&pcs](StructuredData::Object *pc) -> bool {
975 pcs.push_back(pc->GetAsInteger()->GetValue());
982 StructuredData::ObjectSP thread_id_obj =
983 o->GetObjectForDotSeparatedPath("thread_os_id");
984 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
986 uint32_t stop_id = 0;
987 bool stop_id_is_valid = false;
988 HistoryThread *history_thread =
989 new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid);
990 ThreadSP new_thread_sp(history_thread);
991 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
993 // Save this in the Process' ExtendedThreadList so a strong pointer
994 // retains the object
995 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
996 threads->AddThread(new_thread_sp);
1002 lldb::ThreadCollectionSP
1003 ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(
1004 StructuredData::ObjectSP info) {
1005 ThreadCollectionSP threads;
1006 threads.reset(new ThreadCollection());
1008 if (info->GetObjectForDotSeparatedPath("instrumentation_class")
1009 ->GetStringValue() != "ThreadSanitizer")
1012 ProcessSP process_sp = GetProcessSP();
1014 AddThreadsForPath("stacks", threads, process_sp, info);
1015 AddThreadsForPath("mops", threads, process_sp, info);
1016 AddThreadsForPath("locs", threads, process_sp, info);
1017 AddThreadsForPath("mutexes", threads, process_sp, info);
1018 AddThreadsForPath("threads", threads, process_sp, info);