180814287SRaphael Isemann //===-- AppleGetItemInfoHandler.cpp ---------------------------------------===//
22fd83355SJason Molenda //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62fd83355SJason Molenda //
72fd83355SJason Molenda //===----------------------------------------------------------------------===//
82fd83355SJason Molenda 
92fd83355SJason Molenda #include "AppleGetItemInfoHandler.h"
102fd83355SJason Molenda 
118be30215SAlex Langford #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
122fd83355SJason Molenda #include "lldb/Core/Module.h"
132fd83355SJason Molenda #include "lldb/Core/Value.h"
14579e70c9SSean Callanan #include "lldb/Expression/DiagnosticManager.h"
15151c032cSJim Ingham #include "lldb/Expression/FunctionCaller.h"
16151c032cSJim Ingham #include "lldb/Expression/UtilityFunction.h"
172fd83355SJason Molenda #include "lldb/Symbol/Symbol.h"
182fd83355SJason Molenda #include "lldb/Target/ExecutionContext.h"
192fd83355SJason Molenda #include "lldb/Target/Process.h"
202fd83355SJason Molenda #include "lldb/Target/Target.h"
212fd83355SJason Molenda #include "lldb/Target/Thread.h"
22bf9a7730SZachary Turner #include "lldb/Utility/ConstString.h"
236f9e6901SZachary Turner #include "lldb/Utility/Log.h"
24bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
252fd83355SJason Molenda 
262fd83355SJason Molenda using namespace lldb;
272fd83355SJason Molenda using namespace lldb_private;
282fd83355SJason Molenda 
29b9c1b51eSKate Stone const char *AppleGetItemInfoHandler::g_get_item_info_function_name =
30b9c1b51eSKate Stone     "__lldb_backtrace_recording_get_item_info";
31b9c1b51eSKate Stone const char *AppleGetItemInfoHandler::g_get_item_info_function_code =
32b9c1b51eSKate Stone     "                                  \n\
332fd83355SJason Molenda extern \"C\"                                                                                                    \n\
342fd83355SJason Molenda {                                                                                                               \n\
352fd83355SJason Molenda     /*                                                                                                          \n\
362fd83355SJason Molenda      * mach defines                                                                                             \n\
372fd83355SJason Molenda      */                                                                                                         \n\
382fd83355SJason Molenda                                                                                                                 \n\
392fd83355SJason Molenda     typedef unsigned int uint32_t;                                                                              \n\
402fd83355SJason Molenda     typedef unsigned long long uint64_t;                                                                        \n\
412fd83355SJason Molenda     typedef uint32_t mach_port_t;                                                                               \n\
422fd83355SJason Molenda     typedef mach_port_t vm_map_t;                                                                               \n\
432fd83355SJason Molenda     typedef int kern_return_t;                                                                                  \n\
442fd83355SJason Molenda     typedef uint64_t mach_vm_address_t;                                                                         \n\
452fd83355SJason Molenda     typedef uint64_t mach_vm_size_t;                                                                            \n\
462fd83355SJason Molenda                                                                                                                 \n\
472fd83355SJason Molenda     mach_port_t mach_task_self ();                                                                              \n\
482fd83355SJason Molenda     kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size);         \n\
492fd83355SJason Molenda                                                                                                                 \n\
502fd83355SJason Molenda     /*                                                                                                          \n\
512fd83355SJason Molenda      * libBacktraceRecording defines                                                                            \n\
522fd83355SJason Molenda      */                                                                                                         \n\
532fd83355SJason Molenda                                                                                                                 \n\
542fd83355SJason Molenda     typedef uint32_t queue_list_scope_t;                                                                        \n\
552fd83355SJason Molenda     typedef void *dispatch_queue_t;                                                                             \n\
562fd83355SJason Molenda     typedef void *introspection_dispatch_queue_info_t;                                                          \n\
572fd83355SJason Molenda     typedef void *introspection_dispatch_item_info_ref;                                                         \n\
582fd83355SJason Molenda                                                                                                                 \n\
592fd83355SJason Molenda     extern uint64_t __introspection_dispatch_queue_item_get_info (introspection_dispatch_item_info_ref item_info_ref, \n\
602fd83355SJason Molenda                                                  introspection_dispatch_item_info_ref *returned_queues_buffer,  \n\
612fd83355SJason Molenda                                                  uint64_t *returned_queues_buffer_size);                        \n\
622fd83355SJason Molenda     extern int printf(const char *format, ...);                                                                 \n\
632fd83355SJason Molenda                                                                                                                 \n\
642fd83355SJason Molenda     /*                                                                                                          \n\
652fd83355SJason Molenda      * return type define                                                                                       \n\
662fd83355SJason Molenda      */                                                                                                         \n\
672fd83355SJason Molenda                                                                                                                 \n\
682fd83355SJason Molenda     struct get_item_info_return_values                                                                      \n\
692fd83355SJason Molenda     {                                                                                                           \n\
702fd83355SJason Molenda         uint64_t item_info_buffer_ptr;    /* the address of the items buffer from libBacktraceRecording */  \n\
712fd83355SJason Molenda         uint64_t item_info_buffer_size;   /* the size of the items buffer from libBacktraceRecording */     \n\
722fd83355SJason Molenda     };                                                                                                          \n\
732fd83355SJason Molenda                                                                                                                 \n\
742fd83355SJason Molenda     void  __lldb_backtrace_recording_get_item_info                                                          \n\
752fd83355SJason Molenda                                                (struct get_item_info_return_values *return_buffer,          \n\
762fd83355SJason Molenda                                                 int debug,                                                      \n\
772fd83355SJason Molenda                                                 uint64_t /* introspection_dispatch_item_info_ref item_info_ref */ item, \n\
782fd83355SJason Molenda                                                 void *page_to_free,                                             \n\
792fd83355SJason Molenda                                                 uint64_t page_to_free_size)                                     \n\
802fd83355SJason Molenda {                                                                                                               \n\
812fd83355SJason Molenda     if (debug)                                                                                                  \n\
822fd83355SJason 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\
832fd83355SJason Molenda     if (page_to_free != 0)                                                                                      \n\
842fd83355SJason Molenda     {                                                                                                           \n\
852fd83355SJason Molenda         mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
862fd83355SJason Molenda     }                                                                                                           \n\
872fd83355SJason Molenda                                                                                                                 \n\
882fd83355SJason Molenda     __introspection_dispatch_queue_item_get_info ((void*) item,                                                 \n\
892fd83355SJason Molenda                                                   (void**)&return_buffer->item_info_buffer_ptr,                 \n\
902fd83355SJason Molenda                                                   &return_buffer->item_info_buffer_size);                       \n\
912fd83355SJason Molenda }                                                                                                               \n\
922fd83355SJason Molenda }                                                                                                               \n\
932fd83355SJason Molenda ";
942fd83355SJason Molenda 
9516ff8604SSaleem Abdulrasool AppleGetItemInfoHandler::AppleGetItemInfoHandler(Process *process)
96b9c1b51eSKate Stone     : m_process(process), m_get_item_info_impl_code(),
972fd83355SJason Molenda       m_get_item_info_function_mutex(),
982fd83355SJason Molenda       m_get_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
99b9c1b51eSKate Stone       m_get_item_info_retbuffer_mutex() {}
1002fd83355SJason Molenda 
101*fd2433e1SJonas Devlieghere AppleGetItemInfoHandler::~AppleGetItemInfoHandler() = default;
1022fd83355SJason Molenda 
103b9c1b51eSKate Stone void AppleGetItemInfoHandler::Detach() {
1042fd83355SJason Molenda 
105b9c1b51eSKate Stone   if (m_process && m_process->IsAlive() &&
106b9c1b51eSKate Stone       m_get_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
107b9c1b51eSKate Stone     std::unique_lock<std::mutex> lock(m_get_item_info_retbuffer_mutex,
108b9c1b51eSKate Stone                                       std::defer_lock);
1090c86dfb8SReid Kleckner     (void)lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
1102fd83355SJason Molenda     m_process->DeallocateMemory(m_get_item_info_return_buffer_addr);
1112fd83355SJason Molenda   }
1122fd83355SJason Molenda }
1132fd83355SJason Molenda 
1142fd83355SJason Molenda // Compile our __lldb_backtrace_recording_get_item_info() function (from the
11505097246SAdrian Prantl // source above in g_get_item_info_function_code) if we don't find that
11605097246SAdrian Prantl // function in the inferior already with USE_BUILTIN_FUNCTION defined.  (e.g.
11705097246SAdrian Prantl // this would be the case for testing.)
1182fd83355SJason Molenda //
11905097246SAdrian Prantl // Insert the __lldb_backtrace_recording_get_item_info into the inferior
12005097246SAdrian Prantl // process if needed.
1212fd83355SJason Molenda //
122b9c1b51eSKate Stone // Write the get_item_info_arglist into the inferior's memory space to prepare
123b9c1b51eSKate Stone // for the call.
1242fd83355SJason Molenda //
125b9c1b51eSKate Stone // Returns the address of the arguments written down in the inferior process,
12605097246SAdrian Prantl // which can be used to make the function call.
1272fd83355SJason Molenda 
128b9c1b51eSKate Stone lldb::addr_t AppleGetItemInfoHandler::SetupGetItemInfoFunction(
129b9c1b51eSKate Stone     Thread &thread, ValueList &get_item_info_arglist) {
1302fd83355SJason Molenda   ExecutionContext exe_ctx(thread.shared_from_this());
131579e70c9SSean Callanan   DiagnosticManager diagnostics;
1322fd83355SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
1332fd83355SJason Molenda   lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
134151c032cSJim Ingham   FunctionCaller *get_item_info_caller = nullptr;
1352fd83355SJason Molenda 
1362fd83355SJason Molenda   // Scope for mutex locker:
1372fd83355SJason Molenda   {
13816ff8604SSaleem Abdulrasool     std::lock_guard<std::mutex> guard(m_get_item_info_function_mutex);
1392fd83355SJason Molenda 
14005097246SAdrian Prantl     // First stage is to make the UtilityFunction to hold our injected
14105097246SAdrian Prantl     // function:
1422fd83355SJason Molenda 
14370355aceSJonas Devlieghere     if (!m_get_item_info_impl_code) {
144248a1305SKonrad Kleine       if (g_get_item_info_function_code != nullptr) {
145de346cf2SJonas Devlieghere         auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
146de346cf2SJonas Devlieghere             g_get_item_info_function_code, g_get_item_info_function_name,
147de346cf2SJonas Devlieghere             eLanguageTypeObjC, exe_ctx);
148de346cf2SJonas Devlieghere         if (!utility_fn_or_error) {
149de346cf2SJonas Devlieghere           LLDB_LOG_ERROR(log, utility_fn_or_error.takeError(),
150de346cf2SJonas Devlieghere                          "Failed to create utility function: {0}.");
151151c032cSJim Ingham         }
152de346cf2SJonas Devlieghere         m_get_item_info_impl_code = std::move(*utility_fn_or_error);
153b9c1b51eSKate Stone       } else {
15463e5fb76SJonas Devlieghere         LLDB_LOGF(log, "No get-item-info introspection code found.");
1552fd83355SJason Molenda         return LLDB_INVALID_ADDRESS;
1562fd83355SJason Molenda       }
1572fd83355SJason Molenda 
158151c032cSJim Ingham       // Next make the runner function for our implementation utility function.
1590e252e38SAlex Langford       auto type_system_or_err =
160b9c1b51eSKate Stone           thread.GetProcess()->GetTarget().GetScratchTypeSystemForLanguage(
1610e252e38SAlex Langford               eLanguageTypeC);
1620e252e38SAlex Langford       if (auto err = type_system_or_err.takeError()) {
1630e252e38SAlex Langford         LLDB_LOG_ERROR(log, std::move(err),
1640e252e38SAlex Langford                        "Error inseting get-item-info function");
1650e252e38SAlex Langford         return args_addr;
1660e252e38SAlex Langford       }
167b9c1b51eSKate Stone       CompilerType get_item_info_return_type =
1680e252e38SAlex Langford           type_system_or_err->GetBasicTypeFromAST(eBasicTypeVoid)
1690e252e38SAlex Langford               .GetPointerType();
170151c032cSJim Ingham 
1710e252e38SAlex Langford       Status error;
172b9c1b51eSKate Stone       get_item_info_caller = m_get_item_info_impl_code->MakeFunctionCaller(
173b9c1b51eSKate Stone           get_item_info_return_type, get_item_info_arglist,
174b9c1b51eSKate Stone           thread.shared_from_this(), error);
175ed2977faSJason Molenda       if (error.Fail() || get_item_info_caller == nullptr) {
17663e5fb76SJonas Devlieghere         LLDB_LOGF(log, "Error Inserting get-item-info function: \"%s\".",
177b9c1b51eSKate Stone                   error.AsCString());
178151c032cSJim Ingham         return args_addr;
179151c032cSJim Ingham       }
180b9c1b51eSKate Stone     } else {
181151c032cSJim Ingham       // If it's already made, then we can just retrieve the caller:
182151c032cSJim Ingham       get_item_info_caller = m_get_item_info_impl_code->GetFunctionCaller();
183b9c1b51eSKate Stone       if (!get_item_info_caller) {
18463e5fb76SJonas Devlieghere         LLDB_LOGF(log, "Failed to get get-item-info introspection caller.");
185151c032cSJim Ingham         m_get_item_info_impl_code.reset();
1862fd83355SJason Molenda         return args_addr;
1872fd83355SJason Molenda       }
1882fd83355SJason Molenda     }
1892fd83355SJason Molenda   }
1902fd83355SJason Molenda 
191579e70c9SSean Callanan   diagnostics.Clear();
1922fd83355SJason Molenda 
193b9c1b51eSKate Stone   // Now write down the argument values for this particular call.  This looks
19405097246SAdrian Prantl   // like it might be a race condition if other threads were calling into here,
19505097246SAdrian Prantl   // but actually it isn't because we allocate a new args structure for this
19605097246SAdrian Prantl   // call by passing args_addr = LLDB_INVALID_ADDRESS...
1972fd83355SJason Molenda 
198b9c1b51eSKate Stone   if (!get_item_info_caller->WriteFunctionArguments(
199b9c1b51eSKate Stone           exe_ctx, args_addr, get_item_info_arglist, diagnostics)) {
200b9c1b51eSKate Stone     if (log) {
20163e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Error writing get-item-info function arguments.");
202579e70c9SSean Callanan       diagnostics.Dump(log);
203579e70c9SSean Callanan     }
204579e70c9SSean Callanan 
2052fd83355SJason Molenda     return args_addr;
2062fd83355SJason Molenda   }
2072fd83355SJason Molenda 
2082fd83355SJason Molenda   return args_addr;
2092fd83355SJason Molenda }
2102fd83355SJason Molenda 
2112fd83355SJason Molenda AppleGetItemInfoHandler::GetItemInfoReturnInfo
212b9c1b51eSKate Stone AppleGetItemInfoHandler::GetItemInfo(Thread &thread, uint64_t item,
213b9c1b51eSKate Stone                                      addr_t page_to_free,
21497206d57SZachary Turner                                      uint64_t page_to_free_size,
21597206d57SZachary Turner                                      Status &error) {
2162fd83355SJason Molenda   lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
2172fd83355SJason Molenda   ProcessSP process_sp(thread.CalculateProcess());
2182fd83355SJason Molenda   TargetSP target_sp(thread.CalculateTarget());
219594308c7SRaphael Isemann   TypeSystemClang *clang_ast_context =
220594308c7SRaphael Isemann       ScratchTypeSystemClang::GetForTarget(*target_sp);
2212fd83355SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
2222fd83355SJason Molenda 
2232fd83355SJason Molenda   GetItemInfoReturnInfo return_value;
2242fd83355SJason Molenda   return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
2252fd83355SJason Molenda   return_value.item_buffer_size = 0;
2262fd83355SJason Molenda 
2272fd83355SJason Molenda   error.Clear();
2282fd83355SJason Molenda 
229a6682a41SJonas Devlieghere   if (!thread.SafeToCallFunctions()) {
23063e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Not safe to call functions on thread 0x%" PRIx64,
231b9c1b51eSKate Stone               thread.GetID());
232b4892cd2SJason Molenda     error.SetErrorString("Not safe to call functions on this thread.");
233b4892cd2SJason Molenda     return return_value;
234b4892cd2SJason Molenda   }
235b4892cd2SJason Molenda 
2362fd83355SJason Molenda   // Set up the arguments for a call to
2372fd83355SJason Molenda 
2382fd83355SJason Molenda   // struct get_item_info_return_values
2392fd83355SJason Molenda   // {
240b9c1b51eSKate Stone   //     uint64_t item_info_buffer_ptr;    /* the address of the items buffer
241b9c1b51eSKate Stone   //     from libBacktraceRecording */
242b9c1b51eSKate Stone   //     uint64_t item_info_buffer_size;   /* the size of the items buffer from
243b9c1b51eSKate Stone   //     libBacktraceRecording */
2442fd83355SJason Molenda   // };
2452fd83355SJason Molenda   //
2462fd83355SJason Molenda   // void  __lldb_backtrace_recording_get_item_info
247b9c1b51eSKate Stone   //                                            (struct
248b9c1b51eSKate Stone   //                                            get_item_info_return_values
249b9c1b51eSKate Stone   //                                            *return_buffer,
2502fd83355SJason Molenda   //                                             int debug,
2512fd83355SJason Molenda   //                                             uint64_t item,
2522fd83355SJason Molenda   //                                             void *page_to_free,
2532fd83355SJason Molenda   //                                             uint64_t page_to_free_size)
2542fd83355SJason Molenda 
255b9c1b51eSKate Stone   // Where the return_buffer argument points to a 24 byte region of memory
25605097246SAdrian Prantl   // already allocated by lldb in the inferior process.
2572fd83355SJason Molenda 
258b9c1b51eSKate Stone   CompilerType clang_void_ptr_type =
259b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
2602fd83355SJason Molenda   Value return_buffer_ptr_value;
261057efa99SAdrian Prantl   return_buffer_ptr_value.SetValueType(Value::ValueType::Scalar);
26299558cc4SGreg Clayton   return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
2632fd83355SJason Molenda 
264a1e5dc86SGreg Clayton   CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
2652fd83355SJason Molenda   Value debug_value;
266057efa99SAdrian Prantl   debug_value.SetValueType(Value::ValueType::Scalar);
26799558cc4SGreg Clayton   debug_value.SetCompilerType(clang_int_type);
2682fd83355SJason Molenda 
269b9c1b51eSKate Stone   CompilerType clang_uint64_type =
270b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
2712fd83355SJason Molenda   Value item_value;
272057efa99SAdrian Prantl   item_value.SetValueType(Value::ValueType::Scalar);
27399558cc4SGreg Clayton   item_value.SetCompilerType(clang_uint64_type);
2742fd83355SJason Molenda 
2752fd83355SJason Molenda   Value page_to_free_value;
276057efa99SAdrian Prantl   page_to_free_value.SetValueType(Value::ValueType::Scalar);
27799558cc4SGreg Clayton   page_to_free_value.SetCompilerType(clang_void_ptr_type);
2782fd83355SJason Molenda 
2792fd83355SJason Molenda   Value page_to_free_size_value;
280057efa99SAdrian Prantl   page_to_free_size_value.SetValueType(Value::ValueType::Scalar);
28199558cc4SGreg Clayton   page_to_free_size_value.SetCompilerType(clang_uint64_type);
2822fd83355SJason Molenda 
28316ff8604SSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_get_item_info_retbuffer_mutex);
284b9c1b51eSKate Stone   if (m_get_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
285b9c1b51eSKate Stone     addr_t bufaddr = process_sp->AllocateMemory(
286b9c1b51eSKate Stone         32, ePermissionsReadable | ePermissionsWritable, error);
287b9c1b51eSKate Stone     if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
28863e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Failed to allocate memory for return buffer for get "
289b9c1b51eSKate Stone                      "current queues func call");
2902fd83355SJason Molenda       return return_value;
2912fd83355SJason Molenda     }
2922fd83355SJason Molenda     m_get_item_info_return_buffer_addr = bufaddr;
2932fd83355SJason Molenda   }
2942fd83355SJason Molenda 
2952fd83355SJason Molenda   ValueList argument_values;
2962fd83355SJason Molenda 
2972fd83355SJason Molenda   return_buffer_ptr_value.GetScalar() = m_get_item_info_return_buffer_addr;
2982fd83355SJason Molenda   argument_values.PushValue(return_buffer_ptr_value);
2992fd83355SJason Molenda 
3002fd83355SJason Molenda   debug_value.GetScalar() = 0;
3012fd83355SJason Molenda   argument_values.PushValue(debug_value);
3022fd83355SJason Molenda 
3032fd83355SJason Molenda   item_value.GetScalar() = item;
3042fd83355SJason Molenda   argument_values.PushValue(item_value);
3052fd83355SJason Molenda 
3062fd83355SJason Molenda   if (page_to_free != LLDB_INVALID_ADDRESS)
3072fd83355SJason Molenda     page_to_free_value.GetScalar() = page_to_free;
3082fd83355SJason Molenda   else
3092fd83355SJason Molenda     page_to_free_value.GetScalar() = 0;
3102fd83355SJason Molenda   argument_values.PushValue(page_to_free_value);
3112fd83355SJason Molenda 
3122fd83355SJason Molenda   page_to_free_size_value.GetScalar() = page_to_free_size;
3132fd83355SJason Molenda   argument_values.PushValue(page_to_free_size_value);
3142fd83355SJason Molenda 
3152fd83355SJason Molenda   addr_t args_addr = SetupGetItemInfoFunction(thread, argument_values);
3162fd83355SJason Molenda 
317579e70c9SSean Callanan   DiagnosticManager diagnostics;
3182fd83355SJason Molenda   ExecutionContext exe_ctx;
3192fd83355SJason Molenda   EvaluateExpressionOptions options;
3202fd83355SJason Molenda   options.SetUnwindOnError(true);
3212fd83355SJason Molenda   options.SetIgnoreBreakpoints(true);
3222fd83355SJason Molenda   options.SetStopOthers(true);
3235b0a687dSJason Molenda #if __has_feature(address_sanitizer)
3245b0a687dSJason Molenda   options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
3255b0a687dSJason Molenda #else
3265b0a687dSJason Molenda   options.SetTimeout(std::chrono::milliseconds(500));
3275b0a687dSJason Molenda #endif
3284c03ea14SAdrian Prantl   options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
329a0560152SJason Molenda   options.SetTryAllThreads(false);
330c01783a8SRaphael Isemann   options.SetIsForUtilityExpr(true);
3312fd83355SJason Molenda   thread.CalculateExecutionContext(exe_ctx);
3322fd83355SJason Molenda 
333b9c1b51eSKate Stone   if (!m_get_item_info_impl_code) {
334b9c1b51eSKate Stone     error.SetErrorString("Unable to compile function to call "
335b9c1b51eSKate Stone                          "__introspection_dispatch_queue_item_get_info");
3362fd83355SJason Molenda     return return_value;
3372fd83355SJason Molenda   }
3382fd83355SJason Molenda 
3391624a2d3SJim Ingham   ExpressionResults func_call_ret;
3402fd83355SJason Molenda   Value results;
341151c032cSJim Ingham   FunctionCaller *func_caller = m_get_item_info_impl_code->GetFunctionCaller();
342b9c1b51eSKate Stone   if (!func_caller) {
34363e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Could not retrieve function caller for "
344b9c1b51eSKate Stone                    "__introspection_dispatch_queue_item_get_info.");
345b9c1b51eSKate Stone     error.SetErrorString("Could not retrieve function caller for "
346b9c1b51eSKate Stone                          "__introspection_dispatch_queue_item_get_info.");
347151c032cSJim Ingham     return return_value;
348151c032cSJim Ingham   }
349151c032cSJim Ingham 
350b9c1b51eSKate Stone   func_call_ret = func_caller->ExecuteFunction(exe_ctx, &args_addr, options,
351b9c1b51eSKate Stone                                                diagnostics, results);
352b9c1b51eSKate Stone   if (func_call_ret != eExpressionCompleted || !error.Success()) {
35363e5fb76SJonas Devlieghere     LLDB_LOGF(log,
35463e5fb76SJonas Devlieghere               "Unable to call "
355b9c1b51eSKate Stone               "__introspection_dispatch_queue_item_get_info(), got "
356b9c1b51eSKate Stone               "ExpressionResults %d, error contains %s",
357b9c1b51eSKate Stone               func_call_ret, error.AsCString(""));
358b9c1b51eSKate Stone     error.SetErrorString("Unable to call "
359b9c1b51eSKate Stone                          "__introspection_dispatch_queue_get_item_info() for "
360b9c1b51eSKate Stone                          "list of queues");
3612fd83355SJason Molenda     return return_value;
3622fd83355SJason Molenda   }
3632fd83355SJason Molenda 
364b9c1b51eSKate Stone   return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
365b9c1b51eSKate Stone       m_get_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
366b9c1b51eSKate Stone   if (!error.Success() ||
367b9c1b51eSKate Stone       return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
3682fd83355SJason Molenda     return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
3692fd83355SJason Molenda     return return_value;
3702fd83355SJason Molenda   }
3712fd83355SJason Molenda 
372b9c1b51eSKate Stone   return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
373b9c1b51eSKate Stone       m_get_item_info_return_buffer_addr + 8, 8, 0, error);
3742fd83355SJason Molenda 
375b9c1b51eSKate Stone   if (!error.Success()) {
3762fd83355SJason Molenda     return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
3772fd83355SJason Molenda     return return_value;
3782fd83355SJason Molenda   }
37963e5fb76SJonas Devlieghere   LLDB_LOGF(log,
38063e5fb76SJonas Devlieghere             "AppleGetItemInfoHandler called "
381b9c1b51eSKate Stone             "__introspection_dispatch_queue_item_get_info (page_to_free == "
38263e5fb76SJonas Devlieghere             "0x%" PRIx64 ", size = %" PRId64 "), returned page is at 0x%" PRIx64
38363e5fb76SJonas Devlieghere             ", size %" PRId64,
384b9c1b51eSKate Stone             page_to_free, page_to_free_size, return_value.item_buffer_ptr,
385b9c1b51eSKate Stone             return_value.item_buffer_size);
3862fd83355SJason Molenda 
3872fd83355SJason Molenda   return return_value;
3882fd83355SJason Molenda }
389