]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp
Update our device tree files to a Linux 4.10
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / InstrumentationRuntime / ThreadSanitizer / ThreadSanitizerRuntime.cpp
1 //===-- ThreadSanitizerRuntime.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ThreadSanitizerRuntime.h"
11
12 #include "Plugins/Process/Utility/HistoryThread.h"
13 #include "lldb/Breakpoint/StoppointCallbackContext.h"
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginInterface.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/RegularExpression.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Expression/UserExpression.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Symbol/Symbol.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Symbol/VariableList.h"
28 #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
29 #include "lldb/Target/SectionLoadList.h"
30 #include "lldb/Target/StopInfo.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
33
34 using namespace lldb;
35 using namespace lldb_private;
36
37 lldb::InstrumentationRuntimeSP
38 ThreadSanitizerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) {
39   return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp));
40 }
41
42 void ThreadSanitizerRuntime::Initialize() {
43   PluginManager::RegisterPlugin(
44       GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.",
45       CreateInstance, GetTypeStatic);
46 }
47
48 void ThreadSanitizerRuntime::Terminate() {
49   PluginManager::UnregisterPlugin(CreateInstance);
50 }
51
52 lldb_private::ConstString ThreadSanitizerRuntime::GetPluginNameStatic() {
53   return ConstString("ThreadSanitizer");
54 }
55
56 lldb::InstrumentationRuntimeType ThreadSanitizerRuntime::GetTypeStatic() {
57   return eInstrumentationRuntimeTypeThreadSanitizer;
58 }
59
60 ThreadSanitizerRuntime::~ThreadSanitizerRuntime() { Deactivate(); }
61
62 static constexpr std::chrono::seconds g_retrieve_data_function_timeout(2);
63
64 const char *thread_sanitizer_retrieve_report_data_prefix = R"(
65 extern "C"
66 {
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);
88 }
89
90 const int REPORT_TRACE_SIZE = 128;
91 const int REPORT_ARRAY_SIZE = 4;
92
93 struct data {
94     void *report;
95     const char *description;
96     int report_count;
97     
98     void *sleep_trace[REPORT_TRACE_SIZE];
99     
100     int stack_count;
101     struct {
102         int idx;
103         void *trace[REPORT_TRACE_SIZE];
104     } stacks[REPORT_ARRAY_SIZE];
105     
106     int mop_count;
107     struct {
108         int idx;
109         int tid;
110         int size;
111         int write;
112         int atomic;
113         void *addr;
114         void *trace[REPORT_TRACE_SIZE];
115     } mops[REPORT_ARRAY_SIZE];
116     
117     int loc_count;
118     struct {
119         int idx;
120         const char *type;
121         void *addr;
122         unsigned long start;
123         unsigned long size;
124         int tid;
125         int fd;
126         int suppressable;
127         void *trace[REPORT_TRACE_SIZE];
128     } locs[REPORT_ARRAY_SIZE];
129     
130     int mutex_count;
131     struct {
132         int idx;
133         unsigned long mutex_id;
134         void *addr;
135         int destroyed;
136         void *trace[REPORT_TRACE_SIZE];
137     } mutexes[REPORT_ARRAY_SIZE];
138     
139     int thread_count;
140     struct {
141         int idx;
142         int tid;
143         unsigned long os_id;
144         int running;
145         const char *name;
146         int parent_tid;
147         void *trace[REPORT_TRACE_SIZE];
148     } threads[REPORT_ARRAY_SIZE];
149     
150     int unique_tid_count;
151     struct {
152         int idx;
153         int tid;
154     } unique_tids[REPORT_ARRAY_SIZE];
155 };
156 )";
157
158 const char *thread_sanitizer_retrieve_report_data_command = R"(
159 data t = {0};
160
161 t.report = __tsan_get_current_report();
162 __tsan_get_report_data(t.report, &t.description, &t.report_count, &t.stack_count, &t.mop_count, &t.loc_count, &t.mutex_count, &t.thread_count, &t.unique_tid_count, t.sleep_trace, REPORT_TRACE_SIZE);
163
164 if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
165 for (int i = 0; i < t.stack_count; i++) {
166     t.stacks[i].idx = i;
167     __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
168 }
169
170 if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
171 for (int i = 0; i < t.mop_count; i++) {
172     t.mops[i].idx = i;
173     __tsan_get_report_mop(t.report, i, &t.mops[i].tid, &t.mops[i].addr, &t.mops[i].size, &t.mops[i].write, &t.mops[i].atomic, t.mops[i].trace, REPORT_TRACE_SIZE);
174 }
175
176 if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
177 for (int i = 0; i < t.loc_count; i++) {
178     t.locs[i].idx = i;
179     __tsan_get_report_loc(t.report, i, &t.locs[i].type, &t.locs[i].addr, &t.locs[i].start, &t.locs[i].size, &t.locs[i].tid, &t.locs[i].fd, &t.locs[i].suppressable, t.locs[i].trace, REPORT_TRACE_SIZE);
180 }
181
182 if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
183 for (int i = 0; i < t.mutex_count; i++) {
184     t.mutexes[i].idx = i;
185     __tsan_get_report_mutex(t.report, i, &t.mutexes[i].mutex_id, &t.mutexes[i].addr, &t.mutexes[i].destroyed, t.mutexes[i].trace, REPORT_TRACE_SIZE);
186 }
187
188 if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
189 for (int i = 0; i < t.thread_count; i++) {
190     t.threads[i].idx = i;
191     __tsan_get_report_thread(t.report, i, &t.threads[i].tid, &t.threads[i].os_id, &t.threads[i].running, &t.threads[i].name, &t.threads[i].parent_tid, t.threads[i].trace, REPORT_TRACE_SIZE);
192 }
193
194 if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
195 for (int i = 0; i < t.unique_tid_count; i++) {
196     t.unique_tids[i].idx = i;
197     __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
198 }
199
200 t;
201 )";
202
203 static StructuredData::Array *
204 CreateStackTrace(ValueObjectSP o,
205                  const std::string &trace_item_name = ".trace") {
206   StructuredData::Array *trace = new StructuredData::Array();
207   ValueObjectSP trace_value_object =
208       o->GetValueForExpressionPath(trace_item_name.c_str());
209   size_t count = trace_value_object->GetNumChildren();
210   for (size_t j = 0; j < count; j++) {
211     addr_t trace_addr =
212         trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
213     if (trace_addr == 0)
214       break;
215     trace->AddItem(
216         StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
217   }
218   return trace;
219 }
220
221 static StructuredData::Array *ConvertToStructuredArray(
222     ValueObjectSP return_value_sp, const std::string &items_name,
223     const std::string &count_name,
224     std::function<void(ValueObjectSP o, StructuredData::Dictionary *dict)> const
225         &callback) {
226   StructuredData::Array *array = new StructuredData::Array();
227   unsigned int count =
228       return_value_sp->GetValueForExpressionPath(count_name.c_str())
229           ->GetValueAsUnsigned(0);
230   ValueObjectSP objects =
231       return_value_sp->GetValueForExpressionPath(items_name.c_str());
232   for (unsigned int i = 0; i < count; i++) {
233     ValueObjectSP o = objects->GetChildAtIndex(i, true);
234     StructuredData::Dictionary *dict = new StructuredData::Dictionary();
235
236     callback(o, dict);
237
238     array->AddItem(StructuredData::ObjectSP(dict));
239   }
240   return array;
241 }
242
243 static std::string RetrieveString(ValueObjectSP return_value_sp,
244                                   ProcessSP process_sp,
245                                   const std::string &expression_path) {
246   addr_t ptr =
247       return_value_sp->GetValueForExpressionPath(expression_path.c_str())
248           ->GetValueAsUnsigned(0);
249   std::string str;
250   Error error;
251   process_sp->ReadCStringFromMemory(ptr, str, error);
252   return str;
253 }
254
255 static void
256 GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data,
257                        std::map<uint64_t, user_id_t> &thread_id_map) {
258   ConvertToStructuredArray(
259       data, ".threads", ".thread_count",
260       [process_sp, &thread_id_map](ValueObjectSP o,
261                                    StructuredData::Dictionary *dict) {
262         uint64_t thread_id =
263             o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
264         uint64_t thread_os_id =
265             o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
266         user_id_t lldb_user_id = 0;
267
268         bool can_update = true;
269         ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(
270             thread_os_id, can_update);
271         if (lldb_thread) {
272           lldb_user_id = lldb_thread->GetIndexID();
273         } else {
274           // This isn't a live thread anymore.  Ask process to assign a new
275           // Index ID (or return an old one if we've already seen this
276           // thread_os_id).
277           // It will also make sure that no new threads are assigned this Index
278           // ID.
279           lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
280         }
281
282         thread_id_map[thread_id] = lldb_user_id;
283       });
284 }
285
286 static user_id_t Renumber(uint64_t id,
287                           std::map<uint64_t, user_id_t> &thread_id_map) {
288   auto IT = thread_id_map.find(id);
289   if (IT == thread_id_map.end())
290     return 0;
291
292   return IT->second;
293 }
294
295 StructuredData::ObjectSP
296 ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
297   ProcessSP process_sp = GetProcessSP();
298   if (!process_sp)
299     return StructuredData::ObjectSP();
300
301   ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
302   StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
303
304   if (!frame_sp)
305     return StructuredData::ObjectSP();
306
307   EvaluateExpressionOptions options;
308   options.SetUnwindOnError(true);
309   options.SetTryAllThreads(true);
310   options.SetStopOthers(true);
311   options.SetIgnoreBreakpoints(true);
312   options.SetTimeout(g_retrieve_data_function_timeout);
313   options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
314   options.SetAutoApplyFixIts(false);
315   options.SetLanguage(eLanguageTypeObjC_plus_plus);
316
317   ValueObjectSP main_value;
318   ExecutionContext exe_ctx;
319   Error eval_error;
320   frame_sp->CalculateExecutionContext(exe_ctx);
321   ExpressionResults result = UserExpression::Evaluate(
322       exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "",
323       main_value, eval_error);
324   if (result != eExpressionCompleted) {
325     process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
326         "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n",
327         eval_error.AsCString());
328     return StructuredData::ObjectSP();
329   }
330
331   std::map<uint64_t, user_id_t> thread_id_map;
332   GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
333
334   StructuredData::Dictionary *dict = new StructuredData::Dictionary();
335   dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
336   dict->AddStringItem("issue_type",
337                       RetrieveString(main_value, process_sp, ".description"));
338   dict->AddIntegerItem("report_count",
339                        main_value->GetValueForExpressionPath(".report_count")
340                            ->GetValueAsUnsigned(0));
341   dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(
342                                    main_value, ".sleep_trace")));
343
344   StructuredData::Array *stacks = ConvertToStructuredArray(
345       main_value, ".stacks", ".stack_count",
346       [thread_sp](ValueObjectSP o, StructuredData::Dictionary *dict) {
347         dict->AddIntegerItem(
348             "index",
349             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
350         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
351         // "stacks" happen on the current thread
352         dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
353       });
354   dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
355
356   StructuredData::Array *mops = ConvertToStructuredArray(
357       main_value, ".mops", ".mop_count",
358       [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
359         dict->AddIntegerItem(
360             "index",
361             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
362         dict->AddIntegerItem(
363             "thread_id",
364             Renumber(
365                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
366                 thread_id_map));
367         dict->AddIntegerItem(
368             "size",
369             o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
370         dict->AddBooleanItem(
371             "is_write",
372             o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
373         dict->AddBooleanItem(
374             "is_atomic",
375             o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
376         dict->AddIntegerItem(
377             "address",
378             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
379         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
380       });
381   dict->AddItem("mops", StructuredData::ObjectSP(mops));
382
383   StructuredData::Array *locs = ConvertToStructuredArray(
384       main_value, ".locs", ".loc_count",
385       [process_sp, &thread_id_map](ValueObjectSP o,
386                                    StructuredData::Dictionary *dict) {
387         dict->AddIntegerItem(
388             "index",
389             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
390         dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
391         dict->AddIntegerItem(
392             "address",
393             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
394         dict->AddIntegerItem(
395             "start",
396             o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
397         dict->AddIntegerItem(
398             "size",
399             o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
400         dict->AddIntegerItem(
401             "thread_id",
402             Renumber(
403                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
404                 thread_id_map));
405         dict->AddIntegerItem(
406             "file_descriptor",
407             o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
408         dict->AddIntegerItem("suppressable",
409                              o->GetValueForExpressionPath(".suppressable")
410                                  ->GetValueAsUnsigned(0));
411         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
412       });
413   dict->AddItem("locs", StructuredData::ObjectSP(locs));
414
415   StructuredData::Array *mutexes = ConvertToStructuredArray(
416       main_value, ".mutexes", ".mutex_count",
417       [](ValueObjectSP o, StructuredData::Dictionary *dict) {
418         dict->AddIntegerItem(
419             "index",
420             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
421         dict->AddIntegerItem(
422             "mutex_id",
423             o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
424         dict->AddIntegerItem(
425             "address",
426             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
427         dict->AddIntegerItem(
428             "destroyed",
429             o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
430         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
431       });
432   dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
433
434   StructuredData::Array *threads = ConvertToStructuredArray(
435       main_value, ".threads", ".thread_count",
436       [process_sp, &thread_id_map](ValueObjectSP o,
437                                    StructuredData::Dictionary *dict) {
438         dict->AddIntegerItem(
439             "index",
440             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
441         dict->AddIntegerItem(
442             "thread_id",
443             Renumber(
444                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
445                 thread_id_map));
446         dict->AddIntegerItem(
447             "thread_os_id",
448             o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
449         dict->AddIntegerItem(
450             "running",
451             o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
452         dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
453         dict->AddIntegerItem(
454             "parent_thread_id",
455             Renumber(o->GetValueForExpressionPath(".parent_tid")
456                          ->GetValueAsUnsigned(0),
457                      thread_id_map));
458         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
459       });
460   dict->AddItem("threads", StructuredData::ObjectSP(threads));
461
462   StructuredData::Array *unique_tids = ConvertToStructuredArray(
463       main_value, ".unique_tids", ".unique_tid_count",
464       [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
465         dict->AddIntegerItem(
466             "index",
467             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
468         dict->AddIntegerItem(
469             "tid",
470             Renumber(
471                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
472                 thread_id_map));
473       });
474   dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
475
476   return StructuredData::ObjectSP(dict);
477 }
478
479 std::string
480 ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) {
481   std::string description = report->GetAsDictionary()
482                                 ->GetValueForKey("issue_type")
483                                 ->GetAsString()
484                                 ->GetValue();
485
486   if (description == "data-race") {
487     return "Data race";
488   } else if (description == "data-race-vptr") {
489     return "Data race on C++ virtual pointer";
490   } else if (description == "heap-use-after-free") {
491     return "Use of deallocated memory";
492   } else if (description == "heap-use-after-free-vptr") {
493     return "Use of deallocated C++ virtual pointer";
494   } else if (description == "thread-leak") {
495     return "Thread leak";
496   } else if (description == "locked-mutex-destroy") {
497     return "Destruction of a locked mutex";
498   } else if (description == "mutex-double-lock") {
499     return "Double lock of a mutex";
500   } else if (description == "mutex-invalid-access") {
501     return "Use of an uninitialized or destroyed mutex";
502   } else if (description == "mutex-bad-unlock") {
503     return "Unlock of an unlocked mutex (or by a wrong thread)";
504   } else if (description == "mutex-bad-read-lock") {
505     return "Read lock of a write locked mutex";
506   } else if (description == "mutex-bad-read-unlock") {
507     return "Read unlock of a write locked mutex";
508   } else if (description == "signal-unsafe-call") {
509     return "Signal-unsafe call inside a signal handler";
510   } else if (description == "errno-in-signal-handler") {
511     return "Overwrite of errno in a signal handler";
512   } else if (description == "lock-order-inversion") {
513     return "Lock order inversion (potential deadlock)";
514   }
515
516   // for unknown report codes just show the code
517   return description;
518 }
519
520 static std::string Sprintf(const char *format, ...) {
521   StreamString s;
522   va_list args;
523   va_start(args, format);
524   s.PrintfVarArg(format, args);
525   va_end(args);
526   return s.GetString();
527 }
528
529 static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) {
530   lldb_private::Address so_addr;
531   if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
532                                                                        so_addr))
533     return "";
534
535   lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
536   if (!symbol)
537     return "";
538
539   std::string sym_name = symbol->GetName().GetCString();
540   return sym_name;
541 }
542
543 static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr,
544                                             Declaration &decl) {
545   lldb_private::Address so_addr;
546   if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
547                                                                        so_addr))
548     return;
549
550   lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
551   if (!symbol)
552     return;
553
554   ConstString sym_name = symbol->GetMangled().GetName(
555       lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
556
557   ModuleSP module = symbol->CalculateSymbolContextModule();
558   if (!module)
559     return;
560
561   VariableList var_list;
562   module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list);
563   if (var_list.GetSize() < 1)
564     return;
565
566   VariableSP var = var_list.GetVariableAtIndex(0);
567   decl = var->GetDeclaration();
568 }
569
570 addr_t ThreadSanitizerRuntime::GetFirstNonInternalFramePc(
571     StructuredData::ObjectSP trace) {
572   ProcessSP process_sp = GetProcessSP();
573   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
574
575   addr_t result = 0;
576   trace->GetAsArray()->ForEach([process_sp, runtime_module_sp,
577                                 &result](StructuredData::Object *o) -> bool {
578     addr_t addr = o->GetIntegerValue();
579     lldb_private::Address so_addr;
580     if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
581             addr, so_addr))
582       return true;
583
584     if (so_addr.GetModule() == runtime_module_sp)
585       return true;
586
587     result = addr;
588     return false;
589   });
590
591   return result;
592 }
593
594 std::string
595 ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) {
596   ProcessSP process_sp = GetProcessSP();
597
598   std::string summary = report->GetAsDictionary()
599                             ->GetValueForKey("description")
600                             ->GetAsString()
601                             ->GetValue();
602   addr_t pc = 0;
603   if (report->GetAsDictionary()
604           ->GetValueForKey("mops")
605           ->GetAsArray()
606           ->GetSize() > 0)
607     pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
608                                         ->GetValueForKey("mops")
609                                         ->GetAsArray()
610                                         ->GetItemAtIndex(0)
611                                         ->GetAsDictionary()
612                                         ->GetValueForKey("trace"));
613
614   if (report->GetAsDictionary()
615           ->GetValueForKey("stacks")
616           ->GetAsArray()
617           ->GetSize() > 0)
618     pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
619                                         ->GetValueForKey("stacks")
620                                         ->GetAsArray()
621                                         ->GetItemAtIndex(0)
622                                         ->GetAsDictionary()
623                                         ->GetValueForKey("trace"));
624
625   if (pc != 0) {
626     summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
627   }
628
629   if (report->GetAsDictionary()
630           ->GetValueForKey("locs")
631           ->GetAsArray()
632           ->GetSize() > 0) {
633     StructuredData::ObjectSP loc = report->GetAsDictionary()
634                                        ->GetValueForKey("locs")
635                                        ->GetAsArray()
636                                        ->GetItemAtIndex(0);
637     addr_t addr = loc->GetAsDictionary()
638                       ->GetValueForKey("address")
639                       ->GetAsInteger()
640                       ->GetValue();
641     if (addr == 0)
642       addr = loc->GetAsDictionary()
643                  ->GetValueForKey("start")
644                  ->GetAsInteger()
645                  ->GetValue();
646
647     if (addr != 0) {
648       std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
649       if (!global_name.empty()) {
650         summary = summary + " at " + global_name;
651       } else {
652         summary = summary + " at " + Sprintf("0x%llx", addr);
653       }
654     } else {
655       int fd = loc->GetAsDictionary()
656                    ->GetValueForKey("file_descriptor")
657                    ->GetAsInteger()
658                    ->GetValue();
659       if (fd != 0) {
660         summary = summary + " on file descriptor " + Sprintf("%d", fd);
661       }
662     }
663   }
664
665   return summary;
666 }
667
668 addr_t
669 ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) {
670   addr_t result = (addr_t)-1;
671
672   report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
673       [&result](StructuredData::Object *o) -> bool {
674         addr_t addr =
675             o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
676         if (addr < result)
677           result = addr;
678         return true;
679       });
680
681   return (result == (addr_t)-1) ? 0 : result;
682 }
683
684 std::string ThreadSanitizerRuntime::GetLocationDescription(
685     StructuredData::ObjectSP report, addr_t &global_addr,
686     std::string &global_name, std::string &filename, uint32_t &line) {
687   std::string result = "";
688
689   ProcessSP process_sp = GetProcessSP();
690
691   if (report->GetAsDictionary()
692           ->GetValueForKey("locs")
693           ->GetAsArray()
694           ->GetSize() > 0) {
695     StructuredData::ObjectSP loc = report->GetAsDictionary()
696                                        ->GetValueForKey("locs")
697                                        ->GetAsArray()
698                                        ->GetItemAtIndex(0);
699     std::string type =
700         loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
701     if (type == "global") {
702       global_addr = loc->GetAsDictionary()
703                         ->GetValueForKey("address")
704                         ->GetAsInteger()
705                         ->GetValue();
706       global_name = GetSymbolNameFromAddress(process_sp, global_addr);
707       if (!global_name.empty()) {
708         result = Sprintf("'%s' is a global variable (0x%llx)",
709                          global_name.c_str(), global_addr);
710       } else {
711         result = Sprintf("0x%llx is a global variable", global_addr);
712       }
713
714       Declaration decl;
715       GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
716       if (decl.GetFile()) {
717         filename = decl.GetFile().GetPath();
718         line = decl.GetLine();
719       }
720     } else if (type == "heap") {
721       addr_t addr = loc->GetAsDictionary()
722                         ->GetValueForKey("start")
723                         ->GetAsInteger()
724                         ->GetValue();
725       long size = loc->GetAsDictionary()
726                       ->GetValueForKey("size")
727                       ->GetAsInteger()
728                       ->GetValue();
729       result =
730           Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
731     } else if (type == "stack") {
732       int tid = loc->GetAsDictionary()
733                     ->GetValueForKey("thread_id")
734                     ->GetAsInteger()
735                     ->GetValue();
736       result = Sprintf("Location is stack of thread %d", tid);
737     } else if (type == "tls") {
738       int tid = loc->GetAsDictionary()
739                     ->GetValueForKey("thread_id")
740                     ->GetAsInteger()
741                     ->GetValue();
742       result = Sprintf("Location is TLS of thread %d", tid);
743     } else if (type == "fd") {
744       int fd = loc->GetAsDictionary()
745                    ->GetValueForKey("file_descriptor")
746                    ->GetAsInteger()
747                    ->GetValue();
748       result = Sprintf("Location is file descriptor %d", fd);
749     }
750   }
751
752   return result;
753 }
754
755 bool ThreadSanitizerRuntime::NotifyBreakpointHit(
756     void *baton, StoppointCallbackContext *context, user_id_t break_id,
757     user_id_t break_loc_id) {
758   assert(baton && "null baton");
759   if (!baton)
760     return false;
761
762   ThreadSanitizerRuntime *const instance =
763       static_cast<ThreadSanitizerRuntime *>(baton);
764
765   StructuredData::ObjectSP report =
766       instance->RetrieveReportData(context->exe_ctx_ref);
767   std::string stop_reason_description;
768   if (report) {
769     std::string issue_description = instance->FormatDescription(report);
770     report->GetAsDictionary()->AddStringItem("description", issue_description);
771     stop_reason_description = issue_description + " detected";
772     report->GetAsDictionary()->AddStringItem("stop_description",
773                                              stop_reason_description);
774     std::string summary = instance->GenerateSummary(report);
775     report->GetAsDictionary()->AddStringItem("summary", summary);
776     addr_t main_address = instance->GetMainRacyAddress(report);
777     report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
778
779     addr_t global_addr = 0;
780     std::string global_name = "";
781     std::string location_filename = "";
782     uint32_t location_line = 0;
783     std::string location_description = instance->GetLocationDescription(
784         report, global_addr, global_name, location_filename, location_line);
785     report->GetAsDictionary()->AddStringItem("location_description",
786                                              location_description);
787     if (global_addr != 0) {
788       report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
789     }
790     if (!global_name.empty()) {
791       report->GetAsDictionary()->AddStringItem("global_name", global_name);
792     }
793     if (location_filename != "") {
794       report->GetAsDictionary()->AddStringItem("location_filename",
795                                                location_filename);
796       report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
797     }
798
799     bool all_addresses_are_same = true;
800     report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
801         [&all_addresses_are_same,
802          main_address](StructuredData::Object *o) -> bool {
803           addr_t addr =
804               o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
805           if (main_address != addr)
806             all_addresses_are_same = false;
807           return true;
808         });
809     report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
810                                               all_addresses_are_same);
811   }
812
813   ProcessSP process_sp = instance->GetProcessSP();
814   // Make sure this is the right process
815   if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
816     ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
817     if (thread_sp)
818       thread_sp->SetStopInfo(
819           InstrumentationRuntimeStopInfo::
820               CreateStopReasonWithInstrumentationData(
821                   *thread_sp, stop_reason_description, report));
822
823     StreamFileSP stream_sp(
824         process_sp->GetTarget().GetDebugger().GetOutputFile());
825     if (stream_sp) {
826       stream_sp->Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
827                         "info -s' to get extended information about the "
828                         "report.\n");
829     }
830     return true; // Return true to stop the target
831   } else
832     return false; // Let target run
833 }
834
835 const RegularExpression &ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() {
836   static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
837   return regex;
838 }
839
840 bool ThreadSanitizerRuntime::CheckIfRuntimeIsValid(
841     const lldb::ModuleSP module_sp) {
842   static ConstString g_tsan_get_current_report("__tsan_get_current_report");
843   const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
844       g_tsan_get_current_report, lldb::eSymbolTypeAny);
845   return symbol != nullptr;
846 }
847
848 void ThreadSanitizerRuntime::Activate() {
849   if (IsActive())
850     return;
851
852   ProcessSP process_sp = GetProcessSP();
853   if (!process_sp)
854     return;
855
856   ConstString symbol_name("__tsan_on_report");
857   const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
858       symbol_name, eSymbolTypeCode);
859
860   if (symbol == NULL)
861     return;
862
863   if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
864     return;
865
866   Target &target = process_sp->GetTarget();
867   addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
868
869   if (symbol_address == LLDB_INVALID_ADDRESS)
870     return;
871
872   bool internal = true;
873   bool hardware = false;
874   Breakpoint *breakpoint =
875       process_sp->GetTarget()
876           .CreateBreakpoint(symbol_address, internal, hardware)
877           .get();
878   breakpoint->SetCallback(ThreadSanitizerRuntime::NotifyBreakpointHit, this,
879                           true);
880   breakpoint->SetBreakpointKind("thread-sanitizer-report");
881   SetBreakpointID(breakpoint->GetID());
882
883   StreamFileSP stream_sp(process_sp->GetTarget().GetDebugger().GetOutputFile());
884   if (stream_sp) {
885     stream_sp->Printf("ThreadSanitizer debugger support is active.\n");
886   }
887
888   SetActive(true);
889 }
890
891 void ThreadSanitizerRuntime::Deactivate() {
892   if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
893     ProcessSP process_sp = GetProcessSP();
894     if (process_sp) {
895       process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
896       SetBreakpointID(LLDB_INVALID_BREAK_ID);
897     }
898   }
899   SetActive(false);
900 }
901 static std::string GenerateThreadName(const std::string &path,
902                                       StructuredData::Object *o,
903                                       StructuredData::ObjectSP main_info) {
904   std::string result = "additional information";
905
906   if (path == "mops") {
907     int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
908     int thread_id =
909         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
910     bool is_write =
911         o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
912     bool is_atomic =
913         o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
914     addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
915
916     std::string addr_string = Sprintf(" at 0x%llx", addr);
917
918     if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")
919             ->GetBooleanValue()) {
920       addr_string = "";
921     }
922
923     result = Sprintf("%s%s of size %d%s by thread %d",
924                      is_atomic ? "atomic " : "", is_write ? "write" : "read",
925                      size, addr_string.c_str(), thread_id);
926   }
927
928   if (path == "threads") {
929     int thread_id =
930         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
931     result = Sprintf("Thread %d created", thread_id);
932   }
933
934   if (path == "locs") {
935     std::string type =
936         o->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
937     int thread_id =
938         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
939     int fd =
940         o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
941     if (type == "heap") {
942       result = Sprintf("Heap block allocated by thread %d", thread_id);
943     } else if (type == "fd") {
944       result =
945           Sprintf("File descriptor %d created by thread %t", fd, thread_id);
946     }
947   }
948
949   if (path == "mutexes") {
950     int mutex_id =
951         o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
952
953     result = Sprintf("Mutex M%d created", mutex_id);
954   }
955
956   if (path == "stacks") {
957     int thread_id =
958         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
959     result = Sprintf("Thread %d", thread_id);
960   }
961
962   result[0] = toupper(result[0]);
963
964   return result;
965 }
966
967 static void AddThreadsForPath(const std::string &path,
968                               ThreadCollectionSP threads, ProcessSP process_sp,
969                               StructuredData::ObjectSP info) {
970   info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach(
971       [process_sp, threads, path, info](StructuredData::Object *o) -> bool {
972         std::vector<lldb::addr_t> pcs;
973         o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach(
974             [&pcs](StructuredData::Object *pc) -> bool {
975               pcs.push_back(pc->GetAsInteger()->GetValue());
976               return true;
977             });
978
979         if (pcs.size() == 0)
980           return true;
981
982         StructuredData::ObjectSP thread_id_obj =
983             o->GetObjectForDotSeparatedPath("thread_os_id");
984         tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
985
986         uint32_t stop_id = 0;
987         bool stop_id_is_valid = false;
988         HistoryThread *history_thread =
989             new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid);
990         ThreadSP new_thread_sp(history_thread);
991         new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
992
993         // Save this in the Process' ExtendedThreadList so a strong pointer
994         // retains the object
995         process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
996         threads->AddThread(new_thread_sp);
997
998         return true;
999       });
1000 }
1001
1002 lldb::ThreadCollectionSP
1003 ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(
1004     StructuredData::ObjectSP info) {
1005   ThreadCollectionSP threads;
1006   threads.reset(new ThreadCollection());
1007
1008   if (info->GetObjectForDotSeparatedPath("instrumentation_class")
1009           ->GetStringValue() != "ThreadSanitizer")
1010     return threads;
1011
1012   ProcessSP process_sp = GetProcessSP();
1013
1014   AddThreadsForPath("stacks", threads, process_sp, info);
1015   AddThreadsForPath("mops", threads, process_sp, info);
1016   AddThreadsForPath("locs", threads, process_sp, info);
1017   AddThreadsForPath("mutexes", threads, process_sp, info);
1018   AddThreadsForPath("threads", threads, process_sp, info);
1019
1020   return threads;
1021 }