blob: 66b8b8bcf5985bb2d549484023fa7a91f19bc122 [file] [log] [blame]
Jason Molenda2fd83352014-02-05 05:44:54 +00001//===-- AppleGetItemInfoHandler.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 "AppleGetItemInfoHandler.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclCXX.h"
19
20#include "lldb/Core/ConstString.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/Module.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Core/Value.h"
Jim Ingham151c0322015-09-15 21:13:50 +000025#include "lldb/Expression/FunctionCaller.h"
26#include "lldb/Expression/UtilityFunction.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000027#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/Target.h"
32#include "lldb/Target/Thread.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37const char *AppleGetItemInfoHandler::g_get_item_info_function_name = "__lldb_backtrace_recording_get_item_info";
38const char *AppleGetItemInfoHandler::g_get_item_info_function_code = " \n\
39extern \"C\" \n\
40{ \n\
41 /* \n\
42 * mach defines \n\
43 */ \n\
44 \n\
45 typedef unsigned int uint32_t; \n\
46 typedef unsigned long long uint64_t; \n\
47 typedef uint32_t mach_port_t; \n\
48 typedef mach_port_t vm_map_t; \n\
49 typedef int kern_return_t; \n\
50 typedef uint64_t mach_vm_address_t; \n\
51 typedef uint64_t mach_vm_size_t; \n\
52 \n\
53 mach_port_t mach_task_self (); \n\
54 kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
55 \n\
56 /* \n\
57 * libBacktraceRecording defines \n\
58 */ \n\
59 \n\
60 typedef uint32_t queue_list_scope_t; \n\
61 typedef void *dispatch_queue_t; \n\
62 typedef void *introspection_dispatch_queue_info_t; \n\
63 typedef void *introspection_dispatch_item_info_ref; \n\
64 \n\
65 extern uint64_t __introspection_dispatch_queue_item_get_info (introspection_dispatch_item_info_ref item_info_ref, \n\
66 introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
67 uint64_t *returned_queues_buffer_size); \n\
68 extern int printf(const char *format, ...); \n\
69 \n\
70 /* \n\
71 * return type define \n\
72 */ \n\
73 \n\
74 struct get_item_info_return_values \n\
75 { \n\
76 uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
77 uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
78 }; \n\
79 \n\
80 void __lldb_backtrace_recording_get_item_info \n\
81 (struct get_item_info_return_values *return_buffer, \n\
82 int debug, \n\
83 uint64_t /* introspection_dispatch_item_info_ref item_info_ref */ item, \n\
84 void *page_to_free, \n\
85 uint64_t page_to_free_size) \n\
86{ \n\
87 if (debug) \n\
88 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\
89 if (page_to_free != 0) \n\
90 { \n\
91 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
92 } \n\
93 \n\
94 __introspection_dispatch_queue_item_get_info ((void*) item, \n\
95 (void**)&return_buffer->item_info_buffer_ptr, \n\
96 &return_buffer->item_info_buffer_size); \n\
97} \n\
98} \n\
99";
100
101AppleGetItemInfoHandler::AppleGetItemInfoHandler (Process *process) :
102 m_process (process),
Jason Molenda2fd83352014-02-05 05:44:54 +0000103 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()
107{
108}
109
110AppleGetItemInfoHandler::~AppleGetItemInfoHandler ()
111{
112}
113
114void
115AppleGetItemInfoHandler::Detach ()
116{
117
118 if (m_process && m_process->IsAlive() && m_get_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS)
119 {
120 Mutex::Locker locker;
121 locker.TryLock (m_get_item_info_retbuffer_mutex); // Even if we don't get the lock, deallocate the buffer
122 m_process->DeallocateMemory (m_get_item_info_return_buffer_addr);
123 }
124}
125
126// Compile our __lldb_backtrace_recording_get_item_info() function (from the
127// source above in g_get_item_info_function_code) if we don't find that function in the inferior
128// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for testing.)
129//
130// Insert the __lldb_backtrace_recording_get_item_info into the inferior process if needed.
131//
132// Write the get_item_info_arglist into the inferior's memory space to prepare for the call.
133//
134// Returns the address of the arguments written down in the inferior process, which can be used to
135// make the function call.
136
137lldb::addr_t
138AppleGetItemInfoHandler::SetupGetItemInfoFunction (Thread &thread, ValueList &get_item_info_arglist)
139{
140 ExecutionContext exe_ctx (thread.shared_from_this());
Jason Molenda2fd83352014-02-05 05:44:54 +0000141 StreamString errors;
142 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
143 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
Jim Ingham151c0322015-09-15 21:13:50 +0000144 FunctionCaller *get_item_info_caller = nullptr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000145
146 // Scope for mutex locker:
147 {
148 Mutex::Locker locker(m_get_item_info_function_mutex);
149
Jim Ingham151c0322015-09-15 21:13:50 +0000150 // First stage is to make the UtilityFunction to hold our injected function:
Jason Molenda2fd83352014-02-05 05:44:54 +0000151
Jim Ingham151c0322015-09-15 21:13:50 +0000152 if (!m_get_item_info_impl_code.get())
Jason Molenda2fd83352014-02-05 05:44:54 +0000153 {
154 if (g_get_item_info_function_code != NULL)
155 {
Jim Ingham151c0322015-09-15 21:13:50 +0000156 Error error;
157 m_get_item_info_impl_code.reset(exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage (g_get_item_info_function_code,
158 eLanguageTypeObjC,
159 g_get_item_info_function_name,
160 error));
161 if (error.Fail())
162 {
163 if (log)
164 log->Printf ("Failed to get utility function: %s.", error.AsCString());
165 return args_addr;
166 }
167
Jason Molenda2fd83352014-02-05 05:44:54 +0000168 if (!m_get_item_info_impl_code->Install(errors, exe_ctx))
169 {
170 if (log)
171 log->Printf ("Failed to install get-item-info introspection: %s.", errors.GetData());
172 m_get_item_info_impl_code.reset();
173 return args_addr;
174 }
175 }
176 else
177 {
178 if (log)
179 log->Printf("No get-item-info introspection code found.");
180 errors.Printf ("No get-item-info introspection code found.");
181 return LLDB_INVALID_ADDRESS;
182 }
Jim Ingham151c0322015-09-15 21:13:50 +0000183
184 // Next make the runner function for our implementation utility function.
185 Error error;
Jason Molenda2fd83352014-02-05 05:44:54 +0000186
Jim Ingham151c0322015-09-15 21:13:50 +0000187 TypeSystem *type_system = thread.GetProcess()->GetTarget().GetScratchTypeSystemForLanguage(eLanguageTypeC);
188 CompilerType get_item_info_return_type = type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
189
190 get_item_info_caller = m_get_item_info_impl_code->MakeFunctionCaller(get_item_info_return_type,
191 get_item_info_arglist,
192 error);
193 if (error.Fail())
194 {
195 if (log)
196 log->Printf ("Error Inserting get-item-info function: \"%s\".", error.AsCString());
197 return args_addr;
198 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000199 }
200 else
201 {
Jim Ingham151c0322015-09-15 21:13:50 +0000202 // If it's already made, then we can just retrieve the caller:
203 get_item_info_caller = m_get_item_info_impl_code->GetFunctionCaller();
204 if (!get_item_info_caller)
Jason Molenda2fd83352014-02-05 05:44:54 +0000205 {
206 if (log)
Jim Ingham151c0322015-09-15 21:13:50 +0000207 log->Printf ("Failed to get get-item-info introspection caller.");
208 m_get_item_info_impl_code.reset();
Jason Molenda2fd83352014-02-05 05:44:54 +0000209 return args_addr;
210 }
211 }
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_item_info_caller->WriteFunctionArguments (exe_ctx, args_addr, get_item_info_arglist, errors))
Jason Molenda2fd83352014-02-05 05:44:54 +0000221 {
222 if (log)
223 log->Printf ("Error writing get-item-info function arguments: \"%s\".", errors.GetData());
224 return args_addr;
225 }
226
227 return args_addr;
228}
229
230AppleGetItemInfoHandler::GetItemInfoReturnInfo
231AppleGetItemInfoHandler::GetItemInfo (Thread &thread, uint64_t item, 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 GetItemInfoReturnInfo return_value;
240 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
241 return_value.item_buffer_size = 0;
242
243 error.Clear();
244
Jason Molendab4892cd2014-05-13 22:02:48 +0000245 if (thread.SafeToCallFunctions() == false)
246 {
247 if (log)
248 log->Printf ("Not safe to call functions on thread 0x%" PRIx64, thread.GetID());
249 error.SetErrorString ("Not safe to call functions on this thread.");
250 return return_value;
251 }
252
Jason Molenda2fd83352014-02-05 05:44:54 +0000253 // Set up the arguments for a call to
254
255 // struct get_item_info_return_values
256 // {
257 // uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */
258 // uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */
259 // };
260 //
261 // void __lldb_backtrace_recording_get_item_info
262 // (struct get_item_info_return_values *return_buffer,
263 // int debug,
264 // uint64_t item,
265 // void *page_to_free,
266 // uint64_t page_to_free_size)
267
268 // Where the return_buffer argument points to a 24 byte region of memory already allocated by lldb in
269 // the inferior process.
270
Greg Claytona1e5dc82015-08-11 22:53:00 +0000271 CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Jason Molenda2fd83352014-02-05 05:44:54 +0000272 Value return_buffer_ptr_value;
273 return_buffer_ptr_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000274 return_buffer_ptr_value.SetCompilerType (clang_void_ptr_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000275
Greg Claytona1e5dc82015-08-11 22:53:00 +0000276 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
Jason Molenda2fd83352014-02-05 05:44:54 +0000277 Value debug_value;
278 debug_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000279 debug_value.SetCompilerType (clang_int_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000280
Greg Claytona1e5dc82015-08-11 22:53:00 +0000281 CompilerType clang_uint64_type = clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
Jason Molenda2fd83352014-02-05 05:44:54 +0000282 Value item_value;
283 item_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000284 item_value.SetCompilerType (clang_uint64_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000285
286 Value page_to_free_value;
287 page_to_free_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000288 page_to_free_value.SetCompilerType (clang_void_ptr_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000289
290 Value page_to_free_size_value;
291 page_to_free_size_value.SetValueType (Value::eValueTypeScalar);
Greg Clayton99558cc42015-08-24 23:46:31 +0000292 page_to_free_size_value.SetCompilerType (clang_uint64_type);
Jason Molenda2fd83352014-02-05 05:44:54 +0000293
294
295 Mutex::Locker locker(m_get_item_info_retbuffer_mutex);
296 if (m_get_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS)
297 {
298 addr_t bufaddr = process_sp->AllocateMemory (32, ePermissionsReadable | ePermissionsWritable, error);
299 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS)
300 {
301 if (log)
302 log->Printf ("Failed to allocate memory for return buffer for get current queues func call");
303 return return_value;
304 }
305 m_get_item_info_return_buffer_addr = bufaddr;
306 }
307
308 ValueList argument_values;
309
310 return_buffer_ptr_value.GetScalar() = m_get_item_info_return_buffer_addr;
311 argument_values.PushValue (return_buffer_ptr_value);
312
313 debug_value.GetScalar() = 0;
314 argument_values.PushValue (debug_value);
315
316 item_value.GetScalar() = item;
317 argument_values.PushValue (item_value);
318
319 if (page_to_free != LLDB_INVALID_ADDRESS)
320 page_to_free_value.GetScalar() = page_to_free;
321 else
322 page_to_free_value.GetScalar() = 0;
323 argument_values.PushValue (page_to_free_value);
324
325 page_to_free_size_value.GetScalar() = page_to_free_size;
326 argument_values.PushValue (page_to_free_size_value);
327
328 addr_t args_addr = SetupGetItemInfoFunction (thread, argument_values);
329
330 StreamString errors;
331 ExecutionContext exe_ctx;
332 EvaluateExpressionOptions options;
333 options.SetUnwindOnError (true);
334 options.SetIgnoreBreakpoints (true);
335 options.SetStopOthers (true);
Jason Molendaa0560152014-04-25 00:06:26 +0000336 options.SetTimeoutUsec(500000);
337 options.SetTryAllThreads (false);
Jason Molenda2fd83352014-02-05 05:44:54 +0000338 thread.CalculateExecutionContext (exe_ctx);
339
Jim Ingham151c0322015-09-15 21:13:50 +0000340 if (!m_get_item_info_impl_code)
Jason Molenda2fd83352014-02-05 05:44:54 +0000341 {
342 error.SetErrorString ("Unable to compile function to call __introspection_dispatch_queue_item_get_info");
343 return return_value;
344 }
345
346
Jim Ingham1624a2d2014-05-05 02:26:40 +0000347 ExpressionResults func_call_ret;
Jason Molenda2fd83352014-02-05 05:44:54 +0000348 Value results;
Jim Ingham151c0322015-09-15 21:13:50 +0000349 FunctionCaller *func_caller = m_get_item_info_impl_code->GetFunctionCaller();
350 if (!func_caller)
351 {
352 if (log)
353 log->Printf ("Could not retrieve function caller for __introspection_dispatch_queue_item_get_info.");
354 error.SetErrorString("Could not retrieve function caller for __introspection_dispatch_queue_item_get_info.");
355 return return_value;
356 }
357
358 func_call_ret = func_caller->ExecuteFunction (exe_ctx, &args_addr, options, errors, results);
Jim Ingham8646d3c2014-05-05 02:47:44 +0000359 if (func_call_ret != eExpressionCompleted || !error.Success())
Jason Molenda2fd83352014-02-05 05:44:54 +0000360 {
361 if (log)
Jim Ingham1624a2d2014-05-05 02:26:40 +0000362 log->Printf ("Unable to call __introspection_dispatch_queue_item_get_info(), got ExpressionResults %d, error contains %s", func_call_ret, error.AsCString(""));
Jason Molenda2fd83352014-02-05 05:44:54 +0000363 error.SetErrorString ("Unable to call __introspection_dispatch_queue_get_item_info() for list of queues");
364 return return_value;
365 }
366
367 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory (m_get_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
368 if (!error.Success() || return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS)
369 {
370 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
371 return return_value;
372 }
373
374 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory (m_get_item_info_return_buffer_addr + 8, 8, 0, error);
375
376 if (!error.Success())
377 {
378 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
379 return return_value;
380 }
Jason Molenda2a6c2522014-02-15 00:20:40 +0000381 if (log)
382 log->Printf ("AppleGetItemInfoHandler called __introspection_dispatch_queue_item_get_info (page_to_free == 0x%" PRIx64 ", size = %" PRId64 "), returned page is at 0x%" PRIx64 ", size %" PRId64, page_to_free, page_to_free_size, return_value.item_buffer_ptr, return_value.item_buffer_size);
383
Jason Molenda2fd83352014-02-05 05:44:54 +0000384
385 return return_value;
386}