LLDB mainline
InstrumentationRuntimeTSan.cpp
Go to the documentation of this file.
1//===-- InstrumentationRuntimeTSan.cpp ------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10
13#include "lldb/Core/Debugger.h"
14#include "lldb/Core/Module.h"
21#include "lldb/Symbol/Symbol.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
31#include "lldb/Utility/Log.h"
33#include "lldb/Utility/Stream.h"
34
35#include <memory>
36
37using namespace lldb;
38using namespace lldb_private;
39
41
45}
46
49 GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.",
51}
52
55}
56
59}
60
62
64extern "C"
65{
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);
87
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);
91}
92)";
93
95
96const int REPORT_TRACE_SIZE = 128;
97const int REPORT_ARRAY_SIZE = 4;
98
99struct {
100 void *report;
101 const char *description;
102 int report_count;
103
104 void *sleep_trace[REPORT_TRACE_SIZE];
105
106 int stack_count;
107 struct {
108 int idx;
109 void *trace[REPORT_TRACE_SIZE];
110 } stacks[REPORT_ARRAY_SIZE];
111
112 int mop_count;
113 struct {
114 int idx;
115 int tid;
116 int size;
117 int write;
118 int atomic;
119 void *addr;
120 void *trace[REPORT_TRACE_SIZE];
121 } mops[REPORT_ARRAY_SIZE];
122
123 int loc_count;
124 struct {
125 int idx;
126 const char *type;
127 void *addr;
128 unsigned long start;
129 unsigned long size;
130 int tid;
131 int fd;
132 int suppressable;
133 void *trace[REPORT_TRACE_SIZE];
134 const char *object_type;
135 } locs[REPORT_ARRAY_SIZE];
136
137 int mutex_count;
138 struct {
139 int idx;
140 unsigned long mutex_id;
141 void *addr;
142 int destroyed;
143 void *trace[REPORT_TRACE_SIZE];
144 } mutexes[REPORT_ARRAY_SIZE];
145
146 int thread_count;
147 struct {
148 int idx;
149 int tid;
150 unsigned long os_id;
151 int running;
152 const char *name;
153 int parent_tid;
154 void *trace[REPORT_TRACE_SIZE];
155 } threads[REPORT_ARRAY_SIZE];
156
157 int unique_tid_count;
158 struct {
159 int idx;
160 int tid;
161 } unique_tids[REPORT_ARRAY_SIZE];
162} t = {0};
163
164ptr__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");
165
166t.report = __tsan_get_current_report();
167__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);
168
169if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
170for (int i = 0; i < t.stack_count; i++) {
171 t.stacks[i].idx = i;
172 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
173}
174
175if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
176for (int i = 0; i < t.mop_count; i++) {
177 t.mops[i].idx = i;
178 __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);
179}
180
181if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
182for (int i = 0; i < t.loc_count; i++) {
183 t.locs[i].idx = i;
184 __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);
185 if (ptr__tsan_get_report_loc_object_type)
186 ptr__tsan_get_report_loc_object_type(t.report, i, &t.locs[i].object_type);
187}
188
189if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
190for (int i = 0; i < t.mutex_count; i++) {
191 t.mutexes[i].idx = i;
192 __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);
193}
194
195if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
196for (int i = 0; i < t.thread_count; i++) {
197 t.threads[i].idx = i;
198 __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);
199}
200
201if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
202for (int i = 0; i < t.unique_tid_count; i++) {
203 t.unique_tids[i].idx = i;
204 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
205}
206
207t;
208)";
209
212 const std::string &trace_item_name = ".trace") {
213 auto trace_sp = std::make_shared<StructuredData::Array>();
214 ValueObjectSP trace_value_object =
215 o->GetValueForExpressionPath(trace_item_name.c_str());
216 size_t count = trace_value_object->GetNumChildrenIgnoringErrors();
217 for (size_t j = 0; j < count; j++) {
218 addr_t trace_addr =
219 trace_value_object->GetChildAtIndex(j)->GetValueAsUnsigned(0);
220 if (trace_addr == 0)
221 break;
222 trace_sp->AddIntegerItem(trace_addr);
223 }
224 return trace_sp;
225}
226
228 ValueObjectSP return_value_sp, const std::string &items_name,
229 const std::string &count_name,
230 std::function<void(const ValueObjectSP &o,
231 const StructuredData::DictionarySP &dict)> const
232 &callback) {
233 auto array_sp = std::make_shared<StructuredData::Array>();
234 unsigned int count =
235 return_value_sp->GetValueForExpressionPath(count_name.c_str())
236 ->GetValueAsUnsigned(0);
237 ValueObjectSP objects =
238 return_value_sp->GetValueForExpressionPath(items_name.c_str());
239 for (unsigned int i = 0; i < count; i++) {
240 ValueObjectSP o = objects->GetChildAtIndex(i);
241 auto dict_sp = std::make_shared<StructuredData::Dictionary>();
242
243 callback(o, dict_sp);
244
245 array_sp->AddItem(dict_sp);
246 }
247 return array_sp;
248}
249
250static std::string RetrieveString(ValueObjectSP return_value_sp,
251 ProcessSP process_sp,
252 const std::string &expression_path) {
253 addr_t ptr =
254 return_value_sp->GetValueForExpressionPath(expression_path.c_str())
255 ->GetValueAsUnsigned(0);
256 std::string str;
258 process_sp->ReadCStringFromMemory(ptr, str, error);
259 return str;
260}
261
262static void
264 std::map<uint64_t, user_id_t> &thread_id_map) {
266 data, ".threads", ".thread_count",
267 [process_sp, &thread_id_map](const ValueObjectSP &o,
268 const StructuredData::DictionarySP &dict) {
269 uint64_t thread_id =
270 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
271 uint64_t thread_os_id =
272 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
273 user_id_t lldb_user_id = 0;
274
275 bool can_update = true;
276 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(
277 thread_os_id, can_update);
278 if (lldb_thread) {
279 lldb_user_id = lldb_thread->GetIndexID();
280 } else {
281 // This isn't a live thread anymore. Ask process to assign a new
282 // Index ID (or return an old one if we've already seen this
283 // thread_os_id). It will also make sure that no new threads are
284 // assigned this Index ID.
285 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
286 }
287
288 thread_id_map[thread_id] = lldb_user_id;
289 });
290}
291
292static user_id_t Renumber(uint64_t id,
293 std::map<uint64_t, user_id_t> &thread_id_map) {
294 auto IT = thread_id_map.find(id);
295 if (IT == thread_id_map.end())
296 return 0;
297
298 return IT->second;
299}
300
302 ExecutionContextRef exe_ctx_ref) {
303 ProcessSP process_sp = GetProcessSP();
304 if (!process_sp)
306
307 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
308 StackFrameSP frame_sp =
309 thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
310
311 if (!frame_sp)
313
315 options.SetUnwindOnError(true);
316 options.SetTryAllThreads(true);
317 options.SetStopOthers(true);
318 options.SetIgnoreBreakpoints(true);
319 options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
321 options.SetAutoApplyFixIts(false);
323
324 ValueObjectSP main_value;
325 ExecutionContext exe_ctx;
326 Status eval_error;
327 frame_sp->CalculateExecutionContext(exe_ctx);
330 main_value, eval_error);
331 if (result != eExpressionCompleted) {
332 StreamString ss;
333 ss << "cannot evaluate ThreadSanitizer expression:\n";
334 ss << eval_error.AsCString();
336 process_sp->GetTarget().GetDebugger().GetID());
338 }
339
340 std::map<uint64_t, user_id_t> thread_id_map;
341 GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
342
343 auto dict = std::make_shared<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", CreateStackTrace(
351 main_value, ".sleep_trace"));
352
354 main_value, ".stacks", ".stack_count",
355 [thread_sp](const ValueObjectSP &o,
356 const StructuredData::DictionarySP &dict) {
357 dict->AddIntegerItem(
358 "index",
359 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
360 dict->AddItem("trace", CreateStackTrace(o));
361 // "stacks" happen on the current thread
362 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
363 });
364 dict->AddItem("stacks", stacks);
365
367 main_value, ".mops", ".mop_count",
368 [&thread_id_map](const ValueObjectSP &o,
369 const StructuredData::DictionarySP &dict) {
370 dict->AddIntegerItem(
371 "index",
372 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
373 dict->AddIntegerItem(
374 "thread_id",
375 Renumber(
376 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
377 thread_id_map));
378 dict->AddIntegerItem(
379 "size",
380 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
381 dict->AddBooleanItem(
382 "is_write",
383 o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
384 dict->AddBooleanItem(
385 "is_atomic",
386 o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
387 dict->AddIntegerItem(
388 "address",
389 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
390 dict->AddItem("trace", CreateStackTrace(o));
391 });
392 dict->AddItem("mops", mops);
393
395 main_value, ".locs", ".loc_count",
396 [process_sp, &thread_id_map](const ValueObjectSP &o,
397 const StructuredData::DictionarySP &dict) {
398 dict->AddIntegerItem(
399 "index",
400 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
401 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
402 dict->AddIntegerItem(
403 "address",
404 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
405 dict->AddIntegerItem(
406 "start",
407 o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
408 dict->AddIntegerItem(
409 "size",
410 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
411 dict->AddIntegerItem(
412 "thread_id",
413 Renumber(
414 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
415 thread_id_map));
416 dict->AddIntegerItem(
417 "file_descriptor",
418 o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
419 dict->AddIntegerItem("suppressable",
420 o->GetValueForExpressionPath(".suppressable")
421 ->GetValueAsUnsigned(0));
422 dict->AddItem("trace", CreateStackTrace(o));
423 dict->AddStringItem("object_type",
424 RetrieveString(o, process_sp, ".object_type"));
425 });
426 dict->AddItem("locs", locs);
427
429 main_value, ".mutexes", ".mutex_count",
430 [](const ValueObjectSP &o, const StructuredData::DictionarySP &dict) {
431 dict->AddIntegerItem(
432 "index",
433 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
434 dict->AddIntegerItem(
435 "mutex_id",
436 o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
437 dict->AddIntegerItem(
438 "address",
439 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
440 dict->AddIntegerItem(
441 "destroyed",
442 o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
443 dict->AddItem("trace", CreateStackTrace(o));
444 });
445 dict->AddItem("mutexes", mutexes);
446
448 main_value, ".threads", ".thread_count",
449 [process_sp, &thread_id_map](const ValueObjectSP &o,
450 const StructuredData::DictionarySP &dict) {
451 dict->AddIntegerItem(
452 "index",
453 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
454 dict->AddIntegerItem(
455 "thread_id",
456 Renumber(
457 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
458 thread_id_map));
459 dict->AddIntegerItem(
460 "thread_os_id",
461 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
462 dict->AddIntegerItem(
463 "running",
464 o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
465 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
466 dict->AddIntegerItem(
467 "parent_thread_id",
468 Renumber(o->GetValueForExpressionPath(".parent_tid")
469 ->GetValueAsUnsigned(0),
470 thread_id_map));
471 dict->AddItem("trace", CreateStackTrace(o));
472 });
473 dict->AddItem("threads", threads);
474
476 main_value, ".unique_tids", ".unique_tid_count",
477 [&thread_id_map](const ValueObjectSP &o,
478 const StructuredData::DictionarySP &dict) {
479 dict->AddIntegerItem(
480 "index",
481 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
482 dict->AddIntegerItem(
483 "tid",
484 Renumber(
485 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
486 thread_id_map));
487 });
488 dict->AddItem("unique_tids", unique_tids);
489
490 return dict;
491}
492
493std::string
495 std::string description = std::string(report->GetAsDictionary()
496 ->GetValueForKey("issue_type")
497 ->GetAsString()
498 ->GetValue());
499
500 if (description == "data-race") {
501 return "Data race";
502 } else if (description == "data-race-vptr") {
503 return "Data race on C++ virtual pointer";
504 } else if (description == "heap-use-after-free") {
505 return "Use of deallocated memory";
506 } else if (description == "heap-use-after-free-vptr") {
507 return "Use of deallocated C++ virtual pointer";
508 } else if (description == "thread-leak") {
509 return "Thread leak";
510 } else if (description == "locked-mutex-destroy") {
511 return "Destruction of a locked mutex";
512 } else if (description == "mutex-double-lock") {
513 return "Double lock of a mutex";
514 } else if (description == "mutex-invalid-access") {
515 return "Use of an uninitialized or destroyed mutex";
516 } else if (description == "mutex-bad-unlock") {
517 return "Unlock of an unlocked mutex (or by a wrong thread)";
518 } else if (description == "mutex-bad-read-lock") {
519 return "Read lock of a write locked mutex";
520 } else if (description == "mutex-bad-read-unlock") {
521 return "Read unlock of a write locked mutex";
522 } else if (description == "signal-unsafe-call") {
523 return "Signal-unsafe call inside a signal handler";
524 } else if (description == "errno-in-signal-handler") {
525 return "Overwrite of errno in a signal handler";
526 } else if (description == "lock-order-inversion") {
527 return "Lock order inversion (potential deadlock)";
528 } else if (description == "external-race") {
529 return "Race on a library object";
530 } else if (description == "swift-access-race") {
531 return "Swift access race";
532 }
533
534 // for unknown report codes just show the code
535 return description;
536}
537
538static std::string Sprintf(const char *format, ...) {
539 StreamString s;
540 va_list args;
541 va_start(args, format);
542 s.PrintfVarArg(format, args);
543 va_end(args);
544 return std::string(s.GetString());
545}
546
547static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) {
548 lldb_private::Address so_addr;
549 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
550 so_addr))
551 return "";
552
554 if (!symbol)
555 return "";
556
557 std::string sym_name = symbol->GetName().GetCString();
558 return sym_name;
559}
560
562 Declaration &decl) {
563 lldb_private::Address so_addr;
564 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
565 so_addr))
566 return;
567
569 if (!symbol)
570 return;
571
573
574 ModuleSP module = symbol->CalculateSymbolContextModule();
575 if (!module)
576 return;
577
578 VariableList var_list;
579 module->FindGlobalVariables(sym_name, CompilerDeclContext(), 1U, var_list);
580 if (var_list.GetSize() < 1)
581 return;
582
583 VariableSP var = var_list.GetVariableAtIndex(0);
584 decl = var->GetDeclaration();
585}
586
588 StructuredData::ObjectSP trace, bool skip_one_frame) {
589 ProcessSP process_sp = GetProcessSP();
590 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
591
592 StructuredData::Array *trace_array = trace->GetAsArray();
593 for (size_t i = 0; i < trace_array->GetSize(); i++) {
594 if (skip_one_frame && i == 0)
595 continue;
596
597 auto maybe_addr = trace_array->GetItemAtIndexAsInteger<addr_t>(i);
598 if (!maybe_addr)
599 continue;
600 addr_t addr = *maybe_addr;
601
602 lldb_private::Address so_addr;
603 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
604 addr, so_addr))
605 continue;
606
607 if (so_addr.GetModule() == runtime_module_sp)
608 continue;
609
610 return addr;
611 }
612
613 return 0;
614}
615
616std::string
618 ProcessSP process_sp = GetProcessSP();
619
620 std::string summary = std::string(report->GetAsDictionary()
621 ->GetValueForKey("description")
622 ->GetAsString()
623 ->GetValue());
624 bool skip_one_frame =
625 report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
626 "external-race";
627
628 addr_t pc = 0;
629 if (report->GetAsDictionary()
630 ->GetValueForKey("mops")
631 ->GetAsArray()
632 ->GetSize() > 0)
633 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
634 ->GetValueForKey("mops")
635 ->GetAsArray()
636 ->GetItemAtIndex(0)
637 ->GetAsDictionary()
638 ->GetValueForKey("trace"),
639 skip_one_frame);
640
641 if (report->GetAsDictionary()
642 ->GetValueForKey("stacks")
643 ->GetAsArray()
644 ->GetSize() > 0)
645 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
646 ->GetValueForKey("stacks")
647 ->GetAsArray()
648 ->GetItemAtIndex(0)
649 ->GetAsDictionary()
650 ->GetValueForKey("trace"),
651 skip_one_frame);
652
653 if (pc != 0) {
654 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
655 }
656
657 if (report->GetAsDictionary()
658 ->GetValueForKey("locs")
659 ->GetAsArray()
660 ->GetSize() > 0) {
661 StructuredData::ObjectSP loc = report->GetAsDictionary()
662 ->GetValueForKey("locs")
663 ->GetAsArray()
664 ->GetItemAtIndex(0);
665 std::string object_type = std::string(loc->GetAsDictionary()
666 ->GetValueForKey("object_type")
667 ->GetAsString()
668 ->GetValue());
669 if (!object_type.empty()) {
670 summary = "Race on " + object_type + " object";
671 }
672 addr_t addr = loc->GetAsDictionary()
673 ->GetValueForKey("address")
674 ->GetUnsignedIntegerValue();
675 if (addr == 0)
676 addr = loc->GetAsDictionary()
677 ->GetValueForKey("start")
678 ->GetUnsignedIntegerValue();
679
680 if (addr != 0) {
681 std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
682 if (!global_name.empty()) {
683 summary = summary + " at " + global_name;
684 } else {
685 summary = summary + " at " + Sprintf("0x%llx", addr);
686 }
687 } else {
688 int fd = loc->GetAsDictionary()
689 ->GetValueForKey("file_descriptor")
690 ->GetSignedIntegerValue();
691 if (fd != 0) {
692 summary = summary + " on file descriptor " + Sprintf("%d", fd);
693 }
694 }
695 }
696
697 return summary;
698}
699
702 addr_t result = (addr_t)-1;
703
704 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
705 [&result](StructuredData::Object *o) -> bool {
706 addr_t addr = o->GetObjectForDotSeparatedPath("address")
707 ->GetUnsignedIntegerValue();
708 if (addr < result)
709 result = addr;
710 return true;
711 });
712
713 return (result == (addr_t)-1) ? 0 : result;
714}
715
717 StructuredData::ObjectSP report, addr_t &global_addr,
718 std::string &global_name, std::string &filename, uint32_t &line) {
719 std::string result;
720
721 ProcessSP process_sp = GetProcessSP();
722
723 if (report->GetAsDictionary()
724 ->GetValueForKey("locs")
725 ->GetAsArray()
726 ->GetSize() > 0) {
727 StructuredData::ObjectSP loc = report->GetAsDictionary()
728 ->GetValueForKey("locs")
729 ->GetAsArray()
730 ->GetItemAtIndex(0);
731 std::string type = std::string(
732 loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue());
733 if (type == "global") {
734 global_addr = loc->GetAsDictionary()
735 ->GetValueForKey("address")
736 ->GetUnsignedIntegerValue();
737
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);
742 } else {
743 result = Sprintf("0x%llx is a global variable", global_addr);
744 }
745
746 Declaration decl;
747 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
748 if (decl.GetFile()) {
749 filename = decl.GetFile().GetPath();
750 line = decl.GetLine();
751 }
752 } else if (type == "heap") {
753 addr_t addr = loc->GetAsDictionary()
754 ->GetValueForKey("start")
755 ->GetUnsignedIntegerValue();
756
757 size_t size = loc->GetAsDictionary()
758 ->GetValueForKey("size")
759 ->GetUnsignedIntegerValue();
760
761 std::string object_type = std::string(loc->GetAsDictionary()
762 ->GetValueForKey("object_type")
763 ->GetAsString()
764 ->GetValue());
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);
768 } else {
769 result =
770 Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
771 }
772 } else if (type == "stack") {
773 tid_t tid = loc->GetAsDictionary()
774 ->GetValueForKey("thread_id")
775 ->GetUnsignedIntegerValue();
776
777 result = Sprintf("Location is stack of thread %d", tid);
778 } else if (type == "tls") {
779 tid_t tid = loc->GetAsDictionary()
780 ->GetValueForKey("thread_id")
781 ->GetUnsignedIntegerValue();
782
783 result = Sprintf("Location is TLS of thread %d", tid);
784 } else if (type == "fd") {
785 int fd = loc->GetAsDictionary()
786 ->GetValueForKey("file_descriptor")
787 ->GetSignedIntegerValue();
788
789 result = Sprintf("Location is file descriptor %d", fd);
790 }
791 }
792
793 return result;
794}
795
797 void *baton, StoppointCallbackContext *context, user_id_t break_id,
798 user_id_t break_loc_id) {
799 assert(baton && "null baton");
800 if (!baton)
801 return false;
802
803 InstrumentationRuntimeTSan *const instance =
804 static_cast<InstrumentationRuntimeTSan *>(baton);
805
806 ProcessSP process_sp = instance->GetProcessSP();
807
808 if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
809 return false;
810
812 instance->RetrieveReportData(context->exe_ctx_ref);
813 std::string stop_reason_description =
814 "unknown thread sanitizer fault (unable to extract thread sanitizer "
815 "report)";
816 if (report) {
817 std::string issue_description = instance->FormatDescription(report);
818 report->GetAsDictionary()->AddStringItem("description", issue_description);
819 stop_reason_description = issue_description + " detected";
820 report->GetAsDictionary()->AddStringItem("stop_description",
821 stop_reason_description);
822 std::string summary = instance->GenerateSummary(report);
823 report->GetAsDictionary()->AddStringItem("summary", summary);
824 addr_t main_address = instance->GetMainRacyAddress(report);
825 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
826
827 addr_t global_addr = 0;
828 std::string global_name;
829 std::string location_filename;
830 uint32_t location_line = 0;
831 std::string location_description = instance->GetLocationDescription(
832 report, global_addr, global_name, location_filename, location_line);
833 report->GetAsDictionary()->AddStringItem("location_description",
834 location_description);
835 if (global_addr != 0) {
836 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
837 }
838 if (!global_name.empty()) {
839 report->GetAsDictionary()->AddStringItem("global_name", global_name);
840 }
841 if (location_filename != "") {
842 report->GetAsDictionary()->AddStringItem("location_filename",
843 location_filename);
844 report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
845 }
846
847 bool all_addresses_are_same = true;
848 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
849 [&all_addresses_are_same,
850 main_address](StructuredData::Object *o) -> bool {
851 addr_t addr = o->GetObjectForDotSeparatedPath("address")
852 ->GetUnsignedIntegerValue();
853 if (main_address != addr)
854 all_addresses_are_same = false;
855 return true;
856 });
857 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
858 all_addresses_are_same);
859 }
860
861 // Make sure this is the right process
862 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
863 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
864 if (thread_sp)
865 thread_sp->SetStopInfo(
867 CreateStopReasonWithInstrumentationData(
868 *thread_sp, stop_reason_description, report));
869
870 StreamFile &s = process_sp->GetTarget().GetDebugger().GetOutputStream();
871 s.Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
872 "info -s' to get extended information about the "
873 "report.\n");
874
875 return true; // Return true to stop the target
876 } else
877 return false; // Let target run
878}
879
880const RegularExpression &
882 static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
883 return regex;
884}
885
887 const lldb::ModuleSP module_sp) {
888 static ConstString g_tsan_get_current_report("__tsan_get_current_report");
889 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
890 g_tsan_get_current_report, lldb::eSymbolTypeAny);
891 return symbol != nullptr;
892}
893
895 if (IsActive())
896 return;
897
898 ProcessSP process_sp = GetProcessSP();
899 if (!process_sp)
900 return;
901
902 ConstString symbol_name("__tsan_on_report");
903 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
904 symbol_name, eSymbolTypeCode);
905
906 if (symbol == nullptr)
907 return;
908
909 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
910 return;
911
912 Target &target = process_sp->GetTarget();
913 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
914
915 if (symbol_address == LLDB_INVALID_ADDRESS)
916 return;
917
918 const bool internal = true;
919 const bool hardware = false;
920 const bool sync = false;
921 Breakpoint *breakpoint =
922 process_sp->GetTarget()
923 .CreateBreakpoint(symbol_address, internal, hardware)
924 .get();
926 sync);
927 breakpoint->SetBreakpointKind("thread-sanitizer-report");
928 SetBreakpointID(breakpoint->GetID());
929
930 SetActive(true);
931}
932
935 ProcessSP process_sp = GetProcessSP();
936 if (process_sp) {
937 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
939 }
940 }
941 SetActive(false);
942}
943static std::string GenerateThreadName(const std::string &path,
945 StructuredData::ObjectSP main_info) {
946 std::string result = "additional information";
947
948 if (path == "mops") {
949 size_t size =
950 o->GetObjectForDotSeparatedPath("size")->GetUnsignedIntegerValue();
951 tid_t thread_id =
952 o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
953 bool is_write =
954 o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
955 bool is_atomic =
956 o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
957 addr_t addr =
958 o->GetObjectForDotSeparatedPath("address")->GetUnsignedIntegerValue();
959
960 std::string addr_string = Sprintf(" at 0x%llx", addr);
961
962 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")
963 ->GetBooleanValue()) {
964 addr_string = "";
965 }
966
967 if (main_info->GetObjectForDotSeparatedPath("issue_type")
968 ->GetStringValue() == "external-race") {
969 result = Sprintf("%s access by thread %d",
970 is_write ? "mutating" : "read-only", thread_id);
971 } else if (main_info->GetObjectForDotSeparatedPath("issue_type")
972 ->GetStringValue() == "swift-access-race") {
973 result = Sprintf("modifying access by thread %d", thread_id);
974 } else {
975 result = Sprintf("%s%s of size %zu%s by thread %" PRIu64,
976 is_atomic ? "atomic " : "", is_write ? "write" : "read",
977 size, addr_string.c_str(), thread_id);
978 }
979 }
980
981 if (path == "threads") {
982 tid_t thread_id =
983 o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
984 result = Sprintf("Thread %zu created", thread_id);
985 }
986
987 if (path == "locs") {
988 std::string type = std::string(
989 o->GetAsDictionary()->GetValueForKey("type")->GetStringValue());
990 tid_t thread_id =
991 o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
992 int fd = o->GetObjectForDotSeparatedPath("file_descriptor")
993 ->GetSignedIntegerValue();
994 if (type == "heap") {
995 result = Sprintf("Heap block allocated by thread %" PRIu64, thread_id);
996 } else if (type == "fd") {
997 result = Sprintf("File descriptor %d created by thread %" PRIu64, fd,
998 thread_id);
999 }
1000 }
1001
1002 if (path == "mutexes") {
1003 int mutex_id =
1004 o->GetObjectForDotSeparatedPath("mutex_id")->GetSignedIntegerValue();
1005
1006 result = Sprintf("Mutex M%d created", mutex_id);
1007 }
1008
1009 if (path == "stacks") {
1010 tid_t thread_id =
1011 o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
1012 result = Sprintf("Thread %" PRIu64, thread_id);
1013 }
1014
1015 result[0] = toupper(result[0]);
1016
1017 return result;
1018}
1019
1020static void AddThreadsForPath(const std::string &path,
1021 ThreadCollectionSP threads, ProcessSP process_sp,
1023 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach(
1024 [process_sp, threads, path, info](StructuredData::Object *o) -> bool {
1025 std::vector<lldb::addr_t> pcs;
1026 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach(
1027 [&pcs](StructuredData::Object *pc) -> bool {
1028 pcs.push_back(pc->GetUnsignedIntegerValue());
1029 return true;
1030 });
1031
1032 if (pcs.size() == 0)
1033 return true;
1034
1035 StructuredData::ObjectSP thread_id_obj =
1036 o->GetObjectForDotSeparatedPath("thread_os_id");
1037 tid_t tid =
1038 thread_id_obj ? thread_id_obj->GetUnsignedIntegerValue() : 0;
1039
1040 ThreadSP new_thread_sp =
1041 std::make_shared<HistoryThread>(*process_sp, tid, pcs);
1042 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
1043
1044 // Save this in the Process' ExtendedThreadList so a strong pointer
1045 // retains the object
1046 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
1047 threads->AddThread(new_thread_sp);
1048
1049 return true;
1050 });
1051}
1052
1056
1057 ThreadCollectionSP threads = std::make_shared<ThreadCollection>();
1058
1059 if (info->GetObjectForDotSeparatedPath("instrumentation_class")
1060 ->GetStringValue() != "ThreadSanitizer")
1061 return threads;
1062
1063 ProcessSP process_sp = GetProcessSP();
1064
1065 AddThreadsForPath("stacks", threads, process_sp, info);
1066 AddThreadsForPath("mops", threads, process_sp, info);
1067 AddThreadsForPath("locs", threads, process_sp, info);
1068 AddThreadsForPath("mutexes", threads, process_sp, info);
1069 AddThreadsForPath("threads", threads, process_sp, info);
1070
1071 return threads;
1072}
static llvm::raw_ostream & error(Stream &strm)
static void GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, std::map< uint64_t, user_id_t > &thread_id_map)
static StructuredData::ArraySP ConvertToStructuredArray(ValueObjectSP return_value_sp, const std::string &items_name, const std::string &count_name, std::function< void(const ValueObjectSP &o, const StructuredData::DictionarySP &dict)> const &callback)
const char * thread_sanitizer_retrieve_report_data_prefix
static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, Declaration &decl)
static user_id_t Renumber(uint64_t id, std::map< uint64_t, user_id_t > &thread_id_map)
static std::string GenerateThreadName(const std::string &path, StructuredData::Object *o, StructuredData::ObjectSP main_info)
static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr)
static std::string RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, const std::string &expression_path)
static std::string Sprintf(const char *format,...)
static StructuredData::ArraySP CreateStackTrace(ValueObjectSP o, const std::string &trace_item_name=".trace")
const char * thread_sanitizer_retrieve_report_data_command
static void AddThreadsForPath(const std::string &path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info)
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetOpcodeLoadAddress(Target *target, AddressClass addr_class=AddressClass::eInvalid) const
Get the load address as an opcode load address.
Definition: Address.cpp:370
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:285
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
Symbol * CalculateSymbolContextSymbol() const
Definition: Address.cpp:900
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:81
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
Definition: Breakpoint.h:452
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:463
void SetCallback(BreakpointHitCallback callback, void *baton, bool is_synchronous=false)
Set the callback action invoked when the breakpoint is hit.
Definition: Breakpoint.cpp:408
Represents a generic declaration context in a program.
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:214
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
Definition: Debugger.cpp:1587
A class that describes the declaration location of a lldb object.
Definition: Declaration.h:24
uint32_t GetLine() const
Get accessor for the declaration line number.
Definition: Declaration.h:120
FileSpec & GetFile()
Get accessor for file specification.
Definition: Declaration.h:107
void SetUnwindOnError(bool unwind=false)
Definition: Target.h:343
void SetLanguage(lldb::LanguageType language_type)
Definition: Target.h:315
void SetPrefix(const char *prefix)
Definition: Target.h:332
void SetTryAllThreads(bool try_others=true)
Definition: Target.h:376
void SetTimeout(const Timeout< std::micro > &timeout)
Definition: Target.h:364
void SetStopOthers(bool stop_others=true)
Definition: Target.h:380
void SetIgnoreBreakpoints(bool ignore=false)
Definition: Target.h:347
Execution context objects refer to objects in the execution of the program that is being debugged.
lldb::ThreadSP GetThreadSP() const
Get accessor that creates a strong reference from the weak thread reference contained in this object.
lldb::ProcessSP GetProcessSP() const
Get accessor that creates a strong reference from the weak process reference contained in this object...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref)
bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override
Check whether module_sp corresponds to a valid runtime library.
std::string GetLocationDescription(StructuredData::ObjectSP report, lldb::addr_t &global_addr, std::string &global_name, std::string &filename, uint32_t &line)
const RegularExpression & GetPatternForRuntimeLibrary() override
Return a regular expression which can be used to identify a valid version of the runtime library.
lldb::addr_t GetMainRacyAddress(StructuredData::ObjectSP report)
static lldb::InstrumentationRuntimeSP CreateInstance(const lldb::ProcessSP &process_sp)
static lldb::InstrumentationRuntimeType GetTypeStatic()
void Activate() override
Register a breakpoint in the runtime library and perform any other necessary initialization.
static bool NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
lldb::addr_t GetFirstNonInternalFramePc(StructuredData::ObjectSP trace, bool skip_one_frame=false)
std::string GenerateSummary(StructuredData::ObjectSP report)
lldb::ThreadCollectionSP GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override
std::string FormatDescription(StructuredData::ObjectSP report)
ConstString GetName(NamePreference preference=ePreferDemangled) const
Best name get accessor.
Definition: Mangled.cpp:327
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
An error handling class.
Definition: Status.h:44
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
lldb::break_id_t GetID() const
Definition: Stoppoint.cpp:22
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:143
std::optional< IntType > GetItemAtIndexAsInteger(size_t idx) const
ObjectSP GetValueForKey(llvm::StringRef key) const
ObjectSP GetObjectForDotSeparatedPath(llvm::StringRef path)
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
bool ValueIsAddress() const
Definition: Symbol.cpp:169
lldb::ModuleSP CalculateSymbolContextModule() override
Definition: Symbol.cpp:449
Mangled & GetMangled()
Definition: Symbol.h:146
Address & GetAddressRef()
Definition: Symbol.h:72
ConstString GetName() const
Definition: Symbol.cpp:552
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition: Target.cpp:395
static lldb::ExpressionResults Evaluate(ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, llvm::StringRef expr_cstr, llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp, Status &error, std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Evaluate one expression in the scratch context of the target passed in the exe_ctx and return its res...
lldb::VariableSP GetVariableAtIndex(size_t idx) const
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:37
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
@ DoNoSelectMostRelevantFrame
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:412
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:438
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:472
@ eLanguageTypeObjC_plus_plus
Objective-C++.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:381
InstrumentationRuntimeType
@ eInstrumentationRuntimeTypeThreadSanitizer
std::shared_ptr< lldb_private::Variable > VariableSP
Definition: lldb-forward.h:474
uint64_t user_id_t
Definition: lldb-types.h:82
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::InstrumentationRuntime > InstrumentationRuntimeSP
Definition: lldb-forward.h:352
uint64_t tid_t
Definition: lldb-types.h:84
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365
std::shared_ptr< lldb_private::ThreadCollection > ThreadCollectionSP
Definition: lldb-forward.h:440