blob: 658c70afd6054daca3657fce347bdf2ead2c4bb9 [file] [log] [blame]
Kuba Brecka6a831432016-03-23 15:36:22 +00001//===-- 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 "lldb/Breakpoint/StoppointCallbackContext.h"
13#include "lldb/Core/Debugger.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Core/ModuleList.h"
16#include "lldb/Core/RegularExpression.h"
17#include "lldb/Core/PluginInterface.h"
18#include "lldb/Core/PluginManager.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"
Kuba Breckabcdce3f2016-04-28 15:27:10 +000026#include "lldb/Symbol/Variable.h"
27#include "lldb/Symbol/VariableList.h"
Kuba Brecka6a831432016-03-23 15:36:22 +000028#include "lldb/Target/InstrumentationRuntimeStopInfo.h"
Kuba Brecka1aad8fb2016-04-10 18:57:38 +000029#include "lldb/Target/SectionLoadList.h"
Kuba Brecka6a831432016-03-23 15:36:22 +000030#include "lldb/Target/StopInfo.h"
31#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
Kuba Brecka1aad8fb2016-04-10 18:57:38 +000033#include "Plugins/Process/Utility/HistoryThread.h"
Kuba Brecka6a831432016-03-23 15:36:22 +000034
35using namespace lldb;
36using namespace lldb_private;
37
38lldb::InstrumentationRuntimeSP
39ThreadSanitizerRuntime::CreateInstance (const lldb::ProcessSP &process_sp)
40{
41 return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp));
42}
43
44void
45ThreadSanitizerRuntime::Initialize()
46{
47 PluginManager::RegisterPlugin (GetPluginNameStatic(),
48 "ThreadSanitizer instrumentation runtime plugin.",
49 CreateInstance,
50 GetTypeStatic);
51}
52
53void
54ThreadSanitizerRuntime::Terminate()
55{
56 PluginManager::UnregisterPlugin (CreateInstance);
57}
58
59lldb_private::ConstString
60ThreadSanitizerRuntime::GetPluginNameStatic()
61{
62 return ConstString("ThreadSanitizer");
63}
64
65lldb::InstrumentationRuntimeType
66ThreadSanitizerRuntime::GetTypeStatic()
67{
68 return eInstrumentationRuntimeTypeThreadSanitizer;
69}
70
Kuba Brecka6a831432016-03-23 15:36:22 +000071ThreadSanitizerRuntime::~ThreadSanitizerRuntime()
72{
73 Deactivate();
74}
75
76static bool
77ModuleContainsTSanRuntime(ModuleSP module_sp)
78{
79 static ConstString g_tsan_get_current_report("__tsan_get_current_report");
80 const Symbol* symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny);
81 return symbol != nullptr;
82}
83
84void
85ThreadSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
86{
87 if (IsActive())
88 return;
89
90 if (GetRuntimeModuleSP()) {
91 Activate();
92 return;
93 }
94
95 module_list.ForEach ([this](const lldb::ModuleSP module_sp) -> bool
96 {
97 const FileSpec & file_spec = module_sp->GetFileSpec();
98 if (! file_spec)
99 return true; // Keep iterating through modules
100
101 llvm::StringRef module_basename(file_spec.GetFilename().GetStringRef());
102 if (module_sp->IsExecutable() || module_basename.startswith("libclang_rt.tsan_"))
103 {
104 if (ModuleContainsTSanRuntime(module_sp))
105 {
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000106 SetRuntimeModuleSP(module_sp);
Kuba Brecka6a831432016-03-23 15:36:22 +0000107 Activate();
108 return false; // Stop iterating
109 }
110 }
111
112 return true; // Keep iterating through modules
113 });
114}
115
Kuba Brecka6a831432016-03-23 15:36:22 +0000116#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000
117
118const char *
119thread_sanitizer_retrieve_report_data_prefix = R"(
120extern "C"
121{
122 void *__tsan_get_current_report();
123 int __tsan_get_report_data(void *report, const char **description, int *count,
124 int *stack_count, int *mop_count, int *loc_count,
125 int *mutex_count, int *thread_count,
126 int *unique_tid_count, void **sleep_trace,
127 unsigned long trace_size);
128 int __tsan_get_report_stack(void *report, unsigned long idx, void **trace,
129 unsigned long trace_size);
130 int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr,
131 int *size, int *write, int *atomic, void **trace,
132 unsigned long trace_size);
133 int __tsan_get_report_loc(void *report, unsigned long idx, const char **type,
134 void **addr, unsigned long *start, unsigned long *size, int *tid,
135 int *fd, int *suppressable, void **trace,
136 unsigned long trace_size);
137 int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr,
138 int *destroyed, void **trace, unsigned long trace_size);
Kuba Brecka5b31c422016-04-22 10:40:14 +0000139 int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id,
Kuba Brecka6a831432016-03-23 15:36:22 +0000140 int *running, const char **name, int *parent_tid,
141 void **trace, unsigned long trace_size);
142 int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid);
143}
144
145const int REPORT_TRACE_SIZE = 128;
146const int REPORT_ARRAY_SIZE = 4;
147
148struct data {
149 void *report;
150 const char *description;
151 int report_count;
152
153 void *sleep_trace[REPORT_TRACE_SIZE];
154
155 int stack_count;
156 struct {
157 int idx;
158 void *trace[REPORT_TRACE_SIZE];
159 } stacks[REPORT_ARRAY_SIZE];
160
161 int mop_count;
162 struct {
163 int idx;
164 int tid;
165 int size;
166 int write;
167 int atomic;
168 void *addr;
169 void *trace[REPORT_TRACE_SIZE];
170 } mops[REPORT_ARRAY_SIZE];
171
172 int loc_count;
173 struct {
174 int idx;
175 const char *type;
176 void *addr;
177 unsigned long start;
178 unsigned long size;
179 int tid;
180 int fd;
181 int suppressable;
182 void *trace[REPORT_TRACE_SIZE];
183 } locs[REPORT_ARRAY_SIZE];
184
185 int mutex_count;
186 struct {
187 int idx;
188 unsigned long mutex_id;
189 void *addr;
190 int destroyed;
191 void *trace[REPORT_TRACE_SIZE];
192 } mutexes[REPORT_ARRAY_SIZE];
193
194 int thread_count;
195 struct {
196 int idx;
197 int tid;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000198 unsigned long os_id;
Kuba Brecka6a831432016-03-23 15:36:22 +0000199 int running;
200 const char *name;
201 int parent_tid;
202 void *trace[REPORT_TRACE_SIZE];
203 } threads[REPORT_ARRAY_SIZE];
204
205 int unique_tid_count;
206 struct {
207 int idx;
208 int tid;
209 } unique_tids[REPORT_ARRAY_SIZE];
210};
211)";
212
213const char *
214thread_sanitizer_retrieve_report_data_command = R"(
215data t = {0};
216
217t.report = __tsan_get_current_report();
218__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);
219
220if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
221for (int i = 0; i < t.stack_count; i++) {
222 t.stacks[i].idx = i;
223 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
224}
225
226if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
227for (int i = 0; i < t.mop_count; i++) {
228 t.mops[i].idx = i;
229 __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);
230}
231
232if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
233for (int i = 0; i < t.loc_count; i++) {
234 t.locs[i].idx = i;
235 __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);
236}
237
238if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
239for (int i = 0; i < t.mutex_count; i++) {
240 t.mutexes[i].idx = i;
241 __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);
242}
243
244if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
245for (int i = 0; i < t.thread_count; i++) {
246 t.threads[i].idx = i;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000247 __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);
Kuba Brecka6a831432016-03-23 15:36:22 +0000248}
249
Kuba Brecka058c3022016-03-30 10:50:24 +0000250if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
Kuba Brecka6a831432016-03-23 15:36:22 +0000251for (int i = 0; i < t.unique_tid_count; i++) {
252 t.unique_tids[i].idx = i;
253 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
254}
255
256t;
257)";
258
259static StructuredData::Array *
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000260CreateStackTrace(ValueObjectSP o, const std::string &trace_item_name = ".trace") {
Kuba Brecka6a831432016-03-23 15:36:22 +0000261 StructuredData::Array *trace = new StructuredData::Array();
262 ValueObjectSP trace_value_object = o->GetValueForExpressionPath(trace_item_name.c_str());
263 for (int j = 0; j < 8; j++) {
264 addr_t trace_addr = trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
265 if (trace_addr == 0)
266 break;
267 trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
268 }
269 return trace;
270}
271
272static StructuredData::Array *
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000273ConvertToStructuredArray(ValueObjectSP return_value_sp, const std::string &items_name, const std::string &count_name, std::function <void(ValueObjectSP o, StructuredData::Dictionary *dict)> const &callback)
Kuba Brecka6a831432016-03-23 15:36:22 +0000274{
275 StructuredData::Array *array = new StructuredData::Array();
276 unsigned int count = return_value_sp->GetValueForExpressionPath(count_name.c_str())->GetValueAsUnsigned(0);
277 ValueObjectSP objects = return_value_sp->GetValueForExpressionPath(items_name.c_str());
Pavel Labath1b46a722016-03-30 09:42:59 +0000278 for (unsigned int i = 0; i < count; i++) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000279 ValueObjectSP o = objects->GetChildAtIndex(i, true);
280 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
281
282 callback(o, dict);
283
284 array->AddItem(StructuredData::ObjectSP(dict));
285 }
286 return array;
287}
288
289static std::string
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000290RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, const std::string &expression_path)
Kuba Brecka6a831432016-03-23 15:36:22 +0000291{
292 addr_t ptr = return_value_sp->GetValueForExpressionPath(expression_path.c_str())->GetValueAsUnsigned(0);
293 std::string str;
294 Error error;
295 process_sp->ReadCStringFromMemory(ptr, str, error);
296 return str;
297}
298
Kuba Brecka5b31c422016-04-22 10:40:14 +0000299static void
300GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, std::map<uint64_t, user_id_t> &thread_id_map)
301{
302 ConvertToStructuredArray(data, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
303 uint64_t thread_id = o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
304 uint64_t thread_os_id = o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
305 user_id_t lldb_user_id = 0;
306
307 bool can_update = true;
308 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(thread_os_id, can_update);
309 if (lldb_thread) {
310 lldb_user_id = lldb_thread->GetIndexID();
311 } else {
312 // This isn't a live thread anymore. Ask process to assign a new Index ID (or return an old one if we've already seen this thread_os_id).
313 // It will also make sure that no new threads are assigned this Index ID.
314 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
315 }
316
317 thread_id_map[thread_id] = lldb_user_id;
318 });
319}
320
321static user_id_t Renumber(uint64_t id, std::map<uint64_t, user_id_t> &thread_id_map) {
Vedant Kumarcbba4b22016-08-01 16:37:37 +0000322 auto IT = thread_id_map.find(id);
323 if (IT == thread_id_map.end())
Kuba Brecka5b31c422016-04-22 10:40:14 +0000324 return 0;
325
Vedant Kumarcbba4b22016-08-01 16:37:37 +0000326 return IT->second;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000327}
328
Kuba Brecka6a831432016-03-23 15:36:22 +0000329StructuredData::ObjectSP
330ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref)
331{
332 ProcessSP process_sp = GetProcessSP();
333 if (!process_sp)
334 return StructuredData::ObjectSP();
335
336 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
337 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
338
339 if (!frame_sp)
340 return StructuredData::ObjectSP();
341
342 EvaluateExpressionOptions options;
343 options.SetUnwindOnError(true);
344 options.SetTryAllThreads(true);
345 options.SetStopOthers(true);
346 options.SetIgnoreBreakpoints(true);
347 options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC);
348 options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
Kuba Breckac359d5c2016-07-06 11:46:20 +0000349 options.SetAutoApplyFixIts(false);
350 options.SetLanguage(eLanguageTypeObjC_plus_plus);
Kuba Brecka6a831432016-03-23 15:36:22 +0000351
352 ValueObjectSP main_value;
353 ExecutionContext exe_ctx;
354 Error eval_error;
355 frame_sp->CalculateExecutionContext(exe_ctx);
356 ExpressionResults result = UserExpression::Evaluate (exe_ctx,
357 options,
358 thread_sanitizer_retrieve_report_data_command,
359 "",
360 main_value,
361 eval_error);
362 if (result != eExpressionCompleted) {
363 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString());
364 return StructuredData::ObjectSP();
365 }
366
Kuba Brecka5b31c422016-04-22 10:40:14 +0000367 std::map<uint64_t, user_id_t> thread_id_map;
368 GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
369
Kuba Brecka6a831432016-03-23 15:36:22 +0000370 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
371 dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000372 dict->AddStringItem("issue_type", RetrieveString(main_value, process_sp, ".description"));
Kuba Brecka6a831432016-03-23 15:36:22 +0000373 dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0));
374 dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace")));
375
Kuba Brecka04297512016-05-24 20:35:28 +0000376 StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [thread_sp] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000377 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
378 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
Kuba Brecka04297512016-05-24 20:35:28 +0000379 // "stacks" happen on the current thread
380 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
Kuba Brecka6a831432016-03-23 15:36:22 +0000381 });
382 dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
383
Kuba Brecka5b31c422016-04-22 10:40:14 +0000384 StructuredData::Array *mops = ConvertToStructuredArray(main_value, ".mops", ".mop_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000385 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000386 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000387 dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
388 dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
389 dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
390 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
391 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
392 });
393 dict->AddItem("mops", StructuredData::ObjectSP(mops));
394
Kuba Brecka5b31c422016-04-22 10:40:14 +0000395 StructuredData::Array *locs = ConvertToStructuredArray(main_value, ".locs", ".loc_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000396 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
397 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
398 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
399 dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
400 dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000401 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000402 dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
403 dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0));
404 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
405 });
406 dict->AddItem("locs", StructuredData::ObjectSP(locs));
407
408 StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) {
409 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
410 dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
411 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
412 dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
413 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
414 });
415 dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
416
Kuba Brecka5b31c422016-04-22 10:40:14 +0000417 StructuredData::Array *threads = ConvertToStructuredArray(main_value, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000418 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000419 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
420 dict->AddIntegerItem("thread_os_id", o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
Kuba Brecka6a831432016-03-23 15:36:22 +0000421 dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
422 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000423 dict->AddIntegerItem("parent_thread_id", Renumber(o->GetValueForExpressionPath(".parent_tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000424 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
425 });
426 dict->AddItem("threads", StructuredData::ObjectSP(threads));
427
Kuba Brecka5b31c422016-04-22 10:40:14 +0000428 StructuredData::Array *unique_tids = ConvertToStructuredArray(main_value, ".unique_tids", ".unique_tid_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000429 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000430 dict->AddIntegerItem("tid", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000431 });
432 dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
433
434 return StructuredData::ObjectSP(dict);
435}
436
437std::string
438ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report)
439{
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000440 std::string description = report->GetAsDictionary()->GetValueForKey("issue_type")->GetAsString()->GetValue();
Kuba Brecka6a831432016-03-23 15:36:22 +0000441
442 if (description == "data-race") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000443 return "Data race";
Kuba Brecka6a831432016-03-23 15:36:22 +0000444 } else if (description == "data-race-vptr") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000445 return "Data race on C++ virtual pointer";
Kuba Brecka6a831432016-03-23 15:36:22 +0000446 } else if (description == "heap-use-after-free") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000447 return "Use of deallocated memory";
Kuba Brecka6a831432016-03-23 15:36:22 +0000448 } else if (description == "heap-use-after-free-vptr") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000449 return "Use of deallocated C++ virtual pointer";
Kuba Brecka6a831432016-03-23 15:36:22 +0000450 } else if (description == "thread-leak") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000451 return "Thread leak";
Kuba Brecka6a831432016-03-23 15:36:22 +0000452 } else if (description == "locked-mutex-destroy") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000453 return "Destruction of a locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000454 } else if (description == "mutex-double-lock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000455 return "Double lock of a mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000456 } else if (description == "mutex-invalid-access") {
Kuba Brecka52ded802016-05-22 14:32:45 +0000457 return "Use of an uninitialized or destroyed mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000458 } else if (description == "mutex-bad-unlock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000459 return "Unlock of an unlocked mutex (or by a wrong thread)";
Kuba Brecka6a831432016-03-23 15:36:22 +0000460 } else if (description == "mutex-bad-read-lock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000461 return "Read lock of a write locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000462 } else if (description == "mutex-bad-read-unlock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000463 return "Read unlock of a write locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000464 } else if (description == "signal-unsafe-call") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000465 return "Signal-unsafe call inside a signal handler";
Kuba Brecka6a831432016-03-23 15:36:22 +0000466 } else if (description == "errno-in-signal-handler") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000467 return "Overwrite of errno in a signal handler";
Kuba Brecka6a831432016-03-23 15:36:22 +0000468 } else if (description == "lock-order-inversion") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000469 return "Lock order inversion (potential deadlock)";
Kuba Brecka6a831432016-03-23 15:36:22 +0000470 }
471
472 // for unknown report codes just show the code
473 return description;
474}
475
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000476static std::string
477Sprintf(const char *format, ...)
478{
479 StreamString s;
480 va_list args;
481 va_start (args, format);
482 s.PrintfVarArg(format, args);
483 va_end (args);
484 return s.GetString();
485}
486
487static std::string
488GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr)
489{
490 lldb_private::Address so_addr;
491 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
492 return "";
493
494 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
495 if (! symbol)
496 return "";
497
498 std::string sym_name = symbol->GetName().GetCString();
499 return sym_name;
500}
501
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000502static void
503GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, Declaration &decl)
504{
505 lldb_private::Address so_addr;
506 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
507 return;
508
509 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
510 if (! symbol)
511 return;
512
Devin Coughlina10ab762016-06-01 21:32:45 +0000513 ConstString sym_name = symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000514
515 ModuleSP module = symbol->CalculateSymbolContextModule();
516 if (! module)
517 return;
518
519 VariableList var_list;
520 module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list);
521 if (var_list.GetSize() < 1)
522 return;
523
524 VariableSP var = var_list.GetVariableAtIndex(0);
525 decl = var->GetDeclaration();
526}
527
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000528addr_t
529ThreadSanitizerRuntime::GetFirstNonInternalFramePc(StructuredData::ObjectSP trace)
530{
531 ProcessSP process_sp = GetProcessSP();
532 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
533
534 addr_t result = 0;
535 trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, &result] (StructuredData::Object *o) -> bool {
536 addr_t addr = o->GetIntegerValue();
537 lldb_private::Address so_addr;
538 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
539 return true;
540
541 if (so_addr.GetModule() == runtime_module_sp)
542 return true;
543
544 result = addr;
545 return false;
546 });
547
548 return result;
549}
550
551std::string
552ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report)
553{
554 ProcessSP process_sp = GetProcessSP();
555
556 std::string summary = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue();
557 addr_t pc = 0;
558 if (report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetSize() > 0)
559 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace"));
560
561 if (report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetSize() > 0)
562 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace"));
563
564 if (pc != 0) {
565 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
566 }
567
568 if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) {
569 StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0);
570 addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue();
571 if (addr == 0)
572 addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue();
573
574 if (addr != 0) {
Kuba Brecka6602e692016-05-24 17:47:23 +0000575 std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
576 if (!global_name.empty()) {
577 summary = summary + " at " + global_name;
578 } else {
579 summary = summary + " at " + Sprintf("0x%llx", addr);
580 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000581 } else {
582 int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue();
583 if (fd != 0) {
584 summary = summary + " on file descriptor " + Sprintf("%d", fd);
585 }
586 }
587 }
588
589 return summary;
590}
591
592addr_t
593ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report)
594{
595 addr_t result = (addr_t)-1;
596
597 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&result] (StructuredData::Object *o) -> bool {
598 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
599 if (addr < result) result = addr;
600 return true;
601 });
602
603 return (result == (addr_t)-1) ? 0 : result;
604}
605
606std::string
Kuba Brecka6602e692016-05-24 17:47:23 +0000607ThreadSanitizerRuntime::GetLocationDescription(StructuredData::ObjectSP report, addr_t &global_addr, std::string &global_name, std::string &filename, uint32_t &line)
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000608{
609 std::string result = "";
610
611 ProcessSP process_sp = GetProcessSP();
612
613 if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) {
614 StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0);
615 std::string type = loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
616 if (type == "global") {
Kuba Brecka6602e692016-05-24 17:47:23 +0000617 global_addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue();
618 global_name = GetSymbolNameFromAddress(process_sp, global_addr);
619 if (!global_name.empty()) {
620 result = Sprintf("'%s' is a global variable (0x%llx)", global_name.c_str(), global_addr);
621 } else {
622 result = Sprintf("0x%llx is a global variable", global_addr);
623 }
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000624
625 Declaration decl;
Kuba Brecka6602e692016-05-24 17:47:23 +0000626 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000627 if (decl.GetFile()) {
628 filename = decl.GetFile().GetPath();
629 line = decl.GetLine();
630 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000631 } else if (type == "heap") {
632 addr_t addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue();
633 long size = loc->GetAsDictionary()->GetValueForKey("size")->GetAsInteger()->GetValue();
634 result = Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
635 } else if (type == "stack") {
636 int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue();
Kuba Brecka1a349b82016-05-22 14:56:33 +0000637 result = Sprintf("Location is stack of thread %d", tid);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000638 } else if (type == "tls") {
639 int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue();
Kuba Brecka1a349b82016-05-22 14:56:33 +0000640 result = Sprintf("Location is TLS of thread %d", tid);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000641 } else if (type == "fd") {
642 int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue();
643 result = Sprintf("Location is file descriptor %d", fd);
644 }
645 }
646
647 return result;
648}
649
Kuba Brecka6a831432016-03-23 15:36:22 +0000650bool
651ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id)
652{
653 assert (baton && "null baton");
654 if (!baton)
655 return false;
656
657 ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton);
658
659 StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000660 std::string stop_reason_description;
Kuba Brecka6a831432016-03-23 15:36:22 +0000661 if (report) {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000662 std::string issue_description = instance->FormatDescription(report);
663 report->GetAsDictionary()->AddStringItem("description", issue_description);
664 stop_reason_description = issue_description + " detected";
665 report->GetAsDictionary()->AddStringItem("stop_description", stop_reason_description);
666 std::string summary = instance->GenerateSummary(report);
667 report->GetAsDictionary()->AddStringItem("summary", summary);
668 addr_t main_address = instance->GetMainRacyAddress(report);
669 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000670
Kuba Brecka6602e692016-05-24 17:47:23 +0000671 addr_t global_addr = 0;
672 std::string global_name = "";
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000673 std::string location_filename = "";
674 uint32_t location_line = 0;
Kuba Brecka6602e692016-05-24 17:47:23 +0000675 std::string location_description = instance->GetLocationDescription(report, global_addr, global_name, location_filename, location_line);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000676 report->GetAsDictionary()->AddStringItem("location_description", location_description);
Kuba Brecka6602e692016-05-24 17:47:23 +0000677 if (global_addr != 0) {
678 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
679 }
680 if (!global_name.empty()) {
681 report->GetAsDictionary()->AddStringItem("global_name", global_name);
682 }
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000683 if (location_filename != "") {
684 report->GetAsDictionary()->AddStringItem("location_filename", location_filename);
685 report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
686 }
Kuba Brecka6602e692016-05-24 17:47:23 +0000687
688 bool all_addresses_are_same = true;
689 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&all_addresses_are_same, main_address] (StructuredData::Object *o) -> bool {
690 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
691 if (main_address != addr) all_addresses_are_same = false;
692 return true;
693 });
694 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same", all_addresses_are_same);
Kuba Brecka6a831432016-03-23 15:36:22 +0000695 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000696
Kuba Brecka6a831432016-03-23 15:36:22 +0000697 ProcessSP process_sp = instance->GetProcessSP();
698 // Make sure this is the right process
699 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP())
700 {
701 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
702 if (thread_sp)
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000703 thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(*thread_sp, stop_reason_description.c_str(), report));
Kuba Brecka6a831432016-03-23 15:36:22 +0000704
705 StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile());
706 if (stream_sp)
707 {
708 stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n");
709 }
710 return true; // Return true to stop the target
711 }
712 else
713 return false; // Let target run
714}
715
716void
717ThreadSanitizerRuntime::Activate()
718{
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000719 if (IsActive())
Kuba Brecka6a831432016-03-23 15:36:22 +0000720 return;
721
722 ProcessSP process_sp = GetProcessSP();
723 if (!process_sp)
724 return;
725
726 ConstString symbol_name ("__tsan_on_report");
727 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode);
728
729 if (symbol == NULL)
730 return;
731
732 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
733 return;
734
735 Target &target = process_sp->GetTarget();
736 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
737
738 if (symbol_address == LLDB_INVALID_ADDRESS)
739 return;
740
741 bool internal = true;
742 bool hardware = false;
743 Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get();
744 breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true);
745 breakpoint->SetBreakpointKind ("thread-sanitizer-report");
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000746 SetBreakpointID(breakpoint->GetID());
Kuba Brecka6a831432016-03-23 15:36:22 +0000747
748 StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile());
749 if (stream_sp)
750 {
751 stream_sp->Printf ("ThreadSanitizer debugger support is active.\n");
752 }
753
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000754 SetActive(true);
Kuba Brecka6a831432016-03-23 15:36:22 +0000755}
756
757void
758ThreadSanitizerRuntime::Deactivate()
759{
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000760 if (GetBreakpointID() != LLDB_INVALID_BREAK_ID)
Kuba Brecka6a831432016-03-23 15:36:22 +0000761 {
762 ProcessSP process_sp = GetProcessSP();
763 if (process_sp)
764 {
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000765 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
766 SetBreakpointID(LLDB_INVALID_BREAK_ID);
Kuba Brecka6a831432016-03-23 15:36:22 +0000767 }
768 }
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000769 SetActive(false);
Kuba Brecka6a831432016-03-23 15:36:22 +0000770}
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000771
772static std::string
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000773GenerateThreadName(const std::string &path, StructuredData::Object *o, StructuredData::ObjectSP main_info) {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000774 std::string result = "additional information";
775
776 if (path == "mops") {
777 int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
778 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
779 bool is_write = o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
780 bool is_atomic = o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
781 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
782
Kuba Brecka6602e692016-05-24 17:47:23 +0000783 std::string addr_string = Sprintf(" at 0x%llx", addr);
784
785 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")->GetBooleanValue()){
786 addr_string = "";
787 }
788
789 result = Sprintf("%s%s of size %d%s by thread %d", is_atomic ? "atomic " : "", is_write ? "write" : "read", size, addr_string.c_str(), thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000790 }
791
792 if (path == "threads") {
793 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
Kuba Brecka5c7b3632016-05-01 11:26:06 +0000794 result = Sprintf("Thread %d created", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000795 }
796
797 if (path == "locs") {
798 std::string type = o->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
799 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
800 int fd = o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
801 if (type == "heap") {
Kuba Brecka1a349b82016-05-22 14:56:33 +0000802 result = Sprintf("Heap block allocated by thread %d", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000803 } else if (type == "fd") {
Kuba Brecka1a349b82016-05-22 14:56:33 +0000804 result = Sprintf("File descriptor %d created by thread %t", fd, thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000805 }
806 }
807
808 if (path == "mutexes") {
809 int mutex_id = o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
810
Kuba Brecka5c7b3632016-05-01 11:26:06 +0000811 result = Sprintf("Mutex M%d created", mutex_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000812 }
813
814 if (path == "stacks") {
Kuba Brecka04297512016-05-24 20:35:28 +0000815 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
816 result = Sprintf("Thread %d", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000817 }
818
819 result[0] = toupper(result[0]);
820
821 return result;
822}
823
824static void
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000825AddThreadsForPath(const std::string &path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info)
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000826{
Kuba Brecka6602e692016-05-24 17:47:23 +0000827 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach([process_sp, threads, path, info] (StructuredData::Object *o) -> bool {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000828 std::vector<lldb::addr_t> pcs;
829 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach([&pcs] (StructuredData::Object *pc) -> bool {
830 pcs.push_back(pc->GetAsInteger()->GetValue());
831 return true;
832 });
833
834 if (pcs.size() == 0)
835 return true;
836
Kuba Brecka5b31c422016-04-22 10:40:14 +0000837 StructuredData::ObjectSP thread_id_obj = o->GetObjectForDotSeparatedPath("thread_os_id");
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000838 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
839
840 uint32_t stop_id = 0;
841 bool stop_id_is_valid = false;
842 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid);
843 ThreadSP new_thread_sp(history_thread);
Kuba Brecka6602e692016-05-24 17:47:23 +0000844 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000845
846 // Save this in the Process' ExtendedThreadList so a strong pointer retains the object
847 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
848 threads->AddThread(new_thread_sp);
849
850 return true;
851 });
852}
853
854lldb::ThreadCollectionSP
855ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)
856{
857 ThreadCollectionSP threads;
858 threads.reset(new ThreadCollection());
859
860 if (info->GetObjectForDotSeparatedPath("instrumentation_class")->GetStringValue() != "ThreadSanitizer")
861 return threads;
862
863 ProcessSP process_sp = GetProcessSP();
864
865 AddThreadsForPath("stacks", threads, process_sp, info);
866 AddThreadsForPath("mops", threads, process_sp, info);
867 AddThreadsForPath("locs", threads, process_sp, info);
868 AddThreadsForPath("mutexes", threads, process_sp, info);
869 AddThreadsForPath("threads", threads, process_sp, info);
870
871 return threads;
872}