1dda28197Spatrick //===-- AppleGetQueuesHandler.cpp -----------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "AppleGetQueuesHandler.h"
10061da546Spatrick
11dda28197Spatrick #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12061da546Spatrick #include "lldb/Core/Module.h"
13061da546Spatrick #include "lldb/Core/Value.h"
14061da546Spatrick #include "lldb/Expression/DiagnosticManager.h"
15061da546Spatrick #include "lldb/Expression/FunctionCaller.h"
16061da546Spatrick #include "lldb/Expression/UtilityFunction.h"
17061da546Spatrick #include "lldb/Symbol/Symbol.h"
18061da546Spatrick #include "lldb/Target/ExecutionContext.h"
19061da546Spatrick #include "lldb/Target/Process.h"
20061da546Spatrick #include "lldb/Target/Target.h"
21061da546Spatrick #include "lldb/Target/Thread.h"
22061da546Spatrick #include "lldb/Utility/ConstString.h"
23*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
24061da546Spatrick #include "lldb/Utility/Log.h"
25061da546Spatrick #include "lldb/Utility/StreamString.h"
26061da546Spatrick
27061da546Spatrick using namespace lldb;
28061da546Spatrick using namespace lldb_private;
29061da546Spatrick
30061da546Spatrick const char *AppleGetQueuesHandler::g_get_current_queues_function_name =
31061da546Spatrick "__lldb_backtrace_recording_get_current_queues";
32061da546Spatrick const char *AppleGetQueuesHandler::g_get_current_queues_function_code =
33061da546Spatrick " \n\
34061da546Spatrick extern \"C\" \n\
35061da546Spatrick { \n\
36061da546Spatrick /* \n\
37061da546Spatrick * mach defines \n\
38061da546Spatrick */ \n\
39061da546Spatrick \n\
40061da546Spatrick typedef unsigned int uint32_t; \n\
41061da546Spatrick typedef unsigned long long uint64_t; \n\
42061da546Spatrick typedef uint32_t mach_port_t; \n\
43061da546Spatrick typedef mach_port_t vm_map_t; \n\
44061da546Spatrick typedef int kern_return_t; \n\
45061da546Spatrick typedef uint64_t mach_vm_address_t; \n\
46061da546Spatrick typedef uint64_t mach_vm_size_t; \n\
47061da546Spatrick \n\
48061da546Spatrick mach_port_t mach_task_self (); \n\
49061da546Spatrick kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
50061da546Spatrick \n\
51061da546Spatrick /* \n\
52061da546Spatrick * libBacktraceRecording defines \n\
53061da546Spatrick */ \n\
54061da546Spatrick \n\
55061da546Spatrick typedef uint32_t queue_list_scope_t; \n\
56061da546Spatrick typedef void *introspection_dispatch_queue_info_t; \n\
57061da546Spatrick \n\
58061da546Spatrick extern uint64_t __introspection_dispatch_get_queues (queue_list_scope_t scope, \n\
59061da546Spatrick introspection_dispatch_queue_info_t *returned_queues_buffer, \n\
60061da546Spatrick uint64_t *returned_queues_buffer_size); \n\
61061da546Spatrick extern int printf(const char *format, ...); \n\
62061da546Spatrick \n\
63061da546Spatrick /* \n\
64061da546Spatrick * return type define \n\
65061da546Spatrick */ \n\
66061da546Spatrick \n\
67061da546Spatrick struct get_current_queues_return_values \n\
68061da546Spatrick { \n\
69061da546Spatrick uint64_t queues_buffer_ptr; /* the address of the queues buffer from libBacktraceRecording */ \n\
70061da546Spatrick uint64_t queues_buffer_size; /* the size of the queues buffer from libBacktraceRecording */ \n\
71061da546Spatrick uint64_t count; /* the number of queues included in the queues buffer */ \n\
72061da546Spatrick }; \n\
73061da546Spatrick \n\
74061da546Spatrick void __lldb_backtrace_recording_get_current_queues \n\
75061da546Spatrick (struct get_current_queues_return_values *return_buffer, \n\
76061da546Spatrick int debug, \n\
77061da546Spatrick void *page_to_free, \n\
78061da546Spatrick uint64_t page_to_free_size) \n\
79061da546Spatrick { \n\
80061da546Spatrick if (debug) \n\
81061da546Spatrick 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\
82061da546Spatrick if (page_to_free != 0) \n\
83061da546Spatrick { \n\
84061da546Spatrick mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
85061da546Spatrick } \n\
86061da546Spatrick \n\
87061da546Spatrick return_buffer->count = __introspection_dispatch_get_queues ( \n\
88061da546Spatrick /* QUEUES_WITH_ANY_ITEMS */ 2, \n\
89061da546Spatrick (void**)&return_buffer->queues_buffer_ptr, \n\
90061da546Spatrick &return_buffer->queues_buffer_size); \n\
91061da546Spatrick if (debug) \n\
92061da546Spatrick printf(\"result was count %lld\\n\", return_buffer->count); \n\
93061da546Spatrick } \n\
94061da546Spatrick } \n\
95061da546Spatrick ";
96061da546Spatrick
AppleGetQueuesHandler(Process * process)97061da546Spatrick AppleGetQueuesHandler::AppleGetQueuesHandler(Process *process)
98061da546Spatrick : m_process(process), m_get_queues_impl_code_up(),
99061da546Spatrick m_get_queues_function_mutex(),
100061da546Spatrick m_get_queues_return_buffer_addr(LLDB_INVALID_ADDRESS),
101061da546Spatrick m_get_queues_retbuffer_mutex() {}
102061da546Spatrick
103be691f3bSpatrick AppleGetQueuesHandler::~AppleGetQueuesHandler() = default;
104061da546Spatrick
Detach()105061da546Spatrick void AppleGetQueuesHandler::Detach() {
106061da546Spatrick
107061da546Spatrick if (m_process && m_process->IsAlive() &&
108061da546Spatrick m_get_queues_return_buffer_addr != LLDB_INVALID_ADDRESS) {
109061da546Spatrick std::unique_lock<std::mutex> lock(m_get_queues_retbuffer_mutex,
110061da546Spatrick std::defer_lock);
111dda28197Spatrick (void)lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
112061da546Spatrick m_process->DeallocateMemory(m_get_queues_return_buffer_addr);
113061da546Spatrick }
114061da546Spatrick }
115061da546Spatrick
116061da546Spatrick // Construct a CompilerType for the structure that
117061da546Spatrick // g_get_current_queues_function_code will return by value so we can extract
118061da546Spatrick // the fields after performing the function call. i.e. we are getting this
119061da546Spatrick // struct returned to us:
120061da546Spatrick //
121061da546Spatrick // struct get_current_queues_return_values
122061da546Spatrick // {
123061da546Spatrick // introspection_dispatch_queue_info_t *queues_buffer;
124061da546Spatrick // uint64_t queues_buffer_size;
125061da546Spatrick // uint64_t count;
126061da546Spatrick // };
127061da546Spatrick
128061da546Spatrick // Compile our __lldb_backtrace_recording_get_current_queues() function (from
129061da546Spatrick // the source above in g_get_current_queues_function_code) if we don't find
130061da546Spatrick // that function in the inferior already with USE_BUILTIN_FUNCTION defined.
131061da546Spatrick // (e.g. this would be the case for testing.)
132061da546Spatrick //
133061da546Spatrick // Insert the __lldb_backtrace_recording_get_current_queues into the inferior
134061da546Spatrick // process if needed.
135061da546Spatrick //
136061da546Spatrick // Write the get_queues_arglist into the inferior's memory space to prepare for
137061da546Spatrick // the call.
138061da546Spatrick //
139061da546Spatrick // Returns the address of the arguments written down in the inferior process,
140061da546Spatrick // which can be used to make the function call.
141061da546Spatrick
142061da546Spatrick lldb::addr_t
SetupGetQueuesFunction(Thread & thread,ValueList & get_queues_arglist)143061da546Spatrick AppleGetQueuesHandler::SetupGetQueuesFunction(Thread &thread,
144061da546Spatrick ValueList &get_queues_arglist) {
145061da546Spatrick ThreadSP thread_sp(thread.shared_from_this());
146061da546Spatrick ExecutionContext exe_ctx(thread_sp);
147061da546Spatrick
148061da546Spatrick Address impl_code_address;
149061da546Spatrick DiagnosticManager diagnostics;
150*f6aab3d8Srobert Log *log = GetLog(LLDBLog::SystemRuntime);
151061da546Spatrick lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
152061da546Spatrick
153061da546Spatrick FunctionCaller *get_queues_caller = nullptr;
154061da546Spatrick
155061da546Spatrick // Scope for mutex locker:
156061da546Spatrick {
157061da546Spatrick std::lock_guard<std::mutex> guard(m_get_queues_function_mutex);
158061da546Spatrick
159061da546Spatrick // First stage is to make the ClangUtility to hold our injected function:
160061da546Spatrick
161061da546Spatrick if (!m_get_queues_impl_code_up) {
162061da546Spatrick if (g_get_current_queues_function_code != nullptr) {
163be691f3bSpatrick auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
164be691f3bSpatrick g_get_current_queues_function_code,
165be691f3bSpatrick g_get_current_queues_function_name, eLanguageTypeC, exe_ctx);
166be691f3bSpatrick if (!utility_fn_or_error) {
167be691f3bSpatrick LLDB_LOG_ERROR(log, utility_fn_or_error.takeError(),
168be691f3bSpatrick "Failed to create UtilityFunction for queues "
169be691f3bSpatrick "introspection: {0}.");
170061da546Spatrick return args_addr;
171061da546Spatrick }
172be691f3bSpatrick m_get_queues_impl_code_up = std::move(*utility_fn_or_error);
173061da546Spatrick } else {
174061da546Spatrick if (log) {
175061da546Spatrick LLDB_LOGF(log, "No queues introspection code found.");
176061da546Spatrick diagnostics.Dump(log);
177061da546Spatrick }
178061da546Spatrick return LLDB_INVALID_ADDRESS;
179061da546Spatrick }
180061da546Spatrick }
181061da546Spatrick
182061da546Spatrick // Next make the runner function for our implementation utility function.
183*f6aab3d8Srobert TypeSystemClangSP scratch_ts_sp =
184be691f3bSpatrick ScratchTypeSystemClang::GetForTarget(thread.GetProcess()->GetTarget());
185061da546Spatrick CompilerType get_queues_return_type =
186*f6aab3d8Srobert scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
187061da546Spatrick Status error;
188061da546Spatrick get_queues_caller = m_get_queues_impl_code_up->MakeFunctionCaller(
189061da546Spatrick get_queues_return_type, get_queues_arglist, thread_sp, error);
190061da546Spatrick if (error.Fail() || get_queues_caller == nullptr) {
191061da546Spatrick LLDB_LOGF(log,
192061da546Spatrick "Could not get function caller for get-queues function: %s.",
193061da546Spatrick error.AsCString());
194061da546Spatrick return args_addr;
195061da546Spatrick }
196061da546Spatrick }
197061da546Spatrick
198061da546Spatrick diagnostics.Clear();
199061da546Spatrick
200061da546Spatrick // Now write down the argument values for this particular call. This looks
201061da546Spatrick // like it might be a race condition if other threads were calling into here,
202061da546Spatrick // but actually it isn't because we allocate a new args structure for this
203061da546Spatrick // call by passing args_addr = LLDB_INVALID_ADDRESS...
204061da546Spatrick
205061da546Spatrick if (!get_queues_caller->WriteFunctionArguments(
206061da546Spatrick exe_ctx, args_addr, get_queues_arglist, diagnostics)) {
207061da546Spatrick if (log) {
208061da546Spatrick LLDB_LOGF(log, "Error writing get-queues function arguments.");
209061da546Spatrick diagnostics.Dump(log);
210061da546Spatrick }
211061da546Spatrick return args_addr;
212061da546Spatrick }
213061da546Spatrick
214061da546Spatrick return args_addr;
215061da546Spatrick }
216061da546Spatrick
217061da546Spatrick AppleGetQueuesHandler::GetQueuesReturnInfo
GetCurrentQueues(Thread & thread,addr_t page_to_free,uint64_t page_to_free_size,Status & error)218061da546Spatrick AppleGetQueuesHandler::GetCurrentQueues(Thread &thread, addr_t page_to_free,
219061da546Spatrick uint64_t page_to_free_size,
220061da546Spatrick Status &error) {
221061da546Spatrick lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
222061da546Spatrick ProcessSP process_sp(thread.CalculateProcess());
223061da546Spatrick TargetSP target_sp(thread.CalculateTarget());
224*f6aab3d8Srobert TypeSystemClangSP scratch_ts_sp =
225be691f3bSpatrick ScratchTypeSystemClang::GetForTarget(*target_sp);
226*f6aab3d8Srobert Log *log = GetLog(LLDBLog::SystemRuntime);
227061da546Spatrick
228061da546Spatrick GetQueuesReturnInfo return_value;
229061da546Spatrick return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
230061da546Spatrick return_value.queues_buffer_size = 0;
231061da546Spatrick return_value.count = 0;
232061da546Spatrick
233061da546Spatrick error.Clear();
234061da546Spatrick
235061da546Spatrick if (!thread.SafeToCallFunctions()) {
236061da546Spatrick LLDB_LOGF(log, "Not safe to call functions on thread 0x%" PRIx64,
237061da546Spatrick thread.GetID());
238061da546Spatrick error.SetErrorString("Not safe to call functions on this thread.");
239061da546Spatrick return return_value;
240061da546Spatrick }
241061da546Spatrick
242061da546Spatrick // Set up the arguments for a call to
243061da546Spatrick
244061da546Spatrick // struct get_current_queues_return_values
245061da546Spatrick // {
246061da546Spatrick // uint64_t queues_buffer_ptr; /* the address of the queues buffer from
247061da546Spatrick // libBacktraceRecording */
248061da546Spatrick // uint64_t queues_buffer_size; /* the size of the queues buffer from
249061da546Spatrick // libBacktraceRecording */
250061da546Spatrick // uint64_t count; /* the number of queues included in the
251061da546Spatrick // queues buffer */
252061da546Spatrick // };
253061da546Spatrick //
254061da546Spatrick // void
255061da546Spatrick // __lldb_backtrace_recording_get_current_queues
256061da546Spatrick // (struct
257061da546Spatrick // get_current_queues_return_values
258061da546Spatrick // *return_buffer,
259061da546Spatrick // void *page_to_free,
260061da546Spatrick // uint64_t page_to_free_size);
261061da546Spatrick
262061da546Spatrick // Where the return_buffer argument points to a 24 byte region of memory
263061da546Spatrick // already allocated by lldb in the inferior process.
264061da546Spatrick
265061da546Spatrick CompilerType clang_void_ptr_type =
266*f6aab3d8Srobert scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
267061da546Spatrick Value return_buffer_ptr_value;
268be691f3bSpatrick return_buffer_ptr_value.SetValueType(Value::ValueType::Scalar);
269061da546Spatrick return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
270061da546Spatrick
271*f6aab3d8Srobert CompilerType clang_int_type = scratch_ts_sp->GetBasicType(eBasicTypeInt);
272061da546Spatrick Value debug_value;
273be691f3bSpatrick debug_value.SetValueType(Value::ValueType::Scalar);
274061da546Spatrick debug_value.SetCompilerType(clang_int_type);
275061da546Spatrick
276061da546Spatrick Value page_to_free_value;
277be691f3bSpatrick page_to_free_value.SetValueType(Value::ValueType::Scalar);
278061da546Spatrick page_to_free_value.SetCompilerType(clang_void_ptr_type);
279061da546Spatrick
280061da546Spatrick CompilerType clang_uint64_type =
281*f6aab3d8Srobert scratch_ts_sp->GetBasicType(eBasicTypeUnsignedLongLong);
282061da546Spatrick Value page_to_free_size_value;
283be691f3bSpatrick page_to_free_size_value.SetValueType(Value::ValueType::Scalar);
284061da546Spatrick page_to_free_size_value.SetCompilerType(clang_uint64_type);
285061da546Spatrick
286061da546Spatrick std::lock_guard<std::mutex> guard(m_get_queues_retbuffer_mutex);
287061da546Spatrick if (m_get_queues_return_buffer_addr == LLDB_INVALID_ADDRESS) {
288061da546Spatrick addr_t bufaddr = process_sp->AllocateMemory(
289061da546Spatrick 32, ePermissionsReadable | ePermissionsWritable, error);
290061da546Spatrick if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
291061da546Spatrick LLDB_LOGF(log, "Failed to allocate memory for return buffer for get "
292061da546Spatrick "current queues func call");
293061da546Spatrick return return_value;
294061da546Spatrick }
295061da546Spatrick m_get_queues_return_buffer_addr = bufaddr;
296061da546Spatrick }
297061da546Spatrick
298061da546Spatrick ValueList argument_values;
299061da546Spatrick
300061da546Spatrick return_buffer_ptr_value.GetScalar() = m_get_queues_return_buffer_addr;
301061da546Spatrick argument_values.PushValue(return_buffer_ptr_value);
302061da546Spatrick
303061da546Spatrick debug_value.GetScalar() = 0;
304061da546Spatrick argument_values.PushValue(debug_value);
305061da546Spatrick
306061da546Spatrick if (page_to_free != LLDB_INVALID_ADDRESS)
307061da546Spatrick page_to_free_value.GetScalar() = page_to_free;
308061da546Spatrick else
309061da546Spatrick page_to_free_value.GetScalar() = 0;
310061da546Spatrick argument_values.PushValue(page_to_free_value);
311061da546Spatrick
312061da546Spatrick page_to_free_size_value.GetScalar() = page_to_free_size;
313061da546Spatrick argument_values.PushValue(page_to_free_size_value);
314061da546Spatrick
315061da546Spatrick addr_t args_addr = SetupGetQueuesFunction(thread, argument_values);
316061da546Spatrick
317061da546Spatrick if (!m_get_queues_impl_code_up) {
318061da546Spatrick error.SetErrorString(
319061da546Spatrick "Unable to compile __introspection_dispatch_get_queues.");
320061da546Spatrick return return_value;
321061da546Spatrick }
322061da546Spatrick
323061da546Spatrick FunctionCaller *get_queues_caller =
324061da546Spatrick m_get_queues_impl_code_up->GetFunctionCaller();
325061da546Spatrick
326061da546Spatrick if (get_queues_caller == nullptr) {
327061da546Spatrick error.SetErrorString(
328061da546Spatrick "Unable to get caller for call __introspection_dispatch_get_queues");
329061da546Spatrick return return_value;
330061da546Spatrick }
331061da546Spatrick
332061da546Spatrick DiagnosticManager diagnostics;
333061da546Spatrick ExecutionContext exe_ctx;
334061da546Spatrick EvaluateExpressionOptions options;
335061da546Spatrick options.SetUnwindOnError(true);
336061da546Spatrick options.SetIgnoreBreakpoints(true);
337061da546Spatrick options.SetStopOthers(true);
338061da546Spatrick #if __has_feature(address_sanitizer)
339061da546Spatrick options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
340061da546Spatrick #else
341061da546Spatrick options.SetTimeout(std::chrono::milliseconds(500));
342061da546Spatrick #endif
343061da546Spatrick options.SetTryAllThreads(false);
344061da546Spatrick options.SetIsForUtilityExpr(true);
345061da546Spatrick thread.CalculateExecutionContext(exe_ctx);
346061da546Spatrick
347061da546Spatrick ExpressionResults func_call_ret;
348061da546Spatrick Value results;
349061da546Spatrick func_call_ret = get_queues_caller->ExecuteFunction(
350061da546Spatrick exe_ctx, &args_addr, options, diagnostics, results);
351061da546Spatrick if (func_call_ret != eExpressionCompleted || !error.Success()) {
352061da546Spatrick LLDB_LOGF(log,
353061da546Spatrick "Unable to call introspection_get_dispatch_queues(), got "
354061da546Spatrick "ExpressionResults %d, error contains %s",
355061da546Spatrick func_call_ret, error.AsCString(""));
356061da546Spatrick error.SetErrorString("Unable to call introspection_get_dispatch_queues() "
357061da546Spatrick "for list of queues");
358061da546Spatrick return return_value;
359061da546Spatrick }
360061da546Spatrick
361061da546Spatrick return_value.queues_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
362061da546Spatrick m_get_queues_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
363061da546Spatrick if (!error.Success() ||
364061da546Spatrick return_value.queues_buffer_ptr == LLDB_INVALID_ADDRESS) {
365061da546Spatrick return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
366061da546Spatrick return return_value;
367061da546Spatrick }
368061da546Spatrick
369061da546Spatrick return_value.queues_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
370061da546Spatrick m_get_queues_return_buffer_addr + 8, 8, 0, error);
371061da546Spatrick
372061da546Spatrick if (!error.Success()) {
373061da546Spatrick return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
374061da546Spatrick return return_value;
375061da546Spatrick }
376061da546Spatrick
377061da546Spatrick return_value.count = m_process->ReadUnsignedIntegerFromMemory(
378061da546Spatrick m_get_queues_return_buffer_addr + 16, 8, 0, error);
379061da546Spatrick if (!error.Success()) {
380061da546Spatrick return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
381061da546Spatrick return return_value;
382061da546Spatrick }
383061da546Spatrick
384061da546Spatrick LLDB_LOGF(log,
385061da546Spatrick "AppleGetQueuesHandler called "
386061da546Spatrick "__introspection_dispatch_get_queues (page_to_free == "
387061da546Spatrick "0x%" PRIx64 ", size = %" PRId64 "), returned page is at 0x%" PRIx64
388061da546Spatrick ", size %" PRId64 ", count = %" PRId64,
389061da546Spatrick page_to_free, page_to_free_size, return_value.queues_buffer_ptr,
390061da546Spatrick return_value.queues_buffer_size, return_value.count);
391061da546Spatrick
392061da546Spatrick return return_value;
393061da546Spatrick }
394