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/StreamFile.h"
19 #include "lldb/Core/ValueObject.h"
20 #include "lldb/Expression/UserExpression.h"
21 #include "lldb/Interpreter/CommandReturnObject.h"
22 #include "lldb/Symbol/Symbol.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Symbol/Variable.h"
25 #include "lldb/Symbol/VariableList.h"
26 #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
27 #include "lldb/Target/SectionLoadList.h"
28 #include "lldb/Target/StopInfo.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Utility/RegularExpression.h"
32 #include "lldb/Utility/Stream.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);
89 // TODO: dlsym won't work on Windows.
90 void *dlsym(void* handle, const char* symbol);
91 int (*ptr__tsan_get_report_loc_object_type)(void *report, unsigned long idx, const char **object_type);
94 const int REPORT_TRACE_SIZE = 128;
95 const int REPORT_ARRAY_SIZE = 4;
99 const char *description;
102 void *sleep_trace[REPORT_TRACE_SIZE];
107 void *trace[REPORT_TRACE_SIZE];
108 } stacks[REPORT_ARRAY_SIZE];
118 void *trace[REPORT_TRACE_SIZE];
119 } mops[REPORT_ARRAY_SIZE];
131 void *trace[REPORT_TRACE_SIZE];
132 const char *object_type;
133 } locs[REPORT_ARRAY_SIZE];
138 unsigned long mutex_id;
141 void *trace[REPORT_TRACE_SIZE];
142 } mutexes[REPORT_ARRAY_SIZE];
152 void *trace[REPORT_TRACE_SIZE];
153 } threads[REPORT_ARRAY_SIZE];
155 int unique_tid_count;
159 } unique_tids[REPORT_ARRAY_SIZE];
163 const char *thread_sanitizer_retrieve_report_data_command = R"(
166 ptr__tsan_get_report_loc_object_type = (typeof(ptr__tsan_get_report_loc_object_type))(void *)dlsym((void*)-2 /*RTLD_DEFAULT*/, "__tsan_get_report_loc_object_type");
168 t.report = __tsan_get_current_report();
169 __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);
171 if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
172 for (int i = 0; i < t.stack_count; i++) {
174 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
177 if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
178 for (int i = 0; i < t.mop_count; i++) {
180 __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);
183 if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
184 for (int i = 0; i < t.loc_count; i++) {
186 __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);
187 if (ptr__tsan_get_report_loc_object_type)
188 ptr__tsan_get_report_loc_object_type(t.report, i, &t.locs[i].object_type);
191 if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
192 for (int i = 0; i < t.mutex_count; i++) {
193 t.mutexes[i].idx = i;
194 __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);
197 if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
198 for (int i = 0; i < t.thread_count; i++) {
199 t.threads[i].idx = i;
200 __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);
203 if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
204 for (int i = 0; i < t.unique_tid_count; i++) {
205 t.unique_tids[i].idx = i;
206 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
212 static StructuredData::Array *
213 CreateStackTrace(ValueObjectSP o,
214 const std::string &trace_item_name = ".trace") {
215 StructuredData::Array *trace = new StructuredData::Array();
216 ValueObjectSP trace_value_object =
217 o->GetValueForExpressionPath(trace_item_name.c_str());
218 size_t count = trace_value_object->GetNumChildren();
219 for (size_t j = 0; j < count; j++) {
221 trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
225 StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
230 static StructuredData::Array *ConvertToStructuredArray(
231 ValueObjectSP return_value_sp, const std::string &items_name,
232 const std::string &count_name,
233 std::function<void(ValueObjectSP o, StructuredData::Dictionary *dict)> const
235 StructuredData::Array *array = new StructuredData::Array();
237 return_value_sp->GetValueForExpressionPath(count_name.c_str())
238 ->GetValueAsUnsigned(0);
239 ValueObjectSP objects =
240 return_value_sp->GetValueForExpressionPath(items_name.c_str());
241 for (unsigned int i = 0; i < count; i++) {
242 ValueObjectSP o = objects->GetChildAtIndex(i, true);
243 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
247 array->AddItem(StructuredData::ObjectSP(dict));
252 static std::string RetrieveString(ValueObjectSP return_value_sp,
253 ProcessSP process_sp,
254 const std::string &expression_path) {
256 return_value_sp->GetValueForExpressionPath(expression_path.c_str())
257 ->GetValueAsUnsigned(0);
260 process_sp->ReadCStringFromMemory(ptr, str, error);
265 GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data,
266 std::map<uint64_t, user_id_t> &thread_id_map) {
267 ConvertToStructuredArray(
268 data, ".threads", ".thread_count",
269 [process_sp, &thread_id_map](ValueObjectSP o,
270 StructuredData::Dictionary *dict) {
272 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
273 uint64_t thread_os_id =
274 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
275 user_id_t lldb_user_id = 0;
277 bool can_update = true;
278 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(
279 thread_os_id, can_update);
281 lldb_user_id = lldb_thread->GetIndexID();
283 // This isn't a live thread anymore. Ask process to assign a new
284 // Index ID (or return an old one if we've already seen this
286 // It will also make sure that no new threads are assigned this Index
288 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
291 thread_id_map[thread_id] = lldb_user_id;
295 static user_id_t Renumber(uint64_t id,
296 std::map<uint64_t, user_id_t> &thread_id_map) {
297 auto IT = thread_id_map.find(id);
298 if (IT == thread_id_map.end())
304 StructuredData::ObjectSP
305 ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
306 ProcessSP process_sp = GetProcessSP();
308 return StructuredData::ObjectSP();
310 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
311 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
314 return StructuredData::ObjectSP();
316 EvaluateExpressionOptions options;
317 options.SetUnwindOnError(true);
318 options.SetTryAllThreads(true);
319 options.SetStopOthers(true);
320 options.SetIgnoreBreakpoints(true);
321 options.SetTimeout(g_retrieve_data_function_timeout);
322 options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
323 options.SetAutoApplyFixIts(false);
324 options.SetLanguage(eLanguageTypeObjC_plus_plus);
326 ValueObjectSP main_value;
327 ExecutionContext exe_ctx;
329 frame_sp->CalculateExecutionContext(exe_ctx);
330 ExpressionResults result = UserExpression::Evaluate(
331 exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "",
332 main_value, eval_error);
333 if (result != eExpressionCompleted) {
334 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
335 "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n",
336 eval_error.AsCString());
337 return StructuredData::ObjectSP();
340 std::map<uint64_t, user_id_t> thread_id_map;
341 GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
343 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
344 dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
345 dict->AddStringItem("issue_type",
346 RetrieveString(main_value, process_sp, ".description"));
347 dict->AddIntegerItem("report_count",
348 main_value->GetValueForExpressionPath(".report_count")
349 ->GetValueAsUnsigned(0));
350 dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(
351 main_value, ".sleep_trace")));
353 StructuredData::Array *stacks = ConvertToStructuredArray(
354 main_value, ".stacks", ".stack_count",
355 [thread_sp](ValueObjectSP o, StructuredData::Dictionary *dict) {
356 dict->AddIntegerItem(
358 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
359 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
360 // "stacks" happen on the current thread
361 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
363 dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
365 StructuredData::Array *mops = ConvertToStructuredArray(
366 main_value, ".mops", ".mop_count",
367 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
368 dict->AddIntegerItem(
370 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
371 dict->AddIntegerItem(
374 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
376 dict->AddIntegerItem(
378 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
379 dict->AddBooleanItem(
381 o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
382 dict->AddBooleanItem(
384 o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
385 dict->AddIntegerItem(
387 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
388 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
390 dict->AddItem("mops", StructuredData::ObjectSP(mops));
392 StructuredData::Array *locs = ConvertToStructuredArray(
393 main_value, ".locs", ".loc_count",
394 [process_sp, &thread_id_map](ValueObjectSP o,
395 StructuredData::Dictionary *dict) {
396 dict->AddIntegerItem(
398 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
399 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
400 dict->AddIntegerItem(
402 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
403 dict->AddIntegerItem(
405 o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
406 dict->AddIntegerItem(
408 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
409 dict->AddIntegerItem(
412 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
414 dict->AddIntegerItem(
416 o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
417 dict->AddIntegerItem("suppressable",
418 o->GetValueForExpressionPath(".suppressable")
419 ->GetValueAsUnsigned(0));
420 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
421 dict->AddStringItem("object_type",
422 RetrieveString(o, process_sp, ".object_type"));
424 dict->AddItem("locs", StructuredData::ObjectSP(locs));
426 StructuredData::Array *mutexes = ConvertToStructuredArray(
427 main_value, ".mutexes", ".mutex_count",
428 [](ValueObjectSP o, StructuredData::Dictionary *dict) {
429 dict->AddIntegerItem(
431 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
432 dict->AddIntegerItem(
434 o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
435 dict->AddIntegerItem(
437 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
438 dict->AddIntegerItem(
440 o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
441 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
443 dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
445 StructuredData::Array *threads = ConvertToStructuredArray(
446 main_value, ".threads", ".thread_count",
447 [process_sp, &thread_id_map](ValueObjectSP o,
448 StructuredData::Dictionary *dict) {
449 dict->AddIntegerItem(
451 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
452 dict->AddIntegerItem(
455 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
457 dict->AddIntegerItem(
459 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
460 dict->AddIntegerItem(
462 o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
463 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
464 dict->AddIntegerItem(
466 Renumber(o->GetValueForExpressionPath(".parent_tid")
467 ->GetValueAsUnsigned(0),
469 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
471 dict->AddItem("threads", StructuredData::ObjectSP(threads));
473 StructuredData::Array *unique_tids = ConvertToStructuredArray(
474 main_value, ".unique_tids", ".unique_tid_count",
475 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
476 dict->AddIntegerItem(
478 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
479 dict->AddIntegerItem(
482 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
485 dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
487 return StructuredData::ObjectSP(dict);
491 ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) {
492 std::string description = report->GetAsDictionary()
493 ->GetValueForKey("issue_type")
497 if (description == "data-race") {
499 } else if (description == "data-race-vptr") {
500 return "Data race on C++ virtual pointer";
501 } else if (description == "heap-use-after-free") {
502 return "Use of deallocated memory";
503 } else if (description == "heap-use-after-free-vptr") {
504 return "Use of deallocated C++ virtual pointer";
505 } else if (description == "thread-leak") {
506 return "Thread leak";
507 } else if (description == "locked-mutex-destroy") {
508 return "Destruction of a locked mutex";
509 } else if (description == "mutex-double-lock") {
510 return "Double lock of a mutex";
511 } else if (description == "mutex-invalid-access") {
512 return "Use of an uninitialized or destroyed mutex";
513 } else if (description == "mutex-bad-unlock") {
514 return "Unlock of an unlocked mutex (or by a wrong thread)";
515 } else if (description == "mutex-bad-read-lock") {
516 return "Read lock of a write locked mutex";
517 } else if (description == "mutex-bad-read-unlock") {
518 return "Read unlock of a write locked mutex";
519 } else if (description == "signal-unsafe-call") {
520 return "Signal-unsafe call inside a signal handler";
521 } else if (description == "errno-in-signal-handler") {
522 return "Overwrite of errno in a signal handler";
523 } else if (description == "lock-order-inversion") {
524 return "Lock order inversion (potential deadlock)";
525 } else if (description == "external-race") {
526 return "Race on a library object";
527 } else if (description == "swift-access-race") {
528 return "Swift access race";
531 // for unknown report codes just show the code
535 static std::string Sprintf(const char *format, ...) {
538 va_start(args, format);
539 s.PrintfVarArg(format, args);
541 return s.GetString();
544 static std::string GetSymbolNameFromAddress(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 std::string sym_name = symbol->GetName().GetCString();
558 static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr,
560 lldb_private::Address so_addr;
561 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
565 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
569 ConstString sym_name = symbol->GetMangled().GetName(
570 lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
572 ModuleSP module = symbol->CalculateSymbolContextModule();
576 VariableList var_list;
577 module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list);
578 if (var_list.GetSize() < 1)
581 VariableSP var = var_list.GetVariableAtIndex(0);
582 decl = var->GetDeclaration();
585 addr_t ThreadSanitizerRuntime::GetFirstNonInternalFramePc(
586 StructuredData::ObjectSP trace, bool skip_one_frame) {
587 ProcessSP process_sp = GetProcessSP();
588 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
590 StructuredData::Array *trace_array = trace->GetAsArray();
591 for (int i = 0; i < trace_array->GetSize(); i++) {
592 if (skip_one_frame && i == 0)
596 if (!trace_array->GetItemAtIndexAsInteger(i, addr))
599 lldb_private::Address so_addr;
600 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
604 if (so_addr.GetModule() == runtime_module_sp)
614 ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) {
615 ProcessSP process_sp = GetProcessSP();
617 std::string summary = report->GetAsDictionary()
618 ->GetValueForKey("description")
621 bool skip_one_frame =
622 report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
626 if (report->GetAsDictionary()
627 ->GetValueForKey("mops")
630 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
631 ->GetValueForKey("mops")
635 ->GetValueForKey("trace"),
638 if (report->GetAsDictionary()
639 ->GetValueForKey("stacks")
642 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
643 ->GetValueForKey("stacks")
647 ->GetValueForKey("trace"),
651 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
654 if (report->GetAsDictionary()
655 ->GetValueForKey("locs")
658 StructuredData::ObjectSP loc = report->GetAsDictionary()
659 ->GetValueForKey("locs")
662 std::string object_type = loc->GetAsDictionary()
663 ->GetValueForKey("object_type")
666 if (!object_type.empty()) {
667 summary = "Race on " + object_type + " object";
669 addr_t addr = loc->GetAsDictionary()
670 ->GetValueForKey("address")
674 addr = loc->GetAsDictionary()
675 ->GetValueForKey("start")
680 std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
681 if (!global_name.empty()) {
682 summary = summary + " at " + global_name;
684 summary = summary + " at " + Sprintf("0x%llx", addr);
687 int fd = loc->GetAsDictionary()
688 ->GetValueForKey("file_descriptor")
692 summary = summary + " on file descriptor " + Sprintf("%d", fd);
701 ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) {
702 addr_t result = (addr_t)-1;
704 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
705 [&result](StructuredData::Object *o) -> bool {
707 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
713 return (result == (addr_t)-1) ? 0 : result;
716 std::string ThreadSanitizerRuntime::GetLocationDescription(
717 StructuredData::ObjectSP report, addr_t &global_addr,
718 std::string &global_name, std::string &filename, uint32_t &line) {
719 std::string result = "";
721 ProcessSP process_sp = GetProcessSP();
723 if (report->GetAsDictionary()
724 ->GetValueForKey("locs")
727 StructuredData::ObjectSP loc = report->GetAsDictionary()
728 ->GetValueForKey("locs")
732 loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
733 if (type == "global") {
734 global_addr = loc->GetAsDictionary()
735 ->GetValueForKey("address")
738 global_name = GetSymbolNameFromAddress(process_sp, global_addr);
739 if (!global_name.empty()) {
740 result = Sprintf("'%s' is a global variable (0x%llx)",
741 global_name.c_str(), global_addr);
743 result = Sprintf("0x%llx is a global variable", global_addr);
747 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
748 if (decl.GetFile()) {
749 filename = decl.GetFile().GetPath();
750 line = decl.GetLine();
752 } else if (type == "heap") {
753 addr_t addr = loc->GetAsDictionary()
754 ->GetValueForKey("start")
757 long size = loc->GetAsDictionary()
758 ->GetValueForKey("size")
761 std::string object_type = loc->GetAsDictionary()
762 ->GetValueForKey("object_type")
765 if (!object_type.empty()) {
766 result = Sprintf("Location is a %ld-byte %s object at 0x%llx", size,
767 object_type.c_str(), addr);
770 Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
772 } else if (type == "stack") {
773 int tid = loc->GetAsDictionary()
774 ->GetValueForKey("thread_id")
777 result = Sprintf("Location is stack of thread %d", tid);
778 } else if (type == "tls") {
779 int tid = loc->GetAsDictionary()
780 ->GetValueForKey("thread_id")
783 result = Sprintf("Location is TLS of thread %d", tid);
784 } else if (type == "fd") {
785 int fd = loc->GetAsDictionary()
786 ->GetValueForKey("file_descriptor")
789 result = Sprintf("Location is file descriptor %d", fd);
796 bool ThreadSanitizerRuntime::NotifyBreakpointHit(
797 void *baton, StoppointCallbackContext *context, user_id_t break_id,
798 user_id_t break_loc_id) {
799 assert(baton && "null baton");
803 ThreadSanitizerRuntime *const instance =
804 static_cast<ThreadSanitizerRuntime *>(baton);
806 StructuredData::ObjectSP report =
807 instance->RetrieveReportData(context->exe_ctx_ref);
808 std::string stop_reason_description;
810 std::string issue_description = instance->FormatDescription(report);
811 report->GetAsDictionary()->AddStringItem("description", issue_description);
812 stop_reason_description = issue_description + " detected";
813 report->GetAsDictionary()->AddStringItem("stop_description",
814 stop_reason_description);
815 std::string summary = instance->GenerateSummary(report);
816 report->GetAsDictionary()->AddStringItem("summary", summary);
817 addr_t main_address = instance->GetMainRacyAddress(report);
818 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
820 addr_t global_addr = 0;
821 std::string global_name = "";
822 std::string location_filename = "";
823 uint32_t location_line = 0;
824 std::string location_description = instance->GetLocationDescription(
825 report, global_addr, global_name, location_filename, location_line);
826 report->GetAsDictionary()->AddStringItem("location_description",
827 location_description);
828 if (global_addr != 0) {
829 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
831 if (!global_name.empty()) {
832 report->GetAsDictionary()->AddStringItem("global_name", global_name);
834 if (location_filename != "") {
835 report->GetAsDictionary()->AddStringItem("location_filename",
837 report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
840 bool all_addresses_are_same = true;
841 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
842 [&all_addresses_are_same,
843 main_address](StructuredData::Object *o) -> bool {
845 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
846 if (main_address != addr)
847 all_addresses_are_same = false;
850 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
851 all_addresses_are_same);
854 ProcessSP process_sp = instance->GetProcessSP();
855 // Make sure this is the right process
856 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
857 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
859 thread_sp->SetStopInfo(
860 InstrumentationRuntimeStopInfo::
861 CreateStopReasonWithInstrumentationData(
862 *thread_sp, stop_reason_description, report));
864 StreamFileSP stream_sp(
865 process_sp->GetTarget().GetDebugger().GetOutputFile());
867 stream_sp->Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
868 "info -s' to get extended information about the "
871 return true; // Return true to stop the target
873 return false; // Let target run
876 const RegularExpression &ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() {
877 static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
881 bool ThreadSanitizerRuntime::CheckIfRuntimeIsValid(
882 const lldb::ModuleSP module_sp) {
883 static ConstString g_tsan_get_current_report("__tsan_get_current_report");
884 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
885 g_tsan_get_current_report, lldb::eSymbolTypeAny);
886 return symbol != nullptr;
889 void ThreadSanitizerRuntime::Activate() {
893 ProcessSP process_sp = GetProcessSP();
897 ConstString symbol_name("__tsan_on_report");
898 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
899 symbol_name, eSymbolTypeCode);
904 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
907 Target &target = process_sp->GetTarget();
908 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
910 if (symbol_address == LLDB_INVALID_ADDRESS)
913 bool internal = true;
914 bool hardware = false;
915 Breakpoint *breakpoint =
916 process_sp->GetTarget()
917 .CreateBreakpoint(symbol_address, internal, hardware)
919 breakpoint->SetCallback(ThreadSanitizerRuntime::NotifyBreakpointHit, this,
921 breakpoint->SetBreakpointKind("thread-sanitizer-report");
922 SetBreakpointID(breakpoint->GetID());
924 StreamFileSP stream_sp(process_sp->GetTarget().GetDebugger().GetOutputFile());
926 stream_sp->Printf("ThreadSanitizer debugger support is active.\n");
932 void ThreadSanitizerRuntime::Deactivate() {
933 if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
934 ProcessSP process_sp = GetProcessSP();
936 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
937 SetBreakpointID(LLDB_INVALID_BREAK_ID);
942 static std::string GenerateThreadName(const std::string &path,
943 StructuredData::Object *o,
944 StructuredData::ObjectSP main_info) {
945 std::string result = "additional information";
947 if (path == "mops") {
948 int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
950 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
952 o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
954 o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
955 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
957 std::string addr_string = Sprintf(" at 0x%llx", addr);
959 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")
960 ->GetBooleanValue()) {
964 if (main_info->GetObjectForDotSeparatedPath("issue_type")
965 ->GetStringValue() == "external-race") {
966 result = Sprintf("%s access by thread %d",
967 is_write ? "mutating" : "read-only", thread_id);
968 } else if (main_info->GetObjectForDotSeparatedPath("issue_type")
969 ->GetStringValue() == "swift-access-race") {
970 result = Sprintf("modifying access by thread %d", thread_id);
972 result = Sprintf("%s%s of size %d%s by thread %d",
973 is_atomic ? "atomic " : "", is_write ? "write" : "read",
974 size, addr_string.c_str(), thread_id);
978 if (path == "threads") {
980 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
981 result = Sprintf("Thread %d created", thread_id);
984 if (path == "locs") {
986 o->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
988 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
990 o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
991 if (type == "heap") {
992 result = Sprintf("Heap block allocated by thread %d", thread_id);
993 } else if (type == "fd") {
995 Sprintf("File descriptor %d created by thread %t", fd, thread_id);
999 if (path == "mutexes") {
1001 o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
1003 result = Sprintf("Mutex M%d created", mutex_id);
1006 if (path == "stacks") {
1008 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
1009 result = Sprintf("Thread %d", thread_id);
1012 result[0] = toupper(result[0]);
1017 static void AddThreadsForPath(const std::string &path,
1018 ThreadCollectionSP threads, ProcessSP process_sp,
1019 StructuredData::ObjectSP info) {
1020 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach(
1021 [process_sp, threads, path, info](StructuredData::Object *o) -> bool {
1022 std::vector<lldb::addr_t> pcs;
1023 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach(
1024 [&pcs](StructuredData::Object *pc) -> bool {
1025 pcs.push_back(pc->GetAsInteger()->GetValue());
1029 if (pcs.size() == 0)
1032 StructuredData::ObjectSP thread_id_obj =
1033 o->GetObjectForDotSeparatedPath("thread_os_id");
1034 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
1036 uint32_t stop_id = 0;
1037 bool stop_id_is_valid = false;
1038 HistoryThread *history_thread =
1039 new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid);
1040 ThreadSP new_thread_sp(history_thread);
1041 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
1043 // Save this in the Process' ExtendedThreadList so a strong pointer
1044 // retains the object
1045 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
1046 threads->AddThread(new_thread_sp);
1052 lldb::ThreadCollectionSP
1053 ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(
1054 StructuredData::ObjectSP info) {
1055 ThreadCollectionSP threads;
1056 threads.reset(new ThreadCollection());
1058 if (info->GetObjectForDotSeparatedPath("instrumentation_class")
1059 ->GetStringValue() != "ThreadSanitizer")
1062 ProcessSP process_sp = GetProcessSP();
1064 AddThreadsForPath("stacks", threads, process_sp, info);
1065 AddThreadsForPath("mops", threads, process_sp, info);
1066 AddThreadsForPath("locs", threads, process_sp, info);
1067 AddThreadsForPath("mutexes", threads, process_sp, info);
1068 AddThreadsForPath("threads", threads, process_sp, info);