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