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