1b9c1b51eSKate Stone //===-- AppleGetItemInfoHandler.cpp -------------------------------*- C++
2b9c1b51eSKate Stone //-*-===//
32fd83355SJason Molenda //
42fd83355SJason Molenda //                     The LLVM Compiler Infrastructure
52fd83355SJason Molenda //
62fd83355SJason Molenda // This file is distributed under the University of Illinois Open Source
72fd83355SJason Molenda // License. See LICENSE.TXT for details.
82fd83355SJason Molenda //
92fd83355SJason Molenda //===----------------------------------------------------------------------===//
102fd83355SJason Molenda 
112fd83355SJason Molenda #include "AppleGetItemInfoHandler.h"
122fd83355SJason Molenda 
132fd83355SJason Molenda // C Includes
142fd83355SJason Molenda // C++ Includes
152fd83355SJason Molenda // Other libraries and framework includes
162fd83355SJason Molenda // Project includes
172fd83355SJason Molenda 
182fd83355SJason Molenda #include "lldb/Core/Module.h"
192fd83355SJason Molenda #include "lldb/Core/Value.h"
20579e70c9SSean Callanan #include "lldb/Expression/DiagnosticManager.h"
21151c032cSJim Ingham #include "lldb/Expression/FunctionCaller.h"
22151c032cSJim Ingham #include "lldb/Expression/UtilityFunction.h"
232fd83355SJason Molenda #include "lldb/Symbol/ClangASTContext.h"
242fd83355SJason Molenda #include "lldb/Symbol/Symbol.h"
252fd83355SJason Molenda #include "lldb/Target/ExecutionContext.h"
262fd83355SJason Molenda #include "lldb/Target/Process.h"
272fd83355SJason Molenda #include "lldb/Target/Target.h"
282fd83355SJason Molenda #include "lldb/Target/Thread.h"
29bf9a7730SZachary Turner #include "lldb/Utility/ConstString.h"
306f9e6901SZachary Turner #include "lldb/Utility/Log.h"
31bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
322fd83355SJason Molenda 
332fd83355SJason Molenda using namespace lldb;
342fd83355SJason Molenda using namespace lldb_private;
352fd83355SJason Molenda 
36b9c1b51eSKate Stone const char *AppleGetItemInfoHandler::g_get_item_info_function_name =
37b9c1b51eSKate Stone     "__lldb_backtrace_recording_get_item_info";
38b9c1b51eSKate Stone const char *AppleGetItemInfoHandler::g_get_item_info_function_code =
39b9c1b51eSKate Stone     "                                  \n\
402fd83355SJason Molenda extern \"C\"                                                                                                    \n\
412fd83355SJason Molenda {                                                                                                               \n\
422fd83355SJason Molenda     /*                                                                                                          \n\
432fd83355SJason Molenda      * mach defines                                                                                             \n\
442fd83355SJason Molenda      */                                                                                                         \n\
452fd83355SJason Molenda                                                                                                                 \n\
462fd83355SJason Molenda     typedef unsigned int uint32_t;                                                                              \n\
472fd83355SJason Molenda     typedef unsigned long long uint64_t;                                                                        \n\
482fd83355SJason Molenda     typedef uint32_t mach_port_t;                                                                               \n\
492fd83355SJason Molenda     typedef mach_port_t vm_map_t;                                                                               \n\
502fd83355SJason Molenda     typedef int kern_return_t;                                                                                  \n\
512fd83355SJason Molenda     typedef uint64_t mach_vm_address_t;                                                                         \n\
522fd83355SJason Molenda     typedef uint64_t mach_vm_size_t;                                                                            \n\
532fd83355SJason Molenda                                                                                                                 \n\
542fd83355SJason Molenda     mach_port_t mach_task_self ();                                                                              \n\
552fd83355SJason Molenda     kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size);         \n\
562fd83355SJason Molenda                                                                                                                 \n\
572fd83355SJason Molenda     /*                                                                                                          \n\
582fd83355SJason Molenda      * libBacktraceRecording defines                                                                            \n\
592fd83355SJason Molenda      */                                                                                                         \n\
602fd83355SJason Molenda                                                                                                                 \n\
612fd83355SJason Molenda     typedef uint32_t queue_list_scope_t;                                                                        \n\
622fd83355SJason Molenda     typedef void *dispatch_queue_t;                                                                             \n\
632fd83355SJason Molenda     typedef void *introspection_dispatch_queue_info_t;                                                          \n\
642fd83355SJason Molenda     typedef void *introspection_dispatch_item_info_ref;                                                         \n\
652fd83355SJason Molenda                                                                                                                 \n\
662fd83355SJason Molenda     extern uint64_t __introspection_dispatch_queue_item_get_info (introspection_dispatch_item_info_ref item_info_ref, \n\
672fd83355SJason Molenda                                                  introspection_dispatch_item_info_ref *returned_queues_buffer,  \n\
682fd83355SJason Molenda                                                  uint64_t *returned_queues_buffer_size);                        \n\
692fd83355SJason Molenda     extern int printf(const char *format, ...);                                                                 \n\
702fd83355SJason Molenda                                                                                                                 \n\
712fd83355SJason Molenda     /*                                                                                                          \n\
722fd83355SJason Molenda      * return type define                                                                                       \n\
732fd83355SJason Molenda      */                                                                                                         \n\
742fd83355SJason Molenda                                                                                                                 \n\
752fd83355SJason Molenda     struct get_item_info_return_values                                                                      \n\
762fd83355SJason Molenda     {                                                                                                           \n\
772fd83355SJason Molenda         uint64_t item_info_buffer_ptr;    /* the address of the items buffer from libBacktraceRecording */  \n\
782fd83355SJason Molenda         uint64_t item_info_buffer_size;   /* the size of the items buffer from libBacktraceRecording */     \n\
792fd83355SJason Molenda     };                                                                                                          \n\
802fd83355SJason Molenda                                                                                                                 \n\
812fd83355SJason Molenda     void  __lldb_backtrace_recording_get_item_info                                                          \n\
822fd83355SJason Molenda                                                (struct get_item_info_return_values *return_buffer,          \n\
832fd83355SJason Molenda                                                 int debug,                                                      \n\
842fd83355SJason Molenda                                                 uint64_t /* introspection_dispatch_item_info_ref item_info_ref */ item, \n\
852fd83355SJason Molenda                                                 void *page_to_free,                                             \n\
862fd83355SJason Molenda                                                 uint64_t page_to_free_size)                                     \n\
872fd83355SJason Molenda {                                                                                                               \n\
882fd83355SJason Molenda     if (debug)                                                                                                  \n\
892fd83355SJason Molenda       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\
902fd83355SJason Molenda     if (page_to_free != 0)                                                                                      \n\
912fd83355SJason Molenda     {                                                                                                           \n\
922fd83355SJason Molenda         mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
932fd83355SJason Molenda     }                                                                                                           \n\
942fd83355SJason Molenda                                                                                                                 \n\
952fd83355SJason Molenda     __introspection_dispatch_queue_item_get_info ((void*) item,                                                 \n\
962fd83355SJason Molenda                                                   (void**)&return_buffer->item_info_buffer_ptr,                 \n\
972fd83355SJason Molenda                                                   &return_buffer->item_info_buffer_size);                       \n\
982fd83355SJason Molenda }                                                                                                               \n\
992fd83355SJason Molenda }                                                                                                               \n\
1002fd83355SJason Molenda ";
1012fd83355SJason Molenda 
10216ff8604SSaleem Abdulrasool AppleGetItemInfoHandler::AppleGetItemInfoHandler(Process *process)
103b9c1b51eSKate Stone     : m_process(process), m_get_item_info_impl_code(),
1042fd83355SJason Molenda       m_get_item_info_function_mutex(),
1052fd83355SJason Molenda       m_get_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
106b9c1b51eSKate Stone       m_get_item_info_retbuffer_mutex() {}
1072fd83355SJason Molenda 
108b9c1b51eSKate Stone AppleGetItemInfoHandler::~AppleGetItemInfoHandler() {}
1092fd83355SJason Molenda 
110b9c1b51eSKate Stone void AppleGetItemInfoHandler::Detach() {
1112fd83355SJason Molenda 
112b9c1b51eSKate Stone   if (m_process && m_process->IsAlive() &&
113b9c1b51eSKate Stone       m_get_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
114b9c1b51eSKate Stone     std::unique_lock<std::mutex> lock(m_get_item_info_retbuffer_mutex,
115b9c1b51eSKate Stone                                       std::defer_lock);
11616ff8604SSaleem Abdulrasool     lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
1172fd83355SJason Molenda     m_process->DeallocateMemory(m_get_item_info_return_buffer_addr);
1182fd83355SJason Molenda   }
1192fd83355SJason Molenda }
1202fd83355SJason Molenda 
1212fd83355SJason Molenda // Compile our __lldb_backtrace_recording_get_item_info() function (from the
12205097246SAdrian Prantl // source above in g_get_item_info_function_code) if we don't find that
12305097246SAdrian Prantl // function in the inferior already with USE_BUILTIN_FUNCTION defined.  (e.g.
12405097246SAdrian Prantl // this would be the case for testing.)
1252fd83355SJason Molenda //
12605097246SAdrian Prantl // Insert the __lldb_backtrace_recording_get_item_info into the inferior
12705097246SAdrian Prantl // process if needed.
1282fd83355SJason Molenda //
129b9c1b51eSKate Stone // Write the get_item_info_arglist into the inferior's memory space to prepare
130b9c1b51eSKate Stone // for the call.
1312fd83355SJason Molenda //
132b9c1b51eSKate Stone // Returns the address of the arguments written down in the inferior process,
13305097246SAdrian Prantl // which can be used to make the function call.
1342fd83355SJason Molenda 
135b9c1b51eSKate Stone lldb::addr_t AppleGetItemInfoHandler::SetupGetItemInfoFunction(
136b9c1b51eSKate Stone     Thread &thread, ValueList &get_item_info_arglist) {
1372fd83355SJason Molenda   ExecutionContext exe_ctx(thread.shared_from_this());
138579e70c9SSean Callanan   DiagnosticManager diagnostics;
1392fd83355SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
1402fd83355SJason Molenda   lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
141151c032cSJim Ingham   FunctionCaller *get_item_info_caller = nullptr;
1422fd83355SJason Molenda 
1432fd83355SJason Molenda   // Scope for mutex locker:
1442fd83355SJason Molenda   {
14516ff8604SSaleem Abdulrasool     std::lock_guard<std::mutex> guard(m_get_item_info_function_mutex);
1462fd83355SJason Molenda 
14705097246SAdrian Prantl     // First stage is to make the UtilityFunction to hold our injected
14805097246SAdrian Prantl     // function:
1492fd83355SJason Molenda 
150b9c1b51eSKate Stone     if (!m_get_item_info_impl_code.get()) {
151b9c1b51eSKate Stone       if (g_get_item_info_function_code != NULL) {
15297206d57SZachary Turner         Status error;
153b9c1b51eSKate Stone         m_get_item_info_impl_code.reset(
154b9c1b51eSKate Stone             exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
155b9c1b51eSKate Stone                 g_get_item_info_function_code, eLanguageTypeObjC,
156b9c1b51eSKate Stone                 g_get_item_info_function_name, error));
157b9c1b51eSKate Stone         if (error.Fail()) {
158151c032cSJim Ingham           if (log)
159b9c1b51eSKate Stone             log->Printf("Failed to get utility function: %s.",
160b9c1b51eSKate Stone                         error.AsCString());
161151c032cSJim Ingham           return args_addr;
162151c032cSJim Ingham         }
163151c032cSJim Ingham 
164b9c1b51eSKate Stone         if (!m_get_item_info_impl_code->Install(diagnostics, exe_ctx)) {
165b9c1b51eSKate Stone           if (log) {
166579e70c9SSean Callanan             log->Printf("Failed to install get-item-info introspection.");
167579e70c9SSean Callanan             diagnostics.Dump(log);
168579e70c9SSean Callanan           }
1692fd83355SJason Molenda           m_get_item_info_impl_code.reset();
1702fd83355SJason Molenda           return args_addr;
1712fd83355SJason Molenda         }
172b9c1b51eSKate Stone       } else {
1732fd83355SJason Molenda         if (log)
1742fd83355SJason Molenda           log->Printf("No get-item-info introspection code found.");
1752fd83355SJason Molenda         return LLDB_INVALID_ADDRESS;
1762fd83355SJason Molenda       }
1772fd83355SJason Molenda 
178151c032cSJim Ingham       // Next make the runner function for our implementation utility function.
17997206d57SZachary Turner       Status error;
180151c032cSJim Ingham 
181b9c1b51eSKate Stone       TypeSystem *type_system =
182b9c1b51eSKate Stone           thread.GetProcess()->GetTarget().GetScratchTypeSystemForLanguage(
183b9c1b51eSKate Stone               nullptr, eLanguageTypeC);
184b9c1b51eSKate Stone       CompilerType get_item_info_return_type =
185b9c1b51eSKate Stone           type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
186151c032cSJim Ingham 
187b9c1b51eSKate Stone       get_item_info_caller = m_get_item_info_impl_code->MakeFunctionCaller(
188b9c1b51eSKate Stone           get_item_info_return_type, get_item_info_arglist,
189b9c1b51eSKate Stone           thread.shared_from_this(), error);
190ed2977faSJason Molenda       if (error.Fail() || get_item_info_caller == nullptr) {
191151c032cSJim Ingham         if (log)
192b9c1b51eSKate Stone           log->Printf("Error Inserting get-item-info function: \"%s\".",
193b9c1b51eSKate Stone                       error.AsCString());
194151c032cSJim Ingham         return args_addr;
195151c032cSJim Ingham       }
196b9c1b51eSKate Stone     } else {
197151c032cSJim Ingham       // If it's already made, then we can just retrieve the caller:
198151c032cSJim Ingham       get_item_info_caller = m_get_item_info_impl_code->GetFunctionCaller();
199b9c1b51eSKate Stone       if (!get_item_info_caller) {
2002fd83355SJason Molenda         if (log)
201151c032cSJim Ingham           log->Printf("Failed to get get-item-info introspection caller.");
202151c032cSJim Ingham         m_get_item_info_impl_code.reset();
2032fd83355SJason Molenda         return args_addr;
2042fd83355SJason Molenda       }
2052fd83355SJason Molenda     }
2062fd83355SJason Molenda   }
2072fd83355SJason Molenda 
208579e70c9SSean Callanan   diagnostics.Clear();
2092fd83355SJason Molenda 
210b9c1b51eSKate Stone   // Now write down the argument values for this particular call.  This looks
21105097246SAdrian Prantl   // like it might be a race condition if other threads were calling into here,
21205097246SAdrian Prantl   // but actually it isn't because we allocate a new args structure for this
21305097246SAdrian Prantl   // call by passing args_addr = LLDB_INVALID_ADDRESS...
2142fd83355SJason Molenda 
215b9c1b51eSKate Stone   if (!get_item_info_caller->WriteFunctionArguments(
216b9c1b51eSKate Stone           exe_ctx, args_addr, get_item_info_arglist, diagnostics)) {
217b9c1b51eSKate Stone     if (log) {
218579e70c9SSean Callanan       log->Printf("Error writing get-item-info function arguments.");
219579e70c9SSean Callanan       diagnostics.Dump(log);
220579e70c9SSean Callanan     }
221579e70c9SSean Callanan 
2222fd83355SJason Molenda     return args_addr;
2232fd83355SJason Molenda   }
2242fd83355SJason Molenda 
2252fd83355SJason Molenda   return args_addr;
2262fd83355SJason Molenda }
2272fd83355SJason Molenda 
2282fd83355SJason Molenda AppleGetItemInfoHandler::GetItemInfoReturnInfo
229b9c1b51eSKate Stone AppleGetItemInfoHandler::GetItemInfo(Thread &thread, uint64_t item,
230b9c1b51eSKate Stone                                      addr_t page_to_free,
23197206d57SZachary Turner                                      uint64_t page_to_free_size,
23297206d57SZachary Turner                                      Status &error) {
2332fd83355SJason Molenda   lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
2342fd83355SJason Molenda   ProcessSP process_sp(thread.CalculateProcess());
2352fd83355SJason Molenda   TargetSP target_sp(thread.CalculateTarget());
2362fd83355SJason Molenda   ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
2372fd83355SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
2382fd83355SJason Molenda 
2392fd83355SJason Molenda   GetItemInfoReturnInfo return_value;
2402fd83355SJason Molenda   return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
2412fd83355SJason Molenda   return_value.item_buffer_size = 0;
2422fd83355SJason Molenda 
2432fd83355SJason Molenda   error.Clear();
2442fd83355SJason Molenda 
245b9c1b51eSKate Stone   if (thread.SafeToCallFunctions() == false) {
246b4892cd2SJason Molenda     if (log)
247b9c1b51eSKate Stone       log->Printf("Not safe to call functions on thread 0x%" PRIx64,
248b9c1b51eSKate Stone                   thread.GetID());
249b4892cd2SJason Molenda     error.SetErrorString("Not safe to call functions on this thread.");
250b4892cd2SJason Molenda     return return_value;
251b4892cd2SJason Molenda   }
252b4892cd2SJason Molenda 
2532fd83355SJason Molenda   // Set up the arguments for a call to
2542fd83355SJason Molenda 
2552fd83355SJason Molenda   // struct get_item_info_return_values
2562fd83355SJason Molenda   // {
257b9c1b51eSKate Stone   //     uint64_t item_info_buffer_ptr;    /* the address of the items buffer
258b9c1b51eSKate Stone   //     from libBacktraceRecording */
259b9c1b51eSKate Stone   //     uint64_t item_info_buffer_size;   /* the size of the items buffer from
260b9c1b51eSKate Stone   //     libBacktraceRecording */
2612fd83355SJason Molenda   // };
2622fd83355SJason Molenda   //
2632fd83355SJason Molenda   // void  __lldb_backtrace_recording_get_item_info
264b9c1b51eSKate Stone   //                                            (struct
265b9c1b51eSKate Stone   //                                            get_item_info_return_values
266b9c1b51eSKate Stone   //                                            *return_buffer,
2672fd83355SJason Molenda   //                                             int debug,
2682fd83355SJason Molenda   //                                             uint64_t item,
2692fd83355SJason Molenda   //                                             void *page_to_free,
2702fd83355SJason Molenda   //                                             uint64_t page_to_free_size)
2712fd83355SJason Molenda 
272b9c1b51eSKate Stone   // Where the return_buffer argument points to a 24 byte region of memory
27305097246SAdrian Prantl   // already allocated by lldb in the inferior process.
2742fd83355SJason Molenda 
275b9c1b51eSKate Stone   CompilerType clang_void_ptr_type =
276b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
2772fd83355SJason Molenda   Value return_buffer_ptr_value;
2782fd83355SJason Molenda   return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
27999558cc4SGreg Clayton   return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
2802fd83355SJason Molenda 
281a1e5dc86SGreg Clayton   CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
2822fd83355SJason Molenda   Value debug_value;
2832fd83355SJason Molenda   debug_value.SetValueType(Value::eValueTypeScalar);
28499558cc4SGreg Clayton   debug_value.SetCompilerType(clang_int_type);
2852fd83355SJason Molenda 
286b9c1b51eSKate Stone   CompilerType clang_uint64_type =
287b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
2882fd83355SJason Molenda   Value item_value;
2892fd83355SJason Molenda   item_value.SetValueType(Value::eValueTypeScalar);
29099558cc4SGreg Clayton   item_value.SetCompilerType(clang_uint64_type);
2912fd83355SJason Molenda 
2922fd83355SJason Molenda   Value page_to_free_value;
2932fd83355SJason Molenda   page_to_free_value.SetValueType(Value::eValueTypeScalar);
29499558cc4SGreg Clayton   page_to_free_value.SetCompilerType(clang_void_ptr_type);
2952fd83355SJason Molenda 
2962fd83355SJason Molenda   Value page_to_free_size_value;
2972fd83355SJason Molenda   page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
29899558cc4SGreg Clayton   page_to_free_size_value.SetCompilerType(clang_uint64_type);
2992fd83355SJason Molenda 
30016ff8604SSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_get_item_info_retbuffer_mutex);
301b9c1b51eSKate Stone   if (m_get_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
302b9c1b51eSKate Stone     addr_t bufaddr = process_sp->AllocateMemory(
303b9c1b51eSKate Stone         32, ePermissionsReadable | ePermissionsWritable, error);
304b9c1b51eSKate Stone     if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
3052fd83355SJason Molenda       if (log)
306b9c1b51eSKate Stone         log->Printf("Failed to allocate memory for return buffer for get "
307b9c1b51eSKate Stone                     "current queues func call");
3082fd83355SJason Molenda       return return_value;
3092fd83355SJason Molenda     }
3102fd83355SJason Molenda     m_get_item_info_return_buffer_addr = bufaddr;
3112fd83355SJason Molenda   }
3122fd83355SJason Molenda 
3132fd83355SJason Molenda   ValueList argument_values;
3142fd83355SJason Molenda 
3152fd83355SJason Molenda   return_buffer_ptr_value.GetScalar() = m_get_item_info_return_buffer_addr;
3162fd83355SJason Molenda   argument_values.PushValue(return_buffer_ptr_value);
3172fd83355SJason Molenda 
3182fd83355SJason Molenda   debug_value.GetScalar() = 0;
3192fd83355SJason Molenda   argument_values.PushValue(debug_value);
3202fd83355SJason Molenda 
3212fd83355SJason Molenda   item_value.GetScalar() = item;
3222fd83355SJason Molenda   argument_values.PushValue(item_value);
3232fd83355SJason Molenda 
3242fd83355SJason Molenda   if (page_to_free != LLDB_INVALID_ADDRESS)
3252fd83355SJason Molenda     page_to_free_value.GetScalar() = page_to_free;
3262fd83355SJason Molenda   else
3272fd83355SJason Molenda     page_to_free_value.GetScalar() = 0;
3282fd83355SJason Molenda   argument_values.PushValue(page_to_free_value);
3292fd83355SJason Molenda 
3302fd83355SJason Molenda   page_to_free_size_value.GetScalar() = page_to_free_size;
3312fd83355SJason Molenda   argument_values.PushValue(page_to_free_size_value);
3322fd83355SJason Molenda 
3332fd83355SJason Molenda   addr_t args_addr = SetupGetItemInfoFunction(thread, argument_values);
3342fd83355SJason Molenda 
335579e70c9SSean Callanan   DiagnosticManager diagnostics;
3362fd83355SJason Molenda   ExecutionContext exe_ctx;
3372fd83355SJason Molenda   EvaluateExpressionOptions options;
3382fd83355SJason Molenda   options.SetUnwindOnError(true);
3392fd83355SJason Molenda   options.SetIgnoreBreakpoints(true);
3402fd83355SJason Molenda   options.SetStopOthers(true);
34143d35418SPavel Labath   options.SetTimeout(std::chrono::milliseconds(500));
342a0560152SJason Molenda   options.SetTryAllThreads(false);
343*c01783a8SRaphael Isemann   options.SetIsForUtilityExpr(true);
3442fd83355SJason Molenda   thread.CalculateExecutionContext(exe_ctx);
3452fd83355SJason Molenda 
346b9c1b51eSKate Stone   if (!m_get_item_info_impl_code) {
347b9c1b51eSKate Stone     error.SetErrorString("Unable to compile function to call "
348b9c1b51eSKate Stone                          "__introspection_dispatch_queue_item_get_info");
3492fd83355SJason Molenda     return return_value;
3502fd83355SJason Molenda   }
3512fd83355SJason Molenda 
3521624a2d3SJim Ingham   ExpressionResults func_call_ret;
3532fd83355SJason Molenda   Value results;
354151c032cSJim Ingham   FunctionCaller *func_caller = m_get_item_info_impl_code->GetFunctionCaller();
355b9c1b51eSKate Stone   if (!func_caller) {
356151c032cSJim Ingham     if (log)
357b9c1b51eSKate Stone       log->Printf("Could not retrieve function caller for "
358b9c1b51eSKate Stone                   "__introspection_dispatch_queue_item_get_info.");
359b9c1b51eSKate Stone     error.SetErrorString("Could not retrieve function caller for "
360b9c1b51eSKate Stone                          "__introspection_dispatch_queue_item_get_info.");
361151c032cSJim Ingham     return return_value;
362151c032cSJim Ingham   }
363151c032cSJim Ingham 
364b9c1b51eSKate Stone   func_call_ret = func_caller->ExecuteFunction(exe_ctx, &args_addr, options,
365b9c1b51eSKate Stone                                                diagnostics, results);
366b9c1b51eSKate Stone   if (func_call_ret != eExpressionCompleted || !error.Success()) {
3672fd83355SJason Molenda     if (log)
368b9c1b51eSKate Stone       log->Printf("Unable to call "
369b9c1b51eSKate Stone                   "__introspection_dispatch_queue_item_get_info(), got "
370b9c1b51eSKate Stone                   "ExpressionResults %d, error contains %s",
371b9c1b51eSKate Stone                   func_call_ret, error.AsCString(""));
372b9c1b51eSKate Stone     error.SetErrorString("Unable to call "
373b9c1b51eSKate Stone                          "__introspection_dispatch_queue_get_item_info() for "
374b9c1b51eSKate Stone                          "list of queues");
3752fd83355SJason Molenda     return return_value;
3762fd83355SJason Molenda   }
3772fd83355SJason Molenda 
378b9c1b51eSKate Stone   return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
379b9c1b51eSKate Stone       m_get_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
380b9c1b51eSKate Stone   if (!error.Success() ||
381b9c1b51eSKate Stone       return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
3822fd83355SJason Molenda     return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
3832fd83355SJason Molenda     return return_value;
3842fd83355SJason Molenda   }
3852fd83355SJason Molenda 
386b9c1b51eSKate Stone   return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
387b9c1b51eSKate Stone       m_get_item_info_return_buffer_addr + 8, 8, 0, error);
3882fd83355SJason Molenda 
389b9c1b51eSKate Stone   if (!error.Success()) {
3902fd83355SJason Molenda     return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
3912fd83355SJason Molenda     return return_value;
3922fd83355SJason Molenda   }
3932a6c252dSJason Molenda   if (log)
394b9c1b51eSKate Stone     log->Printf("AppleGetItemInfoHandler called "
395b9c1b51eSKate Stone                 "__introspection_dispatch_queue_item_get_info (page_to_free == "
396b9c1b51eSKate Stone                 "0x%" PRIx64 ", size = %" PRId64
397b9c1b51eSKate Stone                 "), returned page is at 0x%" PRIx64 ", size %" PRId64,
398b9c1b51eSKate Stone                 page_to_free, page_to_free_size, return_value.item_buffer_ptr,
399b9c1b51eSKate Stone                 return_value.item_buffer_size);
4002fd83355SJason Molenda 
4012fd83355SJason Molenda   return return_value;
4022fd83355SJason Molenda }
403