blob: 78224ada34009029a4e2ea9b495039e09478c2c4 [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"
Kuba Brecka6a831432016-03-23 15:36:22 +000015#include "lldb/Core/RegularExpression.h"
16#include "lldb/Core/PluginInterface.h"
17#include "lldb/Core/PluginManager.h"
18#include "lldb/Core/Stream.h"
19#include "lldb/Core/StreamFile.h"
20#include "lldb/Core/ValueObject.h"
21#include "lldb/Expression/UserExpression.h"
22#include "lldb/Interpreter/CommandReturnObject.h"
23#include "lldb/Symbol/Symbol.h"
24#include "lldb/Symbol/SymbolContext.h"
Kuba Breckabcdce3f2016-04-28 15:27:10 +000025#include "lldb/Symbol/Variable.h"
26#include "lldb/Symbol/VariableList.h"
Kuba Brecka6a831432016-03-23 15:36:22 +000027#include "lldb/Target/InstrumentationRuntimeStopInfo.h"
Kuba Brecka1aad8fb2016-04-10 18:57:38 +000028#include "lldb/Target/SectionLoadList.h"
Kuba Brecka6a831432016-03-23 15:36:22 +000029#include "lldb/Target/StopInfo.h"
30#include "lldb/Target/Target.h"
31#include "lldb/Target/Thread.h"
Kuba Brecka1aad8fb2016-04-10 18:57:38 +000032#include "Plugins/Process/Utility/HistoryThread.h"
Kuba Brecka6a831432016-03-23 15:36:22 +000033
34using namespace lldb;
35using namespace lldb_private;
36
37lldb::InstrumentationRuntimeSP
38ThreadSanitizerRuntime::CreateInstance (const lldb::ProcessSP &process_sp)
39{
40 return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp));
41}
42
43void
44ThreadSanitizerRuntime::Initialize()
45{
46 PluginManager::RegisterPlugin (GetPluginNameStatic(),
47 "ThreadSanitizer instrumentation runtime plugin.",
48 CreateInstance,
49 GetTypeStatic);
50}
51
52void
53ThreadSanitizerRuntime::Terminate()
54{
55 PluginManager::UnregisterPlugin (CreateInstance);
56}
57
58lldb_private::ConstString
59ThreadSanitizerRuntime::GetPluginNameStatic()
60{
61 return ConstString("ThreadSanitizer");
62}
63
64lldb::InstrumentationRuntimeType
65ThreadSanitizerRuntime::GetTypeStatic()
66{
67 return eInstrumentationRuntimeTypeThreadSanitizer;
68}
69
Kuba Brecka6a831432016-03-23 15:36:22 +000070ThreadSanitizerRuntime::~ThreadSanitizerRuntime()
71{
72 Deactivate();
73}
74
Kuba Brecka6a831432016-03-23 15:36:22 +000075#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000
76
77const char *
78thread_sanitizer_retrieve_report_data_prefix = R"(
79extern "C"
80{
81 void *__tsan_get_current_report();
82 int __tsan_get_report_data(void *report, const char **description, int *count,
83 int *stack_count, int *mop_count, int *loc_count,
84 int *mutex_count, int *thread_count,
85 int *unique_tid_count, void **sleep_trace,
86 unsigned long trace_size);
87 int __tsan_get_report_stack(void *report, unsigned long idx, void **trace,
88 unsigned long trace_size);
89 int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr,
90 int *size, int *write, int *atomic, void **trace,
91 unsigned long trace_size);
92 int __tsan_get_report_loc(void *report, unsigned long idx, const char **type,
93 void **addr, unsigned long *start, unsigned long *size, int *tid,
94 int *fd, int *suppressable, void **trace,
95 unsigned long trace_size);
96 int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr,
97 int *destroyed, void **trace, unsigned long trace_size);
Kuba Brecka5b31c422016-04-22 10:40:14 +000098 int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id,
Kuba Brecka6a831432016-03-23 15:36:22 +000099 int *running, const char **name, int *parent_tid,
100 void **trace, unsigned long trace_size);
101 int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid);
102}
103
104const int REPORT_TRACE_SIZE = 128;
105const int REPORT_ARRAY_SIZE = 4;
106
107struct data {
108 void *report;
109 const char *description;
110 int report_count;
111
112 void *sleep_trace[REPORT_TRACE_SIZE];
113
114 int stack_count;
115 struct {
116 int idx;
117 void *trace[REPORT_TRACE_SIZE];
118 } stacks[REPORT_ARRAY_SIZE];
119
120 int mop_count;
121 struct {
122 int idx;
123 int tid;
124 int size;
125 int write;
126 int atomic;
127 void *addr;
128 void *trace[REPORT_TRACE_SIZE];
129 } mops[REPORT_ARRAY_SIZE];
130
131 int loc_count;
132 struct {
133 int idx;
134 const char *type;
135 void *addr;
136 unsigned long start;
137 unsigned long size;
138 int tid;
139 int fd;
140 int suppressable;
141 void *trace[REPORT_TRACE_SIZE];
142 } locs[REPORT_ARRAY_SIZE];
143
144 int mutex_count;
145 struct {
146 int idx;
147 unsigned long mutex_id;
148 void *addr;
149 int destroyed;
150 void *trace[REPORT_TRACE_SIZE];
151 } mutexes[REPORT_ARRAY_SIZE];
152
153 int thread_count;
154 struct {
155 int idx;
156 int tid;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000157 unsigned long os_id;
Kuba Brecka6a831432016-03-23 15:36:22 +0000158 int running;
159 const char *name;
160 int parent_tid;
161 void *trace[REPORT_TRACE_SIZE];
162 } threads[REPORT_ARRAY_SIZE];
163
164 int unique_tid_count;
165 struct {
166 int idx;
167 int tid;
168 } unique_tids[REPORT_ARRAY_SIZE];
169};
170)";
171
172const char *
173thread_sanitizer_retrieve_report_data_command = R"(
174data t = {0};
175
176t.report = __tsan_get_current_report();
177__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);
178
179if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
180for (int i = 0; i < t.stack_count; i++) {
181 t.stacks[i].idx = i;
182 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
183}
184
185if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
186for (int i = 0; i < t.mop_count; i++) {
187 t.mops[i].idx = i;
188 __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);
189}
190
191if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
192for (int i = 0; i < t.loc_count; i++) {
193 t.locs[i].idx = i;
194 __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);
195}
196
197if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
198for (int i = 0; i < t.mutex_count; i++) {
199 t.mutexes[i].idx = i;
200 __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);
201}
202
203if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
204for (int i = 0; i < t.thread_count; i++) {
205 t.threads[i].idx = i;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000206 __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 +0000207}
208
Kuba Brecka058c3022016-03-30 10:50:24 +0000209if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
Kuba Brecka6a831432016-03-23 15:36:22 +0000210for (int i = 0; i < t.unique_tid_count; i++) {
211 t.unique_tids[i].idx = i;
212 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
213}
214
215t;
216)";
217
218static StructuredData::Array *
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000219CreateStackTrace(ValueObjectSP o, const std::string &trace_item_name = ".trace") {
Kuba Brecka6a831432016-03-23 15:36:22 +0000220 StructuredData::Array *trace = new StructuredData::Array();
221 ValueObjectSP trace_value_object = o->GetValueForExpressionPath(trace_item_name.c_str());
222 for (int j = 0; j < 8; j++) {
223 addr_t trace_addr = trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
224 if (trace_addr == 0)
225 break;
226 trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
227 }
228 return trace;
229}
230
231static StructuredData::Array *
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000232ConvertToStructuredArray(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 +0000233{
234 StructuredData::Array *array = new StructuredData::Array();
235 unsigned int count = return_value_sp->GetValueForExpressionPath(count_name.c_str())->GetValueAsUnsigned(0);
236 ValueObjectSP objects = return_value_sp->GetValueForExpressionPath(items_name.c_str());
Pavel Labath1b46a722016-03-30 09:42:59 +0000237 for (unsigned int i = 0; i < count; i++) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000238 ValueObjectSP o = objects->GetChildAtIndex(i, true);
239 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
240
241 callback(o, dict);
242
243 array->AddItem(StructuredData::ObjectSP(dict));
244 }
245 return array;
246}
247
248static std::string
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000249RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, const std::string &expression_path)
Kuba Brecka6a831432016-03-23 15:36:22 +0000250{
251 addr_t ptr = return_value_sp->GetValueForExpressionPath(expression_path.c_str())->GetValueAsUnsigned(0);
252 std::string str;
253 Error error;
254 process_sp->ReadCStringFromMemory(ptr, str, error);
255 return str;
256}
257
Kuba Brecka5b31c422016-04-22 10:40:14 +0000258static void
259GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, std::map<uint64_t, user_id_t> &thread_id_map)
260{
261 ConvertToStructuredArray(data, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
262 uint64_t thread_id = o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
263 uint64_t thread_os_id = o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
264 user_id_t lldb_user_id = 0;
265
266 bool can_update = true;
267 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(thread_os_id, can_update);
268 if (lldb_thread) {
269 lldb_user_id = lldb_thread->GetIndexID();
270 } else {
271 // 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).
272 // It will also make sure that no new threads are assigned this Index ID.
273 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
274 }
275
276 thread_id_map[thread_id] = lldb_user_id;
277 });
278}
279
280static user_id_t Renumber(uint64_t id, std::map<uint64_t, user_id_t> &thread_id_map) {
Vedant Kumarcbba4b22016-08-01 16:37:37 +0000281 auto IT = thread_id_map.find(id);
282 if (IT == thread_id_map.end())
Kuba Brecka5b31c422016-04-22 10:40:14 +0000283 return 0;
284
Vedant Kumarcbba4b22016-08-01 16:37:37 +0000285 return IT->second;
Kuba Brecka5b31c422016-04-22 10:40:14 +0000286}
287
Kuba Brecka6a831432016-03-23 15:36:22 +0000288StructuredData::ObjectSP
289ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref)
290{
291 ProcessSP process_sp = GetProcessSP();
292 if (!process_sp)
293 return StructuredData::ObjectSP();
294
295 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
296 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
297
298 if (!frame_sp)
299 return StructuredData::ObjectSP();
300
301 EvaluateExpressionOptions options;
302 options.SetUnwindOnError(true);
303 options.SetTryAllThreads(true);
304 options.SetStopOthers(true);
305 options.SetIgnoreBreakpoints(true);
306 options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC);
307 options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
Kuba Breckac359d5c2016-07-06 11:46:20 +0000308 options.SetAutoApplyFixIts(false);
309 options.SetLanguage(eLanguageTypeObjC_plus_plus);
Kuba Brecka6a831432016-03-23 15:36:22 +0000310
311 ValueObjectSP main_value;
312 ExecutionContext exe_ctx;
313 Error eval_error;
314 frame_sp->CalculateExecutionContext(exe_ctx);
315 ExpressionResults result = UserExpression::Evaluate (exe_ctx,
316 options,
317 thread_sanitizer_retrieve_report_data_command,
318 "",
319 main_value,
320 eval_error);
321 if (result != eExpressionCompleted) {
322 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString());
323 return StructuredData::ObjectSP();
324 }
325
Kuba Brecka5b31c422016-04-22 10:40:14 +0000326 std::map<uint64_t, user_id_t> thread_id_map;
327 GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
328
Kuba Brecka6a831432016-03-23 15:36:22 +0000329 StructuredData::Dictionary *dict = new StructuredData::Dictionary();
330 dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000331 dict->AddStringItem("issue_type", RetrieveString(main_value, process_sp, ".description"));
Kuba Brecka6a831432016-03-23 15:36:22 +0000332 dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0));
333 dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace")));
334
Kuba Brecka04297512016-05-24 20:35:28 +0000335 StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [thread_sp] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000336 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
337 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
Kuba Brecka04297512016-05-24 20:35:28 +0000338 // "stacks" happen on the current thread
339 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
Kuba Brecka6a831432016-03-23 15:36:22 +0000340 });
341 dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
342
Kuba Brecka5b31c422016-04-22 10:40:14 +0000343 StructuredData::Array *mops = ConvertToStructuredArray(main_value, ".mops", ".mop_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) {
Kuba Brecka6a831432016-03-23 15:36:22 +0000344 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000345 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000346 dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
347 dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
348 dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
349 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
350 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
351 });
352 dict->AddItem("mops", StructuredData::ObjectSP(mops));
353
Kuba Brecka5b31c422016-04-22 10:40:14 +0000354 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 +0000355 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
356 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
357 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
358 dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
359 dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000360 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000361 dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
362 dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0));
363 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
364 });
365 dict->AddItem("locs", StructuredData::ObjectSP(locs));
366
367 StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) {
368 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
369 dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
370 dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
371 dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
372 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
373 });
374 dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
375
Kuba Brecka5b31c422016-04-22 10:40:14 +0000376 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 +0000377 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000378 dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
379 dict->AddIntegerItem("thread_os_id", o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
Kuba Brecka6a831432016-03-23 15:36:22 +0000380 dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
381 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000382 dict->AddIntegerItem("parent_thread_id", Renumber(o->GetValueForExpressionPath(".parent_tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000383 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
384 });
385 dict->AddItem("threads", StructuredData::ObjectSP(threads));
386
Kuba Brecka5b31c422016-04-22 10:40:14 +0000387 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 +0000388 dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
Kuba Brecka5b31c422016-04-22 10:40:14 +0000389 dict->AddIntegerItem("tid", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map));
Kuba Brecka6a831432016-03-23 15:36:22 +0000390 });
391 dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
392
393 return StructuredData::ObjectSP(dict);
394}
395
396std::string
397ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report)
398{
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000399 std::string description = report->GetAsDictionary()->GetValueForKey("issue_type")->GetAsString()->GetValue();
Kuba Brecka6a831432016-03-23 15:36:22 +0000400
401 if (description == "data-race") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000402 return "Data race";
Kuba Brecka6a831432016-03-23 15:36:22 +0000403 } else if (description == "data-race-vptr") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000404 return "Data race on C++ virtual pointer";
Kuba Brecka6a831432016-03-23 15:36:22 +0000405 } else if (description == "heap-use-after-free") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000406 return "Use of deallocated memory";
Kuba Brecka6a831432016-03-23 15:36:22 +0000407 } else if (description == "heap-use-after-free-vptr") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000408 return "Use of deallocated C++ virtual pointer";
Kuba Brecka6a831432016-03-23 15:36:22 +0000409 } else if (description == "thread-leak") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000410 return "Thread leak";
Kuba Brecka6a831432016-03-23 15:36:22 +0000411 } else if (description == "locked-mutex-destroy") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000412 return "Destruction of a locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000413 } else if (description == "mutex-double-lock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000414 return "Double lock of a mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000415 } else if (description == "mutex-invalid-access") {
Kuba Brecka52ded802016-05-22 14:32:45 +0000416 return "Use of an uninitialized or destroyed mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000417 } else if (description == "mutex-bad-unlock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000418 return "Unlock of an unlocked mutex (or by a wrong thread)";
Kuba Brecka6a831432016-03-23 15:36:22 +0000419 } else if (description == "mutex-bad-read-lock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000420 return "Read lock of a write locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000421 } else if (description == "mutex-bad-read-unlock") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000422 return "Read unlock of a write locked mutex";
Kuba Brecka6a831432016-03-23 15:36:22 +0000423 } else if (description == "signal-unsafe-call") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000424 return "Signal-unsafe call inside a signal handler";
Kuba Brecka6a831432016-03-23 15:36:22 +0000425 } else if (description == "errno-in-signal-handler") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000426 return "Overwrite of errno in a signal handler";
Kuba Brecka6a831432016-03-23 15:36:22 +0000427 } else if (description == "lock-order-inversion") {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000428 return "Lock order inversion (potential deadlock)";
Kuba Brecka6a831432016-03-23 15:36:22 +0000429 }
430
431 // for unknown report codes just show the code
432 return description;
433}
434
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000435static std::string
436Sprintf(const char *format, ...)
437{
438 StreamString s;
439 va_list args;
440 va_start (args, format);
441 s.PrintfVarArg(format, args);
442 va_end (args);
443 return s.GetString();
444}
445
446static std::string
447GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr)
448{
449 lldb_private::Address so_addr;
450 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
451 return "";
452
453 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
454 if (! symbol)
455 return "";
456
457 std::string sym_name = symbol->GetName().GetCString();
458 return sym_name;
459}
460
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000461static void
462GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, Declaration &decl)
463{
464 lldb_private::Address so_addr;
465 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
466 return;
467
468 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
469 if (! symbol)
470 return;
471
Devin Coughlina10ab762016-06-01 21:32:45 +0000472 ConstString sym_name = symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000473
474 ModuleSP module = symbol->CalculateSymbolContextModule();
475 if (! module)
476 return;
477
478 VariableList var_list;
479 module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list);
480 if (var_list.GetSize() < 1)
481 return;
482
483 VariableSP var = var_list.GetVariableAtIndex(0);
484 decl = var->GetDeclaration();
485}
486
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000487addr_t
488ThreadSanitizerRuntime::GetFirstNonInternalFramePc(StructuredData::ObjectSP trace)
489{
490 ProcessSP process_sp = GetProcessSP();
491 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
492
493 addr_t result = 0;
494 trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, &result] (StructuredData::Object *o) -> bool {
495 addr_t addr = o->GetIntegerValue();
496 lldb_private::Address so_addr;
497 if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
498 return true;
499
500 if (so_addr.GetModule() == runtime_module_sp)
501 return true;
502
503 result = addr;
504 return false;
505 });
506
507 return result;
508}
509
510std::string
511ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report)
512{
513 ProcessSP process_sp = GetProcessSP();
514
515 std::string summary = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue();
516 addr_t pc = 0;
517 if (report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetSize() > 0)
518 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace"));
519
520 if (report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetSize() > 0)
521 pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace"));
522
523 if (pc != 0) {
524 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
525 }
526
527 if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) {
528 StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0);
529 addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue();
530 if (addr == 0)
531 addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue();
532
533 if (addr != 0) {
Kuba Brecka6602e692016-05-24 17:47:23 +0000534 std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
535 if (!global_name.empty()) {
536 summary = summary + " at " + global_name;
537 } else {
538 summary = summary + " at " + Sprintf("0x%llx", addr);
539 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000540 } else {
541 int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue();
542 if (fd != 0) {
543 summary = summary + " on file descriptor " + Sprintf("%d", fd);
544 }
545 }
546 }
547
548 return summary;
549}
550
551addr_t
552ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report)
553{
554 addr_t result = (addr_t)-1;
555
556 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&result] (StructuredData::Object *o) -> bool {
557 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
558 if (addr < result) result = addr;
559 return true;
560 });
561
562 return (result == (addr_t)-1) ? 0 : result;
563}
564
565std::string
Kuba Brecka6602e692016-05-24 17:47:23 +0000566ThreadSanitizerRuntime::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 +0000567{
568 std::string result = "";
569
570 ProcessSP process_sp = GetProcessSP();
571
572 if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) {
573 StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0);
574 std::string type = loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
575 if (type == "global") {
Kuba Brecka6602e692016-05-24 17:47:23 +0000576 global_addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue();
577 global_name = GetSymbolNameFromAddress(process_sp, global_addr);
578 if (!global_name.empty()) {
579 result = Sprintf("'%s' is a global variable (0x%llx)", global_name.c_str(), global_addr);
580 } else {
581 result = Sprintf("0x%llx is a global variable", global_addr);
582 }
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000583
584 Declaration decl;
Kuba Brecka6602e692016-05-24 17:47:23 +0000585 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000586 if (decl.GetFile()) {
587 filename = decl.GetFile().GetPath();
588 line = decl.GetLine();
589 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000590 } else if (type == "heap") {
591 addr_t addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue();
592 long size = loc->GetAsDictionary()->GetValueForKey("size")->GetAsInteger()->GetValue();
593 result = Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
594 } else if (type == "stack") {
595 int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue();
Kuba Brecka1a349b82016-05-22 14:56:33 +0000596 result = Sprintf("Location is stack of thread %d", tid);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000597 } else if (type == "tls") {
598 int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue();
Kuba Brecka1a349b82016-05-22 14:56:33 +0000599 result = Sprintf("Location is TLS of thread %d", tid);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000600 } else if (type == "fd") {
601 int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue();
602 result = Sprintf("Location is file descriptor %d", fd);
603 }
604 }
605
606 return result;
607}
608
Kuba Brecka6a831432016-03-23 15:36:22 +0000609bool
610ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id)
611{
612 assert (baton && "null baton");
613 if (!baton)
614 return false;
615
616 ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton);
617
618 StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000619 std::string stop_reason_description;
Kuba Brecka6a831432016-03-23 15:36:22 +0000620 if (report) {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000621 std::string issue_description = instance->FormatDescription(report);
622 report->GetAsDictionary()->AddStringItem("description", issue_description);
623 stop_reason_description = issue_description + " detected";
624 report->GetAsDictionary()->AddStringItem("stop_description", stop_reason_description);
625 std::string summary = instance->GenerateSummary(report);
626 report->GetAsDictionary()->AddStringItem("summary", summary);
627 addr_t main_address = instance->GetMainRacyAddress(report);
628 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000629
Kuba Brecka6602e692016-05-24 17:47:23 +0000630 addr_t global_addr = 0;
631 std::string global_name = "";
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000632 std::string location_filename = "";
633 uint32_t location_line = 0;
Kuba Brecka6602e692016-05-24 17:47:23 +0000634 std::string location_description = instance->GetLocationDescription(report, global_addr, global_name, location_filename, location_line);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000635 report->GetAsDictionary()->AddStringItem("location_description", location_description);
Kuba Brecka6602e692016-05-24 17:47:23 +0000636 if (global_addr != 0) {
637 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
638 }
639 if (!global_name.empty()) {
640 report->GetAsDictionary()->AddStringItem("global_name", global_name);
641 }
Kuba Breckabcdce3f2016-04-28 15:27:10 +0000642 if (location_filename != "") {
643 report->GetAsDictionary()->AddStringItem("location_filename", location_filename);
644 report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
645 }
Kuba Brecka6602e692016-05-24 17:47:23 +0000646
647 bool all_addresses_are_same = true;
648 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&all_addresses_are_same, main_address] (StructuredData::Object *o) -> bool {
649 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
650 if (main_address != addr) all_addresses_are_same = false;
651 return true;
652 });
653 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same", all_addresses_are_same);
Kuba Brecka6a831432016-03-23 15:36:22 +0000654 }
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000655
Kuba Brecka6a831432016-03-23 15:36:22 +0000656 ProcessSP process_sp = instance->GetProcessSP();
657 // Make sure this is the right process
658 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP())
659 {
660 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
661 if (thread_sp)
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000662 thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(*thread_sp, stop_reason_description.c_str(), report));
Kuba Brecka6a831432016-03-23 15:36:22 +0000663
664 StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile());
665 if (stream_sp)
666 {
667 stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n");
668 }
669 return true; // Return true to stop the target
670 }
671 else
672 return false; // Let target run
673}
674
Vedant Kumar1c23c142016-08-11 17:28:37 +0000675const RegularExpression &
676ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() {
677 static RegularExpression regex("libclang_rt.tsan_");
678 return regex;
679}
680
681bool
682ThreadSanitizerRuntime::CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)
683{
684 static ConstString g_tsan_get_current_report("__tsan_get_current_report");
685 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny);
686 return symbol != nullptr;
687}
688
Kuba Brecka6a831432016-03-23 15:36:22 +0000689void
690ThreadSanitizerRuntime::Activate()
691{
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000692 if (IsActive())
Kuba Brecka6a831432016-03-23 15:36:22 +0000693 return;
694
695 ProcessSP process_sp = GetProcessSP();
696 if (!process_sp)
697 return;
698
699 ConstString symbol_name ("__tsan_on_report");
700 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode);
701
702 if (symbol == NULL)
703 return;
704
705 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
706 return;
707
708 Target &target = process_sp->GetTarget();
709 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
710
711 if (symbol_address == LLDB_INVALID_ADDRESS)
712 return;
713
714 bool internal = true;
715 bool hardware = false;
716 Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get();
717 breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true);
718 breakpoint->SetBreakpointKind ("thread-sanitizer-report");
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000719 SetBreakpointID(breakpoint->GetID());
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 debugger support is active.\n");
725 }
726
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000727 SetActive(true);
Kuba Brecka6a831432016-03-23 15:36:22 +0000728}
729
730void
731ThreadSanitizerRuntime::Deactivate()
732{
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000733 if (GetBreakpointID() != LLDB_INVALID_BREAK_ID)
Kuba Brecka6a831432016-03-23 15:36:22 +0000734 {
735 ProcessSP process_sp = GetProcessSP();
736 if (process_sp)
737 {
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000738 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
739 SetBreakpointID(LLDB_INVALID_BREAK_ID);
Kuba Brecka6a831432016-03-23 15:36:22 +0000740 }
741 }
Vedant Kumara4fa2e22016-08-11 17:28:33 +0000742 SetActive(false);
Kuba Brecka6a831432016-03-23 15:36:22 +0000743}
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000744static std::string
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000745GenerateThreadName(const std::string &path, StructuredData::Object *o, StructuredData::ObjectSP main_info) {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000746 std::string result = "additional information";
747
748 if (path == "mops") {
749 int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
750 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
751 bool is_write = o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
752 bool is_atomic = o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
753 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
754
Kuba Brecka6602e692016-05-24 17:47:23 +0000755 std::string addr_string = Sprintf(" at 0x%llx", addr);
756
757 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")->GetBooleanValue()){
758 addr_string = "";
759 }
760
761 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 +0000762 }
763
764 if (path == "threads") {
765 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
Kuba Brecka5c7b3632016-05-01 11:26:06 +0000766 result = Sprintf("Thread %d created", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000767 }
768
769 if (path == "locs") {
770 std::string type = o->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
771 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
772 int fd = o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
773 if (type == "heap") {
Kuba Brecka1a349b82016-05-22 14:56:33 +0000774 result = Sprintf("Heap block allocated by thread %d", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000775 } else if (type == "fd") {
Kuba Brecka1a349b82016-05-22 14:56:33 +0000776 result = Sprintf("File descriptor %d created by thread %t", fd, thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000777 }
778 }
779
780 if (path == "mutexes") {
781 int mutex_id = o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
782
Kuba Brecka5c7b3632016-05-01 11:26:06 +0000783 result = Sprintf("Mutex M%d created", mutex_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000784 }
785
786 if (path == "stacks") {
Kuba Brecka04297512016-05-24 20:35:28 +0000787 int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
788 result = Sprintf("Thread %d", thread_id);
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000789 }
790
791 result[0] = toupper(result[0]);
792
793 return result;
794}
795
796static void
Vedant Kumar728b9ab2016-08-01 15:15:49 +0000797AddThreadsForPath(const std::string &path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info)
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000798{
Kuba Brecka6602e692016-05-24 17:47:23 +0000799 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach([process_sp, threads, path, info] (StructuredData::Object *o) -> bool {
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000800 std::vector<lldb::addr_t> pcs;
801 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach([&pcs] (StructuredData::Object *pc) -> bool {
802 pcs.push_back(pc->GetAsInteger()->GetValue());
803 return true;
804 });
805
806 if (pcs.size() == 0)
807 return true;
808
Kuba Brecka5b31c422016-04-22 10:40:14 +0000809 StructuredData::ObjectSP thread_id_obj = o->GetObjectForDotSeparatedPath("thread_os_id");
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000810 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
811
812 uint32_t stop_id = 0;
813 bool stop_id_is_valid = false;
814 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid);
815 ThreadSP new_thread_sp(history_thread);
Kuba Brecka6602e692016-05-24 17:47:23 +0000816 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
Kuba Brecka1aad8fb2016-04-10 18:57:38 +0000817
818 // Save this in the Process' ExtendedThreadList so a strong pointer retains the object
819 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
820 threads->AddThread(new_thread_sp);
821
822 return true;
823 });
824}
825
826lldb::ThreadCollectionSP
827ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)
828{
829 ThreadCollectionSP threads;
830 threads.reset(new ThreadCollection());
831
832 if (info->GetObjectForDotSeparatedPath("instrumentation_class")->GetStringValue() != "ThreadSanitizer")
833 return threads;
834
835 ProcessSP process_sp = GetProcessSP();
836
837 AddThreadsForPath("stacks", threads, process_sp, info);
838 AddThreadsForPath("mops", threads, process_sp, info);
839 AddThreadsForPath("locs", threads, process_sp, info);
840 AddThreadsForPath("mutexes", threads, process_sp, info);
841 AddThreadsForPath("threads", threads, process_sp, info);
842
843 return threads;
844}