1 //===-- TSanRuntime.cpp -----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "TSanRuntime.h"
11 #include "Plugins/Process/Utility/HistoryThread.h"
12 #include "lldb/Breakpoint/StoppointCallbackContext.h"
13 #include "lldb/Core/Debugger.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/PluginInterface.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/StreamFile.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Expression/UserExpression.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Symbol/Symbol.h"
22 #include "lldb/Symbol/SymbolContext.h"
23 #include "lldb/Symbol/Variable.h"
24 #include "lldb/Symbol/VariableList.h"
25 #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/StopInfo.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
30 #include "lldb/Utility/RegularExpression.h"
31 #include "lldb/Utility/Stream.h"
36 using namespace lldb_private;
38 lldb::InstrumentationRuntimeSP
39 ThreadSanitizerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) {
40 return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp));
43 void ThreadSanitizerRuntime::Initialize() {
44 PluginManager::RegisterPlugin(
45 GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.",
46 CreateInstance, GetTypeStatic);
49 void ThreadSanitizerRuntime::Terminate() {
50 PluginManager::UnregisterPlugin(CreateInstance);
53 lldb_private::ConstString ThreadSanitizerRuntime::GetPluginNameStatic() {
54 return ConstString("ThreadSanitizer");
57 lldb::InstrumentationRuntimeType ThreadSanitizerRuntime::GetTypeStatic() {
58 return eInstrumentationRuntimeTypeThreadSanitizer;
61 ThreadSanitizerRuntime::~ThreadSanitizerRuntime() { Deactivate(); }
63 const char *thread_sanitizer_retrieve_report_data_prefix = R"(
66 void *__tsan_get_current_report();
67 int __tsan_get_report_data(void *report, const char **description, int *count,
68 int *stack_count, int *mop_count, int *loc_count,
69 int *mutex_count, int *thread_count,
70 int *unique_tid_count, void **sleep_trace,
71 unsigned long trace_size);
72 int __tsan_get_report_stack(void *report, unsigned long idx, void **trace,
73 unsigned long trace_size);
74 int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr,
75 int *size, int *write, int *atomic, void **trace,
76 unsigned long trace_size);
77 int __tsan_get_report_loc(void *report, unsigned long idx, const char **type,
78 void **addr, unsigned long *start, unsigned long *size, int *tid,
79 int *fd, int *suppressable, void **trace,
80 unsigned long trace_size);
81 int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr,
82 int *destroyed, void **trace, unsigned long trace_size);
83 int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id,
84 int *running, const char **name, int *parent_tid,
85 void **trace, unsigned long trace_size);
86 int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid);
88 // TODO: dlsym won't work on Windows.
89 void *dlsym(void* handle, const char* symbol);
90 int (*ptr__tsan_get_report_loc_object_type)(void *report, unsigned long idx, const char **object_type);
93 const int REPORT_TRACE_SIZE = 128;
94 const int REPORT_ARRAY_SIZE = 4;
98 const char *description;
101 void *sleep_trace[REPORT_TRACE_SIZE];
106 void *trace[REPORT_TRACE_SIZE];
107 } stacks[REPORT_ARRAY_SIZE];
117 void *trace[REPORT_TRACE_SIZE];
118 } mops[REPORT_ARRAY_SIZE];
130 void *trace[REPORT_TRACE_SIZE];
131 const char *object_type;
132 } locs[REPORT_ARRAY_SIZE];
137 unsigned long mutex_id;
140 void *trace[REPORT_TRACE_SIZE];
141 } mutexes[REPORT_ARRAY_SIZE];
151 void *trace[REPORT_TRACE_SIZE];
152 } threads[REPORT_ARRAY_SIZE];
154 int unique_tid_count;
158 } unique_tids[REPORT_ARRAY_SIZE];
162 const char *thread_sanitizer_retrieve_report_data_command = R"(
165 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");
167 t.report = __tsan_get_current_report();
168 __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);
170 if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
171 for (int i = 0; i < t.stack_count; i++) {
173 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
176 if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
177 for (int i = 0; i < t.mop_count; i++) {
179 __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);
182 if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
183 for (int i = 0; i < t.loc_count; i++) {
185 __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);
186 if (ptr__tsan_get_report_loc_object_type)
187 ptr__tsan_get_report_loc_object_type(t.report, i, &t.locs[i].object_type);
190 if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
191 for (int i = 0; i < t.mutex_count; i++) {
192 t.mutexes[i].idx = i;
193 __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);
196 if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
197 for (int i = 0; i < t.thread_count; i++) {
198 t.threads[i].idx = i;
199 __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);
202 if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
203 for (int i = 0; i < t.unique_tid_count; i++) {
204 t.unique_tids[i].idx = i;
205 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
211 static StructuredData::Array *
212 CreateStackTrace(ValueObjectSP o,
213 const std::string &trace_item_name = ".trace") {
214 StructuredData::Array *trace = new StructuredData::Array();
215 ValueObjectSP trace_value_object =
216 o->GetValueForExpressionPath(trace_item_name.c_str());
217 size_t count = trace_value_object->GetNumChildren();
218 for (size_t j = 0; j < count; j++) {
220 trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
224 StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
229 static StructuredData::Array *ConvertToStructuredArray(
230 ValueObjectSP return_value_sp, const std::string &items_name,
231 const std::string &count_name,
232 std::function<void(ValueObjectSP o, StructuredData::Dictionary *dict)> const
234 StructuredData::Array *array = new StructuredData::Array();
236 return_value_sp->GetValueForExpressionPath(count_name.c_str())
237 ->GetValueAsUnsigned(0);
238 ValueObjectSP objects =
239 return_value_sp->GetValueForExpressionPath(items_name.c_str());
240 for (unsigned int i = 0; i < count; i++) {
241 ValueObjectSP o = objects->GetChildAtIndex(i, true);
242 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
246 array->AddItem(StructuredData::ObjectSP(dict));
251 static std::string RetrieveString(ValueObjectSP return_value_sp,
252 ProcessSP process_sp,
253 const std::string &expression_path) {
255 return_value_sp->GetValueForExpressionPath(expression_path.c_str())
256 ->GetValueAsUnsigned(0);
259 process_sp->ReadCStringFromMemory(ptr, str, error);
264 GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data,
265 std::map<uint64_t, user_id_t> &thread_id_map) {
266 ConvertToStructuredArray(
267 data, ".threads", ".thread_count",
268 [process_sp, &thread_id_map](ValueObjectSP o,
269 StructuredData::Dictionary *dict) {
271 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
272 uint64_t thread_os_id =
273 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
274 user_id_t lldb_user_id = 0;
276 bool can_update = true;
277 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(
278 thread_os_id, can_update);
280 lldb_user_id = lldb_thread->GetIndexID();
282 // This isn't a live thread anymore. Ask process to assign a new
283 // Index ID (or return an old one if we've already seen this
284 // thread_os_id). It will also make sure that no new threads are
285 // assigned this Index ID.
286 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
289 thread_id_map[thread_id] = lldb_user_id;
293 static user_id_t Renumber(uint64_t id,
294 std::map<uint64_t, user_id_t> &thread_id_map) {
295 auto IT = thread_id_map.find(id);
296 if (IT == thread_id_map.end())
302 StructuredData::ObjectSP
303 ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
304 ProcessSP process_sp = GetProcessSP();
306 return StructuredData::ObjectSP();
308 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
309 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
312 return StructuredData::ObjectSP();
314 EvaluateExpressionOptions options;
315 options.SetUnwindOnError(true);
316 options.SetTryAllThreads(true);
317 options.SetStopOthers(true);
318 options.SetIgnoreBreakpoints(true);
319 options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
320 options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
321 options.SetAutoApplyFixIts(false);
322 options.SetLanguage(eLanguageTypeObjC_plus_plus);
324 ValueObjectSP main_value;
325 ExecutionContext exe_ctx;
327 frame_sp->CalculateExecutionContext(exe_ctx);
328 ExpressionResults result = UserExpression::Evaluate(
329 exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "",
330 main_value, eval_error);
331 if (result != eExpressionCompleted) {
332 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
333 "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n",
334 eval_error.AsCString());
335 return StructuredData::ObjectSP();
338 std::map<uint64_t, user_id_t> thread_id_map;
339 GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
341 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
342 dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
343 dict->AddStringItem("issue_type",
344 RetrieveString(main_value, process_sp, ".description"));
345 dict->AddIntegerItem("report_count",
346 main_value->GetValueForExpressionPath(".report_count")
347 ->GetValueAsUnsigned(0));
348 dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(
349 main_value, ".sleep_trace")));
351 StructuredData::Array *stacks = ConvertToStructuredArray(
352 main_value, ".stacks", ".stack_count",
353 [thread_sp](ValueObjectSP o, StructuredData::Dictionary *dict) {
354 dict->AddIntegerItem(
356 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
357 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
358 // "stacks" happen on the current thread
359 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
361 dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
363 StructuredData::Array *mops = ConvertToStructuredArray(
364 main_value, ".mops", ".mop_count",
365 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
366 dict->AddIntegerItem(
368 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
369 dict->AddIntegerItem(
372 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
374 dict->AddIntegerItem(
376 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
377 dict->AddBooleanItem(
379 o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
380 dict->AddBooleanItem(
382 o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
383 dict->AddIntegerItem(
385 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
386 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
388 dict->AddItem("mops", StructuredData::ObjectSP(mops));
390 StructuredData::Array *locs = ConvertToStructuredArray(
391 main_value, ".locs", ".loc_count",
392 [process_sp, &thread_id_map](ValueObjectSP o,
393 StructuredData::Dictionary *dict) {
394 dict->AddIntegerItem(
396 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
397 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
398 dict->AddIntegerItem(
400 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
401 dict->AddIntegerItem(
403 o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
404 dict->AddIntegerItem(
406 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
407 dict->AddIntegerItem(
410 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
412 dict->AddIntegerItem(
414 o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
415 dict->AddIntegerItem("suppressable",
416 o->GetValueForExpressionPath(".suppressable")
417 ->GetValueAsUnsigned(0));
418 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
419 dict->AddStringItem("object_type",
420 RetrieveString(o, process_sp, ".object_type"));
422 dict->AddItem("locs", StructuredData::ObjectSP(locs));
424 StructuredData::Array *mutexes = ConvertToStructuredArray(
425 main_value, ".mutexes", ".mutex_count",
426 [](ValueObjectSP o, StructuredData::Dictionary *dict) {
427 dict->AddIntegerItem(
429 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
430 dict->AddIntegerItem(
432 o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
433 dict->AddIntegerItem(
435 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
436 dict->AddIntegerItem(
438 o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
439 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
441 dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
443 StructuredData::Array *threads = ConvertToStructuredArray(
444 main_value, ".threads", ".thread_count",
445 [process_sp, &thread_id_map](ValueObjectSP o,
446 StructuredData::Dictionary *dict) {
447 dict->AddIntegerItem(
449 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
450 dict->AddIntegerItem(
453 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
455 dict->AddIntegerItem(
457 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
458 dict->AddIntegerItem(
460 o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
461 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
462 dict->AddIntegerItem(
464 Renumber(o->GetValueForExpressionPath(".parent_tid")
465 ->GetValueAsUnsigned(0),
467 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
469 dict->AddItem("threads", StructuredData::ObjectSP(threads));
471 StructuredData::Array *unique_tids = ConvertToStructuredArray(
472 main_value, ".unique_tids", ".unique_tid_count",
473 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
474 dict->AddIntegerItem(
476 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
477 dict->AddIntegerItem(
480 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
483 dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
485 return StructuredData::ObjectSP(dict);
489 ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) {
490 std::string description = report->GetAsDictionary()
491 ->GetValueForKey("issue_type")
495 if (description == "data-race") {
497 } else if (description == "data-race-vptr") {
498 return "Data race on C++ virtual pointer";
499 } else if (description == "heap-use-after-free") {
500 return "Use of deallocated memory";
501 } else if (description == "heap-use-after-free-vptr") {
502 return "Use of deallocated C++ virtual pointer";
503 } else if (description == "thread-leak") {
504 return "Thread leak";
505 } else if (description == "locked-mutex-destroy") {
506 return "Destruction of a locked mutex";
507 } else if (description == "mutex-double-lock") {
508 return "Double lock of a mutex";
509 } else if (description == "mutex-invalid-access") {
510 return "Use of an uninitialized or destroyed mutex";
511 } else if (description == "mutex-bad-unlock") {
512 return "Unlock of an unlocked mutex (or by a wrong thread)";
513 } else if (description == "mutex-bad-read-lock") {
514 return "Read lock of a write locked mutex";
515 } else if (description == "mutex-bad-read-unlock") {
516 return "Read unlock of a write locked mutex";
517 } else if (description == "signal-unsafe-call") {
518 return "Signal-unsafe call inside a signal handler";
519 } else if (description == "errno-in-signal-handler") {
520 return "Overwrite of errno in a signal handler";
521 } else if (description == "lock-order-inversion") {
522 return "Lock order inversion (potential deadlock)";
523 } else if (description == "external-race") {
524 return "Race on a library object";
525 } else if (description == "swift-access-race") {
526 return "Swift access race";
529 // for unknown report codes just show the code
533 static std::string Sprintf(const char *format, ...) {
536 va_start(args, format);
537 s.PrintfVarArg(format, args);
539 return s.GetString();
542 static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) {
543 lldb_private::Address so_addr;
544 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
548 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
552 std::string sym_name = symbol->GetName().GetCString();
556 static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr,
558 lldb_private::Address so_addr;
559 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
563 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
567 ConstString sym_name = symbol->GetMangled().GetName(
568 lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
570 ModuleSP module = symbol->CalculateSymbolContextModule();
574 VariableList var_list;
575 module->FindGlobalVariables(sym_name, nullptr, 1U, var_list);
576 if (var_list.GetSize() < 1)
579 VariableSP var = var_list.GetVariableAtIndex(0);
580 decl = var->GetDeclaration();
583 addr_t ThreadSanitizerRuntime::GetFirstNonInternalFramePc(
584 StructuredData::ObjectSP trace, bool skip_one_frame) {
585 ProcessSP process_sp = GetProcessSP();
586 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
588 StructuredData::Array *trace_array = trace->GetAsArray();
589 for (size_t i = 0; i < trace_array->GetSize(); i++) {
590 if (skip_one_frame && i == 0)
594 if (!trace_array->GetItemAtIndexAsInteger(i, addr))
597 lldb_private::Address so_addr;
598 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
602 if (so_addr.GetModule() == runtime_module_sp)
612 ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) {
613 ProcessSP process_sp = GetProcessSP();
615 std::string summary = report->GetAsDictionary()
616 ->GetValueForKey("description")
619 bool skip_one_frame =
620 report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
624 if (report->GetAsDictionary()
625 ->GetValueForKey("mops")
628 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
629 ->GetValueForKey("mops")
633 ->GetValueForKey("trace"),
636 if (report->GetAsDictionary()
637 ->GetValueForKey("stacks")
640 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
641 ->GetValueForKey("stacks")
645 ->GetValueForKey("trace"),
649 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
652 if (report->GetAsDictionary()
653 ->GetValueForKey("locs")
656 StructuredData::ObjectSP loc = report->GetAsDictionary()
657 ->GetValueForKey("locs")
660 std::string object_type = loc->GetAsDictionary()
661 ->GetValueForKey("object_type")
664 if (!object_type.empty()) {
665 summary = "Race on " + object_type + " object";
667 addr_t addr = loc->GetAsDictionary()
668 ->GetValueForKey("address")
672 addr = loc->GetAsDictionary()
673 ->GetValueForKey("start")
678 std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
679 if (!global_name.empty()) {
680 summary = summary + " at " + global_name;
682 summary = summary + " at " + Sprintf("0x%llx", addr);
685 int fd = loc->GetAsDictionary()
686 ->GetValueForKey("file_descriptor")
690 summary = summary + " on file descriptor " + Sprintf("%d", fd);
699 ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) {
700 addr_t result = (addr_t)-1;
702 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
703 [&result](StructuredData::Object *o) -> bool {
705 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
711 return (result == (addr_t)-1) ? 0 : result;
714 std::string ThreadSanitizerRuntime::GetLocationDescription(
715 StructuredData::ObjectSP report, addr_t &global_addr,
716 std::string &global_name, std::string &filename, uint32_t &line) {
717 std::string result = "";
719 ProcessSP process_sp = GetProcessSP();
721 if (report->GetAsDictionary()
722 ->GetValueForKey("locs")
725 StructuredData::ObjectSP loc = report->GetAsDictionary()
726 ->GetValueForKey("locs")
730 loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
731 if (type == "global") {
732 global_addr = loc->GetAsDictionary()
733 ->GetValueForKey("address")
736 global_name = GetSymbolNameFromAddress(process_sp, global_addr);
737 if (!global_name.empty()) {
738 result = Sprintf("'%s' is a global variable (0x%llx)",
739 global_name.c_str(), global_addr);
741 result = Sprintf("0x%llx is a global variable", global_addr);
745 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
746 if (decl.GetFile()) {
747 filename = decl.GetFile().GetPath();
748 line = decl.GetLine();
750 } else if (type == "heap") {
751 addr_t addr = loc->GetAsDictionary()
752 ->GetValueForKey("start")
755 long size = loc->GetAsDictionary()
756 ->GetValueForKey("size")
759 std::string object_type = loc->GetAsDictionary()
760 ->GetValueForKey("object_type")
763 if (!object_type.empty()) {
764 result = Sprintf("Location is a %ld-byte %s object at 0x%llx", size,
765 object_type.c_str(), addr);
768 Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
770 } else if (type == "stack") {
771 int tid = loc->GetAsDictionary()
772 ->GetValueForKey("thread_id")
775 result = Sprintf("Location is stack of thread %d", tid);
776 } else if (type == "tls") {
777 int tid = loc->GetAsDictionary()
778 ->GetValueForKey("thread_id")
781 result = Sprintf("Location is TLS of thread %d", tid);
782 } else if (type == "fd") {
783 int fd = loc->GetAsDictionary()
784 ->GetValueForKey("file_descriptor")
787 result = Sprintf("Location is file descriptor %d", fd);
794 bool ThreadSanitizerRuntime::NotifyBreakpointHit(
795 void *baton, StoppointCallbackContext *context, user_id_t break_id,
796 user_id_t break_loc_id) {
797 assert(baton && "null baton");
801 ThreadSanitizerRuntime *const instance =
802 static_cast<ThreadSanitizerRuntime *>(baton);
804 ProcessSP process_sp = instance->GetProcessSP();
806 if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
809 StructuredData::ObjectSP report =
810 instance->RetrieveReportData(context->exe_ctx_ref);
811 std::string stop_reason_description;
813 std::string issue_description = instance->FormatDescription(report);
814 report->GetAsDictionary()->AddStringItem("description", issue_description);
815 stop_reason_description = issue_description + " detected";
816 report->GetAsDictionary()->AddStringItem("stop_description",
817 stop_reason_description);
818 std::string summary = instance->GenerateSummary(report);
819 report->GetAsDictionary()->AddStringItem("summary", summary);
820 addr_t main_address = instance->GetMainRacyAddress(report);
821 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
823 addr_t global_addr = 0;
824 std::string global_name = "";
825 std::string location_filename = "";
826 uint32_t location_line = 0;
827 std::string location_description = instance->GetLocationDescription(
828 report, global_addr, global_name, location_filename, location_line);
829 report->GetAsDictionary()->AddStringItem("location_description",
830 location_description);
831 if (global_addr != 0) {
832 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
834 if (!global_name.empty()) {
835 report->GetAsDictionary()->AddStringItem("global_name", global_name);
837 if (location_filename != "") {
838 report->GetAsDictionary()->AddStringItem("location_filename",
840 report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
843 bool all_addresses_are_same = true;
844 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
845 [&all_addresses_are_same,
846 main_address](StructuredData::Object *o) -> bool {
848 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
849 if (main_address != addr)
850 all_addresses_are_same = false;
853 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
854 all_addresses_are_same);
857 // Make sure this is the right process
858 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
859 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
861 thread_sp->SetStopInfo(
862 InstrumentationRuntimeStopInfo::
863 CreateStopReasonWithInstrumentationData(
864 *thread_sp, stop_reason_description, report));
866 StreamFileSP stream_sp(
867 process_sp->GetTarget().GetDebugger().GetOutputFile());
869 stream_sp->Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
870 "info -s' to get extended information about the "
873 return true; // Return true to stop the target
875 return false; // Let target run
878 const RegularExpression &ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() {
879 static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
883 bool ThreadSanitizerRuntime::CheckIfRuntimeIsValid(
884 const lldb::ModuleSP module_sp) {
885 static ConstString g_tsan_get_current_report("__tsan_get_current_report");
886 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
887 g_tsan_get_current_report, lldb::eSymbolTypeAny);
888 return symbol != nullptr;
891 void ThreadSanitizerRuntime::Activate() {
895 ProcessSP process_sp = GetProcessSP();
899 ConstString symbol_name("__tsan_on_report");
900 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
901 symbol_name, eSymbolTypeCode);
903 if (symbol == nullptr)
906 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
909 Target &target = process_sp->GetTarget();
910 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
912 if (symbol_address == LLDB_INVALID_ADDRESS)
915 bool internal = true;
916 bool hardware = false;
917 Breakpoint *breakpoint =
918 process_sp->GetTarget()
919 .CreateBreakpoint(symbol_address, internal, hardware)
921 breakpoint->SetCallback(ThreadSanitizerRuntime::NotifyBreakpointHit, this,
923 breakpoint->SetBreakpointKind("thread-sanitizer-report");
924 SetBreakpointID(breakpoint->GetID());
929 void ThreadSanitizerRuntime::Deactivate() {
930 if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
931 ProcessSP process_sp = GetProcessSP();
933 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
934 SetBreakpointID(LLDB_INVALID_BREAK_ID);
939 static std::string GenerateThreadName(const std::string &path,
940 StructuredData::Object *o,
941 StructuredData::ObjectSP main_info) {
942 std::string result = "additional information";
944 if (path == "mops") {
945 int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
947 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
949 o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
951 o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
952 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
954 std::string addr_string = Sprintf(" at 0x%llx", addr);
956 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")
957 ->GetBooleanValue()) {
961 if (main_info->GetObjectForDotSeparatedPath("issue_type")
962 ->GetStringValue() == "external-race") {
963 result = Sprintf("%s access by thread %d",
964 is_write ? "mutating" : "read-only", thread_id);
965 } else if (main_info->GetObjectForDotSeparatedPath("issue_type")
966 ->GetStringValue() == "swift-access-race") {
967 result = Sprintf("modifying access by thread %d", thread_id);
969 result = Sprintf("%s%s of size %d%s by thread %d",
970 is_atomic ? "atomic " : "", is_write ? "write" : "read",
971 size, addr_string.c_str(), thread_id);
975 if (path == "threads") {
977 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
978 result = Sprintf("Thread %d created", thread_id);
981 if (path == "locs") {
983 o->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
985 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
987 o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
988 if (type == "heap") {
989 result = Sprintf("Heap block allocated by thread %d", thread_id);
990 } else if (type == "fd") {
992 Sprintf("File descriptor %d created by thread %t", fd, thread_id);
996 if (path == "mutexes") {
998 o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
1000 result = Sprintf("Mutex M%d created", mutex_id);
1003 if (path == "stacks") {
1005 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
1006 result = Sprintf("Thread %d", thread_id);
1009 result[0] = toupper(result[0]);
1014 static void AddThreadsForPath(const std::string &path,
1015 ThreadCollectionSP threads, ProcessSP process_sp,
1016 StructuredData::ObjectSP info) {
1017 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach(
1018 [process_sp, threads, path, info](StructuredData::Object *o) -> bool {
1019 std::vector<lldb::addr_t> pcs;
1020 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach(
1021 [&pcs](StructuredData::Object *pc) -> bool {
1022 pcs.push_back(pc->GetAsInteger()->GetValue());
1026 if (pcs.size() == 0)
1029 StructuredData::ObjectSP thread_id_obj =
1030 o->GetObjectForDotSeparatedPath("thread_os_id");
1031 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
1033 HistoryThread *history_thread =
1034 new HistoryThread(*process_sp, tid, pcs);
1035 ThreadSP new_thread_sp(history_thread);
1036 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
1038 // Save this in the Process' ExtendedThreadList so a strong pointer
1039 // retains the object
1040 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
1041 threads->AddThread(new_thread_sp);
1047 lldb::ThreadCollectionSP
1048 ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(
1049 StructuredData::ObjectSP info) {
1050 ThreadCollectionSP threads;
1051 threads = std::make_shared<ThreadCollection>();
1053 if (info->GetObjectForDotSeparatedPath("instrumentation_class")
1054 ->GetStringValue() != "ThreadSanitizer")
1057 ProcessSP process_sp = GetProcessSP();
1059 AddThreadsForPath("stacks", threads, process_sp, info);
1060 AddThreadsForPath("mops", threads, process_sp, info);
1061 AddThreadsForPath("locs", threads, process_sp, info);
1062 AddThreadsForPath("mutexes", threads, process_sp, info);
1063 AddThreadsForPath("threads", threads, process_sp, info);