diff options
Diffstat (limited to 'lldb/source/Plugins/InstrumentationRuntime')
4 files changed, 1079 insertions, 937 deletions
diff --git a/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp b/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp index 4e28840acc8..7b905878a0e 100644 --- a/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp +++ b/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp @@ -12,9 +12,9 @@ #include "lldb/Breakpoint/StoppointCallbackContext.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" -#include "lldb/Core/RegularExpression.h" #include "lldb/Core/PluginInterface.h" #include "lldb/Core/PluginManager.h" +#include "lldb/Core/RegularExpression.h" #include "lldb/Core/Stream.h" #include "lldb/Core/StreamFile.h" #include "lldb/Core/ValueObject.h" @@ -30,63 +30,47 @@ using namespace lldb; using namespace lldb_private; lldb::InstrumentationRuntimeSP -AddressSanitizerRuntime::CreateInstance (const lldb::ProcessSP &process_sp) -{ - return InstrumentationRuntimeSP(new AddressSanitizerRuntime(process_sp)); +AddressSanitizerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) { + return InstrumentationRuntimeSP(new AddressSanitizerRuntime(process_sp)); } -void -AddressSanitizerRuntime::Initialize() -{ - PluginManager::RegisterPlugin (GetPluginNameStatic(), - "AddressSanitizer instrumentation runtime plugin.", - CreateInstance, - GetTypeStatic); +void AddressSanitizerRuntime::Initialize() { + PluginManager::RegisterPlugin( + GetPluginNameStatic(), "AddressSanitizer instrumentation runtime plugin.", + CreateInstance, GetTypeStatic); } -void -AddressSanitizerRuntime::Terminate() -{ - PluginManager::UnregisterPlugin (CreateInstance); +void AddressSanitizerRuntime::Terminate() { + PluginManager::UnregisterPlugin(CreateInstance); } -lldb_private::ConstString -AddressSanitizerRuntime::GetPluginNameStatic() -{ - return ConstString("AddressSanitizer"); +lldb_private::ConstString AddressSanitizerRuntime::GetPluginNameStatic() { + return ConstString("AddressSanitizer"); } -lldb::InstrumentationRuntimeType -AddressSanitizerRuntime::GetTypeStatic() -{ - return eInstrumentationRuntimeTypeAddressSanitizer; +lldb::InstrumentationRuntimeType AddressSanitizerRuntime::GetTypeStatic() { + return eInstrumentationRuntimeTypeAddressSanitizer; } -AddressSanitizerRuntime::~AddressSanitizerRuntime() -{ - Deactivate(); -} +AddressSanitizerRuntime::~AddressSanitizerRuntime() { Deactivate(); } const RegularExpression & -AddressSanitizerRuntime::GetPatternForRuntimeLibrary() -{ - // FIXME: This shouldn't include the "dylib" suffix. - static RegularExpression regex("libclang_rt.asan_(.*)_dynamic\\.dylib"); - return regex; +AddressSanitizerRuntime::GetPatternForRuntimeLibrary() { + // FIXME: This shouldn't include the "dylib" suffix. + static RegularExpression regex("libclang_rt.asan_(.*)_dynamic\\.dylib"); + return regex; } -bool -AddressSanitizerRuntime::CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) -{ - const Symbol *symbol = - module_sp->FindFirstSymbolWithNameAndType(ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny); +bool AddressSanitizerRuntime::CheckIfRuntimeIsValid( + const lldb::ModuleSP module_sp) { + const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType( + ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny); - return symbol != nullptr; + return symbol != nullptr; } -#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000 -const char * -address_sanitizer_retrieve_report_data_prefix = R"( +#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2 * 1000 * 1000 +const char *address_sanitizer_retrieve_report_data_prefix = R"( extern "C" { int __asan_report_present(); @@ -100,8 +84,7 @@ size_t __asan_get_report_access_size(); } )"; -const char * -address_sanitizer_retrieve_report_data_command = R"( +const char *address_sanitizer_retrieve_report_data_command = R"( struct { int present; int access_type; @@ -124,194 +107,208 @@ t.description = __asan_get_report_description(); t )"; -StructuredData::ObjectSP -AddressSanitizerRuntime::RetrieveReportData() -{ - ProcessSP process_sp = GetProcessSP(); - if (!process_sp) - return StructuredData::ObjectSP(); - - ThreadSP thread_sp = process_sp->GetThreadList().GetExpressionExecutionThread(); - StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); - - if (!frame_sp) - return StructuredData::ObjectSP(); - - EvaluateExpressionOptions options; - options.SetUnwindOnError(true); - options.SetTryAllThreads(true); - options.SetStopOthers(true); - options.SetIgnoreBreakpoints(true); - options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); - options.SetPrefix(address_sanitizer_retrieve_report_data_prefix); - options.SetAutoApplyFixIts(false); - options.SetLanguage(eLanguageTypeObjC_plus_plus); - - ValueObjectSP return_value_sp; - ExecutionContext exe_ctx; - Error eval_error; - frame_sp->CalculateExecutionContext(exe_ctx); - ExpressionResults result = UserExpression::Evaluate (exe_ctx, - options, - address_sanitizer_retrieve_report_data_command, - "", - return_value_sp, - eval_error); - if (result != eExpressionCompleted) { - process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate AddressSanitizer expression:\n%s\n", eval_error.AsCString()); - return StructuredData::ObjectSP(); - } - - int present = return_value_sp->GetValueForExpressionPath(".present")->GetValueAsUnsigned(0); - if (present != 1) - return StructuredData::ObjectSP(); - - addr_t pc = return_value_sp->GetValueForExpressionPath(".pc")->GetValueAsUnsigned(0); - /* commented out because rdar://problem/18533301 - addr_t bp = return_value_sp->GetValueForExpressionPath(".bp")->GetValueAsUnsigned(0); - addr_t sp = return_value_sp->GetValueForExpressionPath(".sp")->GetValueAsUnsigned(0); - */ - addr_t address = return_value_sp->GetValueForExpressionPath(".address")->GetValueAsUnsigned(0); - addr_t access_type = return_value_sp->GetValueForExpressionPath(".access_type")->GetValueAsUnsigned(0); - addr_t access_size = return_value_sp->GetValueForExpressionPath(".access_size")->GetValueAsUnsigned(0); - addr_t description_ptr = return_value_sp->GetValueForExpressionPath(".description")->GetValueAsUnsigned(0); - std::string description; - Error error; - process_sp->ReadCStringFromMemory(description_ptr, description, error); - - StructuredData::Dictionary *dict = new StructuredData::Dictionary(); - dict->AddStringItem("instrumentation_class", "AddressSanitizer"); - dict->AddStringItem("stop_type", "fatal_error"); - dict->AddIntegerItem("pc", pc); - /* commented out because rdar://problem/18533301 - dict->AddIntegerItem("bp", bp); - dict->AddIntegerItem("sp", sp); - */ - dict->AddIntegerItem("address", address); - dict->AddIntegerItem("access_type", access_type); - dict->AddIntegerItem("access_size", access_size); - dict->AddStringItem("description", description); - - return StructuredData::ObjectSP(dict); +StructuredData::ObjectSP AddressSanitizerRuntime::RetrieveReportData() { + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return StructuredData::ObjectSP(); + + ThreadSP thread_sp = + process_sp->GetThreadList().GetExpressionExecutionThread(); + StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); + + if (!frame_sp) + return StructuredData::ObjectSP(); + + EvaluateExpressionOptions options; + options.SetUnwindOnError(true); + options.SetTryAllThreads(true); + options.SetStopOthers(true); + options.SetIgnoreBreakpoints(true); + options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); + options.SetPrefix(address_sanitizer_retrieve_report_data_prefix); + options.SetAutoApplyFixIts(false); + options.SetLanguage(eLanguageTypeObjC_plus_plus); + + ValueObjectSP return_value_sp; + ExecutionContext exe_ctx; + Error eval_error; + frame_sp->CalculateExecutionContext(exe_ctx); + ExpressionResults result = UserExpression::Evaluate( + exe_ctx, options, address_sanitizer_retrieve_report_data_command, "", + return_value_sp, eval_error); + if (result != eExpressionCompleted) { + process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf( + "Warning: Cannot evaluate AddressSanitizer expression:\n%s\n", + eval_error.AsCString()); + return StructuredData::ObjectSP(); + } + + int present = return_value_sp->GetValueForExpressionPath(".present") + ->GetValueAsUnsigned(0); + if (present != 1) + return StructuredData::ObjectSP(); + + addr_t pc = + return_value_sp->GetValueForExpressionPath(".pc")->GetValueAsUnsigned(0); + /* commented out because rdar://problem/18533301 + addr_t bp = + return_value_sp->GetValueForExpressionPath(".bp")->GetValueAsUnsigned(0); + addr_t sp = + return_value_sp->GetValueForExpressionPath(".sp")->GetValueAsUnsigned(0); + */ + addr_t address = return_value_sp->GetValueForExpressionPath(".address") + ->GetValueAsUnsigned(0); + addr_t access_type = + return_value_sp->GetValueForExpressionPath(".access_type") + ->GetValueAsUnsigned(0); + addr_t access_size = + return_value_sp->GetValueForExpressionPath(".access_size") + ->GetValueAsUnsigned(0); + addr_t description_ptr = + return_value_sp->GetValueForExpressionPath(".description") + ->GetValueAsUnsigned(0); + std::string description; + Error error; + process_sp->ReadCStringFromMemory(description_ptr, description, error); + + StructuredData::Dictionary *dict = new StructuredData::Dictionary(); + dict->AddStringItem("instrumentation_class", "AddressSanitizer"); + dict->AddStringItem("stop_type", "fatal_error"); + dict->AddIntegerItem("pc", pc); + /* commented out because rdar://problem/18533301 + dict->AddIntegerItem("bp", bp); + dict->AddIntegerItem("sp", sp); + */ + dict->AddIntegerItem("address", address); + dict->AddIntegerItem("access_type", access_type); + dict->AddIntegerItem("access_size", access_size); + dict->AddStringItem("description", description); + + return StructuredData::ObjectSP(dict); } std::string -AddressSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) -{ - std::string description = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue(); - if (description == "heap-use-after-free") { - return "Use of deallocated memory detected"; - } else if (description == "heap-buffer-overflow") { - return "Heap buffer overflow detected"; - } else if (description == "stack-buffer-underflow") { - return "Stack buffer underflow detected"; - } else if (description == "initialization-order-fiasco") { - return "Initialization order problem detected"; - } else if (description == "stack-buffer-overflow") { - return "Stack buffer overflow detected"; - } else if (description == "stack-use-after-return") { - return "Use of returned stack memory detected"; - } else if (description == "use-after-poison") { - return "Use of poisoned memory detected"; - } else if (description == "container-overflow") { - return "Container overflow detected"; - } else if (description == "stack-use-after-scope") { - return "Use of out-of-scope stack memory detected"; - } else if (description == "global-buffer-overflow") { - return "Global buffer overflow detected"; - } else if (description == "unknown-crash") { - return "Invalid memory access detected"; - } - - // for unknown report codes just show the code - return description; +AddressSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) { + std::string description = report->GetAsDictionary() + ->GetValueForKey("description") + ->GetAsString() + ->GetValue(); + if (description == "heap-use-after-free") { + return "Use of deallocated memory detected"; + } else if (description == "heap-buffer-overflow") { + return "Heap buffer overflow detected"; + } else if (description == "stack-buffer-underflow") { + return "Stack buffer underflow detected"; + } else if (description == "initialization-order-fiasco") { + return "Initialization order problem detected"; + } else if (description == "stack-buffer-overflow") { + return "Stack buffer overflow detected"; + } else if (description == "stack-use-after-return") { + return "Use of returned stack memory detected"; + } else if (description == "use-after-poison") { + return "Use of poisoned memory detected"; + } else if (description == "container-overflow") { + return "Container overflow detected"; + } else if (description == "stack-use-after-scope") { + return "Use of out-of-scope stack memory detected"; + } else if (description == "global-buffer-overflow") { + return "Global buffer overflow detected"; + } else if (description == "unknown-crash") { + return "Invalid memory access detected"; + } + + // for unknown report codes just show the code + return description; } -bool -AddressSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id) -{ - assert (baton && "null baton"); - if (!baton) - return false; +bool AddressSanitizerRuntime::NotifyBreakpointHit( + void *baton, StoppointCallbackContext *context, user_id_t break_id, + user_id_t break_loc_id) { + assert(baton && "null baton"); + if (!baton) + return false; - AddressSanitizerRuntime *const instance = static_cast<AddressSanitizerRuntime*>(baton); + AddressSanitizerRuntime *const instance = + static_cast<AddressSanitizerRuntime *>(baton); - StructuredData::ObjectSP report = instance->RetrieveReportData(); - std::string description; - if (report) { - description = instance->FormatDescription(report); - } - ProcessSP process_sp = instance->GetProcessSP(); - // Make sure this is the right process - if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) - { - ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); - if (thread_sp) - thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(*thread_sp, description.c_str(), report)); - - StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); - if (stream_sp) - { - stream_sp->Printf ("AddressSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n"); - } - return true; // Return true to stop the target + StructuredData::ObjectSP report = instance->RetrieveReportData(); + std::string description; + if (report) { + description = instance->FormatDescription(report); + } + ProcessSP process_sp = instance->GetProcessSP(); + // Make sure this is the right process + if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) { + ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); + if (thread_sp) + thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo:: + CreateStopReasonWithInstrumentationData( + *thread_sp, description.c_str(), report)); + + StreamFileSP stream_sp( + process_sp->GetTarget().GetDebugger().GetOutputFile()); + if (stream_sp) { + stream_sp->Printf("AddressSanitizer report breakpoint hit. Use 'thread " + "info -s' to get extended information about the " + "report.\n"); } - else - return false; // Let target run + return true; // Return true to stop the target + } else + return false; // Let target run } -void -AddressSanitizerRuntime::Activate() -{ - if (IsActive()) - return; +void AddressSanitizerRuntime::Activate() { + if (IsActive()) + return; - ProcessSP process_sp = GetProcessSP(); - if (!process_sp) - return; - - ConstString symbol_name ("__asan::AsanDie()"); - const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode); - - if (symbol == NULL) - return; - - if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) - return; - - Target &target = process_sp->GetTarget(); - addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); - - if (symbol_address == LLDB_INVALID_ADDRESS) - return; - - bool internal = true; - bool hardware = false; - Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get(); - breakpoint->SetCallback (AddressSanitizerRuntime::NotifyBreakpointHit, this, true); - breakpoint->SetBreakpointKind ("address-sanitizer-report"); - SetBreakpointID(breakpoint->GetID()); - - StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); - if (stream_sp) - { - stream_sp->Printf ("AddressSanitizer debugger support is active. Memory error breakpoint has been installed and you can now use the 'memory history' command.\n"); - } + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return; + + ConstString symbol_name("__asan::AsanDie()"); + const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType( + symbol_name, eSymbolTypeCode); + + if (symbol == NULL) + return; + + if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) + return; + + Target &target = process_sp->GetTarget(); + addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); - SetActive(true); + if (symbol_address == LLDB_INVALID_ADDRESS) + return; + + bool internal = true; + bool hardware = false; + Breakpoint *breakpoint = + process_sp->GetTarget() + .CreateBreakpoint(symbol_address, internal, hardware) + .get(); + breakpoint->SetCallback(AddressSanitizerRuntime::NotifyBreakpointHit, this, + true); + breakpoint->SetBreakpointKind("address-sanitizer-report"); + SetBreakpointID(breakpoint->GetID()); + + StreamFileSP stream_sp(process_sp->GetTarget().GetDebugger().GetOutputFile()); + if (stream_sp) { + stream_sp->Printf("AddressSanitizer debugger support is active. Memory " + "error breakpoint has been installed and you can now use " + "the 'memory history' command.\n"); + } + + SetActive(true); } -void -AddressSanitizerRuntime::Deactivate() -{ - if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) - { - ProcessSP process_sp = GetProcessSP(); - if (process_sp) - { - process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID()); - SetBreakpointID(LLDB_INVALID_BREAK_ID); - } +void AddressSanitizerRuntime::Deactivate() { + if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) { + ProcessSP process_sp = GetProcessSP(); + if (process_sp) { + process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID()); + SetBreakpointID(LLDB_INVALID_BREAK_ID); } - SetActive(false); + } + SetActive(false); } diff --git a/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h b/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h index 06aa5a0b9aa..9fd21c06f30 100644 --- a/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h +++ b/lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h @@ -14,73 +14,58 @@ // C++ Includes // Other libraries and framework includes // Project includes -#include "lldb/lldb-private.h" +#include "lldb/Core/StructuredData.h" #include "lldb/Target/InstrumentationRuntime.h" #include "lldb/Target/Process.h" -#include "lldb/Core/StructuredData.h" +#include "lldb/lldb-private.h" namespace lldb_private { - -class AddressSanitizerRuntime : public lldb_private::InstrumentationRuntime -{ + +class AddressSanitizerRuntime : public lldb_private::InstrumentationRuntime { public: - ~AddressSanitizerRuntime() override; - - static lldb::InstrumentationRuntimeSP - CreateInstance (const lldb::ProcessSP &process_sp); - - static void - Initialize(); - - static void - Terminate(); - - static lldb_private::ConstString - GetPluginNameStatic(); - - static lldb::InstrumentationRuntimeType - GetTypeStatic(); - - lldb_private::ConstString - GetPluginName() override - { - return GetPluginNameStatic(); - } - - virtual lldb::InstrumentationRuntimeType - GetType() { return GetTypeStatic(); } - - uint32_t - GetPluginVersion() override - { - return 1; - } - + ~AddressSanitizerRuntime() override; + + static lldb::InstrumentationRuntimeSP + CreateInstance(const lldb::ProcessSP &process_sp); + + static void Initialize(); + + static void Terminate(); + + static lldb_private::ConstString GetPluginNameStatic(); + + static lldb::InstrumentationRuntimeType GetTypeStatic(); + + lldb_private::ConstString GetPluginName() override { + return GetPluginNameStatic(); + } + + virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); } + + uint32_t GetPluginVersion() override { return 1; } + private: - AddressSanitizerRuntime(const lldb::ProcessSP &process_sp) : lldb_private::InstrumentationRuntime(process_sp) {} - - const RegularExpression & - GetPatternForRuntimeLibrary() override; - - bool - CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override; - - void - Activate() override; - - void - Deactivate(); - - static bool - NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id); - - StructuredData::ObjectSP - RetrieveReportData(); - - std::string - FormatDescription(StructuredData::ObjectSP report); + AddressSanitizerRuntime(const lldb::ProcessSP &process_sp) + : lldb_private::InstrumentationRuntime(process_sp) {} + + const RegularExpression &GetPatternForRuntimeLibrary() override; + + bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override; + + void Activate() override; + + void Deactivate(); + + static bool NotifyBreakpointHit(void *baton, + StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id); + + StructuredData::ObjectSP RetrieveReportData(); + + std::string FormatDescription(StructuredData::ObjectSP report); }; - + } // namespace lldb_private #endif // liblldb_AddressSanitizerRuntime_h_ diff --git a/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp b/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp index 78224ada340..dfac55a4944 100644 --- a/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp +++ b/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp @@ -9,12 +9,13 @@ #include "ThreadSanitizerRuntime.h" +#include "Plugins/Process/Utility/HistoryThread.h" #include "lldb/Breakpoint/StoppointCallbackContext.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" -#include "lldb/Core/RegularExpression.h" #include "lldb/Core/PluginInterface.h" #include "lldb/Core/PluginManager.h" +#include "lldb/Core/RegularExpression.h" #include "lldb/Core/Stream.h" #include "lldb/Core/StreamFile.h" #include "lldb/Core/ValueObject.h" @@ -29,53 +30,38 @@ #include "lldb/Target/StopInfo.h" #include "lldb/Target/Target.h" #include "lldb/Target/Thread.h" -#include "Plugins/Process/Utility/HistoryThread.h" using namespace lldb; using namespace lldb_private; lldb::InstrumentationRuntimeSP -ThreadSanitizerRuntime::CreateInstance (const lldb::ProcessSP &process_sp) -{ - return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp)); +ThreadSanitizerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) { + return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp)); } -void -ThreadSanitizerRuntime::Initialize() -{ - PluginManager::RegisterPlugin (GetPluginNameStatic(), - "ThreadSanitizer instrumentation runtime plugin.", - CreateInstance, - GetTypeStatic); +void ThreadSanitizerRuntime::Initialize() { + PluginManager::RegisterPlugin( + GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.", + CreateInstance, GetTypeStatic); } -void -ThreadSanitizerRuntime::Terminate() -{ - PluginManager::UnregisterPlugin (CreateInstance); +void ThreadSanitizerRuntime::Terminate() { + PluginManager::UnregisterPlugin(CreateInstance); } -lldb_private::ConstString -ThreadSanitizerRuntime::GetPluginNameStatic() -{ - return ConstString("ThreadSanitizer"); +lldb_private::ConstString ThreadSanitizerRuntime::GetPluginNameStatic() { + return ConstString("ThreadSanitizer"); } -lldb::InstrumentationRuntimeType -ThreadSanitizerRuntime::GetTypeStatic() -{ - return eInstrumentationRuntimeTypeThreadSanitizer; +lldb::InstrumentationRuntimeType ThreadSanitizerRuntime::GetTypeStatic() { + return eInstrumentationRuntimeTypeThreadSanitizer; } -ThreadSanitizerRuntime::~ThreadSanitizerRuntime() -{ - Deactivate(); -} +ThreadSanitizerRuntime::~ThreadSanitizerRuntime() { Deactivate(); } -#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000 +#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2 * 1000 * 1000 -const char * -thread_sanitizer_retrieve_report_data_prefix = R"( +const char *thread_sanitizer_retrieve_report_data_prefix = R"( extern "C" { void *__tsan_get_current_report(); @@ -169,8 +155,7 @@ struct data { }; )"; -const char * -thread_sanitizer_retrieve_report_data_command = R"( +const char *thread_sanitizer_retrieve_report_data_command = R"( data t = {0}; t.report = __tsan_get_current_report(); @@ -216,629 +201,820 @@ t; )"; static StructuredData::Array * -CreateStackTrace(ValueObjectSP o, const std::string &trace_item_name = ".trace") { - StructuredData::Array *trace = new StructuredData::Array(); - ValueObjectSP trace_value_object = o->GetValueForExpressionPath(trace_item_name.c_str()); - for (int j = 0; j < 8; j++) { - addr_t trace_addr = trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0); - if (trace_addr == 0) - break; - trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(trace_addr))); - } - return trace; -} +CreateStackTrace(ValueObjectSP o, + const std::string &trace_item_name = ".trace") { + StructuredData::Array *trace = new StructuredData::Array(); + ValueObjectSP trace_value_object = + o->GetValueForExpressionPath(trace_item_name.c_str()); + for (int j = 0; j < 8; j++) { + addr_t trace_addr = + trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0); + if (trace_addr == 0) + break; + trace->AddItem( + StructuredData::ObjectSP(new StructuredData::Integer(trace_addr))); + } + return trace; +} + +static StructuredData::Array *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) { + StructuredData::Array *array = new StructuredData::Array(); + unsigned int count = + return_value_sp->GetValueForExpressionPath(count_name.c_str()) + ->GetValueAsUnsigned(0); + ValueObjectSP objects = + return_value_sp->GetValueForExpressionPath(items_name.c_str()); + for (unsigned int i = 0; i < count; i++) { + ValueObjectSP o = objects->GetChildAtIndex(i, true); + StructuredData::Dictionary *dict = new StructuredData::Dictionary(); -static StructuredData::Array * -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) -{ - StructuredData::Array *array = new StructuredData::Array(); - unsigned int count = return_value_sp->GetValueForExpressionPath(count_name.c_str())->GetValueAsUnsigned(0); - ValueObjectSP objects = return_value_sp->GetValueForExpressionPath(items_name.c_str()); - for (unsigned int i = 0; i < count; i++) { - ValueObjectSP o = objects->GetChildAtIndex(i, true); - StructuredData::Dictionary *dict = new StructuredData::Dictionary(); - - callback(o, dict); - - array->AddItem(StructuredData::ObjectSP(dict)); - } - return array; + callback(o, dict); + + array->AddItem(StructuredData::ObjectSP(dict)); + } + return array; } -static std::string -RetrieveString(ValueObjectSP return_value_sp, ProcessSP process_sp, const std::string &expression_path) -{ - addr_t ptr = return_value_sp->GetValueForExpressionPath(expression_path.c_str())->GetValueAsUnsigned(0); - std::string str; - Error error; - process_sp->ReadCStringFromMemory(ptr, str, error); - return str; +static std::string RetrieveString(ValueObjectSP return_value_sp, + ProcessSP process_sp, + const std::string &expression_path) { + addr_t ptr = + return_value_sp->GetValueForExpressionPath(expression_path.c_str()) + ->GetValueAsUnsigned(0); + std::string str; + Error error; + process_sp->ReadCStringFromMemory(ptr, str, error); + return str; } static void -GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, std::map<uint64_t, user_id_t> &thread_id_map) -{ - ConvertToStructuredArray(data, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { - uint64_t thread_id = o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0); - uint64_t thread_os_id = o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0); +GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, + std::map<uint64_t, user_id_t> &thread_id_map) { + ConvertToStructuredArray( + data, ".threads", ".thread_count", + [process_sp, &thread_id_map](ValueObjectSP o, + StructuredData::Dictionary *dict) { + uint64_t thread_id = + o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0); + uint64_t thread_os_id = + o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0); user_id_t lldb_user_id = 0; - + bool can_update = true; - ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(thread_os_id, can_update); + ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID( + thread_os_id, can_update); if (lldb_thread) { - lldb_user_id = lldb_thread->GetIndexID(); + lldb_user_id = lldb_thread->GetIndexID(); } else { - // 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). - // It will also make sure that no new threads are assigned this Index ID. - lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id); + // 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). + // It will also make sure that no new threads are assigned this Index + // ID. + lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id); } - + thread_id_map[thread_id] = lldb_user_id; - }); + }); } -static user_id_t Renumber(uint64_t id, std::map<uint64_t, user_id_t> &thread_id_map) { - auto IT = thread_id_map.find(id); - if (IT == thread_id_map.end()) - return 0; - - return IT->second; +static user_id_t Renumber(uint64_t id, + std::map<uint64_t, user_id_t> &thread_id_map) { + auto IT = thread_id_map.find(id); + if (IT == thread_id_map.end()) + return 0; + + return IT->second; } StructuredData::ObjectSP -ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) -{ - ProcessSP process_sp = GetProcessSP(); - if (!process_sp) - return StructuredData::ObjectSP(); - - ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); - StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); - - if (!frame_sp) - return StructuredData::ObjectSP(); - - EvaluateExpressionOptions options; - options.SetUnwindOnError(true); - options.SetTryAllThreads(true); - options.SetStopOthers(true); - options.SetIgnoreBreakpoints(true); - options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); - options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix); - options.SetAutoApplyFixIts(false); - options.SetLanguage(eLanguageTypeObjC_plus_plus); - - ValueObjectSP main_value; - ExecutionContext exe_ctx; - Error eval_error; - frame_sp->CalculateExecutionContext(exe_ctx); - ExpressionResults result = UserExpression::Evaluate (exe_ctx, - options, - thread_sanitizer_retrieve_report_data_command, - "", - main_value, - eval_error); - if (result != eExpressionCompleted) { - process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", eval_error.AsCString()); - return StructuredData::ObjectSP(); - } - - std::map<uint64_t, user_id_t> thread_id_map; - GetRenumberedThreadIds(process_sp, main_value, thread_id_map); - - StructuredData::Dictionary *dict = new StructuredData::Dictionary(); - dict->AddStringItem("instrumentation_class", "ThreadSanitizer"); - dict->AddStringItem("issue_type", RetrieveString(main_value, process_sp, ".description")); - dict->AddIntegerItem("report_count", main_value->GetValueForExpressionPath(".report_count")->GetValueAsUnsigned(0)); - dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(main_value, ".sleep_trace"))); - - StructuredData::Array *stacks = ConvertToStructuredArray(main_value, ".stacks", ".stack_count", [thread_sp] (ValueObjectSP o, StructuredData::Dictionary *dict) { - dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); +ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) { + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return StructuredData::ObjectSP(); + + ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); + StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); + + if (!frame_sp) + return StructuredData::ObjectSP(); + + EvaluateExpressionOptions options; + options.SetUnwindOnError(true); + options.SetTryAllThreads(true); + options.SetStopOthers(true); + options.SetIgnoreBreakpoints(true); + options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC); + options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix); + options.SetAutoApplyFixIts(false); + options.SetLanguage(eLanguageTypeObjC_plus_plus); + + ValueObjectSP main_value; + ExecutionContext exe_ctx; + Error eval_error; + frame_sp->CalculateExecutionContext(exe_ctx); + ExpressionResults result = UserExpression::Evaluate( + exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "", + main_value, eval_error); + if (result != eExpressionCompleted) { + process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf( + "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", + eval_error.AsCString()); + return StructuredData::ObjectSP(); + } + + std::map<uint64_t, user_id_t> thread_id_map; + GetRenumberedThreadIds(process_sp, main_value, thread_id_map); + + StructuredData::Dictionary *dict = new StructuredData::Dictionary(); + dict->AddStringItem("instrumentation_class", "ThreadSanitizer"); + dict->AddStringItem("issue_type", + RetrieveString(main_value, process_sp, ".description")); + dict->AddIntegerItem("report_count", + main_value->GetValueForExpressionPath(".report_count") + ->GetValueAsUnsigned(0)); + dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace( + main_value, ".sleep_trace"))); + + StructuredData::Array *stacks = ConvertToStructuredArray( + main_value, ".stacks", ".stack_count", + [thread_sp](ValueObjectSP o, StructuredData::Dictionary *dict) { + dict->AddIntegerItem( + "index", + o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); // "stacks" happen on the current thread dict->AddIntegerItem("thread_id", thread_sp->GetIndexID()); - }); - dict->AddItem("stacks", StructuredData::ObjectSP(stacks)); - - StructuredData::Array *mops = ConvertToStructuredArray(main_value, ".mops", ".mop_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { - dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); - dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); - dict->AddBooleanItem("is_write", o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0)); - dict->AddBooleanItem("is_atomic", o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); + }); + dict->AddItem("stacks", StructuredData::ObjectSP(stacks)); + + StructuredData::Array *mops = ConvertToStructuredArray( + main_value, ".mops", ".mop_count", + [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) { + dict->AddIntegerItem( + "index", + o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "thread_id", + Renumber( + o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), + thread_id_map)); + dict->AddIntegerItem( + "size", + o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); + dict->AddBooleanItem( + "is_write", + o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0)); + dict->AddBooleanItem( + "is_atomic", + o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "address", + o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); - }); - dict->AddItem("mops", StructuredData::ObjectSP(mops)); - - StructuredData::Array *locs = ConvertToStructuredArray(main_value, ".locs", ".loc_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { - dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); + }); + dict->AddItem("mops", StructuredData::ObjectSP(mops)); + + StructuredData::Array *locs = ConvertToStructuredArray( + main_value, ".locs", ".loc_count", + [process_sp, &thread_id_map](ValueObjectSP o, + StructuredData::Dictionary *dict) { + dict->AddIntegerItem( + "index", + o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); dict->AddStringItem("type", RetrieveString(o, process_sp, ".type")); - dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("start", o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("size", o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); - dict->AddIntegerItem("file_descriptor", o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("suppressable", o->GetValueForExpressionPath(".suppressable")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "address", + o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "start", + o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "size", + o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "thread_id", + Renumber( + o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), + thread_id_map)); + dict->AddIntegerItem( + "file_descriptor", + o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0)); + dict->AddIntegerItem("suppressable", + o->GetValueForExpressionPath(".suppressable") + ->GetValueAsUnsigned(0)); dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); - }); - dict->AddItem("locs", StructuredData::ObjectSP(locs)); - - StructuredData::Array *mutexes = ConvertToStructuredArray(main_value, ".mutexes", ".mutex_count", [] (ValueObjectSP o, StructuredData::Dictionary *dict) { - dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("mutex_id", o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("address", o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("destroyed", o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0)); + }); + dict->AddItem("locs", StructuredData::ObjectSP(locs)); + + StructuredData::Array *mutexes = ConvertToStructuredArray( + main_value, ".mutexes", ".mutex_count", + [](ValueObjectSP o, StructuredData::Dictionary *dict) { + dict->AddIntegerItem( + "index", + o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "mutex_id", + o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "address", + o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "destroyed", + o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0)); dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); - }); - dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes)); - - StructuredData::Array *threads = ConvertToStructuredArray(main_value, ".threads", ".thread_count", [process_sp, &thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { - dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("thread_id", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); - dict->AddIntegerItem("thread_os_id", o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("running", o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0)); + }); + dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes)); + + StructuredData::Array *threads = ConvertToStructuredArray( + main_value, ".threads", ".thread_count", + [process_sp, &thread_id_map](ValueObjectSP o, + StructuredData::Dictionary *dict) { + dict->AddIntegerItem( + "index", + o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "thread_id", + Renumber( + o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), + thread_id_map)); + dict->AddIntegerItem( + "thread_os_id", + o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "running", + o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0)); dict->AddStringItem("name", RetrieveString(o, process_sp, ".name")); - dict->AddIntegerItem("parent_thread_id", Renumber(o->GetValueForExpressionPath(".parent_tid")->GetValueAsUnsigned(0), thread_id_map)); + dict->AddIntegerItem( + "parent_thread_id", + Renumber(o->GetValueForExpressionPath(".parent_tid") + ->GetValueAsUnsigned(0), + thread_id_map)); dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); - }); - dict->AddItem("threads", StructuredData::ObjectSP(threads)); - - StructuredData::Array *unique_tids = ConvertToStructuredArray(main_value, ".unique_tids", ".unique_tid_count", [&thread_id_map] (ValueObjectSP o, StructuredData::Dictionary *dict) { - dict->AddIntegerItem("index", o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); - dict->AddIntegerItem("tid", Renumber(o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), thread_id_map)); - }); - dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids)); - - return StructuredData::ObjectSP(dict); + }); + dict->AddItem("threads", StructuredData::ObjectSP(threads)); + + StructuredData::Array *unique_tids = ConvertToStructuredArray( + main_value, ".unique_tids", ".unique_tid_count", + [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) { + dict->AddIntegerItem( + "index", + o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); + dict->AddIntegerItem( + "tid", + Renumber( + o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), + thread_id_map)); + }); + dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids)); + + return StructuredData::ObjectSP(dict); } std::string -ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) -{ - std::string description = report->GetAsDictionary()->GetValueForKey("issue_type")->GetAsString()->GetValue(); - - if (description == "data-race") { - return "Data race"; - } else if (description == "data-race-vptr") { - return "Data race on C++ virtual pointer"; - } else if (description == "heap-use-after-free") { - return "Use of deallocated memory"; - } else if (description == "heap-use-after-free-vptr") { - return "Use of deallocated C++ virtual pointer"; - } else if (description == "thread-leak") { - return "Thread leak"; - } else if (description == "locked-mutex-destroy") { - return "Destruction of a locked mutex"; - } else if (description == "mutex-double-lock") { - return "Double lock of a mutex"; - } else if (description == "mutex-invalid-access") { - return "Use of an uninitialized or destroyed mutex"; - } else if (description == "mutex-bad-unlock") { - return "Unlock of an unlocked mutex (or by a wrong thread)"; - } else if (description == "mutex-bad-read-lock") { - return "Read lock of a write locked mutex"; - } else if (description == "mutex-bad-read-unlock") { - return "Read unlock of a write locked mutex"; - } else if (description == "signal-unsafe-call") { - return "Signal-unsafe call inside a signal handler"; - } else if (description == "errno-in-signal-handler") { - return "Overwrite of errno in a signal handler"; - } else if (description == "lock-order-inversion") { - return "Lock order inversion (potential deadlock)"; - } - - // for unknown report codes just show the code - return description; -} - -static std::string -Sprintf(const char *format, ...) -{ - StreamString s; - va_list args; - va_start (args, format); - s.PrintfVarArg(format, args); - va_end (args); - return s.GetString(); -} - -static std::string -GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) -{ +ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) { + std::string description = report->GetAsDictionary() + ->GetValueForKey("issue_type") + ->GetAsString() + ->GetValue(); + + if (description == "data-race") { + return "Data race"; + } else if (description == "data-race-vptr") { + return "Data race on C++ virtual pointer"; + } else if (description == "heap-use-after-free") { + return "Use of deallocated memory"; + } else if (description == "heap-use-after-free-vptr") { + return "Use of deallocated C++ virtual pointer"; + } else if (description == "thread-leak") { + return "Thread leak"; + } else if (description == "locked-mutex-destroy") { + return "Destruction of a locked mutex"; + } else if (description == "mutex-double-lock") { + return "Double lock of a mutex"; + } else if (description == "mutex-invalid-access") { + return "Use of an uninitialized or destroyed mutex"; + } else if (description == "mutex-bad-unlock") { + return "Unlock of an unlocked mutex (or by a wrong thread)"; + } else if (description == "mutex-bad-read-lock") { + return "Read lock of a write locked mutex"; + } else if (description == "mutex-bad-read-unlock") { + return "Read unlock of a write locked mutex"; + } else if (description == "signal-unsafe-call") { + return "Signal-unsafe call inside a signal handler"; + } else if (description == "errno-in-signal-handler") { + return "Overwrite of errno in a signal handler"; + } else if (description == "lock-order-inversion") { + return "Lock order inversion (potential deadlock)"; + } + + // for unknown report codes just show the code + return description; +} + +static std::string Sprintf(const char *format, ...) { + StreamString s; + va_list args; + va_start(args, format); + s.PrintfVarArg(format, args); + va_end(args); + return s.GetString(); +} + +static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) { + lldb_private::Address so_addr; + if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, + so_addr)) + return ""; + + lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); + if (!symbol) + return ""; + + std::string sym_name = symbol->GetName().GetCString(); + return sym_name; +} + +static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, + Declaration &decl) { + lldb_private::Address so_addr; + if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, + so_addr)) + return; + + lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); + if (!symbol) + return; + + ConstString sym_name = symbol->GetMangled().GetName( + lldb::eLanguageTypeUnknown, Mangled::ePreferMangled); + + ModuleSP module = symbol->CalculateSymbolContextModule(); + if (!module) + return; + + VariableList var_list; + module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list); + if (var_list.GetSize() < 1) + return; + + VariableSP var = var_list.GetVariableAtIndex(0); + decl = var->GetDeclaration(); +} + +addr_t ThreadSanitizerRuntime::GetFirstNonInternalFramePc( + StructuredData::ObjectSP trace) { + ProcessSP process_sp = GetProcessSP(); + ModuleSP runtime_module_sp = GetRuntimeModuleSP(); + + addr_t result = 0; + trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, + &result](StructuredData::Object *o) -> bool { + addr_t addr = o->GetIntegerValue(); lldb_private::Address so_addr; - if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) - return ""; - - lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); - if (! symbol) - return ""; - - std::string sym_name = symbol->GetName().GetCString(); - return sym_name; -} + if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress( + addr, so_addr)) + return true; -static void -GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, Declaration &decl) -{ - lldb_private::Address so_addr; - if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) - return; - - lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); - if (! symbol) - return; - - ConstString sym_name = symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled); - - ModuleSP module = symbol->CalculateSymbolContextModule(); - if (! module) - return; - - VariableList var_list; - module->FindGlobalVariables(sym_name, nullptr, true, 1U, var_list); - if (var_list.GetSize() < 1) - return; - - VariableSP var = var_list.GetVariableAtIndex(0); - decl = var->GetDeclaration(); -} + if (so_addr.GetModule() == runtime_module_sp) + return true; -addr_t -ThreadSanitizerRuntime::GetFirstNonInternalFramePc(StructuredData::ObjectSP trace) -{ - ProcessSP process_sp = GetProcessSP(); - ModuleSP runtime_module_sp = GetRuntimeModuleSP(); - - addr_t result = 0; - trace->GetAsArray()->ForEach([process_sp, runtime_module_sp, &result] (StructuredData::Object *o) -> bool { - addr_t addr = o->GetIntegerValue(); - lldb_private::Address so_addr; - if (! process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) - return true; - - if (so_addr.GetModule() == runtime_module_sp) - return true; - - result = addr; - return false; - }); - - return result; + result = addr; + return false; + }); + + return result; } std::string -ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) -{ - ProcessSP process_sp = GetProcessSP(); - - std::string summary = report->GetAsDictionary()->GetValueForKey("description")->GetAsString()->GetValue(); - addr_t pc = 0; - if (report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetSize() > 0) - pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("mops")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); - - if (report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetSize() > 0) - pc = GetFirstNonInternalFramePc(report->GetAsDictionary()->GetValueForKey("stacks")->GetAsArray()->GetItemAtIndex(0)->GetAsDictionary()->GetValueForKey("trace")); - - if (pc != 0) { - summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc); - } - - if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { - StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); - addr_t addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); - if (addr == 0) - addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); - - if (addr != 0) { - std::string global_name = GetSymbolNameFromAddress(process_sp, addr); - if (!global_name.empty()) { - summary = summary + " at " + global_name; - } else { - summary = summary + " at " + Sprintf("0x%llx", addr); - } - } else { - int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); - if (fd != 0) { - summary = summary + " on file descriptor " + Sprintf("%d", fd); - } - } +ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) { + ProcessSP process_sp = GetProcessSP(); + + std::string summary = report->GetAsDictionary() + ->GetValueForKey("description") + ->GetAsString() + ->GetValue(); + addr_t pc = 0; + if (report->GetAsDictionary() + ->GetValueForKey("mops") + ->GetAsArray() + ->GetSize() > 0) + pc = GetFirstNonInternalFramePc(report->GetAsDictionary() + ->GetValueForKey("mops") + ->GetAsArray() + ->GetItemAtIndex(0) + ->GetAsDictionary() + ->GetValueForKey("trace")); + + if (report->GetAsDictionary() + ->GetValueForKey("stacks") + ->GetAsArray() + ->GetSize() > 0) + pc = GetFirstNonInternalFramePc(report->GetAsDictionary() + ->GetValueForKey("stacks") + ->GetAsArray() + ->GetItemAtIndex(0) + ->GetAsDictionary() + ->GetValueForKey("trace")); + + if (pc != 0) { + summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc); + } + + if (report->GetAsDictionary() + ->GetValueForKey("locs") + ->GetAsArray() + ->GetSize() > 0) { + StructuredData::ObjectSP loc = report->GetAsDictionary() + ->GetValueForKey("locs") + ->GetAsArray() + ->GetItemAtIndex(0); + addr_t addr = loc->GetAsDictionary() + ->GetValueForKey("address") + ->GetAsInteger() + ->GetValue(); + if (addr == 0) + addr = loc->GetAsDictionary() + ->GetValueForKey("start") + ->GetAsInteger() + ->GetValue(); + + if (addr != 0) { + std::string global_name = GetSymbolNameFromAddress(process_sp, addr); + if (!global_name.empty()) { + summary = summary + " at " + global_name; + } else { + summary = summary + " at " + Sprintf("0x%llx", addr); + } + } else { + int fd = loc->GetAsDictionary() + ->GetValueForKey("file_descriptor") + ->GetAsInteger() + ->GetValue(); + if (fd != 0) { + summary = summary + " on file descriptor " + Sprintf("%d", fd); + } } - - return summary; + } + + return summary; } addr_t -ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) -{ - addr_t result = (addr_t)-1; - - report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&result] (StructuredData::Object *o) -> bool { - addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); - if (addr < result) result = addr; +ThreadSanitizerRuntime::GetMainRacyAddress(StructuredData::ObjectSP report) { + addr_t result = (addr_t)-1; + + report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach( + [&result](StructuredData::Object *o) -> bool { + addr_t addr = + o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); + if (addr < result) + result = addr; return true; - }); - - return (result == (addr_t)-1) ? 0 : result; -} - -std::string -ThreadSanitizerRuntime::GetLocationDescription(StructuredData::ObjectSP report, addr_t &global_addr, std::string &global_name, std::string &filename, uint32_t &line) -{ - std::string result = ""; - - ProcessSP process_sp = GetProcessSP(); - - if (report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetSize() > 0) { - StructuredData::ObjectSP loc = report->GetAsDictionary()->GetValueForKey("locs")->GetAsArray()->GetItemAtIndex(0); - std::string type = loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); - if (type == "global") { - global_addr = loc->GetAsDictionary()->GetValueForKey("address")->GetAsInteger()->GetValue(); - global_name = GetSymbolNameFromAddress(process_sp, global_addr); - if (!global_name.empty()) { - result = Sprintf("'%s' is a global variable (0x%llx)", global_name.c_str(), global_addr); - } else { - result = Sprintf("0x%llx is a global variable", global_addr); - } - - Declaration decl; - GetSymbolDeclarationFromAddress(process_sp, global_addr, decl); - if (decl.GetFile()) { - filename = decl.GetFile().GetPath(); - line = decl.GetLine(); - } - } else if (type == "heap") { - addr_t addr = loc->GetAsDictionary()->GetValueForKey("start")->GetAsInteger()->GetValue(); - long size = loc->GetAsDictionary()->GetValueForKey("size")->GetAsInteger()->GetValue(); - result = Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr); - } else if (type == "stack") { - int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); - result = Sprintf("Location is stack of thread %d", tid); - } else if (type == "tls") { - int tid = loc->GetAsDictionary()->GetValueForKey("thread_id")->GetAsInteger()->GetValue(); - result = Sprintf("Location is TLS of thread %d", tid); - } else if (type == "fd") { - int fd = loc->GetAsDictionary()->GetValueForKey("file_descriptor")->GetAsInteger()->GetValue(); - result = Sprintf("Location is file descriptor %d", fd); - } + }); + + return (result == (addr_t)-1) ? 0 : result; +} + +std::string ThreadSanitizerRuntime::GetLocationDescription( + StructuredData::ObjectSP report, addr_t &global_addr, + std::string &global_name, std::string &filename, uint32_t &line) { + std::string result = ""; + + ProcessSP process_sp = GetProcessSP(); + + if (report->GetAsDictionary() + ->GetValueForKey("locs") + ->GetAsArray() + ->GetSize() > 0) { + StructuredData::ObjectSP loc = report->GetAsDictionary() + ->GetValueForKey("locs") + ->GetAsArray() + ->GetItemAtIndex(0); + std::string type = + loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); + if (type == "global") { + global_addr = loc->GetAsDictionary() + ->GetValueForKey("address") + ->GetAsInteger() + ->GetValue(); + global_name = GetSymbolNameFromAddress(process_sp, global_addr); + if (!global_name.empty()) { + result = Sprintf("'%s' is a global variable (0x%llx)", + global_name.c_str(), global_addr); + } else { + result = Sprintf("0x%llx is a global variable", global_addr); + } + + Declaration decl; + GetSymbolDeclarationFromAddress(process_sp, global_addr, decl); + if (decl.GetFile()) { + filename = decl.GetFile().GetPath(); + line = decl.GetLine(); + } + } else if (type == "heap") { + addr_t addr = loc->GetAsDictionary() + ->GetValueForKey("start") + ->GetAsInteger() + ->GetValue(); + long size = loc->GetAsDictionary() + ->GetValueForKey("size") + ->GetAsInteger() + ->GetValue(); + result = + Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr); + } else if (type == "stack") { + int tid = loc->GetAsDictionary() + ->GetValueForKey("thread_id") + ->GetAsInteger() + ->GetValue(); + result = Sprintf("Location is stack of thread %d", tid); + } else if (type == "tls") { + int tid = loc->GetAsDictionary() + ->GetValueForKey("thread_id") + ->GetAsInteger() + ->GetValue(); + result = Sprintf("Location is TLS of thread %d", tid); + } else if (type == "fd") { + int fd = loc->GetAsDictionary() + ->GetValueForKey("file_descriptor") + ->GetAsInteger() + ->GetValue(); + result = Sprintf("Location is file descriptor %d", fd); + } + } + + return result; +} + +bool ThreadSanitizerRuntime::NotifyBreakpointHit( + void *baton, StoppointCallbackContext *context, user_id_t break_id, + user_id_t break_loc_id) { + assert(baton && "null baton"); + if (!baton) + return false; + + ThreadSanitizerRuntime *const instance = + static_cast<ThreadSanitizerRuntime *>(baton); + + StructuredData::ObjectSP report = + instance->RetrieveReportData(context->exe_ctx_ref); + std::string stop_reason_description; + if (report) { + std::string issue_description = instance->FormatDescription(report); + report->GetAsDictionary()->AddStringItem("description", issue_description); + stop_reason_description = issue_description + " detected"; + report->GetAsDictionary()->AddStringItem("stop_description", + stop_reason_description); + std::string summary = instance->GenerateSummary(report); + report->GetAsDictionary()->AddStringItem("summary", summary); + addr_t main_address = instance->GetMainRacyAddress(report); + report->GetAsDictionary()->AddIntegerItem("memory_address", main_address); + + addr_t global_addr = 0; + std::string global_name = ""; + std::string location_filename = ""; + uint32_t location_line = 0; + std::string location_description = instance->GetLocationDescription( + report, global_addr, global_name, location_filename, location_line); + report->GetAsDictionary()->AddStringItem("location_description", + location_description); + if (global_addr != 0) { + report->GetAsDictionary()->AddIntegerItem("global_address", global_addr); + } + if (!global_name.empty()) { + report->GetAsDictionary()->AddStringItem("global_name", global_name); + } + if (location_filename != "") { + report->GetAsDictionary()->AddStringItem("location_filename", + location_filename); + report->GetAsDictionary()->AddIntegerItem("location_line", location_line); } - - return result; -} -bool -ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id) -{ - assert (baton && "null baton"); - if (!baton) - return false; - - ThreadSanitizerRuntime *const instance = static_cast<ThreadSanitizerRuntime*>(baton); - - StructuredData::ObjectSP report = instance->RetrieveReportData(context->exe_ctx_ref); - std::string stop_reason_description; - if (report) { - std::string issue_description = instance->FormatDescription(report); - report->GetAsDictionary()->AddStringItem("description", issue_description); - stop_reason_description = issue_description + " detected"; - report->GetAsDictionary()->AddStringItem("stop_description", stop_reason_description); - std::string summary = instance->GenerateSummary(report); - report->GetAsDictionary()->AddStringItem("summary", summary); - addr_t main_address = instance->GetMainRacyAddress(report); - report->GetAsDictionary()->AddIntegerItem("memory_address", main_address); - - addr_t global_addr = 0; - std::string global_name = ""; - std::string location_filename = ""; - uint32_t location_line = 0; - std::string location_description = instance->GetLocationDescription(report, global_addr, global_name, location_filename, location_line); - report->GetAsDictionary()->AddStringItem("location_description", location_description); - if (global_addr != 0) { - report->GetAsDictionary()->AddIntegerItem("global_address", global_addr); - } - if (!global_name.empty()) { - report->GetAsDictionary()->AddStringItem("global_name", global_name); - } - if (location_filename != "") { - report->GetAsDictionary()->AddStringItem("location_filename", location_filename); - report->GetAsDictionary()->AddIntegerItem("location_line", location_line); - } - - bool all_addresses_are_same = true; - report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach([&all_addresses_are_same, main_address] (StructuredData::Object *o) -> bool { - addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); - if (main_address != addr) all_addresses_are_same = false; - return true; + bool all_addresses_are_same = true; + report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach( + [&all_addresses_are_same, + main_address](StructuredData::Object *o) -> bool { + addr_t addr = + o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); + if (main_address != addr) + all_addresses_are_same = false; + return true; }); - report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same", all_addresses_are_same); + report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same", + all_addresses_are_same); + } + + ProcessSP process_sp = instance->GetProcessSP(); + // Make sure this is the right process + if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) { + ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); + if (thread_sp) + thread_sp->SetStopInfo( + InstrumentationRuntimeStopInfo:: + CreateStopReasonWithInstrumentationData( + *thread_sp, stop_reason_description.c_str(), report)); + + StreamFileSP stream_sp( + process_sp->GetTarget().GetDebugger().GetOutputFile()); + if (stream_sp) { + stream_sp->Printf("ThreadSanitizer report breakpoint hit. Use 'thread " + "info -s' to get extended information about the " + "report.\n"); } - - ProcessSP process_sp = instance->GetProcessSP(); - // Make sure this is the right process - if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) - { - ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); - if (thread_sp) - thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(*thread_sp, stop_reason_description.c_str(), report)); - - StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); - if (stream_sp) - { - stream_sp->Printf ("ThreadSanitizer report breakpoint hit. Use 'thread info -s' to get extended information about the report.\n"); - } - return true; // Return true to stop the target - } - else - return false; // Let target run + return true; // Return true to stop the target + } else + return false; // Let target run } -const RegularExpression & -ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() { +const RegularExpression &ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() { static RegularExpression regex("libclang_rt.tsan_"); return regex; } -bool -ThreadSanitizerRuntime::CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) -{ - static ConstString g_tsan_get_current_report("__tsan_get_current_report"); - const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny); - return symbol != nullptr; +bool ThreadSanitizerRuntime::CheckIfRuntimeIsValid( + const lldb::ModuleSP module_sp) { + static ConstString g_tsan_get_current_report("__tsan_get_current_report"); + const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType( + g_tsan_get_current_report, lldb::eSymbolTypeAny); + return symbol != nullptr; } -void -ThreadSanitizerRuntime::Activate() -{ - if (IsActive()) - return; - - ProcessSP process_sp = GetProcessSP(); - if (!process_sp) - return; - - ConstString symbol_name ("__tsan_on_report"); - const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode); - - if (symbol == NULL) - return; - - if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) - return; - - Target &target = process_sp->GetTarget(); - addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); - - if (symbol_address == LLDB_INVALID_ADDRESS) - return; - - bool internal = true; - bool hardware = false; - Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get(); - breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true); - breakpoint->SetBreakpointKind ("thread-sanitizer-report"); - SetBreakpointID(breakpoint->GetID()); - - StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile()); - if (stream_sp) - { - stream_sp->Printf ("ThreadSanitizer debugger support is active.\n"); - } - - SetActive(true); -} +void ThreadSanitizerRuntime::Activate() { + if (IsActive()) + return; -void -ThreadSanitizerRuntime::Deactivate() -{ - if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) - { - ProcessSP process_sp = GetProcessSP(); - if (process_sp) - { - process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID()); - SetBreakpointID(LLDB_INVALID_BREAK_ID); - } - } - SetActive(false); + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return; + + ConstString symbol_name("__tsan_on_report"); + const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType( + symbol_name, eSymbolTypeCode); + + if (symbol == NULL) + return; + + if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) + return; + + Target &target = process_sp->GetTarget(); + addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); + + if (symbol_address == LLDB_INVALID_ADDRESS) + return; + + bool internal = true; + bool hardware = false; + Breakpoint *breakpoint = + process_sp->GetTarget() + .CreateBreakpoint(symbol_address, internal, hardware) + .get(); + breakpoint->SetCallback(ThreadSanitizerRuntime::NotifyBreakpointHit, this, + true); + breakpoint->SetBreakpointKind("thread-sanitizer-report"); + SetBreakpointID(breakpoint->GetID()); + + StreamFileSP stream_sp(process_sp->GetTarget().GetDebugger().GetOutputFile()); + if (stream_sp) { + stream_sp->Printf("ThreadSanitizer debugger support is active.\n"); + } + + SetActive(true); } -static std::string -GenerateThreadName(const std::string &path, StructuredData::Object *o, StructuredData::ObjectSP main_info) { - std::string result = "additional information"; - - if (path == "mops") { - int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue(); - int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); - bool is_write = o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue(); - bool is_atomic = o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue(); - addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); - - std::string addr_string = Sprintf(" at 0x%llx", addr); - - if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")->GetBooleanValue()){ - addr_string = ""; - } - - 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); - } - - if (path == "threads") { - int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); - result = Sprintf("Thread %d created", thread_id); - } - - if (path == "locs") { - std::string type = o->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); - int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); - int fd = o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue(); - if (type == "heap") { - result = Sprintf("Heap block allocated by thread %d", thread_id); - } else if (type == "fd") { - result = Sprintf("File descriptor %d created by thread %t", fd, thread_id); - } + +void ThreadSanitizerRuntime::Deactivate() { + if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) { + ProcessSP process_sp = GetProcessSP(); + if (process_sp) { + process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID()); + SetBreakpointID(LLDB_INVALID_BREAK_ID); } - - if (path == "mutexes") { - int mutex_id = o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue(); - - result = Sprintf("Mutex M%d created", mutex_id); + } + SetActive(false); +} +static std::string GenerateThreadName(const std::string &path, + StructuredData::Object *o, + StructuredData::ObjectSP main_info) { + std::string result = "additional information"; + + if (path == "mops") { + int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue(); + int thread_id = + o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); + bool is_write = + o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue(); + bool is_atomic = + o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue(); + addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); + + std::string addr_string = Sprintf(" at 0x%llx", addr); + + if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same") + ->GetBooleanValue()) { + addr_string = ""; } - - if (path == "stacks") { - int thread_id = o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); - result = Sprintf("Thread %d", thread_id); + + 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); + } + + if (path == "threads") { + int thread_id = + o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); + result = Sprintf("Thread %d created", thread_id); + } + + if (path == "locs") { + std::string type = + o->GetAsDictionary()->GetValueForKey("type")->GetStringValue(); + int thread_id = + o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); + int fd = + o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue(); + if (type == "heap") { + result = Sprintf("Heap block allocated by thread %d", thread_id); + } else if (type == "fd") { + result = + Sprintf("File descriptor %d created by thread %t", fd, thread_id); } - - result[0] = toupper(result[0]); - - return result; + } + + if (path == "mutexes") { + int mutex_id = + o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue(); + + result = Sprintf("Mutex M%d created", mutex_id); + } + + if (path == "stacks") { + int thread_id = + o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); + result = Sprintf("Thread %d", thread_id); + } + + result[0] = toupper(result[0]); + + return result; } -static void -AddThreadsForPath(const std::string &path, ThreadCollectionSP threads, ProcessSP process_sp, StructuredData::ObjectSP info) -{ - info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach([process_sp, threads, path, info] (StructuredData::Object *o) -> bool { +static void AddThreadsForPath(const std::string &path, + ThreadCollectionSP threads, ProcessSP process_sp, + StructuredData::ObjectSP info) { + info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach( + [process_sp, threads, path, info](StructuredData::Object *o) -> bool { std::vector<lldb::addr_t> pcs; - o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach([&pcs] (StructuredData::Object *pc) -> bool { - pcs.push_back(pc->GetAsInteger()->GetValue()); - return true; - }); - + o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach( + [&pcs](StructuredData::Object *pc) -> bool { + pcs.push_back(pc->GetAsInteger()->GetValue()); + return true; + }); + if (pcs.size() == 0) - return true; - - StructuredData::ObjectSP thread_id_obj = o->GetObjectForDotSeparatedPath("thread_os_id"); + return true; + + StructuredData::ObjectSP thread_id_obj = + o->GetObjectForDotSeparatedPath("thread_os_id"); tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0; - + uint32_t stop_id = 0; bool stop_id_is_valid = false; - HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid); + HistoryThread *history_thread = + new HistoryThread(*process_sp, tid, pcs, stop_id, stop_id_is_valid); ThreadSP new_thread_sp(history_thread); new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str()); - - // Save this in the Process' ExtendedThreadList so a strong pointer retains the object + + // Save this in the Process' ExtendedThreadList so a strong pointer + // retains the object process_sp->GetExtendedThreadList().AddThread(new_thread_sp); threads->AddThread(new_thread_sp); - + return true; - }); + }); } lldb::ThreadCollectionSP -ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) -{ - ThreadCollectionSP threads; - threads.reset(new ThreadCollection()); +ThreadSanitizerRuntime::GetBacktracesFromExtendedStopInfo( + StructuredData::ObjectSP info) { + ThreadCollectionSP threads; + threads.reset(new ThreadCollection()); - if (info->GetObjectForDotSeparatedPath("instrumentation_class")->GetStringValue() != "ThreadSanitizer") - return threads; - - ProcessSP process_sp = GetProcessSP(); - - AddThreadsForPath("stacks", threads, process_sp, info); - AddThreadsForPath("mops", threads, process_sp, info); - AddThreadsForPath("locs", threads, process_sp, info); - AddThreadsForPath("mutexes", threads, process_sp, info); - AddThreadsForPath("threads", threads, process_sp, info); - + if (info->GetObjectForDotSeparatedPath("instrumentation_class") + ->GetStringValue() != "ThreadSanitizer") return threads; + + ProcessSP process_sp = GetProcessSP(); + + AddThreadsForPath("stacks", threads, process_sp, info); + AddThreadsForPath("mops", threads, process_sp, info); + AddThreadsForPath("locs", threads, process_sp, info); + AddThreadsForPath("mutexes", threads, process_sp, info); + AddThreadsForPath("threads", threads, process_sp, info); + + return threads; } diff --git a/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h b/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h index fcec0744341..9b07ca64e32 100644 --- a/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h +++ b/lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h @@ -14,88 +14,72 @@ // C++ Includes // Other libraries and framework includes // Project includes -#include "lldb/lldb-private.h" +#include "lldb/Core/StructuredData.h" #include "lldb/Target/ABI.h" #include "lldb/Target/InstrumentationRuntime.h" -#include "lldb/Core/StructuredData.h" +#include "lldb/lldb-private.h" namespace lldb_private { - -class ThreadSanitizerRuntime : public lldb_private::InstrumentationRuntime -{ + +class ThreadSanitizerRuntime : public lldb_private::InstrumentationRuntime { public: - ~ThreadSanitizerRuntime() override; - - static lldb::InstrumentationRuntimeSP - CreateInstance (const lldb::ProcessSP &process_sp); - - static void - Initialize(); - - static void - Terminate(); - - static lldb_private::ConstString - GetPluginNameStatic(); - - static lldb::InstrumentationRuntimeType - GetTypeStatic(); - - lldb_private::ConstString - GetPluginName() override - { - return GetPluginNameStatic(); - } - - virtual lldb::InstrumentationRuntimeType - GetType() { return GetTypeStatic(); } - - uint32_t - GetPluginVersion() override - { - return 1; - } - - lldb::ThreadCollectionSP - GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override; - + ~ThreadSanitizerRuntime() override; + + static lldb::InstrumentationRuntimeSP + CreateInstance(const lldb::ProcessSP &process_sp); + + static void Initialize(); + + static void Terminate(); + + static lldb_private::ConstString GetPluginNameStatic(); + + static lldb::InstrumentationRuntimeType GetTypeStatic(); + + lldb_private::ConstString GetPluginName() override { + return GetPluginNameStatic(); + } + + virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); } + + uint32_t GetPluginVersion() override { return 1; } + + lldb::ThreadCollectionSP + GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override; + private: - ThreadSanitizerRuntime(const lldb::ProcessSP &process_sp) : lldb_private::InstrumentationRuntime(process_sp) {} - - const RegularExpression & - GetPatternForRuntimeLibrary() override; - - bool - CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override; - - void - Activate() override; - - void - Deactivate(); - - static bool - NotifyBreakpointHit(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id); - - StructuredData::ObjectSP - RetrieveReportData(ExecutionContextRef exe_ctx_ref); - - std::string - FormatDescription(StructuredData::ObjectSP report); - - std::string - GenerateSummary(StructuredData::ObjectSP report); - - lldb::addr_t - GetMainRacyAddress(StructuredData::ObjectSP report); - - std::string - GetLocationDescription(StructuredData::ObjectSP report, lldb::addr_t &global_addr, std::string &global_name, std::string &filename, uint32_t &line); - - lldb::addr_t - GetFirstNonInternalFramePc(StructuredData::ObjectSP trace); + ThreadSanitizerRuntime(const lldb::ProcessSP &process_sp) + : lldb_private::InstrumentationRuntime(process_sp) {} + + const RegularExpression &GetPatternForRuntimeLibrary() override; + + bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override; + + void Activate() override; + + void Deactivate(); + + static bool NotifyBreakpointHit(void *baton, + StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id); + + StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref); + + std::string FormatDescription(StructuredData::ObjectSP report); + + std::string GenerateSummary(StructuredData::ObjectSP report); + + lldb::addr_t GetMainRacyAddress(StructuredData::ObjectSP report); + + std::string GetLocationDescription(StructuredData::ObjectSP report, + lldb::addr_t &global_addr, + std::string &global_name, + std::string &filename, uint32_t &line); + + lldb::addr_t GetFirstNonInternalFramePc(StructuredData::ObjectSP trace); }; - + } // namespace lldb_private #endif // liblldb_ThreadSanitizerRuntime_h_ |