180814287SRaphael Isemann //===-- InstrumentationRuntimeMainThreadChecker.cpp -----------------------===//
2fc1e8551SJonas Devlieghere //
3fc1e8551SJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fc1e8551SJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information.
5fc1e8551SJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fc1e8551SJonas Devlieghere //
7fc1e8551SJonas Devlieghere //===----------------------------------------------------------------------===//
8fc1e8551SJonas Devlieghere
9fc1e8551SJonas Devlieghere #include "InstrumentationRuntimeMainThreadChecker.h"
10fc1e8551SJonas Devlieghere
11fc1e8551SJonas Devlieghere #include "Plugins/Process/Utility/HistoryThread.h"
12fc1e8551SJonas Devlieghere #include "lldb/Breakpoint/StoppointCallbackContext.h"
13fc1e8551SJonas Devlieghere #include "lldb/Core/Module.h"
14fc1e8551SJonas Devlieghere #include "lldb/Core/PluginManager.h"
15fc1e8551SJonas Devlieghere #include "lldb/Symbol/Symbol.h"
16fc1e8551SJonas Devlieghere #include "lldb/Symbol/SymbolContext.h"
17fc1e8551SJonas Devlieghere #include "lldb/Symbol/Variable.h"
18fc1e8551SJonas Devlieghere #include "lldb/Symbol/VariableList.h"
19fc1e8551SJonas Devlieghere #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
20fc1e8551SJonas Devlieghere #include "lldb/Target/RegisterContext.h"
21fc1e8551SJonas Devlieghere #include "lldb/Target/SectionLoadList.h"
22fc1e8551SJonas Devlieghere #include "lldb/Target/StopInfo.h"
23fc1e8551SJonas Devlieghere #include "lldb/Target/Target.h"
24fc1e8551SJonas Devlieghere #include "lldb/Target/Thread.h"
25fc1e8551SJonas Devlieghere #include "lldb/Utility/RegularExpression.h"
26fc1e8551SJonas Devlieghere
27fc1e8551SJonas Devlieghere #include <memory>
28fc1e8551SJonas Devlieghere
29fc1e8551SJonas Devlieghere using namespace lldb;
30fc1e8551SJonas Devlieghere using namespace lldb_private;
31fc1e8551SJonas Devlieghere
LLDB_PLUGIN_DEFINE(InstrumentationRuntimeMainThreadChecker)32bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(InstrumentationRuntimeMainThreadChecker)
33fbb4d1e4SJonas Devlieghere
34fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::
35fc1e8551SJonas Devlieghere ~InstrumentationRuntimeMainThreadChecker() {
36fc1e8551SJonas Devlieghere Deactivate();
37fc1e8551SJonas Devlieghere }
38fc1e8551SJonas Devlieghere
39fc1e8551SJonas Devlieghere lldb::InstrumentationRuntimeSP
CreateInstance(const lldb::ProcessSP & process_sp)40fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::CreateInstance(
41fc1e8551SJonas Devlieghere const lldb::ProcessSP &process_sp) {
42fc1e8551SJonas Devlieghere return InstrumentationRuntimeSP(
43fc1e8551SJonas Devlieghere new InstrumentationRuntimeMainThreadChecker(process_sp));
44fc1e8551SJonas Devlieghere }
45fc1e8551SJonas Devlieghere
Initialize()46fc1e8551SJonas Devlieghere void InstrumentationRuntimeMainThreadChecker::Initialize() {
47fc1e8551SJonas Devlieghere PluginManager::RegisterPlugin(
48fc1e8551SJonas Devlieghere GetPluginNameStatic(),
49fc1e8551SJonas Devlieghere "MainThreadChecker instrumentation runtime plugin.", CreateInstance,
50fc1e8551SJonas Devlieghere GetTypeStatic);
51fc1e8551SJonas Devlieghere }
52fc1e8551SJonas Devlieghere
Terminate()53fc1e8551SJonas Devlieghere void InstrumentationRuntimeMainThreadChecker::Terminate() {
54fc1e8551SJonas Devlieghere PluginManager::UnregisterPlugin(CreateInstance);
55fc1e8551SJonas Devlieghere }
56fc1e8551SJonas Devlieghere
57fc1e8551SJonas Devlieghere lldb::InstrumentationRuntimeType
GetTypeStatic()58fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::GetTypeStatic() {
59fc1e8551SJonas Devlieghere return eInstrumentationRuntimeTypeMainThreadChecker;
60fc1e8551SJonas Devlieghere }
61fc1e8551SJonas Devlieghere
62fc1e8551SJonas Devlieghere const RegularExpression &
GetPatternForRuntimeLibrary()63fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::GetPatternForRuntimeLibrary() {
64fc1e8551SJonas Devlieghere static RegularExpression regex(llvm::StringRef("libMainThreadChecker.dylib"));
65fc1e8551SJonas Devlieghere return regex;
66fc1e8551SJonas Devlieghere }
67fc1e8551SJonas Devlieghere
CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)68fc1e8551SJonas Devlieghere bool InstrumentationRuntimeMainThreadChecker::CheckIfRuntimeIsValid(
69fc1e8551SJonas Devlieghere const lldb::ModuleSP module_sp) {
70fc1e8551SJonas Devlieghere static ConstString test_sym("__main_thread_checker_on_report");
71fc1e8551SJonas Devlieghere const Symbol *symbol =
72fc1e8551SJonas Devlieghere module_sp->FindFirstSymbolWithNameAndType(test_sym, lldb::eSymbolTypeAny);
73fc1e8551SJonas Devlieghere return symbol != nullptr;
74fc1e8551SJonas Devlieghere }
75fc1e8551SJonas Devlieghere
76fc1e8551SJonas Devlieghere StructuredData::ObjectSP
RetrieveReportData(ExecutionContextRef exe_ctx_ref)77fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::RetrieveReportData(
78fc1e8551SJonas Devlieghere ExecutionContextRef exe_ctx_ref) {
79fc1e8551SJonas Devlieghere ProcessSP process_sp = GetProcessSP();
80fc1e8551SJonas Devlieghere if (!process_sp)
81fc1e8551SJonas Devlieghere return StructuredData::ObjectSP();
82fc1e8551SJonas Devlieghere
83fc1e8551SJonas Devlieghere ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
84fc1e8551SJonas Devlieghere StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
85fc1e8551SJonas Devlieghere ModuleSP runtime_module_sp = GetRuntimeModuleSP();
86fc1e8551SJonas Devlieghere Target &target = process_sp->GetTarget();
87fc1e8551SJonas Devlieghere
88fc1e8551SJonas Devlieghere if (!frame_sp)
89fc1e8551SJonas Devlieghere return StructuredData::ObjectSP();
90fc1e8551SJonas Devlieghere
91fc1e8551SJonas Devlieghere RegisterContextSP regctx_sp = frame_sp->GetRegisterContext();
92fc1e8551SJonas Devlieghere if (!regctx_sp)
93fc1e8551SJonas Devlieghere return StructuredData::ObjectSP();
94fc1e8551SJonas Devlieghere
95fc1e8551SJonas Devlieghere const RegisterInfo *reginfo = regctx_sp->GetRegisterInfoByName("arg1");
96fc1e8551SJonas Devlieghere if (!reginfo)
97fc1e8551SJonas Devlieghere return StructuredData::ObjectSP();
98fc1e8551SJonas Devlieghere
99fc1e8551SJonas Devlieghere uint64_t apiname_ptr = regctx_sp->ReadRegisterAsUnsigned(reginfo, 0);
100fc1e8551SJonas Devlieghere if (!apiname_ptr)
101fc1e8551SJonas Devlieghere return StructuredData::ObjectSP();
102fc1e8551SJonas Devlieghere
103*0542d152SKazu Hirata std::string apiName;
104fc1e8551SJonas Devlieghere Status read_error;
105fc1e8551SJonas Devlieghere target.ReadCStringFromMemory(apiname_ptr, apiName, read_error);
106fc1e8551SJonas Devlieghere if (read_error.Fail())
107fc1e8551SJonas Devlieghere return StructuredData::ObjectSP();
108fc1e8551SJonas Devlieghere
109*0542d152SKazu Hirata std::string className;
110*0542d152SKazu Hirata std::string selector;
111fc1e8551SJonas Devlieghere if (apiName.substr(0, 2) == "-[") {
112869f8363SJordan Rupprecht size_t spacePos = apiName.find(' ');
113fc1e8551SJonas Devlieghere if (spacePos != std::string::npos) {
114fc1e8551SJonas Devlieghere className = apiName.substr(2, spacePos - 2);
115fc1e8551SJonas Devlieghere selector = apiName.substr(spacePos + 1, apiName.length() - spacePos - 2);
116fc1e8551SJonas Devlieghere }
117fc1e8551SJonas Devlieghere }
118fc1e8551SJonas Devlieghere
119fc1e8551SJonas Devlieghere // Gather the PCs of the user frames in the backtrace.
120fc1e8551SJonas Devlieghere StructuredData::Array *trace = new StructuredData::Array();
121fc1e8551SJonas Devlieghere auto trace_sp = StructuredData::ObjectSP(trace);
122fc1e8551SJonas Devlieghere StackFrameSP responsible_frame;
123fc1e8551SJonas Devlieghere for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
124fc1e8551SJonas Devlieghere StackFrameSP frame = thread_sp->GetStackFrameAtIndex(I);
12591e90cf6SFred Riss Address addr = frame->GetFrameCodeAddressForSymbolication();
126fc1e8551SJonas Devlieghere if (addr.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
127fc1e8551SJonas Devlieghere continue;
128fc1e8551SJonas Devlieghere
129fc1e8551SJonas Devlieghere // The first non-runtime frame is responsible for the bug.
130fc1e8551SJonas Devlieghere if (!responsible_frame)
131fc1e8551SJonas Devlieghere responsible_frame = frame;
132fc1e8551SJonas Devlieghere
133fc1e8551SJonas Devlieghere lldb::addr_t PC = addr.GetLoadAddress(&target);
134fc1e8551SJonas Devlieghere trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC)));
135fc1e8551SJonas Devlieghere }
136fc1e8551SJonas Devlieghere
137fc1e8551SJonas Devlieghere auto *d = new StructuredData::Dictionary();
138fc1e8551SJonas Devlieghere auto dict_sp = StructuredData::ObjectSP(d);
139fc1e8551SJonas Devlieghere d->AddStringItem("instrumentation_class", "MainThreadChecker");
140fc1e8551SJonas Devlieghere d->AddStringItem("api_name", apiName);
141fc1e8551SJonas Devlieghere d->AddStringItem("class_name", className);
142fc1e8551SJonas Devlieghere d->AddStringItem("selector", selector);
143fc1e8551SJonas Devlieghere d->AddStringItem("description",
144fc1e8551SJonas Devlieghere apiName + " must be used from main thread only");
145fc1e8551SJonas Devlieghere d->AddIntegerItem("tid", thread_sp->GetIndexID());
146fc1e8551SJonas Devlieghere d->AddItem("trace", trace_sp);
147fc1e8551SJonas Devlieghere return dict_sp;
148fc1e8551SJonas Devlieghere }
149fc1e8551SJonas Devlieghere
NotifyBreakpointHit(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)150fc1e8551SJonas Devlieghere bool InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit(
151fc1e8551SJonas Devlieghere void *baton, StoppointCallbackContext *context, user_id_t break_id,
152fc1e8551SJonas Devlieghere user_id_t break_loc_id) {
153fc1e8551SJonas Devlieghere assert(baton && "null baton");
154fc1e8551SJonas Devlieghere if (!baton)
155fc1e8551SJonas Devlieghere return false; ///< false => resume execution.
156fc1e8551SJonas Devlieghere
157fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker *const instance =
158fc1e8551SJonas Devlieghere static_cast<InstrumentationRuntimeMainThreadChecker *>(baton);
159fc1e8551SJonas Devlieghere
160fc1e8551SJonas Devlieghere ProcessSP process_sp = instance->GetProcessSP();
161fc1e8551SJonas Devlieghere ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
162fc1e8551SJonas Devlieghere if (!process_sp || !thread_sp ||
163fc1e8551SJonas Devlieghere process_sp != context->exe_ctx_ref.GetProcessSP())
164fc1e8551SJonas Devlieghere return false;
165fc1e8551SJonas Devlieghere
166fc1e8551SJonas Devlieghere if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
167fc1e8551SJonas Devlieghere return false;
168fc1e8551SJonas Devlieghere
169fc1e8551SJonas Devlieghere StructuredData::ObjectSP report =
170fc1e8551SJonas Devlieghere instance->RetrieveReportData(context->exe_ctx_ref);
171fc1e8551SJonas Devlieghere
172fc1e8551SJonas Devlieghere if (report) {
173adcd0268SBenjamin Kramer std::string description = std::string(report->GetAsDictionary()
174fc1e8551SJonas Devlieghere ->GetValueForKey("description")
175fc1e8551SJonas Devlieghere ->GetAsString()
176adcd0268SBenjamin Kramer ->GetValue());
177fc1e8551SJonas Devlieghere thread_sp->SetStopInfo(
178fc1e8551SJonas Devlieghere InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
179fc1e8551SJonas Devlieghere *thread_sp, description, report));
180fc1e8551SJonas Devlieghere return true;
181fc1e8551SJonas Devlieghere }
182fc1e8551SJonas Devlieghere
183fc1e8551SJonas Devlieghere return false;
184fc1e8551SJonas Devlieghere }
185fc1e8551SJonas Devlieghere
Activate()186fc1e8551SJonas Devlieghere void InstrumentationRuntimeMainThreadChecker::Activate() {
187fc1e8551SJonas Devlieghere if (IsActive())
188fc1e8551SJonas Devlieghere return;
189fc1e8551SJonas Devlieghere
190fc1e8551SJonas Devlieghere ProcessSP process_sp = GetProcessSP();
191fc1e8551SJonas Devlieghere if (!process_sp)
192fc1e8551SJonas Devlieghere return;
193fc1e8551SJonas Devlieghere
194fc1e8551SJonas Devlieghere ModuleSP runtime_module_sp = GetRuntimeModuleSP();
195fc1e8551SJonas Devlieghere
196fc1e8551SJonas Devlieghere ConstString symbol_name("__main_thread_checker_on_report");
197fc1e8551SJonas Devlieghere const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
198fc1e8551SJonas Devlieghere symbol_name, eSymbolTypeCode);
199fc1e8551SJonas Devlieghere
200fc1e8551SJonas Devlieghere if (symbol == nullptr)
201fc1e8551SJonas Devlieghere return;
202fc1e8551SJonas Devlieghere
203fc1e8551SJonas Devlieghere if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
204fc1e8551SJonas Devlieghere return;
205fc1e8551SJonas Devlieghere
206fc1e8551SJonas Devlieghere Target &target = process_sp->GetTarget();
207fc1e8551SJonas Devlieghere addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
208fc1e8551SJonas Devlieghere
209fc1e8551SJonas Devlieghere if (symbol_address == LLDB_INVALID_ADDRESS)
210fc1e8551SJonas Devlieghere return;
211fc1e8551SJonas Devlieghere
212fc1e8551SJonas Devlieghere Breakpoint *breakpoint =
213fc1e8551SJonas Devlieghere process_sp->GetTarget()
214fc1e8551SJonas Devlieghere .CreateBreakpoint(symbol_address, /*internal=*/true,
215fc1e8551SJonas Devlieghere /*hardware=*/false)
216fc1e8551SJonas Devlieghere .get();
217fc1e8551SJonas Devlieghere breakpoint->SetCallback(
218fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit, this, true);
219fc1e8551SJonas Devlieghere breakpoint->SetBreakpointKind("main-thread-checker-report");
220fc1e8551SJonas Devlieghere SetBreakpointID(breakpoint->GetID());
221fc1e8551SJonas Devlieghere
222fc1e8551SJonas Devlieghere SetActive(true);
223fc1e8551SJonas Devlieghere }
224fc1e8551SJonas Devlieghere
Deactivate()225fc1e8551SJonas Devlieghere void InstrumentationRuntimeMainThreadChecker::Deactivate() {
226fc1e8551SJonas Devlieghere SetActive(false);
227fc1e8551SJonas Devlieghere
228fc1e8551SJonas Devlieghere auto BID = GetBreakpointID();
229fc1e8551SJonas Devlieghere if (BID == LLDB_INVALID_BREAK_ID)
230fc1e8551SJonas Devlieghere return;
231fc1e8551SJonas Devlieghere
232fc1e8551SJonas Devlieghere if (ProcessSP process_sp = GetProcessSP()) {
233fc1e8551SJonas Devlieghere process_sp->GetTarget().RemoveBreakpointByID(BID);
234fc1e8551SJonas Devlieghere SetBreakpointID(LLDB_INVALID_BREAK_ID);
235fc1e8551SJonas Devlieghere }
236fc1e8551SJonas Devlieghere }
237fc1e8551SJonas Devlieghere
238fc1e8551SJonas Devlieghere lldb::ThreadCollectionSP
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)239fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::GetBacktracesFromExtendedStopInfo(
240fc1e8551SJonas Devlieghere StructuredData::ObjectSP info) {
241fc1e8551SJonas Devlieghere ThreadCollectionSP threads;
242fc1e8551SJonas Devlieghere threads = std::make_shared<ThreadCollection>();
243fc1e8551SJonas Devlieghere
244fc1e8551SJonas Devlieghere ProcessSP process_sp = GetProcessSP();
245fc1e8551SJonas Devlieghere
246fc1e8551SJonas Devlieghere if (info->GetObjectForDotSeparatedPath("instrumentation_class")
247fc1e8551SJonas Devlieghere ->GetStringValue() != "MainThreadChecker")
248fc1e8551SJonas Devlieghere return threads;
249fc1e8551SJonas Devlieghere
250fc1e8551SJonas Devlieghere std::vector<lldb::addr_t> PCs;
251fc1e8551SJonas Devlieghere auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
252fc1e8551SJonas Devlieghere trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
253fc1e8551SJonas Devlieghere PCs.push_back(PC->GetAsInteger()->GetValue());
254fc1e8551SJonas Devlieghere return true;
255fc1e8551SJonas Devlieghere });
256fc1e8551SJonas Devlieghere
257fc1e8551SJonas Devlieghere if (PCs.empty())
258fc1e8551SJonas Devlieghere return threads;
259fc1e8551SJonas Devlieghere
260fc1e8551SJonas Devlieghere StructuredData::ObjectSP thread_id_obj =
261fc1e8551SJonas Devlieghere info->GetObjectForDotSeparatedPath("tid");
262fc1e8551SJonas Devlieghere tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
263fc1e8551SJonas Devlieghere
26491e90cf6SFred Riss // We gather symbolication addresses above, so no need for HistoryThread to
26591e90cf6SFred Riss // try to infer the call addresses.
26691e90cf6SFred Riss bool pcs_are_call_addresses = true;
26791e90cf6SFred Riss ThreadSP new_thread_sp = std::make_shared<HistoryThread>(
26891e90cf6SFred Riss *process_sp, tid, PCs, pcs_are_call_addresses);
269fc1e8551SJonas Devlieghere
270fc1e8551SJonas Devlieghere // Save this in the Process' ExtendedThreadList so a strong pointer retains
271fc1e8551SJonas Devlieghere // the object
272fc1e8551SJonas Devlieghere process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
273fc1e8551SJonas Devlieghere threads->AddThread(new_thread_sp);
274fc1e8551SJonas Devlieghere
275fc1e8551SJonas Devlieghere return threads;
276fc1e8551SJonas Devlieghere }
277