blob: 7151f12598c62c0af1a9ad2ac45155b15485d7b6 [file] [log] [blame]
Jason Molenda2fd83352014-02-05 05:44:54 +00001//===-- AppleGetThreadItemInfoHandler.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 "AppleGetThreadItemInfoHandler.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"
25#include "lldb/Expression/ClangExpression.h"
26#include "lldb/Expression/ClangFunction.h"
27#include "lldb/Expression/ClangUtilityFunction.h"
28#include "lldb/Symbol/ClangASTContext.h"
29#include "lldb/Symbol/Symbol.h"
30#include "lldb/Target/ExecutionContext.h"
31#include "lldb/Target/Process.h"
32#include "lldb/Target/Target.h"
33#include "lldb/Target/Thread.h"
34
35using namespace lldb;
36using namespace lldb_private;
37
38const char *AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_name = "__lldb_backtrace_recording_get_thread_item_info";
39const char *AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_code = " \n\
40extern \"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\
70 extern void __introspection_dispatch_thread_get_item_info (pthread_t thread, \n\
71 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\
87 void *page_to_free, \n\
88 uint64_t page_to_free_size) \n\
89{ \n\
90 void *pthread_id = pthread_self (); \n\
91 if (debug) \n\
92 printf (\"entering get_thread_item_info with args return_buffer == %p, debug == %d, pthread id == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, (uint64_t) pthread_id, page_to_free, page_to_free_size); \n\
93 if (page_to_free != 0) \n\
94 { \n\
95 mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
96 } \n\
97 \n\
98 __introspection_dispatch_thread_get_item_info (pthread_id, \n\
99 (void**)&return_buffer->item_info_buffer_ptr, \n\
100 &return_buffer->item_info_buffer_size); \n\
101} \n\
102} \n\
103";
104
105AppleGetThreadItemInfoHandler::AppleGetThreadItemInfoHandler (Process *process) :
106 m_process (process),
107 m_get_thread_item_info_function (),
108 m_get_thread_item_info_impl_code (),
109 m_get_thread_item_info_function_mutex(),
110 m_get_thread_item_info_return_buffer_addr (LLDB_INVALID_ADDRESS),
111 m_get_thread_item_info_retbuffer_mutex()
112{
113}
114
115AppleGetThreadItemInfoHandler::~AppleGetThreadItemInfoHandler ()
116{
117}
118
119void
120AppleGetThreadItemInfoHandler::Detach ()
121{
122
123 if (m_process && m_process->IsAlive() && m_get_thread_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS)
124 {
125 Mutex::Locker locker;
126 locker.TryLock (m_get_thread_item_info_retbuffer_mutex); // Even if we don't get the lock, deallocate the buffer
127 m_process->DeallocateMemory (m_get_thread_item_info_return_buffer_addr);
128 }
129}
130
131// Compile our __lldb_backtrace_recording_get_thread_item_info() function (from the
132// source above in g_get_thread_item_info_function_code) if we don't find that function in the inferior
133// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for testing.)
134//
135// Insert the __lldb_backtrace_recording_get_thread_item_info into the inferior process if needed.
136//
137// Write the get_thread_item_info_arglist into the inferior's memory space to prepare for the call.
138//
139// Returns the address of the arguments written down in the inferior process, which can be used to
140// make the function call.
141
142lldb::addr_t
143AppleGetThreadItemInfoHandler::SetupGetThreadItemInfoFunction (Thread &thread, ValueList &get_thread_item_info_arglist)
144{
145 ExecutionContext exe_ctx (thread.shared_from_this());
146 Address impl_code_address;
147 StreamString errors;
148 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
149 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
150
151 // Scope for mutex locker:
152 {
153 Mutex::Locker locker(m_get_thread_item_info_function_mutex);
154
155 // First stage is to make the ClangUtility to hold our injected function:
156
157#define USE_BUILTIN_FUNCTION 0 // Define this to 1 and we will use the get_implementation function found in the target.
158 // This is useful for debugging additions to the get_impl function 'cause you don't have
159 // to bother with string-ifying the code into g_get_thread_item_info_function_code.
160
161 if (USE_BUILTIN_FUNCTION)
162 {
163 ConstString our_utility_function_name("__lldb_backtrace_recording_get_thread_item_info");
164 SymbolContextList sc_list;
165
166 exe_ctx.GetTargetRef().GetImages().FindSymbolsWithNameAndType (our_utility_function_name, eSymbolTypeCode, sc_list);
167 if (sc_list.GetSize() == 1)
168 {
169 SymbolContext sc;
170 sc_list.GetContextAtIndex(0, sc);
171 if (sc.symbol != NULL)
172 impl_code_address = sc.symbol->GetAddress();
173
174 //lldb::addr_t addr = impl_code_address.GetOpcodeLoadAddress (exe_ctx.GetTargetPtr());
175 //printf ("Getting address for our_utility_function: 0x%" PRIx64 ".\n", addr);
176 }
177 else
178 {
179 //printf ("Could not find queues introspection function address.\n");
180 return args_addr;
181 }
182 }
183 else if (!m_get_thread_item_info_impl_code.get())
184 {
185 if (g_get_thread_item_info_function_code != NULL)
186 {
187 m_get_thread_item_info_impl_code.reset (new ClangUtilityFunction (g_get_thread_item_info_function_code,
188 g_get_thread_item_info_function_name));
189 if (!m_get_thread_item_info_impl_code->Install(errors, exe_ctx))
190 {
191 if (log)
192 log->Printf ("Failed to install get-thread-item-info introspection: %s.", errors.GetData());
193 m_get_thread_item_info_impl_code.reset();
194 return args_addr;
195 }
196 }
197 else
198 {
199 if (log)
200 log->Printf("No get-thread-item-info introspection code found.");
201 errors.Printf ("No get-thread-item-info introspection code found.");
202 return LLDB_INVALID_ADDRESS;
203 }
204
205 impl_code_address.Clear();
206 impl_code_address.SetOffset(m_get_thread_item_info_impl_code->StartAddress());
207 }
208 else
209 {
210 impl_code_address.Clear();
211 impl_code_address.SetOffset(m_get_thread_item_info_impl_code->StartAddress());
212 }
213
214 // Next make the runner function for our implementation utility function.
215 if (!m_get_thread_item_info_function.get())
216 {
217 ClangASTContext *clang_ast_context = thread.GetProcess()->GetTarget().GetScratchClangASTContext();
218 ClangASTType get_thread_item_info_return_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
219 m_get_thread_item_info_function.reset(new ClangFunction (thread,
220 get_thread_item_info_return_type,
221 impl_code_address,
222 get_thread_item_info_arglist));
223
224 errors.Clear();
225 unsigned num_errors = m_get_thread_item_info_function->CompileFunction(errors);
226 if (num_errors)
227 {
228 if (log)
229 log->Printf ("Error compiling get-thread-item-info function: \"%s\".", errors.GetData());
230 return args_addr;
231 }
232
233 errors.Clear();
234 if (!m_get_thread_item_info_function->WriteFunctionWrapper(exe_ctx, errors))
235 {
236 if (log)
237 log->Printf ("Error Inserting get-thread-item-info function: \"%s\".", errors.GetData());
238 return args_addr;
239 }
240 }
241 }
242
243 errors.Clear();
244
245 // Now write down the argument values for this particular call. This looks like it might be a race condition
246 // if other threads were calling into here, but actually it isn't because we allocate a new args structure for
247 // this call by passing args_addr = LLDB_INVALID_ADDRESS...
248
249 if (!m_get_thread_item_info_function->WriteFunctionArguments (exe_ctx, args_addr, impl_code_address, get_thread_item_info_arglist, errors))
250 {
251 if (log)
252 log->Printf ("Error writing get-thread-item-info function arguments: \"%s\".", errors.GetData());
253 return args_addr;
254 }
255
256 return args_addr;
257}
258
259AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo
260AppleGetThreadItemInfoHandler::GetThreadItemInfo (Thread &thread, addr_t page_to_free, uint64_t page_to_free_size, Error &error)
261{
262 lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
263 ProcessSP process_sp (thread.CalculateProcess());
264 TargetSP target_sp (thread.CalculateTarget());
265 ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
266 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
267
268 GetThreadItemInfoReturnInfo return_value;
269 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
270 return_value.item_buffer_size = 0;
271
272 error.Clear();
273
274 // Set up the arguments for a call to
275
276 // struct get_thread_item_info_return_values
277 // {
278 // uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */
279 // uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */
280 // };
281 //
282 // void __lldb_backtrace_recording_get_thread_item_info
283 // (struct get_thread_item_info_return_values *return_buffer,
284 // int debug,
285 // void *page_to_free,
286 // uint64_t page_to_free_size)
287
288 // Where the return_buffer argument points to a 24 byte region of memory already allocated by lldb in
289 // the inferior process.
290
291 ClangASTType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
292 Value return_buffer_ptr_value;
293 return_buffer_ptr_value.SetValueType (Value::eValueTypeScalar);
294 return_buffer_ptr_value.SetClangType (clang_void_ptr_type);
295
296 ClangASTType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
297 Value debug_value;
298 debug_value.SetValueType (Value::eValueTypeScalar);
299 debug_value.SetClangType (clang_int_type);
300
301 Value page_to_free_value;
302 page_to_free_value.SetValueType (Value::eValueTypeScalar);
303 page_to_free_value.SetClangType (clang_void_ptr_type);
304
305 ClangASTType clang_uint64_type = clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
306 Value page_to_free_size_value;
307 page_to_free_size_value.SetValueType (Value::eValueTypeScalar);
308 page_to_free_size_value.SetClangType (clang_uint64_type);
309
310
311 Mutex::Locker locker(m_get_thread_item_info_retbuffer_mutex);
312 if (m_get_thread_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS)
313 {
314 addr_t bufaddr = process_sp->AllocateMemory (32, ePermissionsReadable | ePermissionsWritable, error);
315 if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS)
316 {
317 if (log)
318 log->Printf ("Failed to allocate memory for return buffer for get current queues func call");
319 return return_value;
320 }
321 m_get_thread_item_info_return_buffer_addr = bufaddr;
322 }
323
324 ValueList argument_values;
325
326 return_buffer_ptr_value.GetScalar() = m_get_thread_item_info_return_buffer_addr;
327 argument_values.PushValue (return_buffer_ptr_value);
328
329 debug_value.GetScalar() = 0;
330 argument_values.PushValue (debug_value);
331
332 if (page_to_free != LLDB_INVALID_ADDRESS)
333 page_to_free_value.GetScalar() = page_to_free;
334 else
335 page_to_free_value.GetScalar() = 0;
336 argument_values.PushValue (page_to_free_value);
337
338 page_to_free_size_value.GetScalar() = page_to_free_size;
339 argument_values.PushValue (page_to_free_size_value);
340
341 addr_t args_addr = SetupGetThreadItemInfoFunction (thread, argument_values);
342
343 StreamString errors;
344 ExecutionContext exe_ctx;
345 EvaluateExpressionOptions options;
346 options.SetUnwindOnError (true);
347 options.SetIgnoreBreakpoints (true);
348 options.SetStopOthers (true);
349 thread.CalculateExecutionContext (exe_ctx);
350
351 if (m_get_thread_item_info_function == NULL)
352 {
353 error.SetErrorString ("Unable to compile function to call __introspection_dispatch_thread_get_item_info");
354 return return_value;
355 }
356
357
358 ExecutionResults func_call_ret;
359 Value results;
360 func_call_ret = m_get_thread_item_info_function->ExecuteFunction (exe_ctx, &args_addr, options, errors, results);
361 if (func_call_ret != eExecutionCompleted || !error.Success())
362 {
363 if (log)
364 log->Printf ("Unable to call __introspection_dispatch_thread_get_item_info(), got ExecutionResults %d, error contains %s", func_call_ret, error.AsCString(""));
365 error.SetErrorString ("Unable to call __introspection_dispatch_thread_get_item_info() for list of queues");
366 return return_value;
367 }
368
369 return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory (m_get_thread_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
370 if (!error.Success() || return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS)
371 {
372 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
373 return return_value;
374 }
375
376 return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory (m_get_thread_item_info_return_buffer_addr + 8, 8, 0, error);
377
378 if (!error.Success())
379 {
380 return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
381 return return_value;
382 }
383
384 return return_value;
385}