blob: d3ab0fac30a49b99f5ac5e42a8a7ba5142af3bcd [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- AppleGetThreadItemInfoHandler.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 "AppleGetThreadItemInfoHandler.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/ConstString.h"
19#include "lldb/Core/Log.h"
20#include "lldb/Core/Module.h"
21#include "lldb/Core/StreamString.h"
22#include "lldb/Core/Value.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000023#include "lldb/Expression/DiagnosticManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000024#include "lldb/Expression/Expression.h"
25#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"
Jim Ingham151c0322015-09-15 21:13:50 +000031#include "lldb/Target/StackFrame.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000032#include "lldb/Target/Target.h"
33#include "lldb/Target/Thread.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000034#include "lldb/lldb-private.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000035
36using namespace lldb;
37using namespace lldb_private;
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039const char
40 *AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_name =
41 "__lldb_backtrace_recording_get_thread_item_info";
42const char
43 *AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_code =
44 " \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000045extern \"C\" \n\
46{ \n\
47 /* \n\
48 * mach defines \n\
49 */ \n\
50 \n\
51 typedef unsigned int uint32_t; \n\
52 typedef unsigned long long uint64_t; \n\
53 typedef uint32_t mach_port_t; \n\
54 typedef mach_port_t vm_map_t; \n\
55 typedef int kern_return_t; \n\
56 typedef uint64_t mach_vm_address_t; \n\
57 typedef uint64_t mach_vm_size_t; \n\
58 \n\
59 mach_port_t mach_task_self (); \n\
60 kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
61 \n\
62 typedef void *pthread_t; \n\
63 extern int printf(const char *format, ...); \n\
64 extern pthread_t pthread_self(void); \n\
65 \n\
66 /* \n\
67 * libBacktraceRecording defines \n\
68 */ \n\
69 \n\
70 typedef uint32_t queue_list_scope_t; \n\
71 typedef void *dispatch_queue_t; \n\
72 typedef void *introspection_dispatch_queue_info_t; \n\
73 typedef void *introspection_dispatch_item_info_ref; \n\
74 \n\
Jason Molendada276f92014-02-11 00:36:18 +000075 extern void __introspection_dispatch_thread_get_item_info (uint64_t thread_id, \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000076 introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
77 uint64_t *returned_queues_buffer_size); \n\
78 \n\
79 /* \n\
80 * return type define \n\
81 */ \n\
82 \n\
83 struct get_thread_item_info_return_values \n\
84 { \n\
85 uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
86 uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
87 }; \n\
88 \n\
89 void __lldb_backtrace_recording_get_thread_item_info \n\
90 (struct get_thread_item_info_return_values *return_buffer, \n\
91 int debug, \n\
Jason Molendada276f92014-02-11 00:36:18 +000092 uint64_t thread_id, \n\
Jason Molenda2fd83352014-02-05 05:44:54 +000093 void *page_to_free, \n\
94 uint64_t page_to_free_size) \n\
95{ \n\
96 void *pthread_id = pthread_self (); \n\
97 if (debug) \n\
Jason Molendada276f92014-02-11 00:36:18 +000098 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 +000099 if (page_to_free != 0) \n\
100 { \n\
101 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
102 } \n\
103 \n\
Jason Molendada276f92014-02-11 00:36:18 +0000104 __introspection_dispatch_thread_get_item_info (thread_id, \n\
Jason Molenda2fd83352014-02-05 05:44:54 +0000105 (void**)&return_buffer->item_info_buffer_ptr, \n\
106 &return_buffer->item_info_buffer_size); \n\
107} \n\
108} \n\
109";
110
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000111AppleGetThreadItemInfoHandler::AppleGetThreadItemInfoHandler(Process *process)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 : m_process(process), m_get_thread_item_info_impl_code(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000113 m_get_thread_item_info_function_mutex(),
114 m_get_thread_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 m_get_thread_item_info_retbuffer_mutex() {}
116
117AppleGetThreadItemInfoHandler::~AppleGetThreadItemInfoHandler() {}
118
119void AppleGetThreadItemInfoHandler::Detach() {
120
121 if (m_process && m_process->IsAlive() &&
122 m_get_thread_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
123 std::unique_lock<std::mutex> lock(m_get_thread_item_info_retbuffer_mutex,
124 std::defer_lock);
125 lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
126 m_process->DeallocateMemory(m_get_thread_item_info_return_buffer_addr);
127 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130// Compile our __lldb_backtrace_recording_get_thread_item_info() function (from
131// the
132// source above in g_get_thread_item_info_function_code) if we don't find that
133// function in the inferior
134// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
135// testing.)
Jason Molenda2fd83352014-02-05 05:44:54 +0000136//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137// Insert the __lldb_backtrace_recording_get_thread_item_info into the inferior
138// process if needed.
Jason Molenda2fd83352014-02-05 05:44:54 +0000139//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140// Write the get_thread_item_info_arglist into the inferior's memory space to
141// prepare for the call.
142//
143// Returns the address of the arguments written down in the inferior process,
144// which can be used to
Jason Molenda2fd83352014-02-05 05:44:54 +0000145// make the function call.
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147lldb::addr_t AppleGetThreadItemInfoHandler::SetupGetThreadItemInfoFunction(
148 Thread &thread, ValueList &get_thread_item_info_arglist) {
149 ThreadSP thread_sp(thread.shared_from_this());
150 ExecutionContext exe_ctx(thread_sp);
151 Address impl_code_address;
152 DiagnosticManager diagnostics;
153 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
154 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
155 FunctionCaller *get_thread_item_info_caller = nullptr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 // Scope for mutex locker:
158 {
159 std::lock_guard<std::mutex> guard(m_get_thread_item_info_function_mutex);
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 // First stage is to make the ClangUtility to hold our injected function:
Jason Molenda2fd83352014-02-05 05:44:54 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 if (!m_get_thread_item_info_impl_code.get()) {
164 Error error;
165 if (g_get_thread_item_info_function_code != NULL) {
166 m_get_thread_item_info_impl_code.reset(
167 exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
168 g_get_thread_item_info_function_code, eLanguageTypeC,
169 g_get_thread_item_info_function_name, error));
170 if (error.Fail()) {
171 if (log)
172 log->Printf("Failed to get UtilityFunction for "
173 "get-thread-item-info introspection: %s.",
174 error.AsCString());
175 m_get_thread_item_info_impl_code.reset();
176 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000177 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 if (!m_get_thread_item_info_impl_code->Install(diagnostics, exe_ctx)) {
180 if (log) {
181 log->Printf(
182 "Failed to install get-thread-item-info introspection.");
Sean Callanan579e70c2016-03-19 00:03:59 +0000183 diagnostics.Dump(log);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 m_get_thread_item_info_impl_code.reset();
187 return args_addr;
188 }
189 } else {
190 if (log)
191 log->Printf("No get-thread-item-info introspection code found.");
192 return LLDB_INVALID_ADDRESS;
193 }
194
195 // Also make the FunctionCaller for this UtilityFunction:
196
197 ClangASTContext *clang_ast_context =
198 thread.GetProcess()->GetTarget().GetScratchClangASTContext();
199 CompilerType get_thread_item_info_return_type =
200 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
201
202 get_thread_item_info_caller =
203 m_get_thread_item_info_impl_code->MakeFunctionCaller(
204 get_thread_item_info_return_type, get_thread_item_info_arglist,
205 thread_sp, error);
206 if (error.Fail()) {
207 if (log)
208 log->Printf("Failed to install get-thread-item-info introspection "
209 "caller: %s.",
210 error.AsCString());
211 m_get_thread_item_info_impl_code.reset();
212 return args_addr;
213 }
214
215 } else {
216 get_thread_item_info_caller =
217 m_get_thread_item_info_impl_code->GetFunctionCaller();
218 }
219 }
220
221 diagnostics.Clear();
222
223 // Now write down the argument values for this particular call. This looks
224 // like it might be a race condition
225 // if other threads were calling into here, but actually it isn't because we
226 // allocate a new args structure for
227 // this call by passing args_addr = LLDB_INVALID_ADDRESS...
228
229 if (!get_thread_item_info_caller->WriteFunctionArguments(
230 exe_ctx, args_addr, get_thread_item_info_arglist, diagnostics)) {
231 if (log) {
232 log->Printf("Error writing get-thread-item-info function arguments");
233 diagnostics.Dump(log);
234 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000235 return args_addr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 }
237
238 return args_addr;
Jason Molenda2fd83352014-02-05 05:44:54 +0000239}
240
241AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242AppleGetThreadItemInfoHandler::GetThreadItemInfo(Thread &thread,
243 tid_t thread_id,
244 addr_t page_to_free,
245 uint64_t page_to_free_size,
246 Error &error) {
247 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
248 ProcessSP process_sp(thread.CalculateProcess());
249 TargetSP target_sp(thread.CalculateTarget());
250 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
251 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
Jason Molenda2fd83352014-02-05 05:44:54 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 GetThreadItemInfoReturnInfo return_value;
254 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
255 return_value.item_buffer_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 error.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 if (thread.SafeToCallFunctions() == false) {
Jason Molenda2a6c2522014-02-15 00:20:40 +0000260 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 log->Printf("Not safe to call functions on thread 0x%" PRIx64,
262 thread.GetID());
263 error.SetErrorString("Not safe to call functions on this thread.");
Jason Molenda2fd83352014-02-05 05:44:54 +0000264 return return_value;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 }
266
267 // Set up the arguments for a call to
268
269 // struct get_thread_item_info_return_values
270 // {
271 // uint64_t item_info_buffer_ptr; /* the address of the items buffer
272 // from libBacktraceRecording */
273 // uint64_t item_info_buffer_size; /* the size of the items buffer from
274 // libBacktraceRecording */
275 // };
276 //
277 // void __lldb_backtrace_recording_get_thread_item_info
278 // (struct
279 // get_thread_item_info_return_values
280 // *return_buffer,
281 // int debug,
282 // void *page_to_free,
283 // uint64_t page_to_free_size)
284
285 // Where the return_buffer argument points to a 24 byte region of memory
286 // already allocated by lldb in
287 // the inferior process.
288
289 CompilerType clang_void_ptr_type =
290 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
291 Value return_buffer_ptr_value;
292 return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
293 return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
294
295 CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
296 Value debug_value;
297 debug_value.SetValueType(Value::eValueTypeScalar);
298 debug_value.SetCompilerType(clang_int_type);
299
300 CompilerType clang_uint64_type =
301 clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
302 Value thread_id_value;
303 thread_id_value.SetValueType(Value::eValueTypeScalar);
304 thread_id_value.SetCompilerType(clang_uint64_type);
305
306 Value page_to_free_value;
307 page_to_free_value.SetValueType(Value::eValueTypeScalar);
308 page_to_free_value.SetCompilerType(clang_void_ptr_type);
309
310 Value page_to_free_size_value;
311 page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
312 page_to_free_size_value.SetCompilerType(clang_uint64_type);
313
314 std::lock_guard<std::mutex> guard(m_get_thread_item_info_retbuffer_mutex);
315 if (m_get_thread_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
316 addr_t bufaddr = process_sp->AllocateMemory(
317 32, ePermissionsReadable | ePermissionsWritable, error);
318 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
319 if (log)
320 log->Printf("Failed to allocate memory for return buffer for get "
321 "current queues func call");
322 return return_value;
323 }
324 m_get_thread_item_info_return_buffer_addr = bufaddr;
325 }
326
327 ValueList argument_values;
328
329 return_buffer_ptr_value.GetScalar() =
330 m_get_thread_item_info_return_buffer_addr;
331 argument_values.PushValue(return_buffer_ptr_value);
332
333 debug_value.GetScalar() = 0;
334 argument_values.PushValue(debug_value);
335
336 thread_id_value.GetScalar() = thread_id;
337 argument_values.PushValue(thread_id_value);
338
339 if (page_to_free != LLDB_INVALID_ADDRESS)
340 page_to_free_value.GetScalar() = page_to_free;
341 else
342 page_to_free_value.GetScalar() = 0;
343 argument_values.PushValue(page_to_free_value);
344
345 page_to_free_size_value.GetScalar() = page_to_free_size;
346 argument_values.PushValue(page_to_free_size_value);
347
348 addr_t args_addr = SetupGetThreadItemInfoFunction(thread, argument_values);
349
350 DiagnosticManager diagnostics;
351 ExecutionContext exe_ctx;
352 EvaluateExpressionOptions options;
353 FunctionCaller *get_thread_item_info_caller = nullptr;
354
355 options.SetUnwindOnError(true);
356 options.SetIgnoreBreakpoints(true);
357 options.SetStopOthers(true);
358 options.SetTimeoutUsec(500000);
359 options.SetTryAllThreads(false);
360 thread.CalculateExecutionContext(exe_ctx);
361
362 if (!m_get_thread_item_info_impl_code) {
363 error.SetErrorString("Unable to compile function to call "
364 "__introspection_dispatch_thread_get_item_info");
365 return return_value;
366 }
367
368 get_thread_item_info_caller =
369 m_get_thread_item_info_impl_code->GetFunctionCaller();
370
371 if (!get_thread_item_info_caller) {
372 error.SetErrorString("Unable to compile function caller for "
373 "__introspection_dispatch_thread_get_item_info");
374 return return_value;
375 }
376
377 ExpressionResults func_call_ret;
378 Value results;
379 func_call_ret = get_thread_item_info_caller->ExecuteFunction(
380 exe_ctx, &args_addr, options, diagnostics, results);
381 if (func_call_ret != eExpressionCompleted || !error.Success()) {
382 if (log)
383 log->Printf("Unable to call "
384 "__introspection_dispatch_thread_get_item_info(), got "
385 "ExpressionResults %d, error contains %s",
386 func_call_ret, error.AsCString(""));
387 error.SetErrorString("Unable to call "
388 "__introspection_dispatch_thread_get_item_info() for "
389 "list of queues");
390 return return_value;
391 }
392
393 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
394 m_get_thread_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS,
395 error);
396 if (!error.Success() ||
397 return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
398 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
399 return return_value;
400 }
401
402 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
403 m_get_thread_item_info_return_buffer_addr + 8, 8, 0, error);
404
405 if (!error.Success()) {
406 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
407 return return_value;
408 }
409
410 if (log)
411 log->Printf("AppleGetThreadItemInfoHandler called "
412 "__introspection_dispatch_thread_get_item_info (page_to_free "
413 "== 0x%" PRIx64 ", size = %" PRId64
414 "), returned page is at 0x%" PRIx64 ", size %" PRId64,
415 page_to_free, page_to_free_size, return_value.item_buffer_ptr,
416 return_value.item_buffer_size);
417
418 return return_value;
Jason Molenda2fd83352014-02-05 05:44:54 +0000419}