1 //===-- AppleGetThreadItemInfoHandler.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 "AppleGetThreadItemInfoHandler.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/Expression.h"
25 #include "lldb/Expression/FunctionCaller.h"
26 #include "lldb/Expression/UtilityFunction.h"
27 #include "lldb/Symbol/ClangASTContext.h"
28 #include "lldb/Symbol/Symbol.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/StackFrame.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/Thread.h"
34 #include "lldb/lldb-private.h"
37 using namespace lldb_private;
40 *AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_name =
41 "__lldb_backtrace_recording_get_thread_item_info";
43 *AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_code =
51 typedef unsigned int uint32_t; \n\
52 typedef unsigned long long uint64_t; \n\
53 typedef uint32_t mach_port_t; \n\
54 typedef mach_port_t vm_map_t; \n\
55 typedef int kern_return_t; \n\
56 typedef uint64_t mach_vm_address_t; \n\
57 typedef uint64_t mach_vm_size_t; \n\
59 mach_port_t mach_task_self (); \n\
60 kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
62 typedef void *pthread_t; \n\
63 extern int printf(const char *format, ...); \n\
64 extern pthread_t pthread_self(void); \n\
67 * libBacktraceRecording defines \n\
70 typedef uint32_t queue_list_scope_t; \n\
71 typedef void *dispatch_queue_t; \n\
72 typedef void *introspection_dispatch_queue_info_t; \n\
73 typedef void *introspection_dispatch_item_info_ref; \n\
75 extern void __introspection_dispatch_thread_get_item_info (uint64_t thread_id, \n\
76 introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
77 uint64_t *returned_queues_buffer_size); \n\
80 * return type define \n\
83 struct get_thread_item_info_return_values \n\
85 uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
86 uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
89 void __lldb_backtrace_recording_get_thread_item_info \n\
90 (struct get_thread_item_info_return_values *return_buffer, \n\
92 uint64_t thread_id, \n\
93 void *page_to_free, \n\
94 uint64_t page_to_free_size) \n\
96 void *pthread_id = pthread_self (); \n\
98 printf (\"entering get_thread_item_info with args return_buffer == %p, debug == %d, thread id == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, (uint64_t) thread_id, page_to_free, page_to_free_size); \n\
99 if (page_to_free != 0) \n\
101 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
104 __introspection_dispatch_thread_get_item_info (thread_id, \n\
105 (void**)&return_buffer->item_info_buffer_ptr, \n\
106 &return_buffer->item_info_buffer_size); \n\
111 AppleGetThreadItemInfoHandler::AppleGetThreadItemInfoHandler(Process *process)
112 : m_process(process), m_get_thread_item_info_impl_code(),
113 m_get_thread_item_info_function_mutex(),
114 m_get_thread_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
115 m_get_thread_item_info_retbuffer_mutex() {}
117 AppleGetThreadItemInfoHandler::~AppleGetThreadItemInfoHandler() {}
119 void AppleGetThreadItemInfoHandler::Detach() {
121 if (m_process && m_process->IsAlive() &&
122 m_get_thread_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
123 std::unique_lock<std::mutex> lock(m_get_thread_item_info_retbuffer_mutex,
125 lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
126 m_process->DeallocateMemory(m_get_thread_item_info_return_buffer_addr);
130 // Compile our __lldb_backtrace_recording_get_thread_item_info() function (from
132 // source above in g_get_thread_item_info_function_code) if we don't find that
133 // function in the inferior
134 // already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
137 // Insert the __lldb_backtrace_recording_get_thread_item_info into the inferior
138 // process if needed.
140 // Write the get_thread_item_info_arglist into the inferior's memory space to
141 // prepare for the call.
143 // Returns the address of the arguments written down in the inferior process,
144 // which can be used to
145 // make the function call.
147 lldb::addr_t AppleGetThreadItemInfoHandler::SetupGetThreadItemInfoFunction(
148 Thread &thread, ValueList &get_thread_item_info_arglist) {
149 ThreadSP thread_sp(thread.shared_from_this());
150 ExecutionContext exe_ctx(thread_sp);
151 Address impl_code_address;
152 DiagnosticManager diagnostics;
153 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
154 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
155 FunctionCaller *get_thread_item_info_caller = nullptr;
157 // Scope for mutex locker:
159 std::lock_guard<std::mutex> guard(m_get_thread_item_info_function_mutex);
161 // First stage is to make the ClangUtility to hold our injected function:
163 if (!m_get_thread_item_info_impl_code.get()) {
165 if (g_get_thread_item_info_function_code != NULL) {
166 m_get_thread_item_info_impl_code.reset(
167 exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
168 g_get_thread_item_info_function_code, eLanguageTypeC,
169 g_get_thread_item_info_function_name, error));
172 log->Printf("Failed to get UtilityFunction for "
173 "get-thread-item-info introspection: %s.",
175 m_get_thread_item_info_impl_code.reset();
179 if (!m_get_thread_item_info_impl_code->Install(diagnostics, exe_ctx)) {
182 "Failed to install get-thread-item-info introspection.");
183 diagnostics.Dump(log);
186 m_get_thread_item_info_impl_code.reset();
191 log->Printf("No get-thread-item-info introspection code found.");
192 return LLDB_INVALID_ADDRESS;
195 // Also make the FunctionCaller for this UtilityFunction:
197 ClangASTContext *clang_ast_context =
198 thread.GetProcess()->GetTarget().GetScratchClangASTContext();
199 CompilerType get_thread_item_info_return_type =
200 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
202 get_thread_item_info_caller =
203 m_get_thread_item_info_impl_code->MakeFunctionCaller(
204 get_thread_item_info_return_type, get_thread_item_info_arglist,
206 if (error.Fail() || get_thread_item_info_caller == nullptr) {
208 log->Printf("Failed to install get-thread-item-info introspection "
211 m_get_thread_item_info_impl_code.reset();
216 get_thread_item_info_caller =
217 m_get_thread_item_info_impl_code->GetFunctionCaller();
223 // Now write down the argument values for this particular call. This looks
224 // like it might be a race condition
225 // if other threads were calling into here, but actually it isn't because we
226 // allocate a new args structure for
227 // this call by passing args_addr = LLDB_INVALID_ADDRESS...
229 if (!get_thread_item_info_caller->WriteFunctionArguments(
230 exe_ctx, args_addr, get_thread_item_info_arglist, diagnostics)) {
232 log->Printf("Error writing get-thread-item-info function arguments");
233 diagnostics.Dump(log);
241 AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo
242 AppleGetThreadItemInfoHandler::GetThreadItemInfo(Thread &thread,
245 uint64_t page_to_free_size,
247 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
248 ProcessSP process_sp(thread.CalculateProcess());
249 TargetSP target_sp(thread.CalculateTarget());
250 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
251 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
253 GetThreadItemInfoReturnInfo return_value;
254 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
255 return_value.item_buffer_size = 0;
259 if (thread.SafeToCallFunctions() == false) {
261 log->Printf("Not safe to call functions on thread 0x%" PRIx64,
263 error.SetErrorString("Not safe to call functions on this thread.");
267 // Set up the arguments for a call to
269 // struct get_thread_item_info_return_values
271 // uint64_t item_info_buffer_ptr; /* the address of the items buffer
272 // from libBacktraceRecording */
273 // uint64_t item_info_buffer_size; /* the size of the items buffer from
274 // libBacktraceRecording */
277 // void __lldb_backtrace_recording_get_thread_item_info
279 // get_thread_item_info_return_values
282 // void *page_to_free,
283 // uint64_t page_to_free_size)
285 // Where the return_buffer argument points to a 24 byte region of memory
286 // already allocated by lldb in
287 // the inferior process.
289 CompilerType clang_void_ptr_type =
290 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
291 Value return_buffer_ptr_value;
292 return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
293 return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
295 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
297 debug_value.SetValueType(Value::eValueTypeScalar);
298 debug_value.SetCompilerType(clang_int_type);
300 CompilerType clang_uint64_type =
301 clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
302 Value thread_id_value;
303 thread_id_value.SetValueType(Value::eValueTypeScalar);
304 thread_id_value.SetCompilerType(clang_uint64_type);
306 Value page_to_free_value;
307 page_to_free_value.SetValueType(Value::eValueTypeScalar);
308 page_to_free_value.SetCompilerType(clang_void_ptr_type);
310 Value page_to_free_size_value;
311 page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
312 page_to_free_size_value.SetCompilerType(clang_uint64_type);
314 std::lock_guard<std::mutex> guard(m_get_thread_item_info_retbuffer_mutex);
315 if (m_get_thread_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
316 addr_t bufaddr = process_sp->AllocateMemory(
317 32, ePermissionsReadable | ePermissionsWritable, error);
318 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
320 log->Printf("Failed to allocate memory for return buffer for get "
321 "current queues func call");
324 m_get_thread_item_info_return_buffer_addr = bufaddr;
327 ValueList argument_values;
329 return_buffer_ptr_value.GetScalar() =
330 m_get_thread_item_info_return_buffer_addr;
331 argument_values.PushValue(return_buffer_ptr_value);
333 debug_value.GetScalar() = 0;
334 argument_values.PushValue(debug_value);
336 thread_id_value.GetScalar() = thread_id;
337 argument_values.PushValue(thread_id_value);
339 if (page_to_free != LLDB_INVALID_ADDRESS)
340 page_to_free_value.GetScalar() = page_to_free;
342 page_to_free_value.GetScalar() = 0;
343 argument_values.PushValue(page_to_free_value);
345 page_to_free_size_value.GetScalar() = page_to_free_size;
346 argument_values.PushValue(page_to_free_size_value);
348 addr_t args_addr = SetupGetThreadItemInfoFunction(thread, argument_values);
350 DiagnosticManager diagnostics;
351 ExecutionContext exe_ctx;
352 EvaluateExpressionOptions options;
353 FunctionCaller *get_thread_item_info_caller = nullptr;
355 options.SetUnwindOnError(true);
356 options.SetIgnoreBreakpoints(true);
357 options.SetStopOthers(true);
358 options.SetTimeout(std::chrono::milliseconds(500));
359 options.SetTryAllThreads(false);
360 thread.CalculateExecutionContext(exe_ctx);
362 if (!m_get_thread_item_info_impl_code) {
363 error.SetErrorString("Unable to compile function to call "
364 "__introspection_dispatch_thread_get_item_info");
368 get_thread_item_info_caller =
369 m_get_thread_item_info_impl_code->GetFunctionCaller();
371 if (!get_thread_item_info_caller) {
372 error.SetErrorString("Unable to compile function caller for "
373 "__introspection_dispatch_thread_get_item_info");
377 ExpressionResults func_call_ret;
379 func_call_ret = get_thread_item_info_caller->ExecuteFunction(
380 exe_ctx, &args_addr, options, diagnostics, results);
381 if (func_call_ret != eExpressionCompleted || !error.Success()) {
383 log->Printf("Unable to call "
384 "__introspection_dispatch_thread_get_item_info(), got "
385 "ExpressionResults %d, error contains %s",
386 func_call_ret, error.AsCString(""));
387 error.SetErrorString("Unable to call "
388 "__introspection_dispatch_thread_get_item_info() for "
393 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
394 m_get_thread_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS,
396 if (!error.Success() ||
397 return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
398 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
402 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
403 m_get_thread_item_info_return_buffer_addr + 8, 8, 0, error);
405 if (!error.Success()) {
406 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
411 log->Printf("AppleGetThreadItemInfoHandler called "
412 "__introspection_dispatch_thread_get_item_info (page_to_free "
413 "== 0x%" PRIx64 ", size = %" PRId64
414 "), returned page is at 0x%" PRIx64 ", size %" PRId64,
415 page_to_free, page_to_free_size, return_value.item_buffer_ptr,
416 return_value.item_buffer_size);