/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/SystemRuntime/MacOSX/AppleGetQueuesHandler.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- AppleGetQueuesHandler.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 "AppleGetQueuesHandler.h" |
10 | | |
11 | | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
12 | | #include "lldb/Core/Module.h" |
13 | | #include "lldb/Core/Value.h" |
14 | | #include "lldb/Expression/DiagnosticManager.h" |
15 | | #include "lldb/Expression/FunctionCaller.h" |
16 | | #include "lldb/Expression/UtilityFunction.h" |
17 | | #include "lldb/Symbol/Symbol.h" |
18 | | #include "lldb/Target/ExecutionContext.h" |
19 | | #include "lldb/Target/Process.h" |
20 | | #include "lldb/Target/Target.h" |
21 | | #include "lldb/Target/Thread.h" |
22 | | #include "lldb/Utility/ConstString.h" |
23 | | #include "lldb/Utility/LLDBLog.h" |
24 | | #include "lldb/Utility/Log.h" |
25 | | #include "lldb/Utility/StreamString.h" |
26 | | |
27 | | using namespace lldb; |
28 | | using namespace lldb_private; |
29 | | |
30 | | const char *AppleGetQueuesHandler::g_get_current_queues_function_name = |
31 | | "__lldb_backtrace_recording_get_current_queues"; |
32 | | const char *AppleGetQueuesHandler::g_get_current_queues_function_code = |
33 | | " \n\ |
34 | | extern \"C\" \n\ |
35 | | { \n\ |
36 | | /* \n\ |
37 | | * mach defines \n\ |
38 | | */ \n\ |
39 | | \n\ |
40 | | typedef unsigned int uint32_t; \n\ |
41 | | typedef unsigned long long uint64_t; \n\ |
42 | | typedef uint32_t mach_port_t; \n\ |
43 | | typedef mach_port_t vm_map_t; \n\ |
44 | | typedef int kern_return_t; \n\ |
45 | | typedef uint64_t mach_vm_address_t; \n\ |
46 | | typedef uint64_t mach_vm_size_t; \n\ |
47 | | \n\ |
48 | | mach_port_t mach_task_self (); \n\ |
49 | | kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\ |
50 | | \n\ |
51 | | /* \n\ |
52 | | * libBacktraceRecording defines \n\ |
53 | | */ \n\ |
54 | | \n\ |
55 | | typedef uint32_t queue_list_scope_t; \n\ |
56 | | typedef void *introspection_dispatch_queue_info_t; \n\ |
57 | | \n\ |
58 | | extern uint64_t __introspection_dispatch_get_queues (queue_list_scope_t scope, \n\ |
59 | | introspection_dispatch_queue_info_t *returned_queues_buffer, \n\ |
60 | | uint64_t *returned_queues_buffer_size); \n\ |
61 | | extern int printf(const char *format, ...); \n\ |
62 | | \n\ |
63 | | /* \n\ |
64 | | * return type define \n\ |
65 | | */ \n\ |
66 | | \n\ |
67 | | struct get_current_queues_return_values \n\ |
68 | | { \n\ |
69 | | uint64_t queues_buffer_ptr; /* the address of the queues buffer from libBacktraceRecording */ \n\ |
70 | | uint64_t queues_buffer_size; /* the size of the queues buffer from libBacktraceRecording */ \n\ |
71 | | uint64_t count; /* the number of queues included in the queues buffer */ \n\ |
72 | | }; \n\ |
73 | | \n\ |
74 | | void __lldb_backtrace_recording_get_current_queues \n\ |
75 | | (struct get_current_queues_return_values *return_buffer, \n\ |
76 | | int debug, \n\ |
77 | | void *page_to_free, \n\ |
78 | | uint64_t page_to_free_size) \n\ |
79 | | { \n\ |
80 | | if (debug) \n\ |
81 | | printf (\"entering get_current_queues with args %p, %d, 0x%p, 0x%llx\\n\", return_buffer, debug, page_to_free, page_to_free_size); \n\ |
82 | | if (page_to_free != 0) \n\ |
83 | | { \n\ |
84 | | mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\ |
85 | | } \n\ |
86 | | \n\ |
87 | | return_buffer->count = __introspection_dispatch_get_queues ( \n\ |
88 | | /* QUEUES_WITH_ANY_ITEMS */ 2, \n\ |
89 | | (void**)&return_buffer->queues_buffer_ptr, \n\ |
90 | | &return_buffer->queues_buffer_size); \n\ |
91 | | if (debug) \n\ |
92 | | printf(\"result was count %lld\\n\", return_buffer->count); \n\ |
93 | | } \n\ |
94 | | } \n\ |
95 | | "; |
96 | | |
97 | | AppleGetQueuesHandler::AppleGetQueuesHandler(Process *process) |
98 | 2.14k | : m_process(process), m_get_queues_impl_code_up(), |
99 | 2.14k | m_get_queues_function_mutex(), |
100 | | m_get_queues_return_buffer_addr(LLDB_INVALID_ADDRESS), |
101 | 2.14k | m_get_queues_retbuffer_mutex() {} |
102 | | |
103 | 2.14k | AppleGetQueuesHandler::~AppleGetQueuesHandler() = default; |
104 | | |
105 | 0 | void AppleGetQueuesHandler::Detach() { |
106 | |
|
107 | 0 | if (m_process && m_process->IsAlive() && |
108 | 0 | m_get_queues_return_buffer_addr != LLDB_INVALID_ADDRESS) { |
109 | 0 | std::unique_lock<std::mutex> lock(m_get_queues_retbuffer_mutex, |
110 | 0 | std::defer_lock); |
111 | 0 | (void)lock.try_lock(); // Even if we don't get the lock, deallocate the buffer |
112 | 0 | m_process->DeallocateMemory(m_get_queues_return_buffer_addr); |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | // Construct a CompilerType for the structure that |
117 | | // g_get_current_queues_function_code will return by value so we can extract |
118 | | // the fields after performing the function call. i.e. we are getting this |
119 | | // struct returned to us: |
120 | | // |
121 | | // struct get_current_queues_return_values |
122 | | // { |
123 | | // introspection_dispatch_queue_info_t *queues_buffer; |
124 | | // uint64_t queues_buffer_size; |
125 | | // uint64_t count; |
126 | | // }; |
127 | | |
128 | | // Compile our __lldb_backtrace_recording_get_current_queues() function (from |
129 | | // the source above in g_get_current_queues_function_code) if we don't find |
130 | | // that function in the inferior already with USE_BUILTIN_FUNCTION defined. |
131 | | // (e.g. this would be the case for testing.) |
132 | | // |
133 | | // Insert the __lldb_backtrace_recording_get_current_queues into the inferior |
134 | | // process if needed. |
135 | | // |
136 | | // Write the get_queues_arglist into the inferior's memory space to prepare for |
137 | | // the call. |
138 | | // |
139 | | // Returns the address of the arguments written down in the inferior process, |
140 | | // which can be used to make the function call. |
141 | | |
142 | | lldb::addr_t |
143 | | AppleGetQueuesHandler::SetupGetQueuesFunction(Thread &thread, |
144 | 0 | ValueList &get_queues_arglist) { |
145 | 0 | ThreadSP thread_sp(thread.shared_from_this()); |
146 | 0 | ExecutionContext exe_ctx(thread_sp); |
147 | |
|
148 | 0 | Address impl_code_address; |
149 | 0 | DiagnosticManager diagnostics; |
150 | 0 | Log *log = GetLog(LLDBLog::SystemRuntime); |
151 | 0 | lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; |
152 | |
|
153 | 0 | FunctionCaller *get_queues_caller = nullptr; |
154 | | |
155 | | // Scope for mutex locker: |
156 | 0 | { |
157 | 0 | std::lock_guard<std::mutex> guard(m_get_queues_function_mutex); |
158 | | |
159 | | // First stage is to make the ClangUtility to hold our injected function: |
160 | |
|
161 | 0 | if (!m_get_queues_impl_code_up) { |
162 | 0 | if (g_get_current_queues_function_code != nullptr) { |
163 | 0 | auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction( |
164 | 0 | g_get_current_queues_function_code, |
165 | 0 | g_get_current_queues_function_name, eLanguageTypeC, exe_ctx); |
166 | 0 | if (!utility_fn_or_error) { |
167 | 0 | LLDB_LOG_ERROR(log, utility_fn_or_error.takeError(), |
168 | 0 | "Failed to create UtilityFunction for queues " |
169 | 0 | "introspection: {0}."); |
170 | 0 | return args_addr; |
171 | 0 | } |
172 | 0 | m_get_queues_impl_code_up = std::move(*utility_fn_or_error); |
173 | 0 | } else { |
174 | 0 | if (log) { |
175 | 0 | LLDB_LOGF(log, "No queues introspection code found."); |
176 | 0 | diagnostics.Dump(log); |
177 | 0 | } |
178 | 0 | return LLDB_INVALID_ADDRESS; |
179 | 0 | } |
180 | 0 | } |
181 | | |
182 | | // Next make the runner function for our implementation utility function. |
183 | 0 | TypeSystemClangSP scratch_ts_sp = |
184 | 0 | ScratchTypeSystemClang::GetForTarget(thread.GetProcess()->GetTarget()); |
185 | 0 | CompilerType get_queues_return_type = |
186 | 0 | scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType(); |
187 | 0 | Status error; |
188 | 0 | get_queues_caller = m_get_queues_impl_code_up->MakeFunctionCaller( |
189 | 0 | get_queues_return_type, get_queues_arglist, thread_sp, error); |
190 | 0 | if (error.Fail() || get_queues_caller == nullptr) { |
191 | 0 | LLDB_LOGF(log, |
192 | 0 | "Could not get function caller for get-queues function: %s.", |
193 | 0 | error.AsCString()); |
194 | 0 | return args_addr; |
195 | 0 | } |
196 | 0 | } |
197 | | |
198 | 0 | diagnostics.Clear(); |
199 | | |
200 | | // Now write down the argument values for this particular call. This looks |
201 | | // like it might be a race condition if other threads were calling into here, |
202 | | // but actually it isn't because we allocate a new args structure for this |
203 | | // call by passing args_addr = LLDB_INVALID_ADDRESS... |
204 | |
|
205 | 0 | if (!get_queues_caller->WriteFunctionArguments( |
206 | 0 | exe_ctx, args_addr, get_queues_arglist, diagnostics)) { |
207 | 0 | if (log) { |
208 | 0 | LLDB_LOGF(log, "Error writing get-queues function arguments."); |
209 | 0 | diagnostics.Dump(log); |
210 | 0 | } |
211 | 0 | return args_addr; |
212 | 0 | } |
213 | | |
214 | 0 | return args_addr; |
215 | 0 | } |
216 | | |
217 | | AppleGetQueuesHandler::GetQueuesReturnInfo |
218 | | AppleGetQueuesHandler::GetCurrentQueues(Thread &thread, addr_t page_to_free, |
219 | | uint64_t page_to_free_size, |
220 | 0 | Status &error) { |
221 | 0 | lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); |
222 | 0 | ProcessSP process_sp(thread.CalculateProcess()); |
223 | 0 | TargetSP target_sp(thread.CalculateTarget()); |
224 | 0 | TypeSystemClangSP scratch_ts_sp = |
225 | 0 | ScratchTypeSystemClang::GetForTarget(*target_sp); |
226 | 0 | Log *log = GetLog(LLDBLog::SystemRuntime); |
227 | |
|
228 | 0 | GetQueuesReturnInfo return_value; |
229 | 0 | return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS; |
230 | 0 | return_value.queues_buffer_size = 0; |
231 | 0 | return_value.count = 0; |
232 | |
|
233 | 0 | error.Clear(); |
234 | |
|
235 | 0 | if (!thread.SafeToCallFunctions()) { |
236 | 0 | LLDB_LOGF(log, "Not safe to call functions on thread 0x%" PRIx64, |
237 | 0 | thread.GetID()); |
238 | 0 | error.SetErrorString("Not safe to call functions on this thread."); |
239 | 0 | return return_value; |
240 | 0 | } |
241 | | |
242 | | // Set up the arguments for a call to |
243 | | |
244 | | // struct get_current_queues_return_values |
245 | | // { |
246 | | // uint64_t queues_buffer_ptr; /* the address of the queues buffer from |
247 | | // libBacktraceRecording */ |
248 | | // uint64_t queues_buffer_size; /* the size of the queues buffer from |
249 | | // libBacktraceRecording */ |
250 | | // uint64_t count; /* the number of queues included in the |
251 | | // queues buffer */ |
252 | | // }; |
253 | | // |
254 | | // void |
255 | | // __lldb_backtrace_recording_get_current_queues |
256 | | // (struct |
257 | | // get_current_queues_return_values |
258 | | // *return_buffer, |
259 | | // void *page_to_free, |
260 | | // uint64_t page_to_free_size); |
261 | | |
262 | | // Where the return_buffer argument points to a 24 byte region of memory |
263 | | // already allocated by lldb in the inferior process. |
264 | | |
265 | 0 | CompilerType clang_void_ptr_type = |
266 | 0 | scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType(); |
267 | 0 | Value return_buffer_ptr_value; |
268 | 0 | return_buffer_ptr_value.SetValueType(Value::ValueType::Scalar); |
269 | 0 | return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type); |
270 | |
|
271 | 0 | CompilerType clang_int_type = scratch_ts_sp->GetBasicType(eBasicTypeInt); |
272 | 0 | Value debug_value; |
273 | 0 | debug_value.SetValueType(Value::ValueType::Scalar); |
274 | 0 | debug_value.SetCompilerType(clang_int_type); |
275 | |
|
276 | 0 | Value page_to_free_value; |
277 | 0 | page_to_free_value.SetValueType(Value::ValueType::Scalar); |
278 | 0 | page_to_free_value.SetCompilerType(clang_void_ptr_type); |
279 | |
|
280 | 0 | CompilerType clang_uint64_type = |
281 | 0 | scratch_ts_sp->GetBasicType(eBasicTypeUnsignedLongLong); |
282 | 0 | Value page_to_free_size_value; |
283 | 0 | page_to_free_size_value.SetValueType(Value::ValueType::Scalar); |
284 | 0 | page_to_free_size_value.SetCompilerType(clang_uint64_type); |
285 | |
|
286 | 0 | std::lock_guard<std::mutex> guard(m_get_queues_retbuffer_mutex); |
287 | 0 | if (m_get_queues_return_buffer_addr == LLDB_INVALID_ADDRESS) { |
288 | 0 | addr_t bufaddr = process_sp->AllocateMemory( |
289 | 0 | 32, ePermissionsReadable | ePermissionsWritable, error); |
290 | 0 | if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) { |
291 | 0 | LLDB_LOGF(log, "Failed to allocate memory for return buffer for get " |
292 | 0 | "current queues func call"); |
293 | 0 | return return_value; |
294 | 0 | } |
295 | 0 | m_get_queues_return_buffer_addr = bufaddr; |
296 | 0 | } |
297 | | |
298 | 0 | ValueList argument_values; |
299 | |
|
300 | 0 | return_buffer_ptr_value.GetScalar() = m_get_queues_return_buffer_addr; |
301 | 0 | argument_values.PushValue(return_buffer_ptr_value); |
302 | |
|
303 | 0 | debug_value.GetScalar() = 0; |
304 | 0 | argument_values.PushValue(debug_value); |
305 | |
|
306 | 0 | if (page_to_free != LLDB_INVALID_ADDRESS) |
307 | 0 | page_to_free_value.GetScalar() = page_to_free; |
308 | 0 | else |
309 | 0 | page_to_free_value.GetScalar() = 0; |
310 | 0 | argument_values.PushValue(page_to_free_value); |
311 | |
|
312 | 0 | page_to_free_size_value.GetScalar() = page_to_free_size; |
313 | 0 | argument_values.PushValue(page_to_free_size_value); |
314 | |
|
315 | 0 | addr_t args_addr = SetupGetQueuesFunction(thread, argument_values); |
316 | |
|
317 | 0 | if (!m_get_queues_impl_code_up) { |
318 | 0 | error.SetErrorString( |
319 | 0 | "Unable to compile __introspection_dispatch_get_queues."); |
320 | 0 | return return_value; |
321 | 0 | } |
322 | | |
323 | 0 | FunctionCaller *get_queues_caller = |
324 | 0 | m_get_queues_impl_code_up->GetFunctionCaller(); |
325 | |
|
326 | 0 | if (get_queues_caller == nullptr) { |
327 | 0 | error.SetErrorString( |
328 | 0 | "Unable to get caller for call __introspection_dispatch_get_queues"); |
329 | 0 | return return_value; |
330 | 0 | } |
331 | | |
332 | 0 | DiagnosticManager diagnostics; |
333 | 0 | ExecutionContext exe_ctx; |
334 | 0 | EvaluateExpressionOptions options; |
335 | 0 | options.SetUnwindOnError(true); |
336 | 0 | options.SetIgnoreBreakpoints(true); |
337 | 0 | options.SetStopOthers(true); |
338 | | #if __has_feature(address_sanitizer) |
339 | | options.SetTimeout(process_sp->GetUtilityExpressionTimeout()); |
340 | | #else |
341 | 0 | options.SetTimeout(std::chrono::milliseconds(500)); |
342 | 0 | #endif |
343 | 0 | options.SetTryAllThreads(false); |
344 | 0 | options.SetIsForUtilityExpr(true); |
345 | 0 | thread.CalculateExecutionContext(exe_ctx); |
346 | |
|
347 | 0 | ExpressionResults func_call_ret; |
348 | 0 | Value results; |
349 | 0 | func_call_ret = get_queues_caller->ExecuteFunction( |
350 | 0 | exe_ctx, &args_addr, options, diagnostics, results); |
351 | 0 | if (func_call_ret != eExpressionCompleted || !error.Success()) { |
352 | 0 | LLDB_LOGF(log, |
353 | 0 | "Unable to call introspection_get_dispatch_queues(), got " |
354 | 0 | "ExpressionResults %d, error contains %s", |
355 | 0 | func_call_ret, error.AsCString("")); |
356 | 0 | error.SetErrorString("Unable to call introspection_get_dispatch_queues() " |
357 | 0 | "for list of queues"); |
358 | 0 | return return_value; |
359 | 0 | } |
360 | | |
361 | 0 | return_value.queues_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory( |
362 | 0 | m_get_queues_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error); |
363 | 0 | if (!error.Success() || |
364 | 0 | return_value.queues_buffer_ptr == LLDB_INVALID_ADDRESS) { |
365 | 0 | return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS; |
366 | 0 | return return_value; |
367 | 0 | } |
368 | | |
369 | 0 | return_value.queues_buffer_size = m_process->ReadUnsignedIntegerFromMemory( |
370 | 0 | m_get_queues_return_buffer_addr + 8, 8, 0, error); |
371 | |
|
372 | 0 | if (!error.Success()) { |
373 | 0 | return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS; |
374 | 0 | return return_value; |
375 | 0 | } |
376 | | |
377 | 0 | return_value.count = m_process->ReadUnsignedIntegerFromMemory( |
378 | 0 | m_get_queues_return_buffer_addr + 16, 8, 0, error); |
379 | 0 | if (!error.Success()) { |
380 | 0 | return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS; |
381 | 0 | return return_value; |
382 | 0 | } |
383 | | |
384 | 0 | LLDB_LOGF(log, |
385 | 0 | "AppleGetQueuesHandler called " |
386 | 0 | "__introspection_dispatch_get_queues (page_to_free == " |
387 | 0 | "0x%" PRIx64 ", size = %" PRId64 "), returned page is at 0x%" PRIx64 |
388 | 0 | ", size %" PRId64 ", count = %" PRId64, |
389 | 0 | page_to_free, page_to_free_size, return_value.queues_buffer_ptr, |
390 | 0 | return_value.queues_buffer_size, return_value.count); |
391 | |
|
392 | 0 | return return_value; |
393 | 0 | } |