blob: 92e2b24d8484ce49b13c0cc91370394a06300dd9 [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- AppleGetItemInfoHandler.cpp -------------------------------*- C++
2//-*-===//
Jason Molenda2fd83352014-02-05 05:44:54 +00003//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jason Molenda2fd83352014-02-05 05:44:54 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "AppleGetItemInfoHandler.h"
11
Jason Molenda2fd83352014-02-05 05:44:54 +000012
Jason Molenda2fd83352014-02-05 05:44:54 +000013#include "lldb/Core/Module.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000014#include "lldb/Core/Value.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000015#include "lldb/Expression/DiagnosticManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000016#include "lldb/Expression/FunctionCaller.h"
17#include "lldb/Expression/UtilityFunction.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000018#include "lldb/Symbol/ClangASTContext.h"
19#include "lldb/Symbol/Symbol.h"
20#include "lldb/Target/ExecutionContext.h"
21#include "lldb/Target/Process.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/Thread.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000024#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000025#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/StreamString.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000027
28using namespace lldb;
29using namespace lldb_private;
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031const char *AppleGetItemInfoHandler::g_get_item_info_function_name =
32 "__lldb_backtrace_recording_get_item_info";
33const char *AppleGetItemInfoHandler::g_get_item_info_function_code =
34 " \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000035extern \"C\" \n\
36{ \n\
37 /* \n\
38 * mach defines \n\
39 */ \n\
40 \n\
41 typedef unsigned int uint32_t; \n\
42 typedef unsigned long long uint64_t; \n\
43 typedef uint32_t mach_port_t; \n\
44 typedef mach_port_t vm_map_t; \n\
45 typedef int kern_return_t; \n\
46 typedef uint64_t mach_vm_address_t; \n\
47 typedef uint64_t mach_vm_size_t; \n\
48 \n\
49 mach_port_t mach_task_self (); \n\
50 kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
51 \n\
52 /* \n\
53 * libBacktraceRecording defines \n\
54 */ \n\
55 \n\
56 typedef uint32_t queue_list_scope_t; \n\
57 typedef void *dispatch_queue_t; \n\
58 typedef void *introspection_dispatch_queue_info_t; \n\
59 typedef void *introspection_dispatch_item_info_ref; \n\
60 \n\
61 extern uint64_t __introspection_dispatch_queue_item_get_info (introspection_dispatch_item_info_ref item_info_ref, \n\
62 introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
63 uint64_t *returned_queues_buffer_size); \n\
64 extern int printf(const char *format, ...); \n\
65 \n\
66 /* \n\
67 * return type define \n\
68 */ \n\
69 \n\
70 struct get_item_info_return_values \n\
71 { \n\
72 uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
73 uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
74 }; \n\
75 \n\
76 void __lldb_backtrace_recording_get_item_info \n\
77 (struct get_item_info_return_values *return_buffer, \n\
78 int debug, \n\
79 uint64_t /* introspection_dispatch_item_info_ref item_info_ref */ item, \n\
80 void *page_to_free, \n\
81 uint64_t page_to_free_size) \n\
82{ \n\
83 if (debug) \n\
84 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\
85 if (page_to_free != 0) \n\
86 { \n\
87 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
88 } \n\
89 \n\
90 __introspection_dispatch_queue_item_get_info ((void*) item, \n\
91 (void**)&return_buffer->item_info_buffer_ptr, \n\
92 &return_buffer->item_info_buffer_size); \n\
93} \n\
94} \n\
95";
96
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000097AppleGetItemInfoHandler::AppleGetItemInfoHandler(Process *process)
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 : m_process(process), m_get_item_info_impl_code(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000099 m_get_item_info_function_mutex(),
100 m_get_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 m_get_item_info_retbuffer_mutex() {}
Jason Molenda2fd83352014-02-05 05:44:54 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103AppleGetItemInfoHandler::~AppleGetItemInfoHandler() {}
Jason Molenda2fd83352014-02-05 05:44:54 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105void AppleGetItemInfoHandler::Detach() {
Jason Molenda2fd83352014-02-05 05:44:54 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 if (m_process && m_process->IsAlive() &&
108 m_get_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
109 std::unique_lock<std::mutex> lock(m_get_item_info_retbuffer_mutex,
110 std::defer_lock);
111 lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
112 m_process->DeallocateMemory(m_get_item_info_return_buffer_addr);
113 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000114}
115
116// Compile our __lldb_backtrace_recording_get_item_info() function (from the
Adrian Prantl05097242018-04-30 16:49:04 +0000117// source above in g_get_item_info_function_code) if we don't find that
118// function in the inferior already with USE_BUILTIN_FUNCTION defined. (e.g.
119// this would be the case for testing.)
Jason Molenda2fd83352014-02-05 05:44:54 +0000120//
Adrian Prantl05097242018-04-30 16:49:04 +0000121// Insert the __lldb_backtrace_recording_get_item_info into the inferior
122// process if needed.
Jason Molenda2fd83352014-02-05 05:44:54 +0000123//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124// Write the get_item_info_arglist into the inferior's memory space to prepare
125// for the call.
126//
127// Returns the address of the arguments written down in the inferior process,
Adrian Prantl05097242018-04-30 16:49:04 +0000128// which can be used to make the function call.
Jason Molenda2fd83352014-02-05 05:44:54 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130lldb::addr_t AppleGetItemInfoHandler::SetupGetItemInfoFunction(
131 Thread &thread, ValueList &get_item_info_arglist) {
132 ExecutionContext exe_ctx(thread.shared_from_this());
133 DiagnosticManager diagnostics;
134 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
135 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
136 FunctionCaller *get_item_info_caller = nullptr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 // Scope for mutex locker:
139 {
140 std::lock_guard<std::mutex> guard(m_get_item_info_function_mutex);
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000141
Adrian Prantl05097242018-04-30 16:49:04 +0000142 // First stage is to make the UtilityFunction to hold our injected
143 // function:
Jason Molenda2fd83352014-02-05 05:44:54 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 if (!m_get_item_info_impl_code.get()) {
146 if (g_get_item_info_function_code != NULL) {
Zachary Turner97206d52017-05-12 04:51:55 +0000147 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 m_get_item_info_impl_code.reset(
149 exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
150 g_get_item_info_function_code, eLanguageTypeObjC,
151 g_get_item_info_function_name, error));
152 if (error.Fail()) {
153 if (log)
154 log->Printf("Failed to get utility function: %s.",
155 error.AsCString());
156 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000157 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 if (!m_get_item_info_impl_code->Install(diagnostics, exe_ctx)) {
160 if (log) {
161 log->Printf("Failed to install get-item-info introspection.");
Sean Callanan579e70c2016-03-19 00:03:59 +0000162 diagnostics.Dump(log);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 }
164 m_get_item_info_impl_code.reset();
165 return args_addr;
Sean Callanan579e70c2016-03-19 00:03:59 +0000166 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 } else {
168 if (log)
169 log->Printf("No get-item-info introspection code found.");
170 return LLDB_INVALID_ADDRESS;
171 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 // Next make the runner function for our implementation utility function.
Zachary Turner97206d52017-05-12 04:51:55 +0000174 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175
176 TypeSystem *type_system =
177 thread.GetProcess()->GetTarget().GetScratchTypeSystemForLanguage(
178 nullptr, eLanguageTypeC);
179 CompilerType get_item_info_return_type =
180 type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
181
182 get_item_info_caller = m_get_item_info_impl_code->MakeFunctionCaller(
183 get_item_info_return_type, get_item_info_arglist,
184 thread.shared_from_this(), error);
Jason Molendaed2977f2016-10-27 23:52:46 +0000185 if (error.Fail() || get_item_info_caller == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 if (log)
187 log->Printf("Error Inserting get-item-info function: \"%s\".",
188 error.AsCString());
Jason Molenda2fd83352014-02-05 05:44:54 +0000189 return args_addr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 }
191 } else {
192 // If it's already made, then we can just retrieve the caller:
193 get_item_info_caller = m_get_item_info_impl_code->GetFunctionCaller();
194 if (!get_item_info_caller) {
195 if (log)
196 log->Printf("Failed to get get-item-info introspection caller.");
197 m_get_item_info_impl_code.reset();
198 return args_addr;
199 }
200 }
201 }
202
203 diagnostics.Clear();
204
205 // Now write down the argument values for this particular call. This looks
Adrian Prantl05097242018-04-30 16:49:04 +0000206 // like it might be a race condition if other threads were calling into here,
207 // but actually it isn't because we allocate a new args structure for this
208 // call by passing args_addr = LLDB_INVALID_ADDRESS...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209
210 if (!get_item_info_caller->WriteFunctionArguments(
211 exe_ctx, args_addr, get_item_info_arglist, diagnostics)) {
212 if (log) {
213 log->Printf("Error writing get-item-info function arguments.");
214 diagnostics.Dump(log);
Jason Molenda2fd83352014-02-05 05:44:54 +0000215 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000216
Jason Molenda2fd83352014-02-05 05:44:54 +0000217 return args_addr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 }
219
220 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000221}
222
223AppleGetItemInfoHandler::GetItemInfoReturnInfo
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224AppleGetItemInfoHandler::GetItemInfo(Thread &thread, uint64_t item,
225 addr_t page_to_free,
Zachary Turner97206d52017-05-12 04:51:55 +0000226 uint64_t page_to_free_size,
227 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
229 ProcessSP process_sp(thread.CalculateProcess());
230 TargetSP target_sp(thread.CalculateTarget());
231 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
232 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
Jason Molenda2fd83352014-02-05 05:44:54 +0000233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 GetItemInfoReturnInfo return_value;
235 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
236 return_value.item_buffer_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 error.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000239
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000240 if (!thread.SafeToCallFunctions()) {
Jason Molenda2a6c2522014-02-15 00:20:40 +0000241 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 log->Printf("Not safe to call functions on thread 0x%" PRIx64,
243 thread.GetID());
244 error.SetErrorString("Not safe to call functions on this thread.");
Jason Molenda2fd83352014-02-05 05:44:54 +0000245 return return_value;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 }
247
248 // Set up the arguments for a call to
249
250 // struct get_item_info_return_values
251 // {
252 // uint64_t item_info_buffer_ptr; /* the address of the items buffer
253 // from libBacktraceRecording */
254 // uint64_t item_info_buffer_size; /* the size of the items buffer from
255 // libBacktraceRecording */
256 // };
257 //
258 // void __lldb_backtrace_recording_get_item_info
259 // (struct
260 // get_item_info_return_values
261 // *return_buffer,
262 // int debug,
263 // uint64_t item,
264 // void *page_to_free,
265 // uint64_t page_to_free_size)
266
267 // Where the return_buffer argument points to a 24 byte region of memory
Adrian Prantl05097242018-04-30 16:49:04 +0000268 // already allocated by lldb in the inferior process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269
270 CompilerType clang_void_ptr_type =
271 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
272 Value return_buffer_ptr_value;
273 return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
274 return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
275
276 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
277 Value debug_value;
278 debug_value.SetValueType(Value::eValueTypeScalar);
279 debug_value.SetCompilerType(clang_int_type);
280
281 CompilerType clang_uint64_type =
282 clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
283 Value item_value;
284 item_value.SetValueType(Value::eValueTypeScalar);
285 item_value.SetCompilerType(clang_uint64_type);
286
287 Value page_to_free_value;
288 page_to_free_value.SetValueType(Value::eValueTypeScalar);
289 page_to_free_value.SetCompilerType(clang_void_ptr_type);
290
291 Value page_to_free_size_value;
292 page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
293 page_to_free_size_value.SetCompilerType(clang_uint64_type);
294
295 std::lock_guard<std::mutex> guard(m_get_item_info_retbuffer_mutex);
296 if (m_get_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
297 addr_t bufaddr = process_sp->AllocateMemory(
298 32, ePermissionsReadable | ePermissionsWritable, error);
299 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
300 if (log)
301 log->Printf("Failed to allocate memory for return buffer for get "
302 "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 DiagnosticManager diagnostics;
331 ExecutionContext exe_ctx;
332 EvaluateExpressionOptions options;
333 options.SetUnwindOnError(true);
334 options.SetIgnoreBreakpoints(true);
335 options.SetStopOthers(true);
Pavel Labath43d35412016-12-06 11:24:51 +0000336 options.SetTimeout(std::chrono::milliseconds(500));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 options.SetTryAllThreads(false);
Raphael Isemannc01783a2018-08-29 22:50:54 +0000338 options.SetIsForUtilityExpr(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 thread.CalculateExecutionContext(exe_ctx);
340
341 if (!m_get_item_info_impl_code) {
342 error.SetErrorString("Unable to compile function to call "
343 "__introspection_dispatch_queue_item_get_info");
344 return return_value;
345 }
346
347 ExpressionResults func_call_ret;
348 Value results;
349 FunctionCaller *func_caller = m_get_item_info_impl_code->GetFunctionCaller();
350 if (!func_caller) {
351 if (log)
352 log->Printf("Could not retrieve function caller for "
353 "__introspection_dispatch_queue_item_get_info.");
354 error.SetErrorString("Could not retrieve function caller for "
355 "__introspection_dispatch_queue_item_get_info.");
356 return return_value;
357 }
358
359 func_call_ret = func_caller->ExecuteFunction(exe_ctx, &args_addr, options,
360 diagnostics, results);
361 if (func_call_ret != eExpressionCompleted || !error.Success()) {
362 if (log)
363 log->Printf("Unable to call "
364 "__introspection_dispatch_queue_item_get_info(), got "
365 "ExpressionResults %d, error contains %s",
366 func_call_ret, error.AsCString(""));
367 error.SetErrorString("Unable to call "
368 "__introspection_dispatch_queue_get_item_info() for "
369 "list of queues");
370 return return_value;
371 }
372
373 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
374 m_get_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
375 if (!error.Success() ||
376 return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
377 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
378 return return_value;
379 }
380
381 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
382 m_get_item_info_return_buffer_addr + 8, 8, 0, error);
383
384 if (!error.Success()) {
385 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
386 return return_value;
387 }
388 if (log)
389 log->Printf("AppleGetItemInfoHandler called "
390 "__introspection_dispatch_queue_item_get_info (page_to_free == "
391 "0x%" PRIx64 ", size = %" PRId64
392 "), returned page is at 0x%" PRIx64 ", size %" PRId64,
393 page_to_free, page_to_free_size, return_value.item_buffer_ptr,
394 return_value.item_buffer_size);
395
396 return return_value;
Jason Molenda2fd83352014-02-05 05:44:54 +0000397}