blob: a08c26fa4837d1d74aa9b202d224fae8e4e96862 [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
71ThreadSanitizerRuntime::ThreadSanitizerRuntime(const ProcessSP &process_sp) :
72m_is_active(false),
73m_runtime_module_wp(),
74m_process_wp(),
75m_breakpoint_id(0)
76{
77 if (process_sp)
78 m_process_wp = process_sp;
79}
80
81ThreadSanitizerRuntime::~ThreadSanitizerRuntime()
82{
83 Deactivate();
84}
85
86static bool
87ModuleContainsTSanRuntime(ModuleSP module_sp)
88{
89 static ConstString g_tsan_get_current_report("__tsan_get_current_report");
90 const Symbol* symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny);
91 return symbol != nullptr;
92}
93
94void
95ThreadSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
96{
97 if (IsActive())
98 return;
99
100 if (GetRuntimeModuleSP()) {
101 Activate();
102 return;
103 }
104
105 module_list.ForEach ([this](const lldb::ModuleSP module_sp) -> bool
106 {
107 const FileSpec & file_spec = module_sp->GetFileSpec();
108 if (! file_spec)
109 return true; // Keep iterating through modules
110
111 llvm::StringRef module_basename(file_spec.GetFilename().GetStringRef());
112 if (module_sp->IsExecutable() || module_basename.startswith("libclang_rt.tsan_"))
113 {
114 if (ModuleContainsTSanRuntime(module_sp))
115 {
116 m_runtime_module_wp = module_sp;
117 Activate();
118 return false; // Stop iterating
119 }
120 }
121
122 return true; // Keep iterating through modules
123 });
124}
125
126bool
127ThreadSanitizerRuntime::IsActive()
128{
129 return m_is_active;
130}
131
132#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000
133
134const char *
135thread_sanitizer_retrieve_report_data_prefix = R"(
136extern "C"
137{
138 void *__tsan_get_current_report();
139 int __tsan_get_report_data(void *report, const char **description, int *count,
140 int *stack_count, int *mop_count, int *loc_count,
141 int *mutex_count, int *thread_count,
142 int *unique_tid_count, void **sleep_trace,
143 unsigned long trace_size);
144 int __tsan_get_report_stack(void *report, unsigned long idx, void **trace,
145 unsigned long trace_size);
146 int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr,
147 int *size, int *write, int *atomic, void **trace,
148 unsigned long trace_size);
149 int __tsan_get_report_loc(void *report, unsigned long idx, const char **type,
150 void **addr, unsigned long *start, unsigned long *size, int *tid,
151 int *fd, int *suppressable, void **trace,
152 unsigned long trace_size);
153 int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr,
154 int *destroyed, void **trace, unsigned long trace_size);
Kuba Brecka5b31c422016-04-22 10:40:14 +0000155 int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id,
Kuba Brecka6a831432016-03-23 15:36:22 +0000156 int *running, const char **name, int *parent_tid,
157 void **trace, unsigned long trace_size);
158 int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid);
159}
160
161const int REPORT_TRACE_SIZE = 128;
162const int REPORT_ARRAY_SIZE = 4;
163
164struct data {
165 void *report;
166 const char *description;
167 int report_count;
168
169 void *sleep_trace[REPORT_TRACE_SIZE];
170
171 int stack_count;
172 struct {
173 int idx;
174 void *trace[REPORT_TRACE_SIZE];
175 } stacks[REPORT_ARRAY_SIZE];
176
177 int mop_count;
178 struct {
179 int idx;
180 int tid;
181 int size;
182 int write;
183 int atomic;
184 void *addr;
185 void *trace[REPORT_TRACE_SIZE];
186 } mops[REPORT_ARRAY_SIZE];
187
188 int loc_count;
189 struct {
190 int idx;
191 const char *type;
192 void *addr;
193 unsigned long start;
194 unsigned long size;
195 int tid;
196 int fd;
197 int suppressable;
198 void *trace[REPORT_TRACE_SIZE];
199 } locs[REPORT_ARRAY_SIZE];
200
201 int mutex_count;
202 struct {
203 int idx;
204 unsigned long mutex_id;
205 void *addr;
206 int destroyed;
207 void *trace[REPORT_TRACE_SIZE];
208 } mutexes[REPORT_ARRAY_SIZE];
209
210 int thread_count;
211 struct {
212 int idx;
213 int tid;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000214 unsigned long os_id;
Kuba Brecka6a831432016-03-23 15:36:22 +0000215 int running;
216 const char *name;
217 int parent_tid;
218 void *trace[REPORT_TRACE_SIZE];
219 } threads[REPORT_ARRAY_SIZE];
220
221 int unique_tid_count;
222 struct {
223 int idx;
224 int tid;
225 } unique_tids[REPORT_ARRAY_SIZE];
226};
227)";
228
229const char *
230thread_sanitizer_retrieve_report_data_command = R"(
231data t = {0};
232
233t.report = __tsan_get_current_report();
234__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);
235
236if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
237for (int i = 0; i < t.stack_count; i++) {
238 t.stacks[i].idx = i;
239 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
240}
241
242if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
243for (int i = 0; i < t.mop_count; i++) {
244 t.mops[i].idx = i;
245 __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);
246}
247
248if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
249for (int i = 0; i < t.loc_count; i++) {
250 t.locs[i].idx = i;
251 __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);
252}
253
254if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
255for (int i = 0; i < t.mutex_count; i++) {
256 t.mutexes[i].idx = i;
257 __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);
258}
259
260if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
261for (int i = 0; i < t.thread_count; i++) {
262 t.threads[i].idx = i;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000263 __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 +0000264}
265
Kuba Brecka058c3022016-03-30 10:50:24 +0000266if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
Kuba Brecka6a831432016-03-23 15:36:22 +0000267for (int i = 0; i < t.unique_tid_count; i++) {
268 t.unique_tids[i].idx = i;
269 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
270}
271
272t;
273)";
274
275static StructuredData::Array *
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000276CreateStackTrace(ValueObjectSP o, const std::string &trace_item_name = ".trace") {
Kuba Brecka6a831432016-03-23 15:36:22 +0000277 StructuredData::Array *trace = new StructuredData::Array();
278 ValueObjectSP trace_value_object = o->GetValueForExpressionPath(trace_item_name.c_str());
279 for (int j = 0; j < 8; j++) {
280 addr_t trace_addr = trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
281 if (trace_addr == 0)
282 break;
283 trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
284 }
285 return trace;
286}
287
288static StructuredData::Array *
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000289ConvertToStructuredArray(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 +0000290{
291 StructuredData::Array *array = new StructuredData::Array();
292 unsigned int count = return_value_sp->GetValueForExpressionPath(count_name.c_str())->GetValueAsUnsigned(0);
293 ValueObjectSP objects = return_value_sp->GetValueForExpressionPath(items_name.c_str());
Pavel Labath1b46a722016-03-30 09:42:59 +0000294 for (unsigned int i = 0; i < count; i++) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000295 ValueObjectSP o = objects->GetChildAtIndex(i, true);
296 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
297
298 callback(o, dict);
299
300 array->AddItem(StructuredData::ObjectSP(dict));
301 }
302 return array;
303}
304
305static std::string
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000306RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, const std::string &expression_path)
Kuba Brecka6a831432016-03-23 15:36:22 +0000307{
308 addr_t ptr = return_value_sp->GetValueForExpressionPath(expression_path.c_str())->GetValueAsUnsigned(0);
309 std::string str;
310 Error error;
311 process_sp->ReadCStringFromMemory(ptr, str, error);
312 return str;
313}
314
Kuba Brecka5b31c422016-04-22 10:40:14 +0000315static void
316GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, std::map<uint64_t, user_id_t> &thread_id_map)
317{
318 ConvertToStructuredArray(data, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
319 uint64_t thread_id = o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
320 uint64_t thread_os_id = o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
321 user_id_t lldb_user_id = 0;
322
323 bool can_update = true;
324 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(thread_os_id, can_update);
325 if (lldb_thread) {
326 lldb_user_id = lldb_thread->GetIndexID();
327 } else {
328 // 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).
329 // It will also make sure that no new threads are assigned this Index ID.
330 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
331 }
332
333 thread_id_map[thread_id] = lldb_user_id;
334 });
335}
336
337static user_id_t Renumber(uint64_t id, std::map<uint64_t, user_id_t> &thread_id_map) {
Vedant Kumarcbba4b22016-08-01 16:37:37 +0000338 auto IT = thread_id_map.find(id);
339 if (IT == thread_id_map.end())
Kuba Brecka5b31c422016-04-22 10:40:14 +0000340 return 0;
341
Vedant Kumarcbba4b22016-08-01 16:37:37 +0000342 return IT->second;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000343}
344
Kuba Brecka6a831432016-03-23 15:36:22 +0000345StructuredData::ObjectSP
346ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref)
347{
348 ProcessSP process_sp = GetProcessSP();
349 if (!process_sp)
350 return StructuredData::ObjectSP();
351
352 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
353 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
354
355 if (!frame_sp)
356 return StructuredData::ObjectSP();
357
358 EvaluateExpressionOptions options;
359 options.SetUnwindOnError(true);
360 options.SetTryAllThreads(true);
361 options.SetStopOthers(true);
362 options.SetIgnoreBreakpoints(true);
363 options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC);
364 options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
Kuba Breckac359d5c2016-07-06 11:46:20 +0000365 options.SetAutoApplyFixIts(false);
366 options.SetLanguage(eLanguageTypeObjC_plus_plus);
Kuba Brecka6a831432016-03-23 15:36:22 +0000367
368 ValueObjectSP main_value;
369 ExecutionContext exe_ctx;
370 Error eval_error;
371 frame_sp->CalculateExecutionContext(exe_ctx);
372 ExpressionResults result = UserExpression::Evaluate (exe_ctx,
373 options,
374 thread_sanitizer_retrieve_report_data_command,
375 "",
376 main_value,
377 eval_error);
378 if (result != eExpressionCompleted) {
379 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString());
380 return StructuredData::ObjectSP();
381 }
382
Kuba Brecka5b31c422016-04-22 10:40:14 +0000383 std::map<uint64_t, user_id_t> thread_id_map;
384 GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
385
Kuba Brecka6a831432016-03-23 15:36:22 +0000386 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
387 dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000388 dict->AddStringItem("issue_type", RetrieveString(main_value, process_sp, ".description"));
Kuba Brecka6a831432016-03-23 15:36:22 +0000389 dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0));
390 dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace")));
391
Kuba Brecka04297512016-05-24 20:35:28 +0000392 StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [thread_sp] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000393 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
394 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
Kuba Brecka04297512016-05-24 20:35:28 +0000395 // "stacks" happen on the current thread
396 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
Kuba Brecka6a831432016-03-23 15:36:22 +0000397 });
398 dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
399
Kuba Brecka5b31c422016-04-22 10:40:14 +0000400 StructuredData::Array *mops = ConvertToStructuredArray(main_value, ".mops", ".mop_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000401 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000402 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000403 dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
404 dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
405 dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
406 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
407 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
408 });
409 dict->AddItem("mops", StructuredData::ObjectSP(mops));
410
Kuba Brecka5b31c422016-04-22 10:40:14 +0000411 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 +0000412 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
413 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
414 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
415 dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
416 dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000417 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000418 dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
419 dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0));
420 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
421 });
422 dict->AddItem("locs", StructuredData::ObjectSP(locs));
423
424 StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) {
425 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
426 dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
427 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
428 dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
429 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
430 });
431 dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
432
Kuba Brecka5b31c422016-04-22 10:40:14 +0000433 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 +0000434 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000435 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
436 dict->AddIntegerItem("thread_os_id", o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
Kuba Brecka6a831432016-03-23 15:36:22 +0000437 dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
438 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000439 dict->AddIntegerItem("parent_thread_id", Renumber(o->GetValueForExpressionPath(".parent_tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000440 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
441 });
442 dict->AddItem("threads", StructuredData::ObjectSP(threads));
443
Kuba Brecka5b31c422016-04-22 10:40:14 +0000444 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 +0000445 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000446 dict->AddIntegerItem("tid", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000447 });
448 dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
449
450 return StructuredData::ObjectSP(dict);
451}
452
453std::string
454ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report)
455{
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000456 std::string description = report->GetAsDictionary()->GetValueForKey("issue_type")->GetAsString()->GetValue();
Kuba Brecka6a831432016-03-23 15:36:22 +0000457
458 if (description == "data-race") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000459 return "Data race";
Kuba Brecka6a831432016-03-23 15:36:22 +0000460 } else if (description == "data-race-vptr") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000461 return "Data race on C++ virtual pointer";
Kuba Brecka6a831432016-03-23 15:36:22 +0000462 } else if (description == "heap-use-after-free") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000463 return "Use of deallocated memory";
Kuba Brecka6a831432016-03-23 15:36:22 +0000464 } else if (description == "heap-use-after-free-vptr") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000465 return "Use of deallocated C++ virtual pointer";
Kuba Brecka6a831432016-03-23 15:36:22 +0000466 } else if (description == "thread-leak") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000467 return "Thread leak";
Kuba Brecka6a831432016-03-23 15:36:22 +0000468 } else if (description == "locked-mutex-destroy") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000469 return "Destruction of a locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000470 } else if (description == "mutex-double-lock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000471 return "Double lock of a mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000472 } else if (description == "mutex-invalid-access") {
Kuba Brecka52ded802016-05-22 14:32:45 +0000473 return "Use of an uninitialized or destroyed mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000474 } else if (description == "mutex-bad-unlock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000475 return "Unlock of an unlocked mutex (or by a wrong thread)";
Kuba Brecka6a831432016-03-23 15:36:22 +0000476 } else if (description == "mutex-bad-read-lock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000477 return "Read lock of a write locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000478 } else if (description == "mutex-bad-read-unlock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000479 return "Read unlock of a write locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000480 } else if (description == "signal-unsafe-call") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000481 return "Signal-unsafe call inside a signal handler";
Kuba Brecka6a831432016-03-23 15:36:22 +0000482 } else if (description == "errno-in-signal-handler") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000483 return "Overwrite of errno in a signal handler";
Kuba Brecka6a831432016-03-23 15:36:22 +0000484 } else if (description == "lock-order-inversion") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000485 return "Lock order inversion (potential deadlock)";
Kuba Brecka6a831432016-03-23 15:36:22 +0000486 }
487
488 // for unknown report codes just show the code
489 return description;
490}
491
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000492static std::string
493Sprintf(const char *format, ...)
494{
495 StreamString s;
496 va_list args;
497 va_start (args, format);
498 s.PrintfVarArg(format, args);
499 va_end (args);
500 return s.GetString();
501}
502
503static std::string
504GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr)
505{
506 lldb_private::Address so_addr;
507 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
508 return "";
509
510 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
511 if (! symbol)
512 return "";
513
514 std::string sym_name = symbol->GetName().GetCString();
515 return sym_name;
516}
517
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000518static void
519GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, Declaration &decl)
520{
521 lldb_private::Address so_addr;
522 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
523 return;
524
525 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
526 if (! symbol)
527 return;
528
Devin Coughlina10ab762016-06-01 21:32:45 +0000529 ConstString sym_name = symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000530
531 ModuleSP module = symbol->CalculateSymbolContextModule();
532 if (! module)
533 return;
534
535 VariableList var_list;
536 module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list);
537 if (var_list.GetSize() < 1)
538 return;
539
540 VariableSP var = var_list.GetVariableAtIndex(0);
541 decl = var->GetDeclaration();
542}
543
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000544addr_t
545ThreadSanitizerRuntime::GetFirstNonInternalFramePc(StructuredData::ObjectSP trace)
546{
547 ProcessSP process_sp = GetProcessSP();
548 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
549
550 addr_t result = 0;
551 trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, &result] (StructuredData::Object *o) -> bool {
552 addr_t addr = o->GetIntegerValue();
553 lldb_private::Address so_addr;
554 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
555 return true;
556
557 if (so_addr.GetModule() == runtime_module_sp)
558 return true;
559
560 result = addr;
561 return false;
562 });
563
564 return result;
565}
566
567std::string
568ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report)
569{
570 ProcessSP process_sp = GetProcessSP();
571
572 std::string summary = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue();
573 addr_t pc = 0;
574 if (report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetSize() > 0)
575 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace"));
576
577 if (report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetSize() > 0)
578 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace"));
579
580 if (pc != 0) {
581 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
582 }
583
584 if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) {
585 StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0);
586 addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue();
587 if (addr == 0)
588 addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue();
589
590 if (addr != 0) {
Kuba Brecka6602e692016-05-24 17:47:23 +0000591 std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
592 if (!global_name.empty()) {
593 summary = summary + " at " + global_name;
594 } else {
595 summary = summary + " at " + Sprintf("0x%llx", addr);
596 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000597 } else {
598 int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue();
599 if (fd != 0) {
600 summary = summary + " on file descriptor " + Sprintf("%d", fd);
601 }
602 }
603 }
604
605 return summary;
606}
607
608addr_t
609ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report)
610{
611 addr_t result = (addr_t)-1;
612
613 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&result] (StructuredData::Object *o) -> bool {
614 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
615 if (addr < result) result = addr;
616 return true;
617 });
618
619 return (result == (addr_t)-1) ? 0 : result;
620}
621
622std::string
Kuba Brecka6602e692016-05-24 17:47:23 +0000623ThreadSanitizerRuntime::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 +0000624{
625 std::string result = "";
626
627 ProcessSP process_sp = GetProcessSP();
628
629 if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) {
630 StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0);
631 std::string type = loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
632 if (type == "global") {
Kuba Brecka6602e692016-05-24 17:47:23 +0000633 global_addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue();
634 global_name = GetSymbolNameFromAddress(process_sp, global_addr);
635 if (!global_name.empty()) {
636 result = Sprintf("'%s' is a global variable (0x%llx)", global_name.c_str(), global_addr);
637 } else {
638 result = Sprintf("0x%llx is a global variable", global_addr);
639 }
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000640
641 Declaration decl;
Kuba Brecka6602e692016-05-24 17:47:23 +0000642 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000643 if (decl.GetFile()) {
644 filename = decl.GetFile().GetPath();
645 line = decl.GetLine();
646 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000647 } else if (type == "heap") {
648 addr_t addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue();
649 long size = loc->GetAsDictionary()->GetValueForKey("size")->GetAsInteger()->GetValue();
650 result = Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
651 } else if (type == "stack") {
652 int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue();
Kuba Brecka1a349b82016-05-22 14:56:33 +0000653 result = Sprintf("Location is stack of thread %d", tid);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000654 } else if (type == "tls") {
655 int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue();
Kuba Brecka1a349b82016-05-22 14:56:33 +0000656 result = Sprintf("Location is TLS of thread %d", tid);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000657 } else if (type == "fd") {
658 int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue();
659 result = Sprintf("Location is file descriptor %d", fd);
660 }
661 }
662
663 return result;
664}
665
Kuba Brecka6a831432016-03-23 15:36:22 +0000666bool
667ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id)
668{
669 assert (baton && "null baton");
670 if (!baton)
671 return false;
672
673 ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton);
674
675 StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000676 std::string stop_reason_description;
Kuba Brecka6a831432016-03-23 15:36:22 +0000677 if (report) {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000678 std::string issue_description = instance->FormatDescription(report);
679 report->GetAsDictionary()->AddStringItem("description", issue_description);
680 stop_reason_description = issue_description + " detected";
681 report->GetAsDictionary()->AddStringItem("stop_description", stop_reason_description);
682 std::string summary = instance->GenerateSummary(report);
683 report->GetAsDictionary()->AddStringItem("summary", summary);
684 addr_t main_address = instance->GetMainRacyAddress(report);
685 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000686
Kuba Brecka6602e692016-05-24 17:47:23 +0000687 addr_t global_addr = 0;
688 std::string global_name = "";
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000689 std::string location_filename = "";
690 uint32_t location_line = 0;
Kuba Brecka6602e692016-05-24 17:47:23 +0000691 std::string location_description = instance->GetLocationDescription(report, global_addr, global_name, location_filename, location_line);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000692 report->GetAsDictionary()->AddStringItem("location_description", location_description);
Kuba Brecka6602e692016-05-24 17:47:23 +0000693 if (global_addr != 0) {
694 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
695 }
696 if (!global_name.empty()) {
697 report->GetAsDictionary()->AddStringItem("global_name", global_name);
698 }
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000699 if (location_filename != "") {
700 report->GetAsDictionary()->AddStringItem("location_filename", location_filename);
701 report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
702 }
Kuba Brecka6602e692016-05-24 17:47:23 +0000703
704 bool all_addresses_are_same = true;
705 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&all_addresses_are_same, main_address] (StructuredData::Object *o) -> bool {
706 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
707 if (main_address != addr) all_addresses_are_same = false;
708 return true;
709 });
710 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same", all_addresses_are_same);
Kuba Brecka6a831432016-03-23 15:36:22 +0000711 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000712
Kuba Brecka6a831432016-03-23 15:36:22 +0000713 ProcessSP process_sp = instance->GetProcessSP();
714 // Make sure this is the right process
715 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP())
716 {
717 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
718 if (thread_sp)
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000719 thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(*thread_sp, stop_reason_description.c_str(), report));
Kuba Brecka6a831432016-03-23 15:36:22 +0000720
721 StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile());
722 if (stream_sp)
723 {
724 stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n");
725 }
726 return true; // Return true to stop the target
727 }
728 else
729 return false; // Let target run
730}
731
732void
733ThreadSanitizerRuntime::Activate()
734{
735 if (m_is_active)
736 return;
737
738 ProcessSP process_sp = GetProcessSP();
739 if (!process_sp)
740 return;
741
742 ConstString symbol_name ("__tsan_on_report");
743 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode);
744
745 if (symbol == NULL)
746 return;
747
748 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
749 return;
750
751 Target &target = process_sp->GetTarget();
752 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
753
754 if (symbol_address == LLDB_INVALID_ADDRESS)
755 return;
756
757 bool internal = true;
758 bool hardware = false;
759 Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get();
760 breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true);
761 breakpoint->SetBreakpointKind ("thread-sanitizer-report");
762 m_breakpoint_id = breakpoint->GetID();
763
764 StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile());
765 if (stream_sp)
766 {
767 stream_sp->Printf ("ThreadSanitizer debugger support is active.\n");
768 }
769
770 m_is_active = true;
771}
772
773void
774ThreadSanitizerRuntime::Deactivate()
775{
776 if (m_breakpoint_id != LLDB_INVALID_BREAK_ID)
777 {
778 ProcessSP process_sp = GetProcessSP();
779 if (process_sp)
780 {
781 process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id);
782 m_breakpoint_id = LLDB_INVALID_BREAK_ID;
783 }
784 }
785 m_is_active = false;
786}
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000787
788static std::string
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000789GenerateThreadName(const std::string &path, StructuredData::Object *o, StructuredData::ObjectSP main_info) {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000790 std::string result = "additional information";
791
792 if (path == "mops") {
793 int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
794 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
795 bool is_write = o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
796 bool is_atomic = o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
797 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
798
Kuba Brecka6602e692016-05-24 17:47:23 +0000799 std::string addr_string = Sprintf(" at 0x%llx", addr);
800
801 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")->GetBooleanValue()){
802 addr_string = "";
803 }
804
805 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 +0000806 }
807
808 if (path == "threads") {
809 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
Kuba Brecka5c7b3632016-05-01 11:26:06 +0000810 result = Sprintf("Thread %d created", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000811 }
812
813 if (path == "locs") {
814 std::string type = o->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
815 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
816 int fd = o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
817 if (type == "heap") {
Kuba Brecka1a349b82016-05-22 14:56:33 +0000818 result = Sprintf("Heap block allocated by thread %d", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000819 } else if (type == "fd") {
Kuba Brecka1a349b82016-05-22 14:56:33 +0000820 result = Sprintf("File descriptor %d created by thread %t", fd, thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000821 }
822 }
823
824 if (path == "mutexes") {
825 int mutex_id = o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
826
Kuba Brecka5c7b3632016-05-01 11:26:06 +0000827 result = Sprintf("Mutex M%d created", mutex_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000828 }
829
830 if (path == "stacks") {
Kuba Brecka04297512016-05-24 20:35:28 +0000831 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
832 result = Sprintf("Thread %d", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000833 }
834
835 result[0] = toupper(result[0]);
836
837 return result;
838}
839
840static void
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000841AddThreadsForPath(const std::string &path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info)
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000842{
Kuba Brecka6602e692016-05-24 17:47:23 +0000843 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach([process_sp, threads, path, info] (StructuredData::Object *o) -> bool {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000844 std::vector<lldb::addr_t> pcs;
845 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach([&pcs] (StructuredData::Object *pc) -> bool {
846 pcs.push_back(pc->GetAsInteger()->GetValue());
847 return true;
848 });
849
850 if (pcs.size() == 0)
851 return true;
852
Kuba Brecka5b31c422016-04-22 10:40:14 +0000853 StructuredData::ObjectSP thread_id_obj = o->GetObjectForDotSeparatedPath("thread_os_id");
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000854 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
855
856 uint32_t stop_id = 0;
857 bool stop_id_is_valid = false;
858 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid);
859 ThreadSP new_thread_sp(history_thread);
Kuba Brecka6602e692016-05-24 17:47:23 +0000860 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000861
862 // Save this in the Process' ExtendedThreadList so a strong pointer retains the object
863 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
864 threads->AddThread(new_thread_sp);
865
866 return true;
867 });
868}
869
870lldb::ThreadCollectionSP
871ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)
872{
873 ThreadCollectionSP threads;
874 threads.reset(new ThreadCollection());
875
876 if (info->GetObjectForDotSeparatedPath("instrumentation_class")->GetStringValue() != "ThreadSanitizer")
877 return threads;
878
879 ProcessSP process_sp = GetProcessSP();
880
881 AddThreadsForPath("stacks", threads, process_sp, info);
882 AddThreadsForPath("mops", threads, process_sp, info);
883 AddThreadsForPath("locs", threads, process_sp, info);
884 AddThreadsForPath("mutexes", threads, process_sp, info);
885 AddThreadsForPath("threads", threads, process_sp, info);
886
887 return threads;
888}