1 //===-- AppleGetItemInfoHandler.cpp -------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "AppleGetItemInfoHandler.h"
15 // Other libraries and framework includes
18 #include "lldb/Core/ConstString.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Core/Value.h"
23 #include "lldb/Expression/DiagnosticManager.h"
24 #include "lldb/Expression/FunctionCaller.h"
25 #include "lldb/Expression/UtilityFunction.h"
26 #include "lldb/Symbol/ClangASTContext.h"
27 #include "lldb/Symbol/Symbol.h"
28 #include "lldb/Target/ExecutionContext.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
34 using namespace lldb_private;
36 const char *AppleGetItemInfoHandler::g_get_item_info_function_name =
37 "__lldb_backtrace_recording_get_item_info";
38 const char *AppleGetItemInfoHandler::g_get_item_info_function_code =
46 typedef unsigned int uint32_t; \n\
47 typedef unsigned long long uint64_t; \n\
48 typedef uint32_t mach_port_t; \n\
49 typedef mach_port_t vm_map_t; \n\
50 typedef int kern_return_t; \n\
51 typedef uint64_t mach_vm_address_t; \n\
52 typedef uint64_t mach_vm_size_t; \n\
54 mach_port_t mach_task_self (); \n\
55 kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
58 * libBacktraceRecording defines \n\
61 typedef uint32_t queue_list_scope_t; \n\
62 typedef void *dispatch_queue_t; \n\
63 typedef void *introspection_dispatch_queue_info_t; \n\
64 typedef void *introspection_dispatch_item_info_ref; \n\
66 extern uint64_t __introspection_dispatch_queue_item_get_info (introspection_dispatch_item_info_ref item_info_ref, \n\
67 introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
68 uint64_t *returned_queues_buffer_size); \n\
69 extern int printf(const char *format, ...); \n\
72 * return type define \n\
75 struct get_item_info_return_values \n\
77 uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
78 uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
81 void __lldb_backtrace_recording_get_item_info \n\
82 (struct get_item_info_return_values *return_buffer, \n\
84 uint64_t /* introspection_dispatch_item_info_ref item_info_ref */ item, \n\
85 void *page_to_free, \n\
86 uint64_t page_to_free_size) \n\
89 printf (\"entering get_item_info with args return_buffer == %p, debug == %d, item == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, item, page_to_free, page_to_free_size); \n\
90 if (page_to_free != 0) \n\
92 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
95 __introspection_dispatch_queue_item_get_info ((void*) item, \n\
96 (void**)&return_buffer->item_info_buffer_ptr, \n\
97 &return_buffer->item_info_buffer_size); \n\
102 AppleGetItemInfoHandler::AppleGetItemInfoHandler(Process *process)
103 : m_process(process), m_get_item_info_impl_code(),
104 m_get_item_info_function_mutex(),
105 m_get_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
106 m_get_item_info_retbuffer_mutex() {}
108 AppleGetItemInfoHandler::~AppleGetItemInfoHandler() {}
110 void AppleGetItemInfoHandler::Detach() {
112 if (m_process && m_process->IsAlive() &&
113 m_get_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
114 std::unique_lock<std::mutex> lock(m_get_item_info_retbuffer_mutex,
116 lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
117 m_process->DeallocateMemory(m_get_item_info_return_buffer_addr);
121 // Compile our __lldb_backtrace_recording_get_item_info() function (from the
122 // source above in g_get_item_info_function_code) if we don't find that function
124 // already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
127 // Insert the __lldb_backtrace_recording_get_item_info into the inferior process
130 // Write the get_item_info_arglist into the inferior's memory space to prepare
133 // Returns the address of the arguments written down in the inferior process,
134 // which can be used to
135 // make the function call.
137 lldb::addr_t AppleGetItemInfoHandler::SetupGetItemInfoFunction(
138 Thread &thread, ValueList &get_item_info_arglist) {
139 ExecutionContext exe_ctx(thread.shared_from_this());
140 DiagnosticManager diagnostics;
141 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
142 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
143 FunctionCaller *get_item_info_caller = nullptr;
145 // Scope for mutex locker:
147 std::lock_guard<std::mutex> guard(m_get_item_info_function_mutex);
149 // First stage is to make the UtilityFunction to hold our injected function:
151 if (!m_get_item_info_impl_code.get()) {
152 if (g_get_item_info_function_code != NULL) {
154 m_get_item_info_impl_code.reset(
155 exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
156 g_get_item_info_function_code, eLanguageTypeObjC,
157 g_get_item_info_function_name, error));
160 log->Printf("Failed to get utility function: %s.",
165 if (!m_get_item_info_impl_code->Install(diagnostics, exe_ctx)) {
167 log->Printf("Failed to install get-item-info introspection.");
168 diagnostics.Dump(log);
170 m_get_item_info_impl_code.reset();
175 log->Printf("No get-item-info introspection code found.");
176 return LLDB_INVALID_ADDRESS;
179 // Next make the runner function for our implementation utility function.
182 TypeSystem *type_system =
183 thread.GetProcess()->GetTarget().GetScratchTypeSystemForLanguage(
184 nullptr, eLanguageTypeC);
185 CompilerType get_item_info_return_type =
186 type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
188 get_item_info_caller = m_get_item_info_impl_code->MakeFunctionCaller(
189 get_item_info_return_type, get_item_info_arglist,
190 thread.shared_from_this(), error);
191 if (error.Fail() || get_item_info_caller == nullptr) {
193 log->Printf("Error Inserting get-item-info function: \"%s\".",
198 // If it's already made, then we can just retrieve the caller:
199 get_item_info_caller = m_get_item_info_impl_code->GetFunctionCaller();
200 if (!get_item_info_caller) {
202 log->Printf("Failed to get get-item-info introspection caller.");
203 m_get_item_info_impl_code.reset();
211 // Now write down the argument values for this particular call. This looks
212 // like it might be a race condition
213 // if other threads were calling into here, but actually it isn't because we
214 // allocate a new args structure for
215 // this call by passing args_addr = LLDB_INVALID_ADDRESS...
217 if (!get_item_info_caller->WriteFunctionArguments(
218 exe_ctx, args_addr, get_item_info_arglist, diagnostics)) {
220 log->Printf("Error writing get-item-info function arguments.");
221 diagnostics.Dump(log);
230 AppleGetItemInfoHandler::GetItemInfoReturnInfo
231 AppleGetItemInfoHandler::GetItemInfo(Thread &thread, uint64_t item,
233 uint64_t page_to_free_size, Error &error) {
234 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
235 ProcessSP process_sp(thread.CalculateProcess());
236 TargetSP target_sp(thread.CalculateTarget());
237 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
240 GetItemInfoReturnInfo return_value;
241 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
242 return_value.item_buffer_size = 0;
246 if (thread.SafeToCallFunctions() == false) {
248 log->Printf("Not safe to call functions on thread 0x%" PRIx64,
250 error.SetErrorString("Not safe to call functions on this thread.");
254 // Set up the arguments for a call to
256 // struct get_item_info_return_values
258 // uint64_t item_info_buffer_ptr; /* the address of the items buffer
259 // from libBacktraceRecording */
260 // uint64_t item_info_buffer_size; /* the size of the items buffer from
261 // libBacktraceRecording */
264 // void __lldb_backtrace_recording_get_item_info
266 // get_item_info_return_values
270 // void *page_to_free,
271 // uint64_t page_to_free_size)
273 // Where the return_buffer argument points to a 24 byte region of memory
274 // already allocated by lldb in
275 // the inferior process.
277 CompilerType clang_void_ptr_type =
278 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
279 Value return_buffer_ptr_value;
280 return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
281 return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
283 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
285 debug_value.SetValueType(Value::eValueTypeScalar);
286 debug_value.SetCompilerType(clang_int_type);
288 CompilerType clang_uint64_type =
289 clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
291 item_value.SetValueType(Value::eValueTypeScalar);
292 item_value.SetCompilerType(clang_uint64_type);
294 Value page_to_free_value;
295 page_to_free_value.SetValueType(Value::eValueTypeScalar);
296 page_to_free_value.SetCompilerType(clang_void_ptr_type);
298 Value page_to_free_size_value;
299 page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
300 page_to_free_size_value.SetCompilerType(clang_uint64_type);
302 std::lock_guard<std::mutex> guard(m_get_item_info_retbuffer_mutex);
303 if (m_get_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
304 addr_t bufaddr = process_sp->AllocateMemory(
305 32, ePermissionsReadable | ePermissionsWritable, error);
306 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
308 log->Printf("Failed to allocate memory for return buffer for get "
309 "current queues func call");
312 m_get_item_info_return_buffer_addr = bufaddr;
315 ValueList argument_values;
317 return_buffer_ptr_value.GetScalar() = m_get_item_info_return_buffer_addr;
318 argument_values.PushValue(return_buffer_ptr_value);
320 debug_value.GetScalar() = 0;
321 argument_values.PushValue(debug_value);
323 item_value.GetScalar() = item;
324 argument_values.PushValue(item_value);
326 if (page_to_free != LLDB_INVALID_ADDRESS)
327 page_to_free_value.GetScalar() = page_to_free;
329 page_to_free_value.GetScalar() = 0;
330 argument_values.PushValue(page_to_free_value);
332 page_to_free_size_value.GetScalar() = page_to_free_size;
333 argument_values.PushValue(page_to_free_size_value);
335 addr_t args_addr = SetupGetItemInfoFunction(thread, argument_values);
337 DiagnosticManager diagnostics;
338 ExecutionContext exe_ctx;
339 EvaluateExpressionOptions options;
340 options.SetUnwindOnError(true);
341 options.SetIgnoreBreakpoints(true);
342 options.SetStopOthers(true);
343 options.SetTimeout(std::chrono::milliseconds(500));
344 options.SetTryAllThreads(false);
345 thread.CalculateExecutionContext(exe_ctx);
347 if (!m_get_item_info_impl_code) {
348 error.SetErrorString("Unable to compile function to call "
349 "__introspection_dispatch_queue_item_get_info");
353 ExpressionResults func_call_ret;
355 FunctionCaller *func_caller = m_get_item_info_impl_code->GetFunctionCaller();
358 log->Printf("Could not retrieve function caller for "
359 "__introspection_dispatch_queue_item_get_info.");
360 error.SetErrorString("Could not retrieve function caller for "
361 "__introspection_dispatch_queue_item_get_info.");
365 func_call_ret = func_caller->ExecuteFunction(exe_ctx, &args_addr, options,
366 diagnostics, results);
367 if (func_call_ret != eExpressionCompleted || !error.Success()) {
369 log->Printf("Unable to call "
370 "__introspection_dispatch_queue_item_get_info(), got "
371 "ExpressionResults %d, error contains %s",
372 func_call_ret, error.AsCString(""));
373 error.SetErrorString("Unable to call "
374 "__introspection_dispatch_queue_get_item_info() for "
379 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
380 m_get_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
381 if (!error.Success() ||
382 return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
383 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
387 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
388 m_get_item_info_return_buffer_addr + 8, 8, 0, error);
390 if (!error.Success()) {
391 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
395 log->Printf("AppleGetItemInfoHandler called "
396 "__introspection_dispatch_queue_item_get_info (page_to_free == "
397 "0x%" PRIx64 ", size = %" PRId64
398 "), returned page is at 0x%" PRIx64 ", size %" PRId64,
399 page_to_free, page_to_free_size, return_value.item_buffer_ptr,
400 return_value.item_buffer_size);