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