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