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 * |
Vedant Kumar | 728b9ab | 2016-08-01 15:15:49 +0000 | [diff] [blame] | 276 | CreateStackTrace(ValueObjectSP o, const std::string &trace_item_name = ".trace") { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 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 * |
Vedant Kumar | 728b9ab | 2016-08-01 15:15:49 +0000 | [diff] [blame] | 289 | ConvertToStructuredArray(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 Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 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 |
Vedant Kumar | 728b9ab | 2016-08-01 15:15:49 +0000 | [diff] [blame] | 306 | RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, const std::string &expression_path) |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 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) { |
Vedant Kumar | cbba4b2 | 2016-08-01 16:37:37 +0000 | [diff] [blame^] | 338 | auto IT = thread_id_map.find(id); |
| 339 | if (IT == thread_id_map.end()) |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | |
Vedant Kumar | cbba4b2 | 2016-08-01 16:37:37 +0000 | [diff] [blame^] | 342 | return IT->second; |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 345 | StructuredData::ObjectSP |
| 346 | ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) |
| 347 | { |
| 348 | ProcessSP process_sp = GetProcessSP(); |
| 349 | if (!process_sp) |
| 350 | return StructuredData::ObjectSP(); |
| 351 | |
| 352 | ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); |
| 353 | StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); |
| 354 | |
| 355 | if (!frame_sp) |
| 356 | return StructuredData::ObjectSP(); |
| 357 | |
| 358 | EvaluateExpressionOptions options; |
| 359 | options.SetUnwindOnError(true); |
| 360 | options.SetTryAllThreads(true); |
| 361 | options.SetStopOthers(true); |
| 362 | options.SetIgnoreBreakpoints(true); |
| 363 | options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); |
| 364 | options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix); |
Kuba Brecka | c359d5c | 2016-07-06 11:46:20 +0000 | [diff] [blame] | 365 | options.SetAutoApplyFixIts(false); |
| 366 | options.SetLanguage(eLanguageTypeObjC_plus_plus); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 367 | |
| 368 | ValueObjectSP main_value; |
| 369 | ExecutionContext exe_ctx; |
| 370 | Error eval_error; |
| 371 | frame_sp->CalculateExecutionContext(exe_ctx); |
| 372 | ExpressionResults result = UserExpression::Evaluate (exe_ctx, |
| 373 | options, |
| 374 | thread_sanitizer_retrieve_report_data_command, |
| 375 | "", |
| 376 | main_value, |
| 377 | eval_error); |
| 378 | if (result != eExpressionCompleted) { |
| 379 | process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString()); |
| 380 | return StructuredData::ObjectSP(); |
| 381 | } |
| 382 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 383 | std::map<uint64_t, user_id_t> thread_id_map; |
| 384 | GetRenumberedThreadIds(process_sp, main_value, thread_id_map); |
| 385 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 386 | StructuredData::Dictionary *dict = new StructuredData::Dictionary(); |
| 387 | dict->AddStringItem("instrumentation_class", "ThreadSanitizer"); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 388 | dict->AddStringItem("issue_type", RetrieveString(main_value, process_sp, ".description")); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 389 | dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0)); |
| 390 | dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace"))); |
| 391 | |
Kuba Brecka | 0429751 | 2016-05-24 20:35:28 +0000 | [diff] [blame] | 392 | StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [thread_sp] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 393 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 394 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
Kuba Brecka | 0429751 | 2016-05-24 20:35:28 +0000 | [diff] [blame] | 395 | // "stacks" happen on the current thread |
| 396 | dict->AddIntegerItem("thread_id", thread_sp->GetIndexID()); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 397 | }); |
| 398 | dict->AddItem("stacks", StructuredData::ObjectSP(stacks)); |
| 399 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 400 | 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] | 401 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 402 | 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] | 403 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
| 404 | dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0)); |
| 405 | dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0)); |
| 406 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 407 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 408 | }); |
| 409 | dict->AddItem("mops", StructuredData::ObjectSP(mops)); |
| 410 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 411 | 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] | 412 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 413 | dict->AddStringItem("type", RetrieveString(o, process_sp, ".type")); |
| 414 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 415 | dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0)); |
| 416 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 417 | 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] | 418 | dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0)); |
| 419 | dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0)); |
| 420 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 421 | }); |
| 422 | dict->AddItem("locs", StructuredData::ObjectSP(locs)); |
| 423 | |
| 424 | StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 425 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 426 | dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0)); |
| 427 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 428 | dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0)); |
| 429 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 430 | }); |
| 431 | dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes)); |
| 432 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 433 | 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] | 434 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 435 | dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
| 436 | dict->AddIntegerItem("thread_os_id", o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 437 | dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0)); |
| 438 | dict->AddStringItem("name", RetrieveString(o, process_sp, ".name")); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 439 | 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] | 440 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 441 | }); |
| 442 | dict->AddItem("threads", StructuredData::ObjectSP(threads)); |
| 443 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 444 | 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] | 445 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 446 | dict->AddIntegerItem("tid", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 447 | }); |
| 448 | dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids)); |
| 449 | |
| 450 | return StructuredData::ObjectSP(dict); |
| 451 | } |
| 452 | |
| 453 | std::string |
| 454 | ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) |
| 455 | { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 456 | std::string description = report->GetAsDictionary()->GetValueForKey("issue_type")->GetAsString()->GetValue(); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 457 | |
| 458 | if (description == "data-race") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 459 | return "Data race"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 460 | } else if (description == "data-race-vptr") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 461 | return "Data race on C++ virtual pointer"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 462 | } else if (description == "heap-use-after-free") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 463 | return "Use of deallocated memory"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 464 | } else if (description == "heap-use-after-free-vptr") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 465 | return "Use of deallocated C++ virtual pointer"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 466 | } else if (description == "thread-leak") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 467 | return "Thread leak"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 468 | } else if (description == "locked-mutex-destroy") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 469 | return "Destruction of a locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 470 | } else if (description == "mutex-double-lock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 471 | return "Double lock of a mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 472 | } else if (description == "mutex-invalid-access") { |
Kuba Brecka | 52ded80 | 2016-05-22 14:32:45 +0000 | [diff] [blame] | 473 | return "Use of an uninitialized or destroyed mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 474 | } else if (description == "mutex-bad-unlock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 475 | return "Unlock of an unlocked mutex (or by a wrong thread)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 476 | } else if (description == "mutex-bad-read-lock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 477 | return "Read lock of a write locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 478 | } else if (description == "mutex-bad-read-unlock") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 479 | return "Read unlock of a write locked mutex"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 480 | } else if (description == "signal-unsafe-call") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 481 | return "Signal-unsafe call inside a signal handler"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 482 | } else if (description == "errno-in-signal-handler") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 483 | return "Overwrite of errno in a signal handler"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 484 | } else if (description == "lock-order-inversion") { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 485 | return "Lock order inversion (potential deadlock)"; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // for unknown report codes just show the code |
| 489 | return description; |
| 490 | } |
| 491 | |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 492 | static std::string |
| 493 | Sprintf(const char *format, ...) |
| 494 | { |
| 495 | StreamString s; |
| 496 | va_list args; |
| 497 | va_start (args, format); |
| 498 | s.PrintfVarArg(format, args); |
| 499 | va_end (args); |
| 500 | return s.GetString(); |
| 501 | } |
| 502 | |
| 503 | static std::string |
| 504 | GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) |
| 505 | { |
| 506 | lldb_private::Address so_addr; |
| 507 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 508 | return ""; |
| 509 | |
| 510 | lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); |
| 511 | if (! symbol) |
| 512 | return ""; |
| 513 | |
| 514 | std::string sym_name = symbol->GetName().GetCString(); |
| 515 | return sym_name; |
| 516 | } |
| 517 | |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 518 | static void |
| 519 | GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, Declaration &decl) |
| 520 | { |
| 521 | lldb_private::Address so_addr; |
| 522 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 523 | return; |
| 524 | |
| 525 | lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); |
| 526 | if (! symbol) |
| 527 | return; |
| 528 | |
Devin Coughlin | a10ab76 | 2016-06-01 21:32:45 +0000 | [diff] [blame] | 529 | ConstString sym_name = symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled); |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 530 | |
| 531 | ModuleSP module = symbol->CalculateSymbolContextModule(); |
| 532 | if (! module) |
| 533 | return; |
| 534 | |
| 535 | VariableList var_list; |
| 536 | module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list); |
| 537 | if (var_list.GetSize() < 1) |
| 538 | return; |
| 539 | |
| 540 | VariableSP var = var_list.GetVariableAtIndex(0); |
| 541 | decl = var->GetDeclaration(); |
| 542 | } |
| 543 | |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 544 | addr_t |
| 545 | ThreadSanitizerRuntime::GetFirstNonInternalFramePc(StructuredData::ObjectSP trace) |
| 546 | { |
| 547 | ProcessSP process_sp = GetProcessSP(); |
| 548 | ModuleSP runtime_module_sp = GetRuntimeModuleSP(); |
| 549 | |
| 550 | addr_t result = 0; |
| 551 | trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, &result] (StructuredData::Object *o) -> bool { |
| 552 | addr_t addr = o->GetIntegerValue(); |
| 553 | lldb_private::Address so_addr; |
| 554 | if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
| 555 | return true; |
| 556 | |
| 557 | if (so_addr.GetModule() == runtime_module_sp) |
| 558 | return true; |
| 559 | |
| 560 | result = addr; |
| 561 | return false; |
| 562 | }); |
| 563 | |
| 564 | return result; |
| 565 | } |
| 566 | |
| 567 | std::string |
| 568 | ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) |
| 569 | { |
| 570 | ProcessSP process_sp = GetProcessSP(); |
| 571 | |
| 572 | std::string summary = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue(); |
| 573 | addr_t pc = 0; |
| 574 | if (report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetSize() > 0) |
| 575 | pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); |
| 576 | |
| 577 | if (report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetSize() > 0) |
| 578 | pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); |
| 579 | |
| 580 | if (pc != 0) { |
| 581 | summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc); |
| 582 | } |
| 583 | |
| 584 | if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { |
| 585 | StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); |
| 586 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); |
| 587 | if (addr == 0) |
| 588 | addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); |
| 589 | |
| 590 | if (addr != 0) { |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 591 | std::string global_name = GetSymbolNameFromAddress(process_sp, addr); |
| 592 | if (!global_name.empty()) { |
| 593 | summary = summary + " at " + global_name; |
| 594 | } else { |
| 595 | summary = summary + " at " + Sprintf("0x%llx", addr); |
| 596 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 597 | } else { |
| 598 | int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); |
| 599 | if (fd != 0) { |
| 600 | summary = summary + " on file descriptor " + Sprintf("%d", fd); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return summary; |
| 606 | } |
| 607 | |
| 608 | addr_t |
| 609 | ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) |
| 610 | { |
| 611 | addr_t result = (addr_t)-1; |
| 612 | |
| 613 | report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&result] (StructuredData::Object *o) -> bool { |
| 614 | addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); |
| 615 | if (addr < result) result = addr; |
| 616 | return true; |
| 617 | }); |
| 618 | |
| 619 | return (result == (addr_t)-1) ? 0 : result; |
| 620 | } |
| 621 | |
| 622 | std::string |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 623 | ThreadSanitizerRuntime::GetLocationDescription(StructuredData::ObjectSP report, addr_t &global_addr, std::string &global_name, std::string &filename, uint32_t &line) |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 624 | { |
| 625 | std::string result = ""; |
| 626 | |
| 627 | ProcessSP process_sp = GetProcessSP(); |
| 628 | |
| 629 | if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { |
| 630 | StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); |
| 631 | std::string type = loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); |
| 632 | if (type == "global") { |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 633 | global_addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); |
| 634 | global_name = GetSymbolNameFromAddress(process_sp, global_addr); |
| 635 | if (!global_name.empty()) { |
| 636 | result = Sprintf("'%s' is a global variable (0x%llx)", global_name.c_str(), global_addr); |
| 637 | } else { |
| 638 | result = Sprintf("0x%llx is a global variable", global_addr); |
| 639 | } |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 640 | |
| 641 | Declaration decl; |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 642 | GetSymbolDeclarationFromAddress(process_sp, global_addr, decl); |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 643 | if (decl.GetFile()) { |
| 644 | filename = decl.GetFile().GetPath(); |
| 645 | line = decl.GetLine(); |
| 646 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 647 | } else if (type == "heap") { |
| 648 | addr_t addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); |
| 649 | long size = loc->GetAsDictionary()->GetValueForKey("size")->GetAsInteger()->GetValue(); |
| 650 | result = Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr); |
| 651 | } else if (type == "stack") { |
| 652 | int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); |
Kuba Brecka | 1a349b8 | 2016-05-22 14:56:33 +0000 | [diff] [blame] | 653 | result = Sprintf("Location is stack of thread %d", tid); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 654 | } else if (type == "tls") { |
| 655 | int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); |
Kuba Brecka | 1a349b8 | 2016-05-22 14:56:33 +0000 | [diff] [blame] | 656 | result = Sprintf("Location is TLS of thread %d", tid); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 657 | } else if (type == "fd") { |
| 658 | int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); |
| 659 | result = Sprintf("Location is file descriptor %d", fd); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | return result; |
| 664 | } |
| 665 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 666 | bool |
| 667 | ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id) |
| 668 | { |
| 669 | assert (baton && "null baton"); |
| 670 | if (!baton) |
| 671 | return false; |
| 672 | |
| 673 | ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton); |
| 674 | |
| 675 | StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 676 | std::string stop_reason_description; |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 677 | if (report) { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 678 | std::string issue_description = instance->FormatDescription(report); |
| 679 | report->GetAsDictionary()->AddStringItem("description", issue_description); |
| 680 | stop_reason_description = issue_description + " detected"; |
| 681 | report->GetAsDictionary()->AddStringItem("stop_description", stop_reason_description); |
| 682 | std::string summary = instance->GenerateSummary(report); |
| 683 | report->GetAsDictionary()->AddStringItem("summary", summary); |
| 684 | addr_t main_address = instance->GetMainRacyAddress(report); |
| 685 | report->GetAsDictionary()->AddIntegerItem("memory_address", main_address); |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 686 | |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 687 | addr_t global_addr = 0; |
| 688 | std::string global_name = ""; |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 689 | std::string location_filename = ""; |
| 690 | uint32_t location_line = 0; |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 691 | std::string location_description = instance->GetLocationDescription(report, global_addr, global_name, location_filename, location_line); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 692 | report->GetAsDictionary()->AddStringItem("location_description", location_description); |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 693 | if (global_addr != 0) { |
| 694 | report->GetAsDictionary()->AddIntegerItem("global_address", global_addr); |
| 695 | } |
| 696 | if (!global_name.empty()) { |
| 697 | report->GetAsDictionary()->AddStringItem("global_name", global_name); |
| 698 | } |
Kuba Brecka | bcdce3f | 2016-04-28 15:27:10 +0000 | [diff] [blame] | 699 | if (location_filename != "") { |
| 700 | report->GetAsDictionary()->AddStringItem("location_filename", location_filename); |
| 701 | report->GetAsDictionary()->AddIntegerItem("location_line", location_line); |
| 702 | } |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 703 | |
| 704 | bool all_addresses_are_same = true; |
| 705 | report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&all_addresses_are_same, main_address] (StructuredData::Object *o) -> bool { |
| 706 | addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); |
| 707 | if (main_address != addr) all_addresses_are_same = false; |
| 708 | return true; |
| 709 | }); |
| 710 | report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same", all_addresses_are_same); |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 711 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 712 | |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 713 | ProcessSP process_sp = instance->GetProcessSP(); |
| 714 | // Make sure this is the right process |
| 715 | if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) |
| 716 | { |
| 717 | ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); |
| 718 | if (thread_sp) |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 719 | 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] | 720 | |
| 721 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 722 | if (stream_sp) |
| 723 | { |
| 724 | stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n"); |
| 725 | } |
| 726 | return true; // Return true to stop the target |
| 727 | } |
| 728 | else |
| 729 | return false; // Let target run |
| 730 | } |
| 731 | |
| 732 | void |
| 733 | ThreadSanitizerRuntime::Activate() |
| 734 | { |
| 735 | if (m_is_active) |
| 736 | return; |
| 737 | |
| 738 | ProcessSP process_sp = GetProcessSP(); |
| 739 | if (!process_sp) |
| 740 | return; |
| 741 | |
| 742 | ConstString symbol_name ("__tsan_on_report"); |
| 743 | const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode); |
| 744 | |
| 745 | if (symbol == NULL) |
| 746 | return; |
| 747 | |
| 748 | if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) |
| 749 | return; |
| 750 | |
| 751 | Target &target = process_sp->GetTarget(); |
| 752 | addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); |
| 753 | |
| 754 | if (symbol_address == LLDB_INVALID_ADDRESS) |
| 755 | return; |
| 756 | |
| 757 | bool internal = true; |
| 758 | bool hardware = false; |
| 759 | Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get(); |
| 760 | breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true); |
| 761 | breakpoint->SetBreakpointKind ("thread-sanitizer-report"); |
| 762 | m_breakpoint_id = breakpoint->GetID(); |
| 763 | |
| 764 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 765 | if (stream_sp) |
| 766 | { |
| 767 | stream_sp->Printf ("ThreadSanitizer debugger support is active.\n"); |
| 768 | } |
| 769 | |
| 770 | m_is_active = true; |
| 771 | } |
| 772 | |
| 773 | void |
| 774 | ThreadSanitizerRuntime::Deactivate() |
| 775 | { |
| 776 | if (m_breakpoint_id != LLDB_INVALID_BREAK_ID) |
| 777 | { |
| 778 | ProcessSP process_sp = GetProcessSP(); |
| 779 | if (process_sp) |
| 780 | { |
| 781 | process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id); |
| 782 | m_breakpoint_id = LLDB_INVALID_BREAK_ID; |
| 783 | } |
| 784 | } |
| 785 | m_is_active = false; |
| 786 | } |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 787 | |
| 788 | static std::string |
Vedant Kumar | 728b9ab | 2016-08-01 15:15:49 +0000 | [diff] [blame] | 789 | GenerateThreadName(const std::string &path, StructuredData::Object *o, StructuredData::ObjectSP main_info) { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 790 | std::string result = "additional information"; |
| 791 | |
| 792 | if (path == "mops") { |
| 793 | int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue(); |
| 794 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
| 795 | bool is_write = o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue(); |
| 796 | bool is_atomic = o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue(); |
| 797 | addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); |
| 798 | |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 799 | std::string addr_string = Sprintf(" at 0x%llx", addr); |
| 800 | |
| 801 | if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")->GetBooleanValue()){ |
| 802 | addr_string = ""; |
| 803 | } |
| 804 | |
| 805 | result = Sprintf("%s%s of size %d%s by thread %d", is_atomic ? "atomic " : "", is_write ? "write" : "read", size, addr_string.c_str(), thread_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if (path == "threads") { |
| 809 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
Kuba Brecka | 5c7b363 | 2016-05-01 11:26:06 +0000 | [diff] [blame] | 810 | result = Sprintf("Thread %d created", thread_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | if (path == "locs") { |
| 814 | std::string type = o->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); |
| 815 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
| 816 | int fd = o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue(); |
| 817 | if (type == "heap") { |
Kuba Brecka | 1a349b8 | 2016-05-22 14:56:33 +0000 | [diff] [blame] | 818 | result = Sprintf("Heap block allocated by thread %d", thread_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 819 | } else if (type == "fd") { |
Kuba Brecka | 1a349b8 | 2016-05-22 14:56:33 +0000 | [diff] [blame] | 820 | result = Sprintf("File descriptor %d created by thread %t", fd, thread_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | if (path == "mutexes") { |
| 825 | int mutex_id = o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue(); |
| 826 | |
Kuba Brecka | 5c7b363 | 2016-05-01 11:26:06 +0000 | [diff] [blame] | 827 | result = Sprintf("Mutex M%d created", mutex_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (path == "stacks") { |
Kuba Brecka | 0429751 | 2016-05-24 20:35:28 +0000 | [diff] [blame] | 831 | int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); |
| 832 | result = Sprintf("Thread %d", thread_id); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | result[0] = toupper(result[0]); |
| 836 | |
| 837 | return result; |
| 838 | } |
| 839 | |
| 840 | static void |
Vedant Kumar | 728b9ab | 2016-08-01 15:15:49 +0000 | [diff] [blame] | 841 | AddThreadsForPath(const std::string &path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info) |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 842 | { |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 843 | info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach([process_sp, threads, path, info] (StructuredData::Object *o) -> bool { |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 844 | std::vector<lldb::addr_t> pcs; |
| 845 | o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach([&pcs] (StructuredData::Object *pc) -> bool { |
| 846 | pcs.push_back(pc->GetAsInteger()->GetValue()); |
| 847 | return true; |
| 848 | }); |
| 849 | |
| 850 | if (pcs.size() == 0) |
| 851 | return true; |
| 852 | |
Kuba Brecka | 5b31c42 | 2016-04-22 10:40:14 +0000 | [diff] [blame] | 853 | StructuredData::ObjectSP thread_id_obj = o->GetObjectForDotSeparatedPath("thread_os_id"); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 854 | tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0; |
| 855 | |
| 856 | uint32_t stop_id = 0; |
| 857 | bool stop_id_is_valid = false; |
| 858 | HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid); |
| 859 | ThreadSP new_thread_sp(history_thread); |
Kuba Brecka | 6602e69 | 2016-05-24 17:47:23 +0000 | [diff] [blame] | 860 | new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str()); |
Kuba Brecka | 1aad8fb | 2016-04-10 18:57:38 +0000 | [diff] [blame] | 861 | |
| 862 | // Save this in the Process' ExtendedThreadList so a strong pointer retains the object |
| 863 | process_sp->GetExtendedThreadList().AddThread(new_thread_sp); |
| 864 | threads->AddThread(new_thread_sp); |
| 865 | |
| 866 | return true; |
| 867 | }); |
| 868 | } |
| 869 | |
| 870 | lldb::ThreadCollectionSP |
| 871 | ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) |
| 872 | { |
| 873 | ThreadCollectionSP threads; |
| 874 | threads.reset(new ThreadCollection()); |
| 875 | |
| 876 | if (info->GetObjectForDotSeparatedPath("instrumentation_class")->GetStringValue() != "ThreadSanitizer") |
| 877 | return threads; |
| 878 | |
| 879 | ProcessSP process_sp = GetProcessSP(); |
| 880 | |
| 881 | AddThreadsForPath("stacks", threads, process_sp, info); |
| 882 | AddThreadsForPath("mops", threads, process_sp, info); |
| 883 | AddThreadsForPath("locs", threads, process_sp, info); |
| 884 | AddThreadsForPath("mutexes", threads, process_sp, info); |
| 885 | AddThreadsForPath("threads", threads, process_sp, info); |
| 886 | |
| 887 | return threads; |
| 888 | } |