blob: 6d7eaf721918b4e50142cce026eb74f6f731bdce [file] [log] [blame]
Jason Molenda2fd83352014-02-05 05:44:54 +00001//===-- AppleGetQueuesHandler.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "AppleGetQueuesHandler.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jason Molenda2fd83352014-02-05 05:44:54 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclCXX.h"
18
19#include "lldb/Core/ConstString.h"
20#include "lldb/Core/Log.h"
21#include "lldb/Core/Module.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Core/Value.h"
Jim Ingham151c0322015-09-15 21:13:50 +000024#include "lldb/Expression/FunctionCaller.h"
25#include "lldb/Expression/UtilityFunction.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000026#include "lldb/Symbol/ClangASTContext.h"
27#include "lldb/Symbol/Symbol.h"
28#include "lldb/Target/ExecutionContext.h"
29#include "lldb/Target/Process.h"
Bruce Mitchenercfe7d6c2015-09-03 23:57:09 +000030#include "lldb/Target/Target.h"
31#include "lldb/Target/Thread.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000032
33using namespace lldb;
34using namespace lldb_private;
35
36const char *AppleGetQueuesHandler::g_get_current_queues_function_name = "__lldb_backtrace_recording_get_current_queues";
37const char *AppleGetQueuesHandler::g_get_current_queues_function_code = " \n\
38extern \"C\" \n\
39{ \n\
40 /* \n\
41 * mach defines \n\
42 */ \n\
43 \n\
44 typedef unsigned int uint32_t; \n\
45 typedef unsigned long long uint64_t; \n\
46 typedef uint32_t mach_port_t; \n\
47 typedef mach_port_t vm_map_t; \n\
48 typedef int kern_return_t; \n\
49 typedef uint64_t mach_vm_address_t; \n\
50 typedef uint64_t mach_vm_size_t; \n\
51 \n\
52 mach_port_t mach_task_self (); \n\
53 kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
54 \n\
55 /* \n\
56 * libBacktraceRecording defines \n\
57 */ \n\
58 \n\
59 typedef uint32_t queue_list_scope_t; \n\
60 typedef void *introspection_dispatch_queue_info_t; \n\
61 \n\
62 extern uint64_t __introspection_dispatch_get_queues (queue_list_scope_t scope, \n\
63 introspection_dispatch_queue_info_t *returned_queues_buffer, \n\
64 uint64_t *returned_queues_buffer_size); \n\
65 extern int printf(const char *format, ...); \n\
66 \n\
67 /* \n\
68 * return type define \n\
69 */ \n\
70 \n\
71 struct get_current_queues_return_values \n\
72 { \n\
73 uint64_t queues_buffer_ptr; /* the address of the queues buffer from libBacktraceRecording */ \n\
74 uint64_t queues_buffer_size; /* the size of the queues buffer from libBacktraceRecording */ \n\
75 uint64_t count; /* the number of queues included in the queues buffer */ \n\
76 }; \n\
77 \n\
78 void __lldb_backtrace_recording_get_current_queues \n\
79 (struct get_current_queues_return_values *return_buffer, \n\
80 int debug, \n\
81 void *page_to_free, \n\
82 uint64_t page_to_free_size) \n\
83{ \n\
84 if (debug) \n\
85 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\
86 if (page_to_free != 0) \n\
87 { \n\
88 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
89 } \n\
90 \n\
91 return_buffer->count = __introspection_dispatch_get_queues ( \n\
92 /* QUEUES_WITH_ANY_ITEMS */ 2, \n\
93 (void**)&return_buffer->queues_buffer_ptr, \n\
94 &return_buffer->queues_buffer_size); \n\
95 if (debug) \n\
96 printf(\"result was count %lld\\n\", return_buffer->count); \n\
97} \n\
98} \n\
99";
100
101AppleGetQueuesHandler::AppleGetQueuesHandler (Process *process) :
102 m_process (process),
Jim Ingham151c0322015-09-15 21:13:50 +0000103 m_get_queues_impl_code_up (),
Jason Molenda2fd83352014-02-05 05:44:54 +0000104 m_get_queues_function_mutex(),
105 m_get_queues_return_buffer_addr (LLDB_INVALID_ADDRESS),
106 m_get_queues_retbuffer_mutex()
107{
108}
109
110AppleGetQueuesHandler::~AppleGetQueuesHandler ()
111{
112}
113
114void
115AppleGetQueuesHandler::Detach ()
116{
117
118 if (m_process && m_process->IsAlive() && m_get_queues_return_buffer_addr != LLDB_INVALID_ADDRESS)
119 {
120 Mutex::Locker locker;
121 locker.TryLock (m_get_queues_retbuffer_mutex); // Even if we don't get the lock, deallocate the buffer
122 m_process->DeallocateMemory (m_get_queues_return_buffer_addr);
123 }
124}
125
Greg Claytona1e5dc82015-08-11 22:53:00 +0000126// Construct a CompilerType for the structure that g_get_current_queues_function_code will return by value
Jason Molenda2fd83352014-02-05 05:44:54 +0000127// so we can extract the fields after performing the function call.
128// i.e. we are getting this struct returned to us:
129//
130// struct get_current_queues_return_values
131// {
132// introspection_dispatch_queue_info_t *queues_buffer;
133// uint64_t queues_buffer_size;
134// uint64_t count;
135// };
136
137
138// Compile our __lldb_backtrace_recording_get_current_queues() function (from the
139// source above in g_get_current_queues_function_code) if we don't find that function in the inferior
140// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for testing.)
141//
142// Insert the __lldb_backtrace_recording_get_current_queues into the inferior process if needed.
143//
144// Write the get_queues_arglist into the inferior's memory space to prepare for the call.
145//
146// Returns the address of the arguments written down in the inferior process, which can be used to
147// make the function call.
148
149lldb::addr_t
150AppleGetQueuesHandler::SetupGetQueuesFunction (Thread &thread, ValueList &get_queues_arglist)
151{
152 ExecutionContext exe_ctx (thread.shared_from_this());
153 Address impl_code_address;
154 StreamString errors;
155 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
156 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
Jim Ingham151c0322015-09-15 21:13:50 +0000157
158 FunctionCaller *get_queues_caller = nullptr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000159
160 // Scope for mutex locker:
161 {
162 Mutex::Locker locker(m_get_queues_function_mutex);
163
164 // First stage is to make the ClangUtility to hold our injected function:
165
Jim Ingham151c0322015-09-15 21:13:50 +0000166 if (!m_get_queues_impl_code_up.get())
Jason Molenda2fd83352014-02-05 05:44:54 +0000167 {
168 if (g_get_current_queues_function_code != NULL)
169 {
Jim Ingham151c0322015-09-15 21:13:50 +0000170 Error error;
171 m_get_queues_impl_code_up.reset (exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(g_get_current_queues_function_code,
172 eLanguageTypeC,
173 g_get_current_queues_function_name,
174 error));
175 if (error.Fail())
176 {
177 if (log)
178 log->Printf ("Failed to get UtilityFunction for queues introspection: %s.", error.AsCString());
179 return args_addr;
180 }
181
182 if (!m_get_queues_impl_code_up->Install(errors, exe_ctx))
Jason Molenda2fd83352014-02-05 05:44:54 +0000183 {
184 if (log)
185 log->Printf ("Failed to install queues introspection: %s.", errors.GetData());
Jim Ingham151c0322015-09-15 21:13:50 +0000186 m_get_queues_impl_code_up.reset();
Jason Molenda2fd83352014-02-05 05:44:54 +0000187 return args_addr;
188 }
189 }
190 else
191 {
192 if (log)
193 log->Printf("No queues introspection code found.");
194 errors.Printf ("No queues introspection code found.");
195 return LLDB_INVALID_ADDRESS;
196 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000197 }
Jim Ingham151c0322015-09-15 21:13:50 +0000198
Jason Molenda2fd83352014-02-05 05:44:54 +0000199 // Next make the runner function for our implementation utility function.
Jim Ingham151c0322015-09-15 21:13:50 +0000200 ClangASTContext *clang_ast_context = thread.GetProcess()->GetTarget().GetScratchClangASTContext();
201 CompilerType get_queues_return_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
202 Error error;
203 get_queues_caller = m_get_queues_impl_code_up->MakeFunctionCaller (get_queues_return_type,
204 get_queues_arglist,
205 error);
206 if (error.Fail())
Jason Molenda2fd83352014-02-05 05:44:54 +0000207 {
Jim Ingham151c0322015-09-15 21:13:50 +0000208 if (log)
209 log->Printf ("Could not get function caller for get-queues function: %s.", error.AsCString());
210 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000211 }
212 }
213
214 errors.Clear();
215
216 // Now write down the argument values for this particular call. This looks like it might be a race condition
217 // if other threads were calling into here, but actually it isn't because we allocate a new args structure for
218 // this call by passing args_addr = LLDB_INVALID_ADDRESS...
219
Jim Ingham151c0322015-09-15 21:13:50 +0000220 if (!get_queues_caller->WriteFunctionArguments (exe_ctx, args_addr, get_queues_arglist, errors))
Jason Molenda2fd83352014-02-05 05:44:54 +0000221 {
222 if (log)
223 log->Printf ("Error writing get-queues function arguments: \"%s\".", errors.GetData());
224 return args_addr;
225 }
226
227 return args_addr;
228}
229
230AppleGetQueuesHandler::GetQueuesReturnInfo
231AppleGetQueuesHandler::GetCurrentQueues (Thread &thread, addr_t page_to_free, uint64_t page_to_free_size, Error &error)
232{
233 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
234 ProcessSP process_sp (thread.CalculateProcess());
235 TargetSP target_sp (thread.CalculateTarget());
236 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
237 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
238
239 GetQueuesReturnInfo return_value;
240 return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
241 return_value.queues_buffer_size = 0;
242 return_value.count = 0;
243
244 error.Clear();
245
Jason Molendab4892cd2014-05-13 22:02:48 +0000246 if (thread.SafeToCallFunctions() == false)
247 {
248 if (log)
249 log->Printf ("Not safe to call functions on thread 0x%" PRIx64, thread.GetID());
250 error.SetErrorString ("Not safe to call functions on this thread.");
251 return return_value;
252 }
253
Jason Molenda2fd83352014-02-05 05:44:54 +0000254 // Set up the arguments for a call to
255
256 // struct get_current_queues_return_values
257 // {
258 // uint64_t queues_buffer_ptr; /* the address of the queues buffer from libBacktraceRecording */
259 // uint64_t queues_buffer_size; /* the size of the queues buffer from libBacktraceRecording */
260 // uint64_t count; /* the number of queues included in the queues buffer */
261 // };
262 //
263 // void
264 // __lldb_backtrace_recording_get_current_queues
265 // (struct get_current_queues_return_values *return_buffer,
266 // void *page_to_free,
267 // uint64_t page_to_free_size);
268
269 // Where the return_buffer argument points to a 24 byte region of memory already allocated by lldb in
270 // the inferior process.
271
Greg Claytona1e5dc82015-08-11 22:53:00 +0000272 CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Jason Molenda2fd83352014-02-05 05:44:54 +0000273 Value return_buffer_ptr_value;
274 return_buffer_ptr_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000275 return_buffer_ptr_value.SetCompilerType (clang_void_ptr_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000276
Greg Claytona1e5dc82015-08-11 22:53:00 +0000277 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
Jason Molenda2fd83352014-02-05 05:44:54 +0000278 Value debug_value;
279 debug_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000280 debug_value.SetCompilerType (clang_int_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000281
282 Value page_to_free_value;
283 page_to_free_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000284 page_to_free_value.SetCompilerType (clang_void_ptr_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000285
Greg Claytona1e5dc82015-08-11 22:53:00 +0000286 CompilerType clang_uint64_type = clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
Jason Molenda2fd83352014-02-05 05:44:54 +0000287 Value page_to_free_size_value;
288 page_to_free_size_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000289 page_to_free_size_value.SetCompilerType (clang_uint64_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000290
291
292 Mutex::Locker locker(m_get_queues_retbuffer_mutex);
293 if (m_get_queues_return_buffer_addr == LLDB_INVALID_ADDRESS)
294 {
295 addr_t bufaddr = process_sp->AllocateMemory (32, ePermissionsReadable | ePermissionsWritable, error);
296 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS)
297 {
298 if (log)
299 log->Printf ("Failed to allocate memory for return buffer for get current queues func call");
300 return return_value;
301 }
302 m_get_queues_return_buffer_addr = bufaddr;
303 }
304
305 ValueList argument_values;
306
307 return_buffer_ptr_value.GetScalar() = m_get_queues_return_buffer_addr;
308 argument_values.PushValue (return_buffer_ptr_value);
309
310 debug_value.GetScalar() = 0;
311 argument_values.PushValue (debug_value);
312
313 if (page_to_free != LLDB_INVALID_ADDRESS)
314 page_to_free_value.GetScalar() = page_to_free;
315 else
316 page_to_free_value.GetScalar() = 0;
317 argument_values.PushValue (page_to_free_value);
318
319 page_to_free_size_value.GetScalar() = page_to_free_size;
320 argument_values.PushValue (page_to_free_size_value);
321
322 addr_t args_addr = SetupGetQueuesFunction (thread, argument_values);
323
Jim Ingham151c0322015-09-15 21:13:50 +0000324 if (!m_get_queues_impl_code_up)
Jason Molenda2fd83352014-02-05 05:44:54 +0000325 {
Jim Ingham151c0322015-09-15 21:13:50 +0000326 error.SetErrorString ("Unable to compile __introspection_dispatch_get_queues.");
327 return return_value;
328 }
329
330 FunctionCaller *get_queues_caller = m_get_queues_impl_code_up->GetFunctionCaller();
331
332 if (get_queues_caller == NULL)
333 {
334 error.SetErrorString ("Unable to get caller for call __introspection_dispatch_get_queues");
Jason Molendafa4286e2014-06-23 22:45:54 +0000335 return return_value;
Jason Molenda2fd83352014-02-05 05:44:54 +0000336 }
337
338 StreamString errors;
339 ExecutionContext exe_ctx;
340 EvaluateExpressionOptions options;
341 options.SetUnwindOnError (true);
342 options.SetIgnoreBreakpoints (true);
343 options.SetStopOthers (true);
Jason Molendaa0560152014-04-25 00:06:26 +0000344 options.SetTimeoutUsec(500000);
345 options.SetTryAllThreads (false);
Jason Molenda2fd83352014-02-05 05:44:54 +0000346 thread.CalculateExecutionContext (exe_ctx);
347
Jim Ingham1624a2d2014-05-05 02:26:40 +0000348 ExpressionResults func_call_ret;
Jason Molenda2fd83352014-02-05 05:44:54 +0000349 Value results;
Jim Ingham151c0322015-09-15 21:13:50 +0000350 func_call_ret = get_queues_caller->ExecuteFunction (exe_ctx, &args_addr, options, errors, results);
Jim Ingham8646d3c2014-05-05 02:47:44 +0000351 if (func_call_ret != eExpressionCompleted || !error.Success())
Jason Molenda2fd83352014-02-05 05:44:54 +0000352 {
353 if (log)
Jim Ingham1624a2d2014-05-05 02:26:40 +0000354 log->Printf ("Unable to call introspection_get_dispatch_queues(), got ExpressionResults %d, error contains %s", func_call_ret, error.AsCString(""));
Jason Molenda2fd83352014-02-05 05:44:54 +0000355 error.SetErrorString ("Unable to call introspection_get_dispatch_queues() for list of queues");
356 return return_value;
357 }
358
359 return_value.queues_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory (m_get_queues_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
360 if (!error.Success() || return_value.queues_buffer_ptr == LLDB_INVALID_ADDRESS)
361 {
362 return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
363 return return_value;
364 }
365
366 return_value.queues_buffer_size = m_process->ReadUnsignedIntegerFromMemory (m_get_queues_return_buffer_addr + 8, 8, 0, error);
367
368 if (!error.Success())
369 {
370 return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
371 return return_value;
372 }
373
374 return_value.count = m_process->ReadUnsignedIntegerFromMemory (m_get_queues_return_buffer_addr + 16, 8, 0, error);
375 if (!error.Success())
376 {
377 return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
378 return return_value;
379 }
380
Jason Molenda2a6c2522014-02-15 00:20:40 +0000381 if (log)
382 log->Printf ("AppleGetQueuesHandler called __introspection_dispatch_get_queues (page_to_free == 0x%" PRIx64 ", size = %" PRId64 "), returned page is at 0x%" PRIx64 ", size %" PRId64 ", count = %" PRId64, page_to_free, page_to_free_size, return_value.queues_buffer_ptr, return_value.queues_buffer_size, return_value.count);
383
Jason Molenda2fd83352014-02-05 05:44:54 +0000384 return return_value;
385}