Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 1 | //===-- ThreadSanitizerRuntime.cpp ------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ThreadSanitizerRuntime.h" |
| 11 | |
| 12 | #include "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" |
| 26 | #include "lldb/Target/InstrumentationRuntimeStopInfo.h" |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 27 | #include "lldb/Target/SectionLoadList.h" |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 28 | #include "lldb/Target/StopInfo.h" |
| 29 | #include "lldb/Target/Target.h" |
| 30 | #include "lldb/Target/Thread.h" |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 31 | #include "Plugins/Process/Utility/HistoryThread.h" |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace lldb; |
| 34 | using namespace lldb_private; |
| 35 | |
| 36 | lldb::InstrumentationRuntimeSP |
| 37 | ThreadSanitizerRuntime::CreateInstance (const lldb::ProcessSP &process_sp) |
| 38 | { |
| 39 | return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp)); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | ThreadSanitizerRuntime::Initialize() |
| 44 | { |
| 45 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 46 | "ThreadSanitizer instrumentation runtime plugin.", |
| 47 | CreateInstance, |
| 48 | GetTypeStatic); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | ThreadSanitizerRuntime::Terminate() |
| 53 | { |
| 54 | PluginManager::UnregisterPlugin (CreateInstance); |
| 55 | } |
| 56 | |
| 57 | lldb_private::ConstString |
| 58 | ThreadSanitizerRuntime::GetPluginNameStatic() |
| 59 | { |
| 60 | return ConstString("ThreadSanitizer"); |
| 61 | } |
| 62 | |
| 63 | lldb::InstrumentationRuntimeType |
| 64 | ThreadSanitizerRuntime::GetTypeStatic() |
| 65 | { |
| 66 | return eInstrumentationRuntimeTypeThreadSanitizer; |
| 67 | } |
| 68 | |
| 69 | ThreadSanitizerRuntime::ThreadSanitizerRuntime(const ProcessSP &process_sp) : |
| 70 | m_is_active(false), |
| 71 | m_runtime_module_wp(), |
| 72 | m_process_wp(), |
| 73 | m_breakpoint_id(0) |
| 74 | { |
| 75 | if (process_sp) |
| 76 | m_process_wp = process_sp; |
| 77 | } |
| 78 | |
| 79 | ThreadSanitizerRuntime::~ThreadSanitizerRuntime() |
| 80 | { |
| 81 | Deactivate(); |
| 82 | } |
| 83 | |
| 84 | static bool |
| 85 | ModuleContainsTSanRuntime(ModuleSP module_sp) |
| 86 | { |
| 87 | static ConstString g_tsan_get_current_report("__tsan_get_current_report"); |
| 88 | const Symbol* symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny); |
| 89 | return symbol != nullptr; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | ThreadSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list) |
| 94 | { |
| 95 | if (IsActive()) |
| 96 | return; |
| 97 | |
| 98 | if (GetRuntimeModuleSP()) { |
| 99 | Activate(); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | module_list.ForEach ([this](const lldb::ModuleSP module_sp) -> bool |
| 104 | { |
| 105 | const FileSpec & file_spec = module_sp->GetFileSpec(); |
| 106 | if (! file_spec) |
| 107 | return true; // Keep iterating through modules |
| 108 | |
| 109 | llvm::StringRef module_basename(file_spec.GetFilename().GetStringRef()); |
| 110 | if (module_sp->IsExecutable() || module_basename.startswith("libclang_rt.tsan_")) |
| 111 | { |
| 112 | if (ModuleContainsTSanRuntime(module_sp)) |
| 113 | { |
| 114 | m_runtime_module_wp = module_sp; |
| 115 | Activate(); |
| 116 | return false; // Stop iterating |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return true; // Keep iterating through modules |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | bool |
| 125 | ThreadSanitizerRuntime::IsActive() |
| 126 | { |
| 127 | return m_is_active; |
| 128 | } |
| 129 | |
| 130 | #define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000 |
| 131 | |
| 132 | const char * |
| 133 | thread_sanitizer_retrieve_report_data_prefix = R"( |
| 134 | extern "C" |
| 135 | { |
| 136 | void *__tsan_get_current_report(); |
| 137 | int __tsan_get_report_data(void *report, const char **description, int *count, |
| 138 | int *stack_count, int *mop_count, int *loc_count, |
| 139 | int *mutex_count, int *thread_count, |
| 140 | int *unique_tid_count, void **sleep_trace, |
| 141 | unsigned long trace_size); |
| 142 | int __tsan_get_report_stack(void *report, unsigned long idx, void **trace, |
| 143 | unsigned long trace_size); |
| 144 | int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr, |
| 145 | int *size, int *write, int *atomic, void **trace, |
| 146 | unsigned long trace_size); |
| 147 | int __tsan_get_report_loc(void *report, unsigned long idx, const char **type, |
| 148 | void **addr, unsigned long *start, unsigned long *size, int *tid, |
| 149 | int *fd, int *suppressable, void **trace, |
| 150 | unsigned long trace_size); |
| 151 | int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr, |
| 152 | int *destroyed, void **trace, unsigned long trace_size); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 153 | int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id, |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 154 | int *running, const char **name, int *parent_tid, |
| 155 | void **trace, unsigned long trace_size); |
| 156 | int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid); |
| 157 | } |
| 158 | |
| 159 | const int REPORT_TRACE_SIZE = 128; |
| 160 | const int REPORT_ARRAY_SIZE = 4; |
| 161 | |
| 162 | struct data { |
| 163 | void *report; |
| 164 | const char *description; |
| 165 | int report_count; |
| 166 | |
| 167 | void *sleep_trace[REPORT_TRACE_SIZE]; |
| 168 | |
| 169 | int stack_count; |
| 170 | struct { |
| 171 | int idx; |
| 172 | void *trace[REPORT_TRACE_SIZE]; |
| 173 | } stacks[REPORT_ARRAY_SIZE]; |
| 174 | |
| 175 | int mop_count; |
| 176 | struct { |
| 177 | int idx; |
| 178 | int tid; |
| 179 | int size; |
| 180 | int write; |
| 181 | int atomic; |
| 182 | void *addr; |
| 183 | void *trace[REPORT_TRACE_SIZE]; |
| 184 | } mops[REPORT_ARRAY_SIZE]; |
| 185 | |
| 186 | int loc_count; |
| 187 | struct { |
| 188 | int idx; |
| 189 | const char *type; |
| 190 | void *addr; |
| 191 | unsigned long start; |
| 192 | unsigned long size; |
| 193 | int tid; |
| 194 | int fd; |
| 195 | int suppressable; |
| 196 | void *trace[REPORT_TRACE_SIZE]; |
| 197 | } locs[REPORT_ARRAY_SIZE]; |
| 198 | |
| 199 | int mutex_count; |
| 200 | struct { |
| 201 | int idx; |
| 202 | unsigned long mutex_id; |
| 203 | void *addr; |
| 204 | int destroyed; |
| 205 | void *trace[REPORT_TRACE_SIZE]; |
| 206 | } mutexes[REPORT_ARRAY_SIZE]; |
| 207 | |
| 208 | int thread_count; |
| 209 | struct { |
| 210 | int idx; |
| 211 | int tid; |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 212 | unsigned long os_id; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 213 | int running; |
| 214 | const char *name; |
| 215 | int parent_tid; |
| 216 | void *trace[REPORT_TRACE_SIZE]; |
| 217 | } threads[REPORT_ARRAY_SIZE]; |
| 218 | |
| 219 | int unique_tid_count; |
| 220 | struct { |
| 221 | int idx; |
| 222 | int tid; |
| 223 | } unique_tids[REPORT_ARRAY_SIZE]; |
| 224 | }; |
| 225 | )"; |
| 226 | |
| 227 | const char * |
| 228 | thread_sanitizer_retrieve_report_data_command = R"( |
| 229 | data t = {0}; |
| 230 | |
| 231 | t.report = __tsan_get_current_report(); |
| 232 | __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); |
| 233 | |
| 234 | if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE; |
| 235 | for (int i = 0; i < t.stack_count; i++) { |
| 236 | t.stacks[i].idx = i; |
| 237 | __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE); |
| 238 | } |
| 239 | |
| 240 | if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE; |
| 241 | for (int i = 0; i < t.mop_count; i++) { |
| 242 | t.mops[i].idx = i; |
| 243 | __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); |
| 244 | } |
| 245 | |
| 246 | if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE; |
| 247 | for (int i = 0; i < t.loc_count; i++) { |
| 248 | t.locs[i].idx = i; |
| 249 | __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); |
| 250 | } |
| 251 | |
| 252 | if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE; |
| 253 | for (int i = 0; i < t.mutex_count; i++) { |
| 254 | t.mutexes[i].idx = i; |
| 255 | __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); |
| 256 | } |
| 257 | |
| 258 | if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE; |
| 259 | for (int i = 0; i < t.thread_count; i++) { |
| 260 | t.threads[i].idx = i; |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 261 | __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 Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Kuba Brecka | 058c302 | 2016-03-30 10:50:24 +0000 | [diff] [blame] | 264 | if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 265 | for (int i = 0; i < t.unique_tid_count; i++) { |
| 266 | t.unique_tids[i].idx = i; |
| 267 | __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid); |
| 268 | } |
| 269 | |
| 270 | t; |
| 271 | )"; |
| 272 | |
| 273 | static StructuredData::Array * |
| 274 | CreateStackTrace(ValueObjectSP o, std::string trace_item_name = ".trace") { |
| 275 | StructuredData::Array *trace = new StructuredData::Array(); |
| 276 | ValueObjectSP trace_value_object = o->GetValueForExpressionPath(trace_item_name.c_str()); |
| 277 | for (int j = 0; j < 8; j++) { |
| 278 | addr_t trace_addr = trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0); |
| 279 | if (trace_addr == 0) |
| 280 | break; |
| 281 | trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(trace_addr))); |
| 282 | } |
| 283 | return trace; |
| 284 | } |
| 285 | |
| 286 | static StructuredData::Array * |
| 287 | ConvertToStructuredArray(ValueObjectSP return_value_sp, std::string items_name, std::string count_name, std::function <void(ValueObjectSP o, StructuredData::Dictionary *dict)> const &callback) |
| 288 | { |
| 289 | StructuredData::Array *array = new StructuredData::Array(); |
| 290 | unsigned int count = return_value_sp->GetValueForExpressionPath(count_name.c_str())->GetValueAsUnsigned(0); |
| 291 | ValueObjectSP objects = return_value_sp->GetValueForExpressionPath(items_name.c_str()); |
Pavel Labath | 1b46a72 | 2016-03-30 09:42:59 +0000 | [diff] [blame] | 292 | for (unsigned int i = 0; i < count; i++) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 293 | ValueObjectSP o = objects->GetChildAtIndex(i, true); |
| 294 | StructuredData::Dictionary *dict = new StructuredData::Dictionary(); |
| 295 | |
| 296 | callback(o, dict); |
| 297 | |
| 298 | array->AddItem(StructuredData::ObjectSP(dict)); |
| 299 | } |
| 300 | return array; |
| 301 | } |
| 302 | |
| 303 | static std::string |
| 304 | RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, std::string expression_path) |
| 305 | { |
| 306 | addr_t ptr = return_value_sp->GetValueForExpressionPath(expression_path.c_str())->GetValueAsUnsigned(0); |
| 307 | std::string str; |
| 308 | Error error; |
| 309 | process_sp->ReadCStringFromMemory(ptr, str, error); |
| 310 | return str; |
| 311 | } |
| 312 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 313 | static void |
| 314 | GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, std::map<uint64_t, user_id_t> &thread_id_map) |
| 315 | { |
| 316 | ConvertToStructuredArray(data, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 317 | uint64_t thread_id = o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0); |
| 318 | uint64_t thread_os_id = o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0); |
| 319 | user_id_t lldb_user_id = 0; |
| 320 | |
| 321 | bool can_update = true; |
| 322 | ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(thread_os_id, can_update); |
| 323 | if (lldb_thread) { |
| 324 | lldb_user_id = lldb_thread->GetIndexID(); |
| 325 | } else { |
| 326 | // 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). |
| 327 | // It will also make sure that no new threads are assigned this Index ID. |
| 328 | lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id); |
| 329 | } |
| 330 | |
| 331 | thread_id_map[thread_id] = lldb_user_id; |
| 332 | }); |
| 333 | } |
| 334 | |
| 335 | static user_id_t Renumber(uint64_t id, std::map<uint64_t, user_id_t> &thread_id_map) { |
| 336 | if (! thread_id_map.count(id)) |
| 337 | return 0; |
| 338 | |
| 339 | return thread_id_map[id]; |
| 340 | } |
| 341 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 342 | StructuredData::ObjectSP |
| 343 | ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) |
| 344 | { |
| 345 | ProcessSP process_sp = GetProcessSP(); |
| 346 | if (!process_sp) |
| 347 | return StructuredData::ObjectSP(); |
| 348 | |
| 349 | ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); |
| 350 | StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); |
| 351 | |
| 352 | if (!frame_sp) |
| 353 | return StructuredData::ObjectSP(); |
| 354 | |
| 355 | EvaluateExpressionOptions options; |
| 356 | options.SetUnwindOnError(true); |
| 357 | options.SetTryAllThreads(true); |
| 358 | options.SetStopOthers(true); |
| 359 | options.SetIgnoreBreakpoints(true); |
| 360 | options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); |
| 361 | options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix); |
| 362 | |
| 363 | ValueObjectSP main_value; |
| 364 | ExecutionContext exe_ctx; |
| 365 | Error eval_error; |
| 366 | frame_sp->CalculateExecutionContext(exe_ctx); |
| 367 | ExpressionResults result = UserExpression::Evaluate (exe_ctx, |
| 368 | options, |
| 369 | thread_sanitizer_retrieve_report_data_command, |
| 370 | "", |
| 371 | main_value, |
| 372 | eval_error); |
| 373 | if (result != eExpressionCompleted) { |
| 374 | process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString()); |
| 375 | return StructuredData::ObjectSP(); |
| 376 | } |
| 377 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 378 | std::map<uint64_t, user_id_t> thread_id_map; |
| 379 | GetRenumberedThreadIds(process_sp, main_value, thread_id_map); |
| 380 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 381 | StructuredData::Dictionary *dict = new StructuredData::Dictionary(); |
| 382 | dict->AddStringItem("instrumentation_class", "ThreadSanitizer"); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 383 | dict->AddStringItem("issue_type", RetrieveString(main_value, process_sp, ".description")); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 384 | dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0)); |
| 385 | dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace"))); |
| 386 | |
| 387 | StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 388 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 389 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 390 | }); |
| 391 | dict->AddItem("stacks", StructuredData::ObjectSP(stacks)); |
| 392 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 393 | StructuredData::Array *mops = ConvertToStructuredArray(main_value, ".mops", ".mop_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 394 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 395 | dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 396 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
| 397 | dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0)); |
| 398 | dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0)); |
| 399 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 400 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 401 | }); |
| 402 | dict->AddItem("mops", StructuredData::ObjectSP(mops)); |
| 403 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 404 | StructuredData::Array *locs = ConvertToStructuredArray(main_value, ".locs", ".loc_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 405 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 406 | dict->AddStringItem("type", RetrieveString(o, process_sp, ".type")); |
| 407 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 408 | dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0)); |
| 409 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 410 | dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 411 | dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0)); |
| 412 | dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0)); |
| 413 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 414 | }); |
| 415 | dict->AddItem("locs", StructuredData::ObjectSP(locs)); |
| 416 | |
| 417 | StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 418 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 419 | dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0)); |
| 420 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 421 | dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0)); |
| 422 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 423 | }); |
| 424 | dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes)); |
| 425 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 426 | StructuredData::Array *threads = ConvertToStructuredArray(main_value, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 427 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 428 | dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
| 429 | dict->AddIntegerItem("thread_os_id", o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 430 | dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0)); |
| 431 | dict->AddStringItem("name", RetrieveString(o, process_sp, ".name")); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 432 | dict->AddIntegerItem("parent_thread_id", Renumber(o->GetValueForExpressionPath(".parent_tid")->GetValueAsUnsigned(0), thread_id_map)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 433 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 434 | }); |
| 435 | dict->AddItem("threads", StructuredData::ObjectSP(threads)); |
| 436 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 437 | StructuredData::Array *unique_tids = ConvertToStructuredArray(main_value, ".unique_tids", ".unique_tid_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 438 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 439 | dict->AddIntegerItem("tid", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 440 | }); |
| 441 | dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids)); |
| 442 | |
| 443 | return StructuredData::ObjectSP(dict); |
| 444 | } |
| 445 | |
| 446 | std::string |
| 447 | ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) |
| 448 | { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 449 | std::string description = report->GetAsDictionary()->GetValueForKey("issue_type")->GetAsString()->GetValue(); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 450 | |
| 451 | if (description == "data-race") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 452 | return "Data race"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 453 | } else if (description == "data-race-vptr") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 454 | return "Data race on C++ virtual pointer"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 455 | } else if (description == "heap-use-after-free") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 456 | return "Use of deallocated memory"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 457 | } else if (description == "heap-use-after-free-vptr") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 458 | return "Use of deallocated C++ virtual pointer"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 459 | } else if (description == "thread-leak") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 460 | return "Thread leak"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 461 | } else if (description == "locked-mutex-destroy") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 462 | return "Destruction of a locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 463 | } else if (description == "mutex-double-lock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 464 | return "Double lock of a mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 465 | } else if (description == "mutex-invalid-access") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 466 | return "Use of an invalid mutex (e.g. uninitialized or destroyed)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 467 | } else if (description == "mutex-bad-unlock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 468 | return "Unlock of an unlocked mutex (or by a wrong thread)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 469 | } else if (description == "mutex-bad-read-lock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 470 | return "Read lock of a write locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 471 | } else if (description == "mutex-bad-read-unlock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 472 | return "Read unlock of a write locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 473 | } else if (description == "signal-unsafe-call") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 474 | return "Signal-unsafe call inside a signal handler"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 475 | } else if (description == "errno-in-signal-handler") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 476 | return "Overwrite of errno in a signal handler"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 477 | } else if (description == "lock-order-inversion") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 478 | return "Lock order inversion (potential deadlock)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // for unknown report codes just show the code |
| 482 | return description; |
| 483 | } |
| 484 | |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 485 | static std::string |
| 486 | Sprintf(const char *format, ...) |
| 487 | { |
| 488 | StreamString s; |
| 489 | va_list args; |
| 490 | va_start (args, format); |
| 491 | s.PrintfVarArg(format, args); |
| 492 | va_end (args); |
| 493 | return s.GetString(); |
| 494 | } |
| 495 | |
| 496 | static std::string |
| 497 | GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) |
| 498 | { |
| 499 | lldb_private::Address so_addr; |
| 500 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 501 | return ""; |
| 502 | |
| 503 | lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); |
| 504 | if (! symbol) |
| 505 | return ""; |
| 506 | |
| 507 | std::string sym_name = symbol->GetName().GetCString(); |
| 508 | return sym_name; |
| 509 | } |
| 510 | |
| 511 | addr_t |
| 512 | ThreadSanitizerRuntime::GetFirstNonInternalFramePc(StructuredData::ObjectSP trace) |
| 513 | { |
| 514 | ProcessSP process_sp = GetProcessSP(); |
| 515 | ModuleSP runtime_module_sp = GetRuntimeModuleSP(); |
| 516 | |
| 517 | addr_t result = 0; |
| 518 | trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, &result] (StructuredData::Object *o) -> bool { |
| 519 | addr_t addr = o->GetIntegerValue(); |
| 520 | lldb_private::Address so_addr; |
| 521 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 522 | return true; |
| 523 | |
| 524 | if (so_addr.GetModule() == runtime_module_sp) |
| 525 | return true; |
| 526 | |
| 527 | result = addr; |
| 528 | return false; |
| 529 | }); |
| 530 | |
| 531 | return result; |
| 532 | } |
| 533 | |
| 534 | std::string |
| 535 | ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) |
| 536 | { |
| 537 | ProcessSP process_sp = GetProcessSP(); |
| 538 | |
| 539 | std::string summary = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue(); |
| 540 | addr_t pc = 0; |
| 541 | if (report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetSize() > 0) |
| 542 | pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); |
| 543 | |
| 544 | if (report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetSize() > 0) |
| 545 | pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); |
| 546 | |
| 547 | if (pc != 0) { |
| 548 | summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc); |
| 549 | } |
| 550 | |
| 551 | if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { |
| 552 | StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); |
| 553 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); |
| 554 | if (addr == 0) |
| 555 | addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); |
| 556 | |
| 557 | if (addr != 0) { |
| 558 | summary = summary + " at " + Sprintf("0x%llx", addr); |
| 559 | } else { |
| 560 | int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); |
| 561 | if (fd != 0) { |
| 562 | summary = summary + " on file descriptor " + Sprintf("%d", fd); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | return summary; |
| 568 | } |
| 569 | |
| 570 | addr_t |
| 571 | ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) |
| 572 | { |
| 573 | addr_t result = (addr_t)-1; |
| 574 | |
| 575 | report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&result] (StructuredData::Object *o) -> bool { |
| 576 | addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); |
| 577 | if (addr < result) result = addr; |
| 578 | return true; |
| 579 | }); |
| 580 | |
| 581 | return (result == (addr_t)-1) ? 0 : result; |
| 582 | } |
| 583 | |
| 584 | std::string |
| 585 | ThreadSanitizerRuntime::GetLocationDescription(StructuredData::ObjectSP report) |
| 586 | { |
| 587 | std::string result = ""; |
| 588 | |
| 589 | ProcessSP process_sp = GetProcessSP(); |
| 590 | |
| 591 | if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { |
| 592 | StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); |
| 593 | std::string type = loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); |
| 594 | if (type == "global") { |
| 595 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); |
| 596 | std::string global_name = GetSymbolNameFromAddress(process_sp, addr); |
| 597 | result = Sprintf("Location is a global '%s'", global_name.c_str()); |
| 598 | } else if (type == "heap") { |
| 599 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); |
| 600 | long size = loc->GetAsDictionary()->GetValueForKey("size")->GetAsInteger()->GetValue(); |
| 601 | result = Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr); |
| 602 | } else if (type == "stack") { |
| 603 | int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); |
| 604 | result = Sprintf("Location is stack of thread %d", tid); |
| 605 | } else if (type == "tls") { |
| 606 | int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); |
| 607 | result = Sprintf("Location is TLS of thread %d", tid); |
| 608 | } else if (type == "fd") { |
| 609 | int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); |
| 610 | result = Sprintf("Location is file descriptor %d", fd); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return result; |
| 615 | } |
| 616 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 617 | bool |
| 618 | ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id) |
| 619 | { |
| 620 | assert (baton && "null baton"); |
| 621 | if (!baton) |
| 622 | return false; |
| 623 | |
| 624 | ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton); |
| 625 | |
| 626 | StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 627 | std::string stop_reason_description; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 628 | if (report) { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 629 | std::string issue_description = instance->FormatDescription(report); |
| 630 | report->GetAsDictionary()->AddStringItem("description", issue_description); |
| 631 | stop_reason_description = issue_description + " detected"; |
| 632 | report->GetAsDictionary()->AddStringItem("stop_description", stop_reason_description); |
| 633 | std::string summary = instance->GenerateSummary(report); |
| 634 | report->GetAsDictionary()->AddStringItem("summary", summary); |
| 635 | addr_t main_address = instance->GetMainRacyAddress(report); |
| 636 | report->GetAsDictionary()->AddIntegerItem("memory_address", main_address); |
| 637 | std::string location_description = instance->GetLocationDescription(report); |
| 638 | report->GetAsDictionary()->AddStringItem("location_description", location_description); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 639 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 640 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 641 | ProcessSP process_sp = instance->GetProcessSP(); |
| 642 | // Make sure this is the right process |
| 643 | if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) |
| 644 | { |
| 645 | ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); |
| 646 | if (thread_sp) |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 647 | thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(*thread_sp, stop_reason_description.c_str(), report)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 648 | |
| 649 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 650 | if (stream_sp) |
| 651 | { |
| 652 | stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n"); |
| 653 | } |
| 654 | return true; // Return true to stop the target |
| 655 | } |
| 656 | else |
| 657 | return false; // Let target run |
| 658 | } |
| 659 | |
| 660 | void |
| 661 | ThreadSanitizerRuntime::Activate() |
| 662 | { |
| 663 | if (m_is_active) |
| 664 | return; |
| 665 | |
| 666 | ProcessSP process_sp = GetProcessSP(); |
| 667 | if (!process_sp) |
| 668 | return; |
| 669 | |
| 670 | ConstString symbol_name ("__tsan_on_report"); |
| 671 | const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode); |
| 672 | |
| 673 | if (symbol == NULL) |
| 674 | return; |
| 675 | |
| 676 | if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) |
| 677 | return; |
| 678 | |
| 679 | Target &target = process_sp->GetTarget(); |
| 680 | addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); |
| 681 | |
| 682 | if (symbol_address == LLDB_INVALID_ADDRESS) |
| 683 | return; |
| 684 | |
| 685 | bool internal = true; |
| 686 | bool hardware = false; |
| 687 | Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get(); |
| 688 | breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true); |
| 689 | breakpoint->SetBreakpointKind ("thread-sanitizer-report"); |
| 690 | m_breakpoint_id = breakpoint->GetID(); |
| 691 | |
| 692 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 693 | if (stream_sp) |
| 694 | { |
| 695 | stream_sp->Printf ("ThreadSanitizer debugger support is active.\n"); |
| 696 | } |
| 697 | |
| 698 | m_is_active = true; |
| 699 | } |
| 700 | |
| 701 | void |
| 702 | ThreadSanitizerRuntime::Deactivate() |
| 703 | { |
| 704 | if (m_breakpoint_id != LLDB_INVALID_BREAK_ID) |
| 705 | { |
| 706 | ProcessSP process_sp = GetProcessSP(); |
| 707 | if (process_sp) |
| 708 | { |
| 709 | process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id); |
| 710 | m_breakpoint_id = LLDB_INVALID_BREAK_ID; |
| 711 | } |
| 712 | } |
| 713 | m_is_active = false; |
| 714 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 715 | |
| 716 | static std::string |
| 717 | GenerateThreadName(std::string path, StructuredData::Object *o) { |
| 718 | std::string result = "additional information"; |
| 719 | |
| 720 | if (path == "mops") { |
| 721 | int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue(); |
| 722 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
| 723 | bool is_write = o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue(); |
| 724 | bool is_atomic = o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue(); |
| 725 | addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); |
| 726 | |
| 727 | result = Sprintf("%s%s of size %d at 0x%llx by thread %d", is_atomic ? "atomic " : "", is_write ? "write" : "read", size, addr, thread_id); |
| 728 | } |
| 729 | |
| 730 | if (path == "threads") { |
| 731 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 732 | result = Sprintf("thread %d created at", thread_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (path == "locs") { |
| 736 | std::string type = o->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); |
| 737 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
| 738 | int fd = o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue(); |
| 739 | if (type == "heap") { |
| 740 | result = Sprintf("Heap block allocated by thread %d at", thread_id); |
| 741 | } else if (type == "fd") { |
| 742 | result = Sprintf("File descriptor %d created by thread %t at", fd, thread_id); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (path == "mutexes") { |
| 747 | int mutex_id = o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue(); |
| 748 | |
| 749 | result = Sprintf("mutex M%d created at", mutex_id); |
| 750 | } |
| 751 | |
| 752 | if (path == "stacks") { |
| 753 | result = "happened at"; |
| 754 | } |
| 755 | |
| 756 | result[0] = toupper(result[0]); |
| 757 | |
| 758 | return result; |
| 759 | } |
| 760 | |
| 761 | static void |
| 762 | AddThreadsForPath(std::string path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info) |
| 763 | { |
| 764 | info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach([process_sp, threads, path] (StructuredData::Object *o) -> bool { |
| 765 | std::vector<lldb::addr_t> pcs; |
| 766 | o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach([&pcs] (StructuredData::Object *pc) -> bool { |
| 767 | pcs.push_back(pc->GetAsInteger()->GetValue()); |
| 768 | return true; |
| 769 | }); |
| 770 | |
| 771 | if (pcs.size() == 0) |
| 772 | return true; |
| 773 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 774 | StructuredData::ObjectSP thread_id_obj = o->GetObjectForDotSeparatedPath("thread_os_id"); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 775 | tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0; |
| 776 | |
| 777 | uint32_t stop_id = 0; |
| 778 | bool stop_id_is_valid = false; |
| 779 | HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid); |
| 780 | ThreadSP new_thread_sp(history_thread); |
| 781 | new_thread_sp->SetName(GenerateThreadName(path, o).c_str()); |
| 782 | |
| 783 | // Save this in the Process' ExtendedThreadList so a strong pointer retains the object |
| 784 | process_sp->GetExtendedThreadList().AddThread(new_thread_sp); |
| 785 | threads->AddThread(new_thread_sp); |
| 786 | |
| 787 | return true; |
| 788 | }); |
| 789 | } |
| 790 | |
| 791 | lldb::ThreadCollectionSP |
| 792 | ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) |
| 793 | { |
| 794 | ThreadCollectionSP threads; |
| 795 | threads.reset(new ThreadCollection()); |
| 796 | |
| 797 | if (info->GetObjectForDotSeparatedPath("instrumentation_class")->GetStringValue() != "ThreadSanitizer") |
| 798 | return threads; |
| 799 | |
| 800 | ProcessSP process_sp = GetProcessSP(); |
| 801 | |
| 802 | AddThreadsForPath("stacks", threads, process_sp, info); |
| 803 | AddThreadsForPath("mops", threads, process_sp, info); |
| 804 | AddThreadsForPath("locs", threads, process_sp, info); |
| 805 | AddThreadsForPath("mutexes", threads, process_sp, info); |
| 806 | AddThreadsForPath("threads", threads, process_sp, info); |
| 807 | |
| 808 | return threads; |
| 809 | } |