blob: 3f0c7db676f3d32b6e5b6d7e67208431cdb52d4c [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- AppleGetItemInfoHandler.cpp -------------------------------*- C++
2//-*-===//
Jason Molenda2fd83352014-02-05 05:44:54 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#include "AppleGetItemInfoHandler.h"
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17
Jason Molenda2fd83352014-02-05 05:44:54 +000018#include "lldb/Core/Module.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000019#include "lldb/Core/Value.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000020#include "lldb/Expression/DiagnosticManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000021#include "lldb/Expression/FunctionCaller.h"
22#include "lldb/Expression/UtilityFunction.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000023#include "lldb/Symbol/ClangASTContext.h"
24#include "lldb/Symbol/Symbol.h"
25#include "lldb/Target/ExecutionContext.h"
26#include "lldb/Target/Process.h"
27#include "lldb/Target/Target.h"
28#include "lldb/Target/Thread.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000029#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000030#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000031#include "lldb/Utility/StreamString.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000032
33using namespace lldb;
34using namespace lldb_private;
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036const char *AppleGetItemInfoHandler::g_get_item_info_function_name =
37 "__lldb_backtrace_recording_get_item_info";
38const char *AppleGetItemInfoHandler::g_get_item_info_function_code =
39 " \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000040extern \"C\" \n\
41{ \n\
42 /* \n\
43 * mach defines \n\
44 */ \n\
45 \n\
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\
53 \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\
56 \n\
57 /* \n\
58 * libBacktraceRecording defines \n\
59 */ \n\
60 \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\
65 \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\
70 \n\
71 /* \n\
72 * return type define \n\
73 */ \n\
74 \n\
75 struct get_item_info_return_values \n\
76 { \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\
79 }; \n\
80 \n\
81 void __lldb_backtrace_recording_get_item_info \n\
82 (struct get_item_info_return_values *return_buffer, \n\
83 int debug, \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\
87{ \n\
88 if (debug) \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\
91 { \n\
92 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
93 } \n\
94 \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\
98} \n\
99} \n\
100";
101
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000102AppleGetItemInfoHandler::AppleGetItemInfoHandler(Process *process)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 : m_process(process), m_get_item_info_impl_code(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000104 m_get_item_info_function_mutex(),
105 m_get_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 m_get_item_info_retbuffer_mutex() {}
Jason Molenda2fd83352014-02-05 05:44:54 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108AppleGetItemInfoHandler::~AppleGetItemInfoHandler() {}
Jason Molenda2fd83352014-02-05 05:44:54 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110void AppleGetItemInfoHandler::Detach() {
Jason Molenda2fd83352014-02-05 05:44:54 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 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,
115 std::defer_lock);
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);
118 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000119}
120
121// Compile our __lldb_backtrace_recording_get_item_info() function (from the
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122// source above in g_get_item_info_function_code) if we don't find that function
123// in the inferior
124// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
125// testing.)
Jason Molenda2fd83352014-02-05 05:44:54 +0000126//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127// Insert the __lldb_backtrace_recording_get_item_info into the inferior process
128// if needed.
Jason Molenda2fd83352014-02-05 05:44:54 +0000129//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130// Write the get_item_info_arglist into the inferior's memory space to prepare
131// for the call.
132//
133// Returns the address of the arguments written down in the inferior process,
134// which can be used to
Jason Molenda2fd83352014-02-05 05:44:54 +0000135// make the function call.
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137lldb::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;
Jason Molenda2fd83352014-02-05 05:44:54 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 // Scope for mutex locker:
146 {
147 std::lock_guard<std::mutex> guard(m_get_item_info_function_mutex);
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 // First stage is to make the UtilityFunction to hold our injected function:
Jason Molenda2fd83352014-02-05 05:44:54 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 if (!m_get_item_info_impl_code.get()) {
152 if (g_get_item_info_function_code != NULL) {
Zachary Turner97206d52017-05-12 04:51:55 +0000153 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 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));
158 if (error.Fail()) {
159 if (log)
160 log->Printf("Failed to get utility function: %s.",
161 error.AsCString());
162 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000163 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 if (!m_get_item_info_impl_code->Install(diagnostics, exe_ctx)) {
166 if (log) {
167 log->Printf("Failed to install get-item-info introspection.");
Sean Callanan579e70c2016-03-19 00:03:59 +0000168 diagnostics.Dump(log);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 }
170 m_get_item_info_impl_code.reset();
171 return args_addr;
Sean Callanan579e70c2016-03-19 00:03:59 +0000172 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 } else {
174 if (log)
175 log->Printf("No get-item-info introspection code found.");
176 return LLDB_INVALID_ADDRESS;
177 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 // Next make the runner function for our implementation utility function.
Zachary Turner97206d52017-05-12 04:51:55 +0000180 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181
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();
187
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);
Jason Molendaed2977f2016-10-27 23:52:46 +0000191 if (error.Fail() || get_item_info_caller == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 if (log)
193 log->Printf("Error Inserting get-item-info function: \"%s\".",
194 error.AsCString());
Jason Molenda2fd83352014-02-05 05:44:54 +0000195 return args_addr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 }
197 } else {
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) {
201 if (log)
202 log->Printf("Failed to get get-item-info introspection caller.");
203 m_get_item_info_impl_code.reset();
204 return args_addr;
205 }
206 }
207 }
208
209 diagnostics.Clear();
210
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...
216
217 if (!get_item_info_caller->WriteFunctionArguments(
218 exe_ctx, args_addr, get_item_info_arglist, diagnostics)) {
219 if (log) {
220 log->Printf("Error writing get-item-info function arguments.");
221 diagnostics.Dump(log);
Jason Molenda2fd83352014-02-05 05:44:54 +0000222 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000223
Jason Molenda2fd83352014-02-05 05:44:54 +0000224 return args_addr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 }
226
227 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000228}
229
230AppleGetItemInfoHandler::GetItemInfoReturnInfo
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231AppleGetItemInfoHandler::GetItemInfo(Thread &thread, uint64_t item,
232 addr_t page_to_free,
Zachary Turner97206d52017-05-12 04:51:55 +0000233 uint64_t page_to_free_size,
234 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 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));
Jason Molenda2fd83352014-02-05 05:44:54 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 GetItemInfoReturnInfo return_value;
242 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
243 return_value.item_buffer_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 error.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 if (thread.SafeToCallFunctions() == false) {
Jason Molenda2a6c2522014-02-15 00:20:40 +0000248 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 log->Printf("Not safe to call functions on thread 0x%" PRIx64,
250 thread.GetID());
251 error.SetErrorString("Not safe to call functions on this thread.");
Jason Molenda2fd83352014-02-05 05:44:54 +0000252 return return_value;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 }
254
255 // Set up the arguments for a call to
256
257 // struct get_item_info_return_values
258 // {
259 // uint64_t item_info_buffer_ptr; /* the address of the items buffer
260 // from libBacktraceRecording */
261 // uint64_t item_info_buffer_size; /* the size of the items buffer from
262 // libBacktraceRecording */
263 // };
264 //
265 // void __lldb_backtrace_recording_get_item_info
266 // (struct
267 // get_item_info_return_values
268 // *return_buffer,
269 // int debug,
270 // uint64_t item,
271 // void *page_to_free,
272 // uint64_t page_to_free_size)
273
274 // Where the return_buffer argument points to a 24 byte region of memory
275 // already allocated by lldb in
276 // the inferior process.
277
278 CompilerType clang_void_ptr_type =
279 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
280 Value return_buffer_ptr_value;
281 return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
282 return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
283
284 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
285 Value debug_value;
286 debug_value.SetValueType(Value::eValueTypeScalar);
287 debug_value.SetCompilerType(clang_int_type);
288
289 CompilerType clang_uint64_type =
290 clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
291 Value item_value;
292 item_value.SetValueType(Value::eValueTypeScalar);
293 item_value.SetCompilerType(clang_uint64_type);
294
295 Value page_to_free_value;
296 page_to_free_value.SetValueType(Value::eValueTypeScalar);
297 page_to_free_value.SetCompilerType(clang_void_ptr_type);
298
299 Value page_to_free_size_value;
300 page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
301 page_to_free_size_value.SetCompilerType(clang_uint64_type);
302
303 std::lock_guard<std::mutex> guard(m_get_item_info_retbuffer_mutex);
304 if (m_get_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
305 addr_t bufaddr = process_sp->AllocateMemory(
306 32, ePermissionsReadable | ePermissionsWritable, error);
307 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
308 if (log)
309 log->Printf("Failed to allocate memory for return buffer for get "
310 "current queues func call");
311 return return_value;
312 }
313 m_get_item_info_return_buffer_addr = bufaddr;
314 }
315
316 ValueList argument_values;
317
318 return_buffer_ptr_value.GetScalar() = m_get_item_info_return_buffer_addr;
319 argument_values.PushValue(return_buffer_ptr_value);
320
321 debug_value.GetScalar() = 0;
322 argument_values.PushValue(debug_value);
323
324 item_value.GetScalar() = item;
325 argument_values.PushValue(item_value);
326
327 if (page_to_free != LLDB_INVALID_ADDRESS)
328 page_to_free_value.GetScalar() = page_to_free;
329 else
330 page_to_free_value.GetScalar() = 0;
331 argument_values.PushValue(page_to_free_value);
332
333 page_to_free_size_value.GetScalar() = page_to_free_size;
334 argument_values.PushValue(page_to_free_size_value);
335
336 addr_t args_addr = SetupGetItemInfoFunction(thread, argument_values);
337
338 DiagnosticManager diagnostics;
339 ExecutionContext exe_ctx;
340 EvaluateExpressionOptions options;
341 options.SetUnwindOnError(true);
342 options.SetIgnoreBreakpoints(true);
343 options.SetStopOthers(true);
Pavel Labath43d35412016-12-06 11:24:51 +0000344 options.SetTimeout(std::chrono::milliseconds(500));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 options.SetTryAllThreads(false);
346 thread.CalculateExecutionContext(exe_ctx);
347
348 if (!m_get_item_info_impl_code) {
349 error.SetErrorString("Unable to compile function to call "
350 "__introspection_dispatch_queue_item_get_info");
351 return return_value;
352 }
353
354 ExpressionResults func_call_ret;
355 Value results;
356 FunctionCaller *func_caller = m_get_item_info_impl_code->GetFunctionCaller();
357 if (!func_caller) {
358 if (log)
359 log->Printf("Could not retrieve function caller for "
360 "__introspection_dispatch_queue_item_get_info.");
361 error.SetErrorString("Could not retrieve function caller for "
362 "__introspection_dispatch_queue_item_get_info.");
363 return return_value;
364 }
365
366 func_call_ret = func_caller->ExecuteFunction(exe_ctx, &args_addr, options,
367 diagnostics, results);
368 if (func_call_ret != eExpressionCompleted || !error.Success()) {
369 if (log)
370 log->Printf("Unable to call "
371 "__introspection_dispatch_queue_item_get_info(), got "
372 "ExpressionResults %d, error contains %s",
373 func_call_ret, error.AsCString(""));
374 error.SetErrorString("Unable to call "
375 "__introspection_dispatch_queue_get_item_info() for "
376 "list of queues");
377 return return_value;
378 }
379
380 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
381 m_get_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
382 if (!error.Success() ||
383 return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
384 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
385 return return_value;
386 }
387
388 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
389 m_get_item_info_return_buffer_addr + 8, 8, 0, error);
390
391 if (!error.Success()) {
392 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
393 return return_value;
394 }
395 if (log)
396 log->Printf("AppleGetItemInfoHandler called "
397 "__introspection_dispatch_queue_item_get_info (page_to_free == "
398 "0x%" PRIx64 ", size = %" PRId64
399 "), returned page is at 0x%" PRIx64 ", size %" PRId64,
400 page_to_free, page_to_free_size, return_value.item_buffer_ptr,
401 return_value.item_buffer_size);
402
403 return return_value;
Jason Molenda2fd83352014-02-05 05:44:54 +0000404}