blob: 809e2fca2c9fe6162b709130aa7f78b6fd65ff9a [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- AppleGetThreadItemInfoHandler.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 "AppleGetThreadItemInfoHandler.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/Expression.h"
17#include "lldb/Expression/FunctionCaller.h"
18#include "lldb/Expression/UtilityFunction.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000019#include "lldb/Symbol/ClangASTContext.h"
20#include "lldb/Symbol/Symbol.h"
21#include "lldb/Target/ExecutionContext.h"
22#include "lldb/Target/Process.h"
Jim Ingham151c0322015-09-15 21:13:50 +000023#include "lldb/Target/StackFrame.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000024#include "lldb/Target/Target.h"
25#include "lldb/Target/Thread.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000027#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000028#include "lldb/Utility/StreamString.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000029#include "lldb/lldb-private.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000030
31using namespace lldb;
32using namespace lldb_private;
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034const char
35 *AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_name =
36 "__lldb_backtrace_recording_get_thread_item_info";
37const char
38 *AppleGetThreadItemInfoHandler::g_get_thread_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 typedef void *pthread_t; \n\
58 extern int printf(const char *format, ...); \n\
59 extern pthread_t pthread_self(void); \n\
60 \n\
61 /* \n\
62 * libBacktraceRecording defines \n\
63 */ \n\
64 \n\
65 typedef uint32_t queue_list_scope_t; \n\
66 typedef void *dispatch_queue_t; \n\
67 typedef void *introspection_dispatch_queue_info_t; \n\
68 typedef void *introspection_dispatch_item_info_ref; \n\
69 \n\
Jason Molendada276f92014-02-11 00:36:18 +000070 extern void __introspection_dispatch_thread_get_item_info (uint64_t thread_id, \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000071 introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
72 uint64_t *returned_queues_buffer_size); \n\
73 \n\
74 /* \n\
75 * return type define \n\
76 */ \n\
77 \n\
78 struct get_thread_item_info_return_values \n\
79 { \n\
80 uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
81 uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
82 }; \n\
83 \n\
84 void __lldb_backtrace_recording_get_thread_item_info \n\
85 (struct get_thread_item_info_return_values *return_buffer, \n\
86 int debug, \n\
Jason Molendada276f92014-02-11 00:36:18 +000087 uint64_t thread_id, \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000088 void *page_to_free, \n\
89 uint64_t page_to_free_size) \n\
90{ \n\
91 void *pthread_id = pthread_self (); \n\
92 if (debug) \n\
Jason Molendada276f92014-02-11 00:36:18 +000093 printf (\"entering get_thread_item_info with args return_buffer == %p, debug == %d, thread id == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, (uint64_t) thread_id, page_to_free, page_to_free_size); \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000094 if (page_to_free != 0) \n\
95 { \n\
96 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
97 } \n\
98 \n\
Jason Molendada276f92014-02-11 00:36:18 +000099 __introspection_dispatch_thread_get_item_info (thread_id, \n\
Jason Molenda2fd83352014-02-05 05:44:54 +0000100 (void**)&return_buffer->item_info_buffer_ptr, \n\
101 &return_buffer->item_info_buffer_size); \n\
102} \n\
103} \n\
104";
105
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000106AppleGetThreadItemInfoHandler::AppleGetThreadItemInfoHandler(Process *process)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 : m_process(process), m_get_thread_item_info_impl_code(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000108 m_get_thread_item_info_function_mutex(),
109 m_get_thread_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 m_get_thread_item_info_retbuffer_mutex() {}
111
112AppleGetThreadItemInfoHandler::~AppleGetThreadItemInfoHandler() {}
113
114void AppleGetThreadItemInfoHandler::Detach() {
115
116 if (m_process && m_process->IsAlive() &&
117 m_get_thread_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
118 std::unique_lock<std::mutex> lock(m_get_thread_item_info_retbuffer_mutex,
119 std::defer_lock);
120 lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
121 m_process->DeallocateMemory(m_get_thread_item_info_return_buffer_addr);
122 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000123}
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125// Compile our __lldb_backtrace_recording_get_thread_item_info() function (from
Adrian Prantl05097242018-04-30 16:49:04 +0000126// the source above in g_get_thread_item_info_function_code) if we don't find
127// that function in the inferior already with USE_BUILTIN_FUNCTION defined.
128// (e.g. this would be the case for testing.)
Jason Molenda2fd83352014-02-05 05:44:54 +0000129//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130// Insert the __lldb_backtrace_recording_get_thread_item_info into the inferior
131// process if needed.
Jason Molenda2fd83352014-02-05 05:44:54 +0000132//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133// Write the get_thread_item_info_arglist into the inferior's memory space to
134// prepare for the call.
135//
136// Returns the address of the arguments written down in the inferior process,
Adrian Prantl05097242018-04-30 16:49:04 +0000137// which can be used to make the function call.
Jason Molenda2fd83352014-02-05 05:44:54 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139lldb::addr_t AppleGetThreadItemInfoHandler::SetupGetThreadItemInfoFunction(
140 Thread &thread, ValueList &get_thread_item_info_arglist) {
141 ThreadSP thread_sp(thread.shared_from_this());
142 ExecutionContext exe_ctx(thread_sp);
143 Address impl_code_address;
144 DiagnosticManager diagnostics;
145 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
146 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
147 FunctionCaller *get_thread_item_info_caller = nullptr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 // Scope for mutex locker:
150 {
151 std::lock_guard<std::mutex> guard(m_get_thread_item_info_function_mutex);
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 // First stage is to make the ClangUtility to hold our injected function:
Jason Molenda2fd83352014-02-05 05:44:54 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 if (!m_get_thread_item_info_impl_code.get()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000156 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 if (g_get_thread_item_info_function_code != NULL) {
158 m_get_thread_item_info_impl_code.reset(
159 exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
160 g_get_thread_item_info_function_code, eLanguageTypeC,
161 g_get_thread_item_info_function_name, error));
162 if (error.Fail()) {
163 if (log)
164 log->Printf("Failed to get UtilityFunction for "
165 "get-thread-item-info introspection: %s.",
166 error.AsCString());
167 m_get_thread_item_info_impl_code.reset();
168 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000169 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 if (!m_get_thread_item_info_impl_code->Install(diagnostics, exe_ctx)) {
172 if (log) {
173 log->Printf(
174 "Failed to install get-thread-item-info introspection.");
Sean Callanan579e70c2016-03-19 00:03:59 +0000175 diagnostics.Dump(log);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 m_get_thread_item_info_impl_code.reset();
179 return args_addr;
180 }
181 } else {
182 if (log)
183 log->Printf("No get-thread-item-info introspection code found.");
184 return LLDB_INVALID_ADDRESS;
185 }
186
187 // Also make the FunctionCaller for this UtilityFunction:
188
189 ClangASTContext *clang_ast_context =
190 thread.GetProcess()->GetTarget().GetScratchClangASTContext();
191 CompilerType get_thread_item_info_return_type =
192 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
193
194 get_thread_item_info_caller =
195 m_get_thread_item_info_impl_code->MakeFunctionCaller(
196 get_thread_item_info_return_type, get_thread_item_info_arglist,
197 thread_sp, error);
Jason Molendaed2977f2016-10-27 23:52:46 +0000198 if (error.Fail() || get_thread_item_info_caller == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 if (log)
200 log->Printf("Failed to install get-thread-item-info introspection "
201 "caller: %s.",
202 error.AsCString());
203 m_get_thread_item_info_impl_code.reset();
204 return args_addr;
205 }
206
207 } else {
208 get_thread_item_info_caller =
209 m_get_thread_item_info_impl_code->GetFunctionCaller();
210 }
211 }
212
213 diagnostics.Clear();
214
215 // Now write down the argument values for this particular call. This looks
Adrian Prantl05097242018-04-30 16:49:04 +0000216 // like it might be a race condition if other threads were calling into here,
217 // but actually it isn't because we allocate a new args structure for this
218 // call by passing args_addr = LLDB_INVALID_ADDRESS...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219
220 if (!get_thread_item_info_caller->WriteFunctionArguments(
221 exe_ctx, args_addr, get_thread_item_info_arglist, diagnostics)) {
222 if (log) {
223 log->Printf("Error writing get-thread-item-info function arguments");
224 diagnostics.Dump(log);
225 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000226 return args_addr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 }
228
229 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000230}
231
232AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233AppleGetThreadItemInfoHandler::GetThreadItemInfo(Thread &thread,
234 tid_t thread_id,
235 addr_t page_to_free,
236 uint64_t page_to_free_size,
Zachary Turner97206d52017-05-12 04:51:55 +0000237 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
239 ProcessSP process_sp(thread.CalculateProcess());
240 TargetSP target_sp(thread.CalculateTarget());
241 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
242 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
Jason Molenda2fd83352014-02-05 05:44:54 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 GetThreadItemInfoReturnInfo return_value;
245 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
246 return_value.item_buffer_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 error.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000249
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000250 if (!thread.SafeToCallFunctions()) {
Jason Molenda2a6c2522014-02-15 00:20:40 +0000251 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 log->Printf("Not safe to call functions on thread 0x%" PRIx64,
253 thread.GetID());
254 error.SetErrorString("Not safe to call functions on this thread.");
Jason Molenda2fd83352014-02-05 05:44:54 +0000255 return return_value;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 }
257
258 // Set up the arguments for a call to
259
Adrian Prantl05097242018-04-30 16:49:04 +0000260 // struct get_thread_item_info_return_values {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 // uint64_t item_info_buffer_ptr; /* the address of the items buffer
262 // from libBacktraceRecording */
263 // uint64_t item_info_buffer_size; /* the size of the items buffer from
264 // libBacktraceRecording */
265 // };
266 //
267 // void __lldb_backtrace_recording_get_thread_item_info
268 // (struct
269 // get_thread_item_info_return_values
270 // *return_buffer,
271 // int debug,
272 // void *page_to_free,
273 // uint64_t page_to_free_size)
274
275 // Where the return_buffer argument points to a 24 byte region of memory
Adrian Prantl05097242018-04-30 16:49:04 +0000276 // already allocated by lldb in the inferior process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277
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 thread_id_value;
292 thread_id_value.SetValueType(Value::eValueTypeScalar);
293 thread_id_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_thread_item_info_retbuffer_mutex);
304 if (m_get_thread_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_thread_item_info_return_buffer_addr = bufaddr;
314 }
315
316 ValueList argument_values;
317
318 return_buffer_ptr_value.GetScalar() =
319 m_get_thread_item_info_return_buffer_addr;
320 argument_values.PushValue(return_buffer_ptr_value);
321
322 debug_value.GetScalar() = 0;
323 argument_values.PushValue(debug_value);
324
325 thread_id_value.GetScalar() = thread_id;
326 argument_values.PushValue(thread_id_value);
327
328 if (page_to_free != LLDB_INVALID_ADDRESS)
329 page_to_free_value.GetScalar() = page_to_free;
330 else
331 page_to_free_value.GetScalar() = 0;
332 argument_values.PushValue(page_to_free_value);
333
334 page_to_free_size_value.GetScalar() = page_to_free_size;
335 argument_values.PushValue(page_to_free_size_value);
336
337 addr_t args_addr = SetupGetThreadItemInfoFunction(thread, argument_values);
338
339 DiagnosticManager diagnostics;
340 ExecutionContext exe_ctx;
341 EvaluateExpressionOptions options;
342 FunctionCaller *get_thread_item_info_caller = nullptr;
343
344 options.SetUnwindOnError(true);
345 options.SetIgnoreBreakpoints(true);
346 options.SetStopOthers(true);
Pavel Labath43d35412016-12-06 11:24:51 +0000347 options.SetTimeout(std::chrono::milliseconds(500));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 options.SetTryAllThreads(false);
Raphael Isemannc01783a2018-08-29 22:50:54 +0000349 options.SetIsForUtilityExpr(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 thread.CalculateExecutionContext(exe_ctx);
351
352 if (!m_get_thread_item_info_impl_code) {
353 error.SetErrorString("Unable to compile function to call "
354 "__introspection_dispatch_thread_get_item_info");
355 return return_value;
356 }
357
358 get_thread_item_info_caller =
359 m_get_thread_item_info_impl_code->GetFunctionCaller();
360
361 if (!get_thread_item_info_caller) {
362 error.SetErrorString("Unable to compile function caller for "
363 "__introspection_dispatch_thread_get_item_info");
364 return return_value;
365 }
366
367 ExpressionResults func_call_ret;
368 Value results;
369 func_call_ret = get_thread_item_info_caller->ExecuteFunction(
370 exe_ctx, &args_addr, options, diagnostics, results);
371 if (func_call_ret != eExpressionCompleted || !error.Success()) {
372 if (log)
373 log->Printf("Unable to call "
374 "__introspection_dispatch_thread_get_item_info(), got "
375 "ExpressionResults %d, error contains %s",
376 func_call_ret, error.AsCString(""));
377 error.SetErrorString("Unable to call "
378 "__introspection_dispatch_thread_get_item_info() for "
379 "list of queues");
380 return return_value;
381 }
382
383 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
384 m_get_thread_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS,
385 error);
386 if (!error.Success() ||
387 return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
388 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
389 return return_value;
390 }
391
392 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
393 m_get_thread_item_info_return_buffer_addr + 8, 8, 0, error);
394
395 if (!error.Success()) {
396 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
397 return return_value;
398 }
399
400 if (log)
401 log->Printf("AppleGetThreadItemInfoHandler called "
402 "__introspection_dispatch_thread_get_item_info (page_to_free "
403 "== 0x%" PRIx64 ", size = %" PRId64
404 "), returned page is at 0x%" PRIx64 ", size %" PRId64,
405 page_to_free, page_to_free_size, return_value.item_buffer_ptr,
406 return_value.item_buffer_size);
407
408 return return_value;
Jason Molenda2fd83352014-02-05 05:44:54 +0000409}