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 
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
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 
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 
53fc1e8551SJonas Devlieghere void InstrumentationRuntimeMainThreadChecker::Terminate() {
54fc1e8551SJonas Devlieghere   PluginManager::UnregisterPlugin(CreateInstance);
55fc1e8551SJonas Devlieghere }
56fc1e8551SJonas Devlieghere 
57fc1e8551SJonas Devlieghere lldb::InstrumentationRuntimeType
58fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::GetTypeStatic() {
59fc1e8551SJonas Devlieghere   return eInstrumentationRuntimeTypeMainThreadChecker;
60fc1e8551SJonas Devlieghere }
61fc1e8551SJonas Devlieghere 
62fc1e8551SJonas Devlieghere const RegularExpression &
63fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::GetPatternForRuntimeLibrary() {
64fc1e8551SJonas Devlieghere   static RegularExpression regex(llvm::StringRef("libMainThreadChecker.dylib"));
65fc1e8551SJonas Devlieghere   return regex;
66fc1e8551SJonas Devlieghere }
67fc1e8551SJonas Devlieghere 
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
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();
84076341d1SJim Ingham   StackFrameSP frame_sp =
85076341d1SJim Ingham       thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
86fc1e8551SJonas Devlieghere   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
87fc1e8551SJonas Devlieghere   Target &target = process_sp->GetTarget();
88fc1e8551SJonas Devlieghere 
89fc1e8551SJonas Devlieghere   if (!frame_sp)
90fc1e8551SJonas Devlieghere     return StructuredData::ObjectSP();
91fc1e8551SJonas Devlieghere 
92fc1e8551SJonas Devlieghere   RegisterContextSP regctx_sp = frame_sp->GetRegisterContext();
93fc1e8551SJonas Devlieghere   if (!regctx_sp)
94fc1e8551SJonas Devlieghere     return StructuredData::ObjectSP();
95fc1e8551SJonas Devlieghere 
96fc1e8551SJonas Devlieghere   const RegisterInfo *reginfo = regctx_sp->GetRegisterInfoByName("arg1");
97fc1e8551SJonas Devlieghere   if (!reginfo)
98fc1e8551SJonas Devlieghere     return StructuredData::ObjectSP();
99fc1e8551SJonas Devlieghere 
100fc1e8551SJonas Devlieghere   uint64_t apiname_ptr = regctx_sp->ReadRegisterAsUnsigned(reginfo, 0);
101fc1e8551SJonas Devlieghere   if (!apiname_ptr)
102fc1e8551SJonas Devlieghere     return StructuredData::ObjectSP();
103fc1e8551SJonas Devlieghere 
1040542d152SKazu Hirata   std::string apiName;
105fc1e8551SJonas Devlieghere   Status read_error;
106fc1e8551SJonas Devlieghere   target.ReadCStringFromMemory(apiname_ptr, apiName, read_error);
107fc1e8551SJonas Devlieghere   if (read_error.Fail())
108fc1e8551SJonas Devlieghere     return StructuredData::ObjectSP();
109fc1e8551SJonas Devlieghere 
1100542d152SKazu Hirata   std::string className;
1110542d152SKazu Hirata   std::string selector;
112fc1e8551SJonas Devlieghere   if (apiName.substr(0, 2) == "-[") {
113869f8363SJordan Rupprecht     size_t spacePos = apiName.find(' ');
114fc1e8551SJonas Devlieghere     if (spacePos != std::string::npos) {
115fc1e8551SJonas Devlieghere       className = apiName.substr(2, spacePos - 2);
116fc1e8551SJonas Devlieghere       selector = apiName.substr(spacePos + 1, apiName.length() - spacePos - 2);
117fc1e8551SJonas Devlieghere     }
118fc1e8551SJonas Devlieghere   }
119fc1e8551SJonas Devlieghere 
120fc1e8551SJonas Devlieghere   // Gather the PCs of the user frames in the backtrace.
121fc1e8551SJonas Devlieghere   StructuredData::Array *trace = new StructuredData::Array();
122fc1e8551SJonas Devlieghere   auto trace_sp = StructuredData::ObjectSP(trace);
123fc1e8551SJonas Devlieghere   StackFrameSP responsible_frame;
124fc1e8551SJonas Devlieghere   for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
125fc1e8551SJonas Devlieghere     StackFrameSP frame = thread_sp->GetStackFrameAtIndex(I);
12691e90cf6SFred Riss     Address addr = frame->GetFrameCodeAddressForSymbolication();
127fc1e8551SJonas Devlieghere     if (addr.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
128fc1e8551SJonas Devlieghere       continue;
129fc1e8551SJonas Devlieghere 
130fc1e8551SJonas Devlieghere     // The first non-runtime frame is responsible for the bug.
131fc1e8551SJonas Devlieghere     if (!responsible_frame)
132fc1e8551SJonas Devlieghere       responsible_frame = frame;
133fc1e8551SJonas Devlieghere 
134fc1e8551SJonas Devlieghere     lldb::addr_t PC = addr.GetLoadAddress(&target);
1351370a1cbSMed Ismail Bennani     trace->AddIntegerItem(PC);
136fc1e8551SJonas Devlieghere   }
137fc1e8551SJonas Devlieghere 
138fc1e8551SJonas Devlieghere   auto *d = new StructuredData::Dictionary();
139fc1e8551SJonas Devlieghere   auto dict_sp = StructuredData::ObjectSP(d);
140fc1e8551SJonas Devlieghere   d->AddStringItem("instrumentation_class", "MainThreadChecker");
141fc1e8551SJonas Devlieghere   d->AddStringItem("api_name", apiName);
142fc1e8551SJonas Devlieghere   d->AddStringItem("class_name", className);
143fc1e8551SJonas Devlieghere   d->AddStringItem("selector", selector);
144fc1e8551SJonas Devlieghere   d->AddStringItem("description",
145fc1e8551SJonas Devlieghere                    apiName + " must be used from main thread only");
146fc1e8551SJonas Devlieghere   d->AddIntegerItem("tid", thread_sp->GetIndexID());
147fc1e8551SJonas Devlieghere   d->AddItem("trace", trace_sp);
148fc1e8551SJonas Devlieghere   return dict_sp;
149fc1e8551SJonas Devlieghere }
150fc1e8551SJonas Devlieghere 
151fc1e8551SJonas Devlieghere bool InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit(
152fc1e8551SJonas Devlieghere     void *baton, StoppointCallbackContext *context, user_id_t break_id,
153fc1e8551SJonas Devlieghere     user_id_t break_loc_id) {
154fc1e8551SJonas Devlieghere   assert(baton && "null baton");
155fc1e8551SJonas Devlieghere   if (!baton)
156fc1e8551SJonas Devlieghere     return false; ///< false => resume execution.
157fc1e8551SJonas Devlieghere 
158fc1e8551SJonas Devlieghere   InstrumentationRuntimeMainThreadChecker *const instance =
159fc1e8551SJonas Devlieghere       static_cast<InstrumentationRuntimeMainThreadChecker *>(baton);
160fc1e8551SJonas Devlieghere 
161fc1e8551SJonas Devlieghere   ProcessSP process_sp = instance->GetProcessSP();
162fc1e8551SJonas Devlieghere   ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
163fc1e8551SJonas Devlieghere   if (!process_sp || !thread_sp ||
164fc1e8551SJonas Devlieghere       process_sp != context->exe_ctx_ref.GetProcessSP())
165fc1e8551SJonas Devlieghere     return false;
166fc1e8551SJonas Devlieghere 
167fc1e8551SJonas Devlieghere   if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
168fc1e8551SJonas Devlieghere     return false;
169fc1e8551SJonas Devlieghere 
170fc1e8551SJonas Devlieghere   StructuredData::ObjectSP report =
171fc1e8551SJonas Devlieghere       instance->RetrieveReportData(context->exe_ctx_ref);
172fc1e8551SJonas Devlieghere 
173fc1e8551SJonas Devlieghere   if (report) {
174adcd0268SBenjamin Kramer     std::string description = std::string(report->GetAsDictionary()
175fc1e8551SJonas Devlieghere                                               ->GetValueForKey("description")
176fc1e8551SJonas Devlieghere                                               ->GetAsString()
177adcd0268SBenjamin Kramer                                               ->GetValue());
178fc1e8551SJonas Devlieghere     thread_sp->SetStopInfo(
179fc1e8551SJonas Devlieghere         InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
180fc1e8551SJonas Devlieghere             *thread_sp, description, report));
181fc1e8551SJonas Devlieghere     return true;
182fc1e8551SJonas Devlieghere   }
183fc1e8551SJonas Devlieghere 
184fc1e8551SJonas Devlieghere   return false;
185fc1e8551SJonas Devlieghere }
186fc1e8551SJonas Devlieghere 
187fc1e8551SJonas Devlieghere void InstrumentationRuntimeMainThreadChecker::Activate() {
188fc1e8551SJonas Devlieghere   if (IsActive())
189fc1e8551SJonas Devlieghere     return;
190fc1e8551SJonas Devlieghere 
191fc1e8551SJonas Devlieghere   ProcessSP process_sp = GetProcessSP();
192fc1e8551SJonas Devlieghere   if (!process_sp)
193fc1e8551SJonas Devlieghere     return;
194fc1e8551SJonas Devlieghere 
195fc1e8551SJonas Devlieghere   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
196fc1e8551SJonas Devlieghere 
197fc1e8551SJonas Devlieghere   ConstString symbol_name("__main_thread_checker_on_report");
198fc1e8551SJonas Devlieghere   const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
199fc1e8551SJonas Devlieghere       symbol_name, eSymbolTypeCode);
200fc1e8551SJonas Devlieghere 
201fc1e8551SJonas Devlieghere   if (symbol == nullptr)
202fc1e8551SJonas Devlieghere     return;
203fc1e8551SJonas Devlieghere 
204fc1e8551SJonas Devlieghere   if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
205fc1e8551SJonas Devlieghere     return;
206fc1e8551SJonas Devlieghere 
207fc1e8551SJonas Devlieghere   Target &target = process_sp->GetTarget();
208fc1e8551SJonas Devlieghere   addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
209fc1e8551SJonas Devlieghere 
210fc1e8551SJonas Devlieghere   if (symbol_address == LLDB_INVALID_ADDRESS)
211fc1e8551SJonas Devlieghere     return;
212fc1e8551SJonas Devlieghere 
213fc1e8551SJonas Devlieghere   Breakpoint *breakpoint =
214fc1e8551SJonas Devlieghere       process_sp->GetTarget()
215fc1e8551SJonas Devlieghere           .CreateBreakpoint(symbol_address, /*internal=*/true,
216fc1e8551SJonas Devlieghere                             /*hardware=*/false)
217fc1e8551SJonas Devlieghere           .get();
218852a4bdbSJim Ingham   const bool sync = false;
219fc1e8551SJonas Devlieghere   breakpoint->SetCallback(
220852a4bdbSJim Ingham       InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit, this, sync);
221fc1e8551SJonas Devlieghere   breakpoint->SetBreakpointKind("main-thread-checker-report");
222fc1e8551SJonas Devlieghere   SetBreakpointID(breakpoint->GetID());
223fc1e8551SJonas Devlieghere 
224fc1e8551SJonas Devlieghere   SetActive(true);
225fc1e8551SJonas Devlieghere }
226fc1e8551SJonas Devlieghere 
227fc1e8551SJonas Devlieghere void InstrumentationRuntimeMainThreadChecker::Deactivate() {
228fc1e8551SJonas Devlieghere   SetActive(false);
229fc1e8551SJonas Devlieghere 
230fc1e8551SJonas Devlieghere   auto BID = GetBreakpointID();
231fc1e8551SJonas Devlieghere   if (BID == LLDB_INVALID_BREAK_ID)
232fc1e8551SJonas Devlieghere     return;
233fc1e8551SJonas Devlieghere 
234fc1e8551SJonas Devlieghere   if (ProcessSP process_sp = GetProcessSP()) {
235fc1e8551SJonas Devlieghere     process_sp->GetTarget().RemoveBreakpointByID(BID);
236fc1e8551SJonas Devlieghere     SetBreakpointID(LLDB_INVALID_BREAK_ID);
237fc1e8551SJonas Devlieghere   }
238fc1e8551SJonas Devlieghere }
239fc1e8551SJonas Devlieghere 
240fc1e8551SJonas Devlieghere lldb::ThreadCollectionSP
241fc1e8551SJonas Devlieghere InstrumentationRuntimeMainThreadChecker::GetBacktracesFromExtendedStopInfo(
242fc1e8551SJonas Devlieghere     StructuredData::ObjectSP info) {
243fc1e8551SJonas Devlieghere   ThreadCollectionSP threads;
244fc1e8551SJonas Devlieghere   threads = std::make_shared<ThreadCollection>();
245fc1e8551SJonas Devlieghere 
246fc1e8551SJonas Devlieghere   ProcessSP process_sp = GetProcessSP();
247fc1e8551SJonas Devlieghere 
248fc1e8551SJonas Devlieghere   if (info->GetObjectForDotSeparatedPath("instrumentation_class")
249fc1e8551SJonas Devlieghere           ->GetStringValue() != "MainThreadChecker")
250fc1e8551SJonas Devlieghere     return threads;
251fc1e8551SJonas Devlieghere 
252fc1e8551SJonas Devlieghere   std::vector<lldb::addr_t> PCs;
253fc1e8551SJonas Devlieghere   auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
254fc1e8551SJonas Devlieghere   trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
2551370a1cbSMed Ismail Bennani     PCs.push_back(PC->GetUnsignedIntegerValue());
256fc1e8551SJonas Devlieghere     return true;
257fc1e8551SJonas Devlieghere   });
258fc1e8551SJonas Devlieghere 
259fc1e8551SJonas Devlieghere   if (PCs.empty())
260fc1e8551SJonas Devlieghere     return threads;
261fc1e8551SJonas Devlieghere 
262fc1e8551SJonas Devlieghere   StructuredData::ObjectSP thread_id_obj =
263fc1e8551SJonas Devlieghere       info->GetObjectForDotSeparatedPath("tid");
264*b804516dSDhruv Srivastava   lldb::tid_t tid =
265*b804516dSDhruv Srivastava       thread_id_obj ? thread_id_obj->GetUnsignedIntegerValue() : 0;
266fc1e8551SJonas Devlieghere 
26791e90cf6SFred Riss   // We gather symbolication addresses above, so no need for HistoryThread to
26891e90cf6SFred Riss   // try to infer the call addresses.
26991e90cf6SFred Riss   bool pcs_are_call_addresses = true;
27091e90cf6SFred Riss   ThreadSP new_thread_sp = std::make_shared<HistoryThread>(
27191e90cf6SFred Riss       *process_sp, tid, PCs, pcs_are_call_addresses);
272fc1e8551SJonas Devlieghere 
273fc1e8551SJonas Devlieghere   // Save this in the Process' ExtendedThreadList so a strong pointer retains
274fc1e8551SJonas Devlieghere   // the object
275fc1e8551SJonas Devlieghere   process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
276fc1e8551SJonas Devlieghere   threads->AddThread(new_thread_sp);
277fc1e8551SJonas Devlieghere 
278fc1e8551SJonas Devlieghere   return threads;
279fc1e8551SJonas Devlieghere }
280