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" |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/Variable.h" |
| 27 | #include "lldb/Symbol/VariableList.h" |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 28 | #include "lldb/Target/InstrumentationRuntimeStopInfo.h" |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 29 | #include "lldb/Target/SectionLoadList.h" |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 30 | #include "lldb/Target/StopInfo.h" |
| 31 | #include "lldb/Target/Target.h" |
| 32 | #include "lldb/Target/Thread.h" |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 33 | #include "Plugins/Process/Utility/HistoryThread.h" |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
| 38 | lldb::InstrumentationRuntimeSP |
| 39 | ThreadSanitizerRuntime::CreateInstance (const lldb::ProcessSP &process_sp) |
| 40 | { |
| 41 | return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp)); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | ThreadSanitizerRuntime::Initialize() |
| 46 | { |
| 47 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 48 | "ThreadSanitizer instrumentation runtime plugin.", |
| 49 | CreateInstance, |
| 50 | GetTypeStatic); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | ThreadSanitizerRuntime::Terminate() |
| 55 | { |
| 56 | PluginManager::UnregisterPlugin (CreateInstance); |
| 57 | } |
| 58 | |
| 59 | lldb_private::ConstString |
| 60 | ThreadSanitizerRuntime::GetPluginNameStatic() |
| 61 | { |
| 62 | return ConstString("ThreadSanitizer"); |
| 63 | } |
| 64 | |
| 65 | lldb::InstrumentationRuntimeType |
| 66 | ThreadSanitizerRuntime::GetTypeStatic() |
| 67 | { |
| 68 | return eInstrumentationRuntimeTypeThreadSanitizer; |
| 69 | } |
| 70 | |
| 71 | ThreadSanitizerRuntime::ThreadSanitizerRuntime(const ProcessSP &process_sp) : |
| 72 | m_is_active(false), |
| 73 | m_runtime_module_wp(), |
| 74 | m_process_wp(), |
| 75 | m_breakpoint_id(0) |
| 76 | { |
| 77 | if (process_sp) |
| 78 | m_process_wp = process_sp; |
| 79 | } |
| 80 | |
| 81 | ThreadSanitizerRuntime::~ThreadSanitizerRuntime() |
| 82 | { |
| 83 | Deactivate(); |
| 84 | } |
| 85 | |
| 86 | static bool |
| 87 | ModuleContainsTSanRuntime(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 | |
| 94 | void |
| 95 | ThreadSanitizerRuntime::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 | |
| 126 | bool |
| 127 | ThreadSanitizerRuntime::IsActive() |
| 128 | { |
| 129 | return m_is_active; |
| 130 | } |
| 131 | |
| 132 | #define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000 |
| 133 | |
| 134 | const char * |
| 135 | thread_sanitizer_retrieve_report_data_prefix = R"( |
| 136 | extern "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 Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 155 | 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] | 156 | 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 | |
| 161 | const int REPORT_TRACE_SIZE = 128; |
| 162 | const int REPORT_ARRAY_SIZE = 4; |
| 163 | |
| 164 | struct 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 Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 214 | unsigned long os_id; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 215 | 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 | |
| 229 | const char * |
| 230 | thread_sanitizer_retrieve_report_data_command = R"( |
| 231 | data t = {0}; |
| 232 | |
| 233 | t.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 | |
| 236 | if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE; |
| 237 | for (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 | |
| 242 | if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE; |
| 243 | for (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 | |
| 248 | if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE; |
| 249 | for (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 | |
| 254 | if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE; |
| 255 | for (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 | |
| 260 | if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE; |
| 261 | for (int i = 0; i < t.thread_count; i++) { |
| 262 | t.threads[i].idx = i; |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 263 | __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] | 264 | } |
| 265 | |
Kuba Brecka | 058c302 | 2016-03-30 10:50:24 +0000 | [diff] [blame] | 266 | 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] | 267 | for (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 | |
| 272 | t; |
| 273 | )"; |
| 274 | |
| 275 | static StructuredData::Array * |
| 276 | CreateStackTrace(ValueObjectSP o, std::string trace_item_name = ".trace") { |
| 277 | 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 | |
| 288 | static StructuredData::Array * |
| 289 | ConvertToStructuredArray(ValueObjectSP return_value_sp, std::string items_name, std::string count_name, std::function <void(ValueObjectSP o, StructuredData::Dictionary *dict)> const &callback) |
| 290 | { |
| 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 Labath | 1b46a72 | 2016-03-30 09:42:59 +0000 | [diff] [blame] | 294 | for (unsigned int i = 0; i < count; i++) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 295 | 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 | |
| 305 | static std::string |
| 306 | RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, std::string expression_path) |
| 307 | { |
| 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 Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 315 | static void |
| 316 | GetRenumberedThreadIds(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 | |
| 337 | static user_id_t Renumber(uint64_t id, std::map<uint64_t, user_id_t> &thread_id_map) { |
| 338 | if (! thread_id_map.count(id)) |
| 339 | return 0; |
| 340 | |
| 341 | return thread_id_map[id]; |
| 342 | } |
| 343 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 344 | StructuredData::ObjectSP |
| 345 | ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) |
| 346 | { |
| 347 | ProcessSP process_sp = GetProcessSP(); |
| 348 | if (!process_sp) |
| 349 | return StructuredData::ObjectSP(); |
| 350 | |
| 351 | ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); |
| 352 | StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); |
| 353 | |
| 354 | if (!frame_sp) |
| 355 | return StructuredData::ObjectSP(); |
| 356 | |
| 357 | EvaluateExpressionOptions options; |
| 358 | options.SetUnwindOnError(true); |
| 359 | options.SetTryAllThreads(true); |
| 360 | options.SetStopOthers(true); |
| 361 | options.SetIgnoreBreakpoints(true); |
| 362 | options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); |
| 363 | options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix); |
| 364 | |
| 365 | ValueObjectSP main_value; |
| 366 | ExecutionContext exe_ctx; |
| 367 | Error eval_error; |
| 368 | frame_sp->CalculateExecutionContext(exe_ctx); |
| 369 | ExpressionResults result = UserExpression::Evaluate (exe_ctx, |
| 370 | options, |
| 371 | thread_sanitizer_retrieve_report_data_command, |
| 372 | "", |
| 373 | main_value, |
| 374 | eval_error); |
| 375 | if (result != eExpressionCompleted) { |
| 376 | process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString()); |
| 377 | return StructuredData::ObjectSP(); |
| 378 | } |
| 379 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 380 | std::map<uint64_t, user_id_t> thread_id_map; |
| 381 | GetRenumberedThreadIds(process_sp, main_value, thread_id_map); |
| 382 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 383 | StructuredData::Dictionary *dict = new StructuredData::Dictionary(); |
| 384 | dict->AddStringItem("instrumentation_class", "ThreadSanitizer"); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 385 | dict->AddStringItem("issue_type", RetrieveString(main_value, process_sp, ".description")); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 386 | dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0)); |
| 387 | dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace"))); |
| 388 | |
| 389 | StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 390 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 391 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 392 | }); |
| 393 | dict->AddItem("stacks", StructuredData::ObjectSP(stacks)); |
| 394 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 395 | 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] | 396 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 397 | 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] | 398 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
| 399 | dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0)); |
| 400 | dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0)); |
| 401 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 402 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 403 | }); |
| 404 | dict->AddItem("mops", StructuredData::ObjectSP(mops)); |
| 405 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 406 | 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] | 407 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 408 | dict->AddStringItem("type", RetrieveString(o, process_sp, ".type")); |
| 409 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 410 | dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0)); |
| 411 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 412 | 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] | 413 | dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0)); |
| 414 | dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0)); |
| 415 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 416 | }); |
| 417 | dict->AddItem("locs", StructuredData::ObjectSP(locs)); |
| 418 | |
| 419 | StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 420 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 421 | dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0)); |
| 422 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 423 | dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0)); |
| 424 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 425 | }); |
| 426 | dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes)); |
| 427 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 428 | 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] | 429 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 430 | dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
| 431 | dict->AddIntegerItem("thread_os_id", o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 432 | dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0)); |
| 433 | dict->AddStringItem("name", RetrieveString(o, process_sp, ".name")); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 434 | 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] | 435 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 436 | }); |
| 437 | dict->AddItem("threads", StructuredData::ObjectSP(threads)); |
| 438 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 439 | 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] | 440 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 441 | dict->AddIntegerItem("tid", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 442 | }); |
| 443 | dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids)); |
| 444 | |
| 445 | return StructuredData::ObjectSP(dict); |
| 446 | } |
| 447 | |
| 448 | std::string |
| 449 | ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) |
| 450 | { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 451 | std::string description = report->GetAsDictionary()->GetValueForKey("issue_type")->GetAsString()->GetValue(); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 452 | |
| 453 | if (description == "data-race") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 454 | return "Data race"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 455 | } else if (description == "data-race-vptr") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 456 | return "Data race on C++ virtual pointer"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 457 | } else if (description == "heap-use-after-free") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 458 | return "Use of deallocated memory"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 459 | } else if (description == "heap-use-after-free-vptr") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 460 | return "Use of deallocated C++ virtual pointer"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 461 | } else if (description == "thread-leak") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 462 | return "Thread leak"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 463 | } else if (description == "locked-mutex-destroy") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 464 | return "Destruction of a locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 465 | } else if (description == "mutex-double-lock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 466 | return "Double lock of a mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 467 | } else if (description == "mutex-invalid-access") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 468 | return "Use of an invalid mutex (e.g. uninitialized or destroyed)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 469 | } else if (description == "mutex-bad-unlock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 470 | return "Unlock of an unlocked mutex (or by a wrong thread)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 471 | } else if (description == "mutex-bad-read-lock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 472 | return "Read lock of a write locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 473 | } else if (description == "mutex-bad-read-unlock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 474 | return "Read unlock of a write locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 475 | } else if (description == "signal-unsafe-call") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 476 | return "Signal-unsafe call inside a signal handler"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 477 | } else if (description == "errno-in-signal-handler") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 478 | return "Overwrite of errno in a signal handler"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 479 | } else if (description == "lock-order-inversion") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 480 | return "Lock order inversion (potential deadlock)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // for unknown report codes just show the code |
| 484 | return description; |
| 485 | } |
| 486 | |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 487 | static std::string |
| 488 | Sprintf(const char *format, ...) |
| 489 | { |
| 490 | StreamString s; |
| 491 | va_list args; |
| 492 | va_start (args, format); |
| 493 | s.PrintfVarArg(format, args); |
| 494 | va_end (args); |
| 495 | return s.GetString(); |
| 496 | } |
| 497 | |
| 498 | static std::string |
| 499 | GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) |
| 500 | { |
| 501 | lldb_private::Address so_addr; |
| 502 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 503 | return ""; |
| 504 | |
| 505 | lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); |
| 506 | if (! symbol) |
| 507 | return ""; |
| 508 | |
| 509 | std::string sym_name = symbol->GetName().GetCString(); |
| 510 | return sym_name; |
| 511 | } |
| 512 | |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 513 | static void |
| 514 | GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, Declaration &decl) |
| 515 | { |
| 516 | lldb_private::Address so_addr; |
| 517 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 518 | return; |
| 519 | |
| 520 | lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); |
| 521 | if (! symbol) |
| 522 | return; |
| 523 | |
| 524 | ConstString sym_name = symbol->GetName(); |
| 525 | |
| 526 | ModuleSP module = symbol->CalculateSymbolContextModule(); |
| 527 | if (! module) |
| 528 | return; |
| 529 | |
| 530 | VariableList var_list; |
| 531 | module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list); |
| 532 | if (var_list.GetSize() < 1) |
| 533 | return; |
| 534 | |
| 535 | VariableSP var = var_list.GetVariableAtIndex(0); |
| 536 | decl = var->GetDeclaration(); |
| 537 | } |
| 538 | |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 539 | addr_t |
| 540 | ThreadSanitizerRuntime::GetFirstNonInternalFramePc(StructuredData::ObjectSP trace) |
| 541 | { |
| 542 | ProcessSP process_sp = GetProcessSP(); |
| 543 | ModuleSP runtime_module_sp = GetRuntimeModuleSP(); |
| 544 | |
| 545 | addr_t result = 0; |
| 546 | trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, &result] (StructuredData::Object *o) -> bool { |
| 547 | addr_t addr = o->GetIntegerValue(); |
| 548 | lldb_private::Address so_addr; |
| 549 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 550 | return true; |
| 551 | |
| 552 | if (so_addr.GetModule() == runtime_module_sp) |
| 553 | return true; |
| 554 | |
| 555 | result = addr; |
| 556 | return false; |
| 557 | }); |
| 558 | |
| 559 | return result; |
| 560 | } |
| 561 | |
| 562 | std::string |
| 563 | ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) |
| 564 | { |
| 565 | ProcessSP process_sp = GetProcessSP(); |
| 566 | |
| 567 | std::string summary = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue(); |
| 568 | addr_t pc = 0; |
| 569 | if (report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetSize() > 0) |
| 570 | pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); |
| 571 | |
| 572 | if (report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetSize() > 0) |
| 573 | pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); |
| 574 | |
| 575 | if (pc != 0) { |
| 576 | summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc); |
| 577 | } |
| 578 | |
| 579 | if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { |
| 580 | StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); |
| 581 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); |
| 582 | if (addr == 0) |
| 583 | addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); |
| 584 | |
| 585 | if (addr != 0) { |
| 586 | summary = summary + " at " + Sprintf("0x%llx", addr); |
| 587 | } else { |
| 588 | int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); |
| 589 | if (fd != 0) { |
| 590 | summary = summary + " on file descriptor " + Sprintf("%d", fd); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return summary; |
| 596 | } |
| 597 | |
| 598 | addr_t |
| 599 | ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) |
| 600 | { |
| 601 | addr_t result = (addr_t)-1; |
| 602 | |
| 603 | report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&result] (StructuredData::Object *o) -> bool { |
| 604 | addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); |
| 605 | if (addr < result) result = addr; |
| 606 | return true; |
| 607 | }); |
| 608 | |
| 609 | return (result == (addr_t)-1) ? 0 : result; |
| 610 | } |
| 611 | |
| 612 | std::string |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 613 | ThreadSanitizerRuntime::GetLocationDescription(StructuredData::ObjectSP report, std::string &filename, uint32_t &line) |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 614 | { |
| 615 | std::string result = ""; |
| 616 | |
| 617 | ProcessSP process_sp = GetProcessSP(); |
| 618 | |
| 619 | if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { |
| 620 | StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); |
| 621 | std::string type = loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); |
| 622 | if (type == "global") { |
| 623 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); |
| 624 | std::string global_name = GetSymbolNameFromAddress(process_sp, addr); |
| 625 | result = Sprintf("Location is a global '%s'", global_name.c_str()); |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 626 | |
| 627 | Declaration decl; |
| 628 | GetSymbolDeclarationFromAddress(process_sp, addr, decl); |
| 629 | if (decl.GetFile()) { |
| 630 | filename = decl.GetFile().GetPath(); |
| 631 | line = decl.GetLine(); |
| 632 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 633 | } else if (type == "heap") { |
| 634 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); |
| 635 | long size = loc->GetAsDictionary()->GetValueForKey("size")->GetAsInteger()->GetValue(); |
| 636 | result = Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr); |
| 637 | } else if (type == "stack") { |
| 638 | int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); |
| 639 | result = Sprintf("Location is stack of thread %d", tid); |
| 640 | } else if (type == "tls") { |
| 641 | int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); |
| 642 | result = Sprintf("Location is TLS of thread %d", tid); |
| 643 | } else if (type == "fd") { |
| 644 | int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); |
| 645 | result = Sprintf("Location is file descriptor %d", fd); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return result; |
| 650 | } |
| 651 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 652 | bool |
| 653 | ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id) |
| 654 | { |
| 655 | assert (baton && "null baton"); |
| 656 | if (!baton) |
| 657 | return false; |
| 658 | |
| 659 | ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton); |
| 660 | |
| 661 | StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 662 | std::string stop_reason_description; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 663 | if (report) { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 664 | std::string issue_description = instance->FormatDescription(report); |
| 665 | report->GetAsDictionary()->AddStringItem("description", issue_description); |
| 666 | stop_reason_description = issue_description + " detected"; |
| 667 | report->GetAsDictionary()->AddStringItem("stop_description", stop_reason_description); |
| 668 | std::string summary = instance->GenerateSummary(report); |
| 669 | report->GetAsDictionary()->AddStringItem("summary", summary); |
| 670 | addr_t main_address = instance->GetMainRacyAddress(report); |
| 671 | report->GetAsDictionary()->AddIntegerItem("memory_address", main_address); |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 672 | |
| 673 | std::string location_filename = ""; |
| 674 | uint32_t location_line = 0; |
| 675 | std::string location_description = instance->GetLocationDescription(report, location_filename, location_line); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 676 | report->GetAsDictionary()->AddStringItem("location_description", location_description); |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 677 | if (location_filename != "") { |
| 678 | report->GetAsDictionary()->AddStringItem("location_filename", location_filename); |
| 679 | report->GetAsDictionary()->AddIntegerItem("location_line", location_line); |
| 680 | } |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 681 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 682 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 683 | ProcessSP process_sp = instance->GetProcessSP(); |
| 684 | // Make sure this is the right process |
| 685 | if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) |
| 686 | { |
| 687 | ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); |
| 688 | if (thread_sp) |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 689 | 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] | 690 | |
| 691 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 692 | if (stream_sp) |
| 693 | { |
| 694 | stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n"); |
| 695 | } |
| 696 | return true; // Return true to stop the target |
| 697 | } |
| 698 | else |
| 699 | return false; // Let target run |
| 700 | } |
| 701 | |
| 702 | void |
| 703 | ThreadSanitizerRuntime::Activate() |
| 704 | { |
| 705 | if (m_is_active) |
| 706 | return; |
| 707 | |
| 708 | ProcessSP process_sp = GetProcessSP(); |
| 709 | if (!process_sp) |
| 710 | return; |
| 711 | |
| 712 | ConstString symbol_name ("__tsan_on_report"); |
| 713 | const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode); |
| 714 | |
| 715 | if (symbol == NULL) |
| 716 | return; |
| 717 | |
| 718 | if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) |
| 719 | return; |
| 720 | |
| 721 | Target &target = process_sp->GetTarget(); |
| 722 | addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); |
| 723 | |
| 724 | if (symbol_address == LLDB_INVALID_ADDRESS) |
| 725 | return; |
| 726 | |
| 727 | bool internal = true; |
| 728 | bool hardware = false; |
| 729 | Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get(); |
| 730 | breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true); |
| 731 | breakpoint->SetBreakpointKind ("thread-sanitizer-report"); |
| 732 | m_breakpoint_id = breakpoint->GetID(); |
| 733 | |
| 734 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 735 | if (stream_sp) |
| 736 | { |
| 737 | stream_sp->Printf ("ThreadSanitizer debugger support is active.\n"); |
| 738 | } |
| 739 | |
| 740 | m_is_active = true; |
| 741 | } |
| 742 | |
| 743 | void |
| 744 | ThreadSanitizerRuntime::Deactivate() |
| 745 | { |
| 746 | if (m_breakpoint_id != LLDB_INVALID_BREAK_ID) |
| 747 | { |
| 748 | ProcessSP process_sp = GetProcessSP(); |
| 749 | if (process_sp) |
| 750 | { |
| 751 | process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id); |
| 752 | m_breakpoint_id = LLDB_INVALID_BREAK_ID; |
| 753 | } |
| 754 | } |
| 755 | m_is_active = false; |
| 756 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 757 | |
| 758 | static std::string |
| 759 | GenerateThreadName(std::string path, StructuredData::Object *o) { |
| 760 | std::string result = "additional information"; |
| 761 | |
| 762 | if (path == "mops") { |
| 763 | int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue(); |
| 764 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
| 765 | bool is_write = o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue(); |
| 766 | bool is_atomic = o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue(); |
| 767 | addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); |
| 768 | |
| 769 | result = Sprintf("%s%s of size %d at 0x%llx by thread %d", is_atomic ? "atomic " : "", is_write ? "write" : "read", size, addr, thread_id); |
| 770 | } |
| 771 | |
| 772 | if (path == "threads") { |
| 773 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 774 | result = Sprintf("thread %d created at", thread_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | if (path == "locs") { |
| 778 | std::string type = o->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); |
| 779 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
| 780 | int fd = o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue(); |
| 781 | if (type == "heap") { |
| 782 | result = Sprintf("Heap block allocated by thread %d at", thread_id); |
| 783 | } else if (type == "fd") { |
| 784 | result = Sprintf("File descriptor %d created by thread %t at", fd, thread_id); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | if (path == "mutexes") { |
| 789 | int mutex_id = o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue(); |
| 790 | |
| 791 | result = Sprintf("mutex M%d created at", mutex_id); |
| 792 | } |
| 793 | |
| 794 | if (path == "stacks") { |
| 795 | result = "happened at"; |
| 796 | } |
| 797 | |
| 798 | result[0] = toupper(result[0]); |
| 799 | |
| 800 | return result; |
| 801 | } |
| 802 | |
| 803 | static void |
| 804 | AddThreadsForPath(std::string path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info) |
| 805 | { |
| 806 | info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach([process_sp, threads, path] (StructuredData::Object *o) -> bool { |
| 807 | std::vector<lldb::addr_t> pcs; |
| 808 | o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach([&pcs] (StructuredData::Object *pc) -> bool { |
| 809 | pcs.push_back(pc->GetAsInteger()->GetValue()); |
| 810 | return true; |
| 811 | }); |
| 812 | |
| 813 | if (pcs.size() == 0) |
| 814 | return true; |
| 815 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 816 | StructuredData::ObjectSP thread_id_obj = o->GetObjectForDotSeparatedPath("thread_os_id"); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 817 | tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0; |
| 818 | |
| 819 | uint32_t stop_id = 0; |
| 820 | bool stop_id_is_valid = false; |
| 821 | HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid); |
| 822 | ThreadSP new_thread_sp(history_thread); |
| 823 | new_thread_sp->SetName(GenerateThreadName(path, o).c_str()); |
| 824 | |
| 825 | // Save this in the Process' ExtendedThreadList so a strong pointer retains the object |
| 826 | process_sp->GetExtendedThreadList().AddThread(new_thread_sp); |
| 827 | threads->AddThread(new_thread_sp); |
| 828 | |
| 829 | return true; |
| 830 | }); |
| 831 | } |
| 832 | |
| 833 | lldb::ThreadCollectionSP |
| 834 | ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) |
| 835 | { |
| 836 | ThreadCollectionSP threads; |
| 837 | threads.reset(new ThreadCollection()); |
| 838 | |
| 839 | if (info->GetObjectForDotSeparatedPath("instrumentation_class")->GetStringValue() != "ThreadSanitizer") |
| 840 | return threads; |
| 841 | |
| 842 | ProcessSP process_sp = GetProcessSP(); |
| 843 | |
| 844 | AddThreadsForPath("stacks", threads, process_sp, info); |
| 845 | AddThreadsForPath("mops", threads, process_sp, info); |
| 846 | AddThreadsForPath("locs", threads, process_sp, info); |
| 847 | AddThreadsForPath("mutexes", threads, process_sp, info); |
| 848 | AddThreadsForPath("threads", threads, process_sp, info); |
| 849 | |
| 850 | return threads; |
| 851 | } |