Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 1 | //===-- ThreadSanitizerRuntime.cpp ------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ThreadSanitizerRuntime.h" |
| 11 | |
| 12 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 13 | #include "lldb/Core/Debugger.h" |
| 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/ModuleList.h" |
| 16 | #include "lldb/Core/RegularExpression.h" |
| 17 | #include "lldb/Core/PluginInterface.h" |
| 18 | #include "lldb/Core/PluginManager.h" |
| 19 | #include "lldb/Core/Stream.h" |
| 20 | #include "lldb/Core/StreamFile.h" |
| 21 | #include "lldb/Core/ValueObject.h" |
| 22 | #include "lldb/Expression/UserExpression.h" |
| 23 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 24 | #include "lldb/Symbol/Symbol.h" |
| 25 | #include "lldb/Symbol/SymbolContext.h" |
| 26 | #include "lldb/Target/InstrumentationRuntimeStopInfo.h" |
| 27 | #include "lldb/Target/StopInfo.h" |
| 28 | #include "lldb/Target/Target.h" |
| 29 | #include "lldb/Target/Thread.h" |
| 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| 34 | lldb::InstrumentationRuntimeSP |
| 35 | ThreadSanitizerRuntime::CreateInstance (const lldb::ProcessSP &process_sp) |
| 36 | { |
| 37 | return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp)); |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | ThreadSanitizerRuntime::Initialize() |
| 42 | { |
| 43 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 44 | "ThreadSanitizer instrumentation runtime plugin.", |
| 45 | CreateInstance, |
| 46 | GetTypeStatic); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | ThreadSanitizerRuntime::Terminate() |
| 51 | { |
| 52 | PluginManager::UnregisterPlugin (CreateInstance); |
| 53 | } |
| 54 | |
| 55 | lldb_private::ConstString |
| 56 | ThreadSanitizerRuntime::GetPluginNameStatic() |
| 57 | { |
| 58 | return ConstString("ThreadSanitizer"); |
| 59 | } |
| 60 | |
| 61 | lldb::InstrumentationRuntimeType |
| 62 | ThreadSanitizerRuntime::GetTypeStatic() |
| 63 | { |
| 64 | return eInstrumentationRuntimeTypeThreadSanitizer; |
| 65 | } |
| 66 | |
| 67 | ThreadSanitizerRuntime::ThreadSanitizerRuntime(const ProcessSP &process_sp) : |
| 68 | m_is_active(false), |
| 69 | m_runtime_module_wp(), |
| 70 | m_process_wp(), |
| 71 | m_breakpoint_id(0) |
| 72 | { |
| 73 | if (process_sp) |
| 74 | m_process_wp = process_sp; |
| 75 | } |
| 76 | |
| 77 | ThreadSanitizerRuntime::~ThreadSanitizerRuntime() |
| 78 | { |
| 79 | Deactivate(); |
| 80 | } |
| 81 | |
| 82 | static bool |
| 83 | ModuleContainsTSanRuntime(ModuleSP module_sp) |
| 84 | { |
| 85 | static ConstString g_tsan_get_current_report("__tsan_get_current_report"); |
| 86 | const Symbol* symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny); |
| 87 | return symbol != nullptr; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | ThreadSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list) |
| 92 | { |
| 93 | if (IsActive()) |
| 94 | return; |
| 95 | |
| 96 | if (GetRuntimeModuleSP()) { |
| 97 | Activate(); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | module_list.ForEach ([this](const lldb::ModuleSP module_sp) -> bool |
| 102 | { |
| 103 | const FileSpec & file_spec = module_sp->GetFileSpec(); |
| 104 | if (! file_spec) |
| 105 | return true; // Keep iterating through modules |
| 106 | |
| 107 | llvm::StringRef module_basename(file_spec.GetFilename().GetStringRef()); |
| 108 | if (module_sp->IsExecutable() || module_basename.startswith("libclang_rt.tsan_")) |
| 109 | { |
| 110 | if (ModuleContainsTSanRuntime(module_sp)) |
| 111 | { |
| 112 | m_runtime_module_wp = module_sp; |
| 113 | Activate(); |
| 114 | return false; // Stop iterating |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return true; // Keep iterating through modules |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | bool |
| 123 | ThreadSanitizerRuntime::IsActive() |
| 124 | { |
| 125 | return m_is_active; |
| 126 | } |
| 127 | |
| 128 | #define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000 |
| 129 | |
| 130 | const char * |
| 131 | thread_sanitizer_retrieve_report_data_prefix = R"( |
| 132 | extern "C" |
| 133 | { |
| 134 | void *__tsan_get_current_report(); |
| 135 | int __tsan_get_report_data(void *report, const char **description, int *count, |
| 136 | int *stack_count, int *mop_count, int *loc_count, |
| 137 | int *mutex_count, int *thread_count, |
| 138 | int *unique_tid_count, void **sleep_trace, |
| 139 | unsigned long trace_size); |
| 140 | int __tsan_get_report_stack(void *report, unsigned long idx, void **trace, |
| 141 | unsigned long trace_size); |
| 142 | int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr, |
| 143 | int *size, int *write, int *atomic, void **trace, |
| 144 | unsigned long trace_size); |
| 145 | int __tsan_get_report_loc(void *report, unsigned long idx, const char **type, |
| 146 | void **addr, unsigned long *start, unsigned long *size, int *tid, |
| 147 | int *fd, int *suppressable, void **trace, |
| 148 | unsigned long trace_size); |
| 149 | int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr, |
| 150 | int *destroyed, void **trace, unsigned long trace_size); |
| 151 | int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *pid, |
| 152 | int *running, const char **name, int *parent_tid, |
| 153 | void **trace, unsigned long trace_size); |
| 154 | int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid); |
| 155 | } |
| 156 | |
| 157 | const int REPORT_TRACE_SIZE = 128; |
| 158 | const int REPORT_ARRAY_SIZE = 4; |
| 159 | |
| 160 | struct data { |
| 161 | void *report; |
| 162 | const char *description; |
| 163 | int report_count; |
| 164 | |
| 165 | void *sleep_trace[REPORT_TRACE_SIZE]; |
| 166 | |
| 167 | int stack_count; |
| 168 | struct { |
| 169 | int idx; |
| 170 | void *trace[REPORT_TRACE_SIZE]; |
| 171 | } stacks[REPORT_ARRAY_SIZE]; |
| 172 | |
| 173 | int mop_count; |
| 174 | struct { |
| 175 | int idx; |
| 176 | int tid; |
| 177 | int size; |
| 178 | int write; |
| 179 | int atomic; |
| 180 | void *addr; |
| 181 | void *trace[REPORT_TRACE_SIZE]; |
| 182 | } mops[REPORT_ARRAY_SIZE]; |
| 183 | |
| 184 | int loc_count; |
| 185 | struct { |
| 186 | int idx; |
| 187 | const char *type; |
| 188 | void *addr; |
| 189 | unsigned long start; |
| 190 | unsigned long size; |
| 191 | int tid; |
| 192 | int fd; |
| 193 | int suppressable; |
| 194 | void *trace[REPORT_TRACE_SIZE]; |
| 195 | } locs[REPORT_ARRAY_SIZE]; |
| 196 | |
| 197 | int mutex_count; |
| 198 | struct { |
| 199 | int idx; |
| 200 | unsigned long mutex_id; |
| 201 | void *addr; |
| 202 | int destroyed; |
| 203 | void *trace[REPORT_TRACE_SIZE]; |
| 204 | } mutexes[REPORT_ARRAY_SIZE]; |
| 205 | |
| 206 | int thread_count; |
| 207 | struct { |
| 208 | int idx; |
| 209 | int tid; |
| 210 | unsigned long pid; |
| 211 | int running; |
| 212 | const char *name; |
| 213 | int parent_tid; |
| 214 | void *trace[REPORT_TRACE_SIZE]; |
| 215 | } threads[REPORT_ARRAY_SIZE]; |
| 216 | |
| 217 | int unique_tid_count; |
| 218 | struct { |
| 219 | int idx; |
| 220 | int tid; |
| 221 | } unique_tids[REPORT_ARRAY_SIZE]; |
| 222 | }; |
| 223 | )"; |
| 224 | |
| 225 | const char * |
| 226 | thread_sanitizer_retrieve_report_data_command = R"( |
| 227 | data t = {0}; |
| 228 | |
| 229 | t.report = __tsan_get_current_report(); |
| 230 | __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); |
| 231 | |
| 232 | if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE; |
| 233 | for (int i = 0; i < t.stack_count; i++) { |
| 234 | t.stacks[i].idx = i; |
| 235 | __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE); |
| 236 | } |
| 237 | |
| 238 | if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE; |
| 239 | for (int i = 0; i < t.mop_count; i++) { |
| 240 | t.mops[i].idx = i; |
| 241 | __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); |
| 242 | } |
| 243 | |
| 244 | if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE; |
| 245 | for (int i = 0; i < t.loc_count; i++) { |
| 246 | t.locs[i].idx = i; |
| 247 | __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); |
| 248 | } |
| 249 | |
| 250 | if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE; |
| 251 | for (int i = 0; i < t.mutex_count; i++) { |
| 252 | t.mutexes[i].idx = i; |
| 253 | __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); |
| 254 | } |
| 255 | |
| 256 | if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE; |
| 257 | for (int i = 0; i < t.thread_count; i++) { |
| 258 | t.threads[i].idx = i; |
| 259 | __tsan_get_report_thread(t.report, i, &t.threads[i].tid, &t.threads[i].pid, &t.threads[i].running, &t.threads[i].name, &t.threads[i].parent_tid, t.threads[i].trace, REPORT_TRACE_SIZE); |
| 260 | } |
| 261 | |
Kuba Brecka | 058c302 | 2016-03-30 10:50:24 +0000 | [diff] [blame^] | 262 | 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] | 263 | for (int i = 0; i < t.unique_tid_count; i++) { |
| 264 | t.unique_tids[i].idx = i; |
| 265 | __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid); |
| 266 | } |
| 267 | |
| 268 | t; |
| 269 | )"; |
| 270 | |
| 271 | static StructuredData::Array * |
| 272 | CreateStackTrace(ValueObjectSP o, std::string trace_item_name = ".trace") { |
| 273 | StructuredData::Array *trace = new StructuredData::Array(); |
| 274 | ValueObjectSP trace_value_object = o->GetValueForExpressionPath(trace_item_name.c_str()); |
| 275 | for (int j = 0; j < 8; j++) { |
| 276 | addr_t trace_addr = trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0); |
| 277 | if (trace_addr == 0) |
| 278 | break; |
| 279 | trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(trace_addr))); |
| 280 | } |
| 281 | return trace; |
| 282 | } |
| 283 | |
| 284 | static StructuredData::Array * |
| 285 | ConvertToStructuredArray(ValueObjectSP return_value_sp, std::string items_name, std::string count_name, std::function <void(ValueObjectSP o, StructuredData::Dictionary *dict)> const &callback) |
| 286 | { |
| 287 | StructuredData::Array *array = new StructuredData::Array(); |
| 288 | unsigned int count = return_value_sp->GetValueForExpressionPath(count_name.c_str())->GetValueAsUnsigned(0); |
| 289 | ValueObjectSP objects = return_value_sp->GetValueForExpressionPath(items_name.c_str()); |
Pavel Labath | 1b46a72 | 2016-03-30 09:42:59 +0000 | [diff] [blame] | 290 | for (unsigned int i = 0; i < count; i++) { |
Kuba Brecka | 6a83143 | 2016-03-23 15:36:22 +0000 | [diff] [blame] | 291 | ValueObjectSP o = objects->GetChildAtIndex(i, true); |
| 292 | StructuredData::Dictionary *dict = new StructuredData::Dictionary(); |
| 293 | |
| 294 | callback(o, dict); |
| 295 | |
| 296 | array->AddItem(StructuredData::ObjectSP(dict)); |
| 297 | } |
| 298 | return array; |
| 299 | } |
| 300 | |
| 301 | static std::string |
| 302 | RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, std::string expression_path) |
| 303 | { |
| 304 | addr_t ptr = return_value_sp->GetValueForExpressionPath(expression_path.c_str())->GetValueAsUnsigned(0); |
| 305 | std::string str; |
| 306 | Error error; |
| 307 | process_sp->ReadCStringFromMemory(ptr, str, error); |
| 308 | return str; |
| 309 | } |
| 310 | |
| 311 | StructuredData::ObjectSP |
| 312 | ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) |
| 313 | { |
| 314 | ProcessSP process_sp = GetProcessSP(); |
| 315 | if (!process_sp) |
| 316 | return StructuredData::ObjectSP(); |
| 317 | |
| 318 | ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); |
| 319 | StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); |
| 320 | |
| 321 | if (!frame_sp) |
| 322 | return StructuredData::ObjectSP(); |
| 323 | |
| 324 | EvaluateExpressionOptions options; |
| 325 | options.SetUnwindOnError(true); |
| 326 | options.SetTryAllThreads(true); |
| 327 | options.SetStopOthers(true); |
| 328 | options.SetIgnoreBreakpoints(true); |
| 329 | options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); |
| 330 | options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix); |
| 331 | |
| 332 | ValueObjectSP main_value; |
| 333 | ExecutionContext exe_ctx; |
| 334 | Error eval_error; |
| 335 | frame_sp->CalculateExecutionContext(exe_ctx); |
| 336 | ExpressionResults result = UserExpression::Evaluate (exe_ctx, |
| 337 | options, |
| 338 | thread_sanitizer_retrieve_report_data_command, |
| 339 | "", |
| 340 | main_value, |
| 341 | eval_error); |
| 342 | if (result != eExpressionCompleted) { |
| 343 | process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString()); |
| 344 | return StructuredData::ObjectSP(); |
| 345 | } |
| 346 | |
| 347 | StructuredData::Dictionary *dict = new StructuredData::Dictionary(); |
| 348 | dict->AddStringItem("instrumentation_class", "ThreadSanitizer"); |
| 349 | dict->AddStringItem("description", RetrieveString(main_value, process_sp, ".description")); |
| 350 | dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0)); |
| 351 | dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace"))); |
| 352 | |
| 353 | StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 354 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 355 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 356 | }); |
| 357 | dict->AddItem("stacks", StructuredData::ObjectSP(stacks)); |
| 358 | |
| 359 | StructuredData::Array *mops = ConvertToStructuredArray(main_value, ".mops", ".mop_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 360 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 361 | dict->AddIntegerItem("thread_id", o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0)); |
| 362 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
| 363 | dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0)); |
| 364 | dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0)); |
| 365 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 366 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 367 | }); |
| 368 | dict->AddItem("mops", StructuredData::ObjectSP(mops)); |
| 369 | |
| 370 | StructuredData::Array *locs = ConvertToStructuredArray(main_value, ".locs", ".loc_count", [process_sp] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 371 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 372 | dict->AddStringItem("type", RetrieveString(o, process_sp, ".type")); |
| 373 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 374 | dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0)); |
| 375 | dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); |
| 376 | dict->AddIntegerItem("thread_id", o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0)); |
| 377 | dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0)); |
| 378 | dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0)); |
| 379 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 380 | }); |
| 381 | dict->AddItem("locs", StructuredData::ObjectSP(locs)); |
| 382 | |
| 383 | StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 384 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 385 | dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0)); |
| 386 | dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); |
| 387 | dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0)); |
| 388 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 389 | }); |
| 390 | dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes)); |
| 391 | |
| 392 | StructuredData::Array *threads = ConvertToStructuredArray(main_value, ".threads", ".thread_count", [process_sp] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 393 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 394 | dict->AddIntegerItem("thread_id", o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0)); |
| 395 | dict->AddIntegerItem("process_id", o->GetValueForExpressionPath(".pid")->GetValueAsUnsigned(0)); |
| 396 | dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0)); |
| 397 | dict->AddStringItem("name", RetrieveString(o, process_sp, ".name")); |
| 398 | dict->AddIntegerItem("parent_thread_id", o->GetValueForExpressionPath(".parent_tid")->GetValueAsUnsigned(0)); |
| 399 | dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); |
| 400 | }); |
| 401 | dict->AddItem("threads", StructuredData::ObjectSP(threads)); |
| 402 | |
| 403 | StructuredData::Array *unique_tids = ConvertToStructuredArray(main_value, ".unique_tids", ".unique_tid_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { |
| 404 | dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); |
| 405 | dict->AddIntegerItem("tid", o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0)); |
| 406 | }); |
| 407 | dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids)); |
| 408 | |
| 409 | return StructuredData::ObjectSP(dict); |
| 410 | } |
| 411 | |
| 412 | std::string |
| 413 | ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) |
| 414 | { |
| 415 | std::string description = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue(); |
| 416 | |
| 417 | if (description == "data-race") { |
| 418 | return "Data race detected"; |
| 419 | } else if (description == "data-race-vptr") { |
| 420 | return "Data race on C++ virtual pointer detected"; |
| 421 | } else if (description == "heap-use-after-free") { |
| 422 | return "Use of deallocated memory detected"; |
| 423 | } else if (description == "heap-use-after-free-vptr") { |
| 424 | return "Use of deallocated C++ virtual pointer detected"; |
| 425 | } else if (description == "thread-leak") { |
| 426 | return "Thread leak detected"; |
| 427 | } else if (description == "locked-mutex-destroy") { |
| 428 | return "Destruction of a locked mutex detected"; |
| 429 | } else if (description == "mutex-double-lock") { |
| 430 | return "Double lock of a mutex detected"; |
| 431 | } else if (description == "mutex-invalid-access") { |
| 432 | return "Use of an invalid mutex (e.g. uninitialized or destroyed) detected"; |
| 433 | } else if (description == "mutex-bad-unlock") { |
| 434 | return "Unlock of an unlocked mutex (or by a wrong thread) detected"; |
| 435 | } else if (description == "mutex-bad-read-lock") { |
| 436 | return "Read lock of a write locked mutex detected"; |
| 437 | } else if (description == "mutex-bad-read-unlock") { |
| 438 | return "Read unlock of a write locked mutex detected"; |
| 439 | } else if (description == "signal-unsafe-call") { |
| 440 | return "Signal-unsafe call inside a signal handler detected"; |
| 441 | } else if (description == "errno-in-signal-handler") { |
| 442 | return "Overwrite of errno in a signal handler detected"; |
| 443 | } else if (description == "lock-order-inversion") { |
| 444 | return "Lock order inversion (potential deadlock) detected"; |
| 445 | } |
| 446 | |
| 447 | // for unknown report codes just show the code |
| 448 | return description; |
| 449 | } |
| 450 | |
| 451 | bool |
| 452 | ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id) |
| 453 | { |
| 454 | assert (baton && "null baton"); |
| 455 | if (!baton) |
| 456 | return false; |
| 457 | |
| 458 | ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton); |
| 459 | |
| 460 | StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref); |
| 461 | std::string description; |
| 462 | if (report) { |
| 463 | description = instance->FormatDescription(report); |
| 464 | } |
| 465 | ProcessSP process_sp = instance->GetProcessSP(); |
| 466 | // Make sure this is the right process |
| 467 | if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) |
| 468 | { |
| 469 | ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); |
| 470 | if (thread_sp) |
| 471 | thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(*thread_sp, description.c_str(), report)); |
| 472 | |
| 473 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 474 | if (stream_sp) |
| 475 | { |
| 476 | stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n"); |
| 477 | } |
| 478 | return true; // Return true to stop the target |
| 479 | } |
| 480 | else |
| 481 | return false; // Let target run |
| 482 | } |
| 483 | |
| 484 | void |
| 485 | ThreadSanitizerRuntime::Activate() |
| 486 | { |
| 487 | if (m_is_active) |
| 488 | return; |
| 489 | |
| 490 | ProcessSP process_sp = GetProcessSP(); |
| 491 | if (!process_sp) |
| 492 | return; |
| 493 | |
| 494 | ConstString symbol_name ("__tsan_on_report"); |
| 495 | const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode); |
| 496 | |
| 497 | if (symbol == NULL) |
| 498 | return; |
| 499 | |
| 500 | if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) |
| 501 | return; |
| 502 | |
| 503 | Target &target = process_sp->GetTarget(); |
| 504 | addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); |
| 505 | |
| 506 | if (symbol_address == LLDB_INVALID_ADDRESS) |
| 507 | return; |
| 508 | |
| 509 | bool internal = true; |
| 510 | bool hardware = false; |
| 511 | Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get(); |
| 512 | breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true); |
| 513 | breakpoint->SetBreakpointKind ("thread-sanitizer-report"); |
| 514 | m_breakpoint_id = breakpoint->GetID(); |
| 515 | |
| 516 | StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); |
| 517 | if (stream_sp) |
| 518 | { |
| 519 | stream_sp->Printf ("ThreadSanitizer debugger support is active.\n"); |
| 520 | } |
| 521 | |
| 522 | m_is_active = true; |
| 523 | } |
| 524 | |
| 525 | void |
| 526 | ThreadSanitizerRuntime::Deactivate() |
| 527 | { |
| 528 | if (m_breakpoint_id != LLDB_INVALID_BREAK_ID) |
| 529 | { |
| 530 | ProcessSP process_sp = GetProcessSP(); |
| 531 | if (process_sp) |
| 532 | { |
| 533 | process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id); |
| 534 | m_breakpoint_id = LLDB_INVALID_BREAK_ID; |
| 535 | } |
| 536 | } |
| 537 | m_is_active = false; |
| 538 | } |