blob: cda7264d17b235fb1b8c1e70c7b516e07d26d37c [file] [log] [blame]
Jason Molenda2fd83352014-02-05 05:44:54 +00001//===-- AppleGetPendingItemsHandler.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 "AppleGetPendingItemsHandler.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16
Jason Molenda2fd83352014-02-05 05:44:54 +000017#include "lldb/Core/ConstString.h"
18#include "lldb/Core/Log.h"
19#include "lldb/Core/Module.h"
20#include "lldb/Core/StreamString.h"
21#include "lldb/Core/Value.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000022#include "lldb/Expression/DiagnosticManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000023#include "lldb/Expression/FunctionCaller.h"
24#include "lldb/Expression/UtilityFunction.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000025#include "lldb/Symbol/ClangASTContext.h"
26#include "lldb/Symbol/Symbol.h"
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35const char *AppleGetPendingItemsHandler::g_get_pending_items_function_name = "__lldb_backtrace_recording_get_pending_items";
36const char *AppleGetPendingItemsHandler::g_get_pending_items_function_code = " \n\
37extern \"C\" \n\
38{ \n\
39 /* \n\
40 * mach defines \n\
41 */ \n\
42 \n\
43 typedef unsigned int uint32_t; \n\
44 typedef unsigned long long uint64_t; \n\
45 typedef uint32_t mach_port_t; \n\
46 typedef mach_port_t vm_map_t; \n\
47 typedef int kern_return_t; \n\
48 typedef uint64_t mach_vm_address_t; \n\
49 typedef uint64_t mach_vm_size_t; \n\
50 \n\
51 mach_port_t mach_task_self (); \n\
52 kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
53 \n\
54 /* \n\
55 * libBacktraceRecording defines \n\
56 */ \n\
57 \n\
58 typedef uint32_t queue_list_scope_t; \n\
59 typedef void *dispatch_queue_t; \n\
60 typedef void *introspection_dispatch_queue_info_t; \n\
61 typedef void *introspection_dispatch_item_info_ref; \n\
62 \n\
63 extern uint64_t __introspection_dispatch_queue_get_pending_items (dispatch_queue_t queue, \n\
64 introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
65 uint64_t *returned_queues_buffer_size); \n\
66 extern int printf(const char *format, ...); \n\
67 \n\
68 /* \n\
69 * return type define \n\
70 */ \n\
71 \n\
72 struct get_pending_items_return_values \n\
73 { \n\
74 uint64_t pending_items_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
75 uint64_t pending_items_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
76 uint64_t count; /* the number of items included in the queues buffer */ \n\
77 }; \n\
78 \n\
79 void __lldb_backtrace_recording_get_pending_items \n\
80 (struct get_pending_items_return_values *return_buffer, \n\
81 int debug, \n\
82 uint64_t /* dispatch_queue_t */ queue, \n\
83 void *page_to_free, \n\
84 uint64_t page_to_free_size) \n\
85{ \n\
86 if (debug) \n\
87 printf (\"entering get_pending_items with args return_buffer == %p, debug == %d, queue == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, queue, page_to_free, page_to_free_size); \n\
88 if (page_to_free != 0) \n\
89 { \n\
90 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
91 } \n\
92 \n\
93 return_buffer->count = __introspection_dispatch_queue_get_pending_items ( \n\
94 (void*) queue, \n\
95 (void**)&return_buffer->pending_items_buffer_ptr, \n\
96 &return_buffer->pending_items_buffer_size); \n\
97 if (debug) \n\
98 printf(\"result was count %lld\\n\", return_buffer->count); \n\
99} \n\
100} \n\
101";
102
103AppleGetPendingItemsHandler::AppleGetPendingItemsHandler (Process *process) :
104 m_process (process),
Jason Molenda2fd83352014-02-05 05:44:54 +0000105 m_get_pending_items_impl_code (),
106 m_get_pending_items_function_mutex(),
107 m_get_pending_items_return_buffer_addr (LLDB_INVALID_ADDRESS),
108 m_get_pending_items_retbuffer_mutex()
109{
110}
111
112AppleGetPendingItemsHandler::~AppleGetPendingItemsHandler ()
113{
114}
115
116void
117AppleGetPendingItemsHandler::Detach ()
118{
119
120 if (m_process && m_process->IsAlive() && m_get_pending_items_return_buffer_addr != LLDB_INVALID_ADDRESS)
121 {
122 Mutex::Locker locker;
123 locker.TryLock (m_get_pending_items_retbuffer_mutex); // Even if we don't get the lock, deallocate the buffer
124 m_process->DeallocateMemory (m_get_pending_items_return_buffer_addr);
125 }
126}
127
128// Compile our __lldb_backtrace_recording_get_pending_items() function (from the
129// source above in g_get_pending_items_function_code) if we don't find that function in the inferior
130// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for testing.)
131//
132// Insert the __lldb_backtrace_recording_get_pending_items into the inferior process if needed.
133//
134// Write the get_pending_items_arglist into the inferior's memory space to prepare for the call.
135//
136// Returns the address of the arguments written down in the inferior process, which can be used to
137// make the function call.
138
139lldb::addr_t
Sean Callanan579e70c2016-03-19 00:03:59 +0000140AppleGetPendingItemsHandler::SetupGetPendingItemsFunction(Thread &thread, ValueList &get_pending_items_arglist)
Jason Molenda2fd83352014-02-05 05:44:54 +0000141{
Sean Callanan579e70c2016-03-19 00:03:59 +0000142 ExecutionContext exe_ctx(thread.shared_from_this());
143 DiagnosticManager diagnostics;
144 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
Jason Molenda2fd83352014-02-05 05:44:54 +0000145 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
Jim Ingham151c0322015-09-15 21:13:50 +0000146 FunctionCaller *get_pending_items_caller = nullptr;
Sean Callanan579e70c2016-03-19 00:03:59 +0000147
Jason Molenda2fd83352014-02-05 05:44:54 +0000148 // Scope for mutex locker:
149 {
150 Mutex::Locker locker(m_get_pending_items_function_mutex);
151
152 // First stage is to make the ClangUtility to hold our injected function:
153
Jim Ingham151c0322015-09-15 21:13:50 +0000154 if (!m_get_pending_items_impl_code.get())
Jason Molenda2fd83352014-02-05 05:44:54 +0000155 {
156 if (g_get_pending_items_function_code != NULL)
157 {
Jim Ingham151c0322015-09-15 21:13:50 +0000158 Error error;
159 m_get_pending_items_impl_code.reset (exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(g_get_pending_items_function_code,
160 eLanguageTypeObjC,
161 g_get_pending_items_function_name,
162 error));
163 if (error.Fail())
164 {
165 if (log)
166 log->Printf ("Failed to get UtilityFunction for pending-items introspection: %s.", error.AsCString());
167 return args_addr;
168 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000169
170 if (!m_get_pending_items_impl_code->Install(diagnostics, exe_ctx))
Jason Molenda2fd83352014-02-05 05:44:54 +0000171 {
172 if (log)
Sean Callanan579e70c2016-03-19 00:03:59 +0000173 {
174 log->Printf("Failed to install pending-items introspection.");
175 diagnostics.Dump(log);
176 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000177 m_get_pending_items_impl_code.reset();
178 return args_addr;
179 }
180 }
181 else
182 {
183 if (log)
184 log->Printf("No pending-items introspection code found.");
Jason Molenda2fd83352014-02-05 05:44:54 +0000185 return LLDB_INVALID_ADDRESS;
186 }
187
Jim Ingham151c0322015-09-15 21:13:50 +0000188 // Next make the runner function for our implementation utility function.
189 Error error;
Jason Molenda2fd83352014-02-05 05:44:54 +0000190 ClangASTContext *clang_ast_context = thread.GetProcess()->GetTarget().GetScratchClangASTContext();
Greg Claytona1e5dc82015-08-11 22:53:00 +0000191 CompilerType get_pending_items_return_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Jim Ingham151c0322015-09-15 21:13:50 +0000192 get_pending_items_caller = m_get_pending_items_impl_code->MakeFunctionCaller (get_pending_items_return_type,
193 get_pending_items_arglist,
194 error);
195 if (error.Fail())
Jason Molenda2fd83352014-02-05 05:44:54 +0000196 {
197 if (log)
Jim Ingham151c0322015-09-15 21:13:50 +0000198 log->Printf ("Failed to install pending-items introspection function caller: %s.", error.AsCString());
199 m_get_pending_items_impl_code.reset();
Jason Molenda2fd83352014-02-05 05:44:54 +0000200 return args_addr;
201 }
202 }
203 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000204
205 diagnostics.Clear();
206
Jason Molendad76b2a42015-10-29 00:08:03 +0000207 if (get_pending_items_caller == nullptr)
208 {
209 if (log)
210 log->Printf ("Failed to get get_pending_items_caller.");
211 return LLDB_INVALID_ADDRESS;
212 }
213
Jason Molenda2fd83352014-02-05 05:44:54 +0000214 // Now write down the argument values for this particular call. This looks like it might be a race condition
215 // if other threads were calling into here, but actually it isn't because we allocate a new args structure for
216 // this call by passing args_addr = LLDB_INVALID_ADDRESS...
217
Sean Callanan579e70c2016-03-19 00:03:59 +0000218 if (!get_pending_items_caller->WriteFunctionArguments(exe_ctx, args_addr, get_pending_items_arglist, diagnostics))
Jason Molenda2fd83352014-02-05 05:44:54 +0000219 {
220 if (log)
Sean Callanan579e70c2016-03-19 00:03:59 +0000221 {
222 log->Printf("Error writing pending-items function arguments.");
223 diagnostics.Dump(log);
224 }
225
Jason Molenda2fd83352014-02-05 05:44:54 +0000226 return args_addr;
227 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000228
Jason Molenda2fd83352014-02-05 05:44:54 +0000229 return args_addr;
230}
231
232AppleGetPendingItemsHandler::GetPendingItemsReturnInfo
233AppleGetPendingItemsHandler::GetPendingItems (Thread &thread, addr_t queue, addr_t page_to_free, uint64_t page_to_free_size, Error &error)
234{
235 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
236 ProcessSP process_sp (thread.CalculateProcess());
237 TargetSP target_sp (thread.CalculateTarget());
238 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
239 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
240
241 GetPendingItemsReturnInfo return_value;
242 return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
243 return_value.items_buffer_size = 0;
244 return_value.count = 0;
245
246 error.Clear();
247
Jason Molendab4892cd2014-05-13 22:02:48 +0000248 if (thread.SafeToCallFunctions() == false)
249 {
250 if (log)
251 log->Printf ("Not safe to call functions on thread 0x%" PRIx64, thread.GetID());
252 error.SetErrorString ("Not safe to call functions on this thread.");
253 return return_value;
254 }
255
Jason Molenda2fd83352014-02-05 05:44:54 +0000256 // Set up the arguments for a call to
257
258 // struct get_pending_items_return_values
259 // {
260 // uint64_t pending_items_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */
261 // uint64_t pending_items_buffer_size; /* the size of the items buffer from libBacktraceRecording */
262 // uint64_t count; /* the number of items included in the queues buffer */
263 // };
264 //
265 // void __lldb_backtrace_recording_get_pending_items
266 // (struct get_pending_items_return_values *return_buffer,
267 // int debug,
268 // uint64_t /* dispatch_queue_t */ queue
269 // void *page_to_free,
270 // uint64_t page_to_free_size)
271
272 // Where the return_buffer argument points to a 24 byte region of memory already allocated by lldb in
273 // the inferior process.
274
Greg Claytona1e5dc82015-08-11 22:53:00 +0000275 CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Jason Molenda2fd83352014-02-05 05:44:54 +0000276 Value return_buffer_ptr_value;
277 return_buffer_ptr_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000278 return_buffer_ptr_value.SetCompilerType (clang_void_ptr_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000279
Greg Claytona1e5dc82015-08-11 22:53:00 +0000280 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
Jason Molenda2fd83352014-02-05 05:44:54 +0000281 Value debug_value;
282 debug_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000283 debug_value.SetCompilerType (clang_int_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000284
Greg Claytona1e5dc82015-08-11 22:53:00 +0000285 CompilerType clang_uint64_type = clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
Jason Molenda2fd83352014-02-05 05:44:54 +0000286 Value queue_value;
287 queue_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000288 queue_value.SetCompilerType (clang_uint64_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000289
290 Value page_to_free_value;
291 page_to_free_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000292 page_to_free_value.SetCompilerType (clang_void_ptr_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000293
294 Value page_to_free_size_value;
295 page_to_free_size_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000296 page_to_free_size_value.SetCompilerType (clang_uint64_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000297
298
299 Mutex::Locker locker(m_get_pending_items_retbuffer_mutex);
300 if (m_get_pending_items_return_buffer_addr == LLDB_INVALID_ADDRESS)
301 {
302 addr_t bufaddr = process_sp->AllocateMemory (32, ePermissionsReadable | ePermissionsWritable, error);
303 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS)
304 {
305 if (log)
306 log->Printf ("Failed to allocate memory for return buffer for get current queues func call");
307 return return_value;
308 }
309 m_get_pending_items_return_buffer_addr = bufaddr;
310 }
311
312 ValueList argument_values;
313
314 return_buffer_ptr_value.GetScalar() = m_get_pending_items_return_buffer_addr;
315 argument_values.PushValue (return_buffer_ptr_value);
316
317 debug_value.GetScalar() = 0;
318 argument_values.PushValue (debug_value);
319
320 queue_value.GetScalar() = queue;
321 argument_values.PushValue (queue_value);
322
323 if (page_to_free != LLDB_INVALID_ADDRESS)
324 page_to_free_value.GetScalar() = page_to_free;
325 else
326 page_to_free_value.GetScalar() = 0;
327 argument_values.PushValue (page_to_free_value);
328
329 page_to_free_size_value.GetScalar() = page_to_free_size;
330 argument_values.PushValue (page_to_free_size_value);
331
Sean Callanan579e70c2016-03-19 00:03:59 +0000332 addr_t args_addr = SetupGetPendingItemsFunction(thread, argument_values);
Jason Molenda2fd83352014-02-05 05:44:54 +0000333
Sean Callanan579e70c2016-03-19 00:03:59 +0000334 DiagnosticManager diagnostics;
Jason Molenda2fd83352014-02-05 05:44:54 +0000335 ExecutionContext exe_ctx;
Jim Ingham151c0322015-09-15 21:13:50 +0000336 FunctionCaller *get_pending_items_caller = m_get_pending_items_impl_code->GetFunctionCaller();
Sean Callanan579e70c2016-03-19 00:03:59 +0000337
Jason Molenda2fd83352014-02-05 05:44:54 +0000338 EvaluateExpressionOptions options;
339 options.SetUnwindOnError (true);
340 options.SetIgnoreBreakpoints (true);
341 options.SetStopOthers (true);
Jason Molendaa0560152014-04-25 00:06:26 +0000342 options.SetTimeoutUsec(500000);
343 options.SetTryAllThreads (false);
Jason Molenda2fd83352014-02-05 05:44:54 +0000344 thread.CalculateExecutionContext (exe_ctx);
345
Jim Ingham151c0322015-09-15 21:13:50 +0000346 if (get_pending_items_caller == NULL)
Jason Molenda2fd83352014-02-05 05:44:54 +0000347 {
348 error.SetErrorString ("Unable to compile function to call __introspection_dispatch_queue_get_pending_items");
Jason Molendafa4286e2014-06-23 22:45:54 +0000349 return return_value;
Jason Molenda2fd83352014-02-05 05:44:54 +0000350 }
351
Jim Ingham1624a2d2014-05-05 02:26:40 +0000352 ExpressionResults func_call_ret;
Jason Molenda2fd83352014-02-05 05:44:54 +0000353 Value results;
Sean Callanan579e70c2016-03-19 00:03:59 +0000354 func_call_ret = get_pending_items_caller->ExecuteFunction(exe_ctx, &args_addr, options, diagnostics, results);
Jim Ingham8646d3c2014-05-05 02:47:44 +0000355 if (func_call_ret != eExpressionCompleted || !error.Success())
Jason Molenda2fd83352014-02-05 05:44:54 +0000356 {
357 if (log)
Jim Ingham1624a2d2014-05-05 02:26:40 +0000358 log->Printf ("Unable to call __introspection_dispatch_queue_get_pending_items(), got ExpressionResults %d, error contains %s", func_call_ret, error.AsCString(""));
Jason Molenda2fd83352014-02-05 05:44:54 +0000359 error.SetErrorString ("Unable to call __introspection_dispatch_queue_get_pending_items() for list of queues");
360 return return_value;
361 }
362
363 return_value.items_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory (m_get_pending_items_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
364 if (!error.Success() || return_value.items_buffer_ptr == LLDB_INVALID_ADDRESS)
365 {
366 return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
367 return return_value;
368 }
369
370 return_value.items_buffer_size = m_process->ReadUnsignedIntegerFromMemory (m_get_pending_items_return_buffer_addr + 8, 8, 0, error);
371
372 if (!error.Success())
373 {
374 return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
375 return return_value;
376 }
377
378 return_value.count = m_process->ReadUnsignedIntegerFromMemory (m_get_pending_items_return_buffer_addr + 16, 8, 0, error);
379 if (!error.Success())
380 {
381 return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
382 return return_value;
383 }
384
Jason Molenda2a6c2522014-02-15 00:20:40 +0000385 if (log)
Jason Molenda25286312014-03-10 23:18:34 +0000386 log->Printf ("AppleGetPendingItemsHandler called __introspection_dispatch_queue_get_pending_items (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.items_buffer_ptr, return_value.items_buffer_size, return_value.count);
Jason Molenda2a6c2522014-02-15 00:20:40 +0000387
Jason Molenda2fd83352014-02-05 05:44:54 +0000388 return return_value;
389}