Path: blob/main/contrib/llvm-project/lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp
39644 views
//===-- InstrumentationRuntimeMainThreadChecker.cpp -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "InstrumentationRuntimeMainThreadChecker.h"910#include "Plugins/Process/Utility/HistoryThread.h"11#include "lldb/Breakpoint/StoppointCallbackContext.h"12#include "lldb/Core/Module.h"13#include "lldb/Core/PluginManager.h"14#include "lldb/Symbol/Symbol.h"15#include "lldb/Symbol/SymbolContext.h"16#include "lldb/Symbol/Variable.h"17#include "lldb/Symbol/VariableList.h"18#include "lldb/Target/InstrumentationRuntimeStopInfo.h"19#include "lldb/Target/RegisterContext.h"20#include "lldb/Target/SectionLoadList.h"21#include "lldb/Target/StopInfo.h"22#include "lldb/Target/Target.h"23#include "lldb/Target/Thread.h"24#include "lldb/Utility/RegularExpression.h"2526#include <memory>2728using namespace lldb;29using namespace lldb_private;3031LLDB_PLUGIN_DEFINE(InstrumentationRuntimeMainThreadChecker)3233InstrumentationRuntimeMainThreadChecker::34~InstrumentationRuntimeMainThreadChecker() {35Deactivate();36}3738lldb::InstrumentationRuntimeSP39InstrumentationRuntimeMainThreadChecker::CreateInstance(40const lldb::ProcessSP &process_sp) {41return InstrumentationRuntimeSP(42new InstrumentationRuntimeMainThreadChecker(process_sp));43}4445void InstrumentationRuntimeMainThreadChecker::Initialize() {46PluginManager::RegisterPlugin(47GetPluginNameStatic(),48"MainThreadChecker instrumentation runtime plugin.", CreateInstance,49GetTypeStatic);50}5152void InstrumentationRuntimeMainThreadChecker::Terminate() {53PluginManager::UnregisterPlugin(CreateInstance);54}5556lldb::InstrumentationRuntimeType57InstrumentationRuntimeMainThreadChecker::GetTypeStatic() {58return eInstrumentationRuntimeTypeMainThreadChecker;59}6061const RegularExpression &62InstrumentationRuntimeMainThreadChecker::GetPatternForRuntimeLibrary() {63static RegularExpression regex(llvm::StringRef("libMainThreadChecker.dylib"));64return regex;65}6667bool InstrumentationRuntimeMainThreadChecker::CheckIfRuntimeIsValid(68const lldb::ModuleSP module_sp) {69static ConstString test_sym("__main_thread_checker_on_report");70const Symbol *symbol =71module_sp->FindFirstSymbolWithNameAndType(test_sym, lldb::eSymbolTypeAny);72return symbol != nullptr;73}7475StructuredData::ObjectSP76InstrumentationRuntimeMainThreadChecker::RetrieveReportData(77ExecutionContextRef exe_ctx_ref) {78ProcessSP process_sp = GetProcessSP();79if (!process_sp)80return StructuredData::ObjectSP();8182ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();83StackFrameSP frame_sp =84thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);85ModuleSP runtime_module_sp = GetRuntimeModuleSP();86Target &target = process_sp->GetTarget();8788if (!frame_sp)89return StructuredData::ObjectSP();9091RegisterContextSP regctx_sp = frame_sp->GetRegisterContext();92if (!regctx_sp)93return StructuredData::ObjectSP();9495const RegisterInfo *reginfo = regctx_sp->GetRegisterInfoByName("arg1");96if (!reginfo)97return StructuredData::ObjectSP();9899uint64_t apiname_ptr = regctx_sp->ReadRegisterAsUnsigned(reginfo, 0);100if (!apiname_ptr)101return StructuredData::ObjectSP();102103std::string apiName;104Status read_error;105target.ReadCStringFromMemory(apiname_ptr, apiName, read_error);106if (read_error.Fail())107return StructuredData::ObjectSP();108109std::string className;110std::string selector;111if (apiName.substr(0, 2) == "-[") {112size_t spacePos = apiName.find(' ');113if (spacePos != std::string::npos) {114className = apiName.substr(2, spacePos - 2);115selector = apiName.substr(spacePos + 1, apiName.length() - spacePos - 2);116}117}118119// Gather the PCs of the user frames in the backtrace.120StructuredData::Array *trace = new StructuredData::Array();121auto trace_sp = StructuredData::ObjectSP(trace);122StackFrameSP responsible_frame;123for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {124StackFrameSP frame = thread_sp->GetStackFrameAtIndex(I);125Address addr = frame->GetFrameCodeAddressForSymbolication();126if (addr.GetModule() == runtime_module_sp) // Skip PCs from the runtime.127continue;128129// The first non-runtime frame is responsible for the bug.130if (!responsible_frame)131responsible_frame = frame;132133lldb::addr_t PC = addr.GetLoadAddress(&target);134trace->AddIntegerItem(PC);135}136137auto *d = new StructuredData::Dictionary();138auto dict_sp = StructuredData::ObjectSP(d);139d->AddStringItem("instrumentation_class", "MainThreadChecker");140d->AddStringItem("api_name", apiName);141d->AddStringItem("class_name", className);142d->AddStringItem("selector", selector);143d->AddStringItem("description",144apiName + " must be used from main thread only");145d->AddIntegerItem("tid", thread_sp->GetIndexID());146d->AddItem("trace", trace_sp);147return dict_sp;148}149150bool InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit(151void *baton, StoppointCallbackContext *context, user_id_t break_id,152user_id_t break_loc_id) {153assert(baton && "null baton");154if (!baton)155return false; ///< false => resume execution.156157InstrumentationRuntimeMainThreadChecker *const instance =158static_cast<InstrumentationRuntimeMainThreadChecker *>(baton);159160ProcessSP process_sp = instance->GetProcessSP();161ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();162if (!process_sp || !thread_sp ||163process_sp != context->exe_ctx_ref.GetProcessSP())164return false;165166if (process_sp->GetModIDRef().IsLastResumeForUserExpression())167return false;168169StructuredData::ObjectSP report =170instance->RetrieveReportData(context->exe_ctx_ref);171172if (report) {173std::string description = std::string(report->GetAsDictionary()174->GetValueForKey("description")175->GetAsString()176->GetValue());177thread_sp->SetStopInfo(178InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(179*thread_sp, description, report));180return true;181}182183return false;184}185186void InstrumentationRuntimeMainThreadChecker::Activate() {187if (IsActive())188return;189190ProcessSP process_sp = GetProcessSP();191if (!process_sp)192return;193194ModuleSP runtime_module_sp = GetRuntimeModuleSP();195196ConstString symbol_name("__main_thread_checker_on_report");197const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(198symbol_name, eSymbolTypeCode);199200if (symbol == nullptr)201return;202203if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())204return;205206Target &target = process_sp->GetTarget();207addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);208209if (symbol_address == LLDB_INVALID_ADDRESS)210return;211212Breakpoint *breakpoint =213process_sp->GetTarget()214.CreateBreakpoint(symbol_address, /*internal=*/true,215/*hardware=*/false)216.get();217const bool sync = false;218breakpoint->SetCallback(219InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit, this, sync);220breakpoint->SetBreakpointKind("main-thread-checker-report");221SetBreakpointID(breakpoint->GetID());222223SetActive(true);224}225226void InstrumentationRuntimeMainThreadChecker::Deactivate() {227SetActive(false);228229auto BID = GetBreakpointID();230if (BID == LLDB_INVALID_BREAK_ID)231return;232233if (ProcessSP process_sp = GetProcessSP()) {234process_sp->GetTarget().RemoveBreakpointByID(BID);235SetBreakpointID(LLDB_INVALID_BREAK_ID);236}237}238239lldb::ThreadCollectionSP240InstrumentationRuntimeMainThreadChecker::GetBacktracesFromExtendedStopInfo(241StructuredData::ObjectSP info) {242ThreadCollectionSP threads;243threads = std::make_shared<ThreadCollection>();244245ProcessSP process_sp = GetProcessSP();246247if (info->GetObjectForDotSeparatedPath("instrumentation_class")248->GetStringValue() != "MainThreadChecker")249return threads;250251std::vector<lldb::addr_t> PCs;252auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();253trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {254PCs.push_back(PC->GetUnsignedIntegerValue());255return true;256});257258if (PCs.empty())259return threads;260261StructuredData::ObjectSP thread_id_obj =262info->GetObjectForDotSeparatedPath("tid");263tid_t tid = thread_id_obj ? thread_id_obj->GetUnsignedIntegerValue() : 0;264265// We gather symbolication addresses above, so no need for HistoryThread to266// try to infer the call addresses.267bool pcs_are_call_addresses = true;268ThreadSP new_thread_sp = std::make_shared<HistoryThread>(269*process_sp, tid, PCs, pcs_are_call_addresses);270271// Save this in the Process' ExtendedThreadList so a strong pointer retains272// the object273process_sp->GetExtendedThreadList().AddThread(new_thread_sp);274threads->AddThread(new_thread_sp);275276return threads;277}278279280