180814287SRaphael Isemann //===-- AppleGetPendingItemsHandler.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 "AppleGetPendingItemsHandler.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"
23*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
246f9e6901SZachary Turner #include "lldb/Utility/Log.h"
25bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
262fd83355SJason Molenda 
272fd83355SJason Molenda using namespace lldb;
282fd83355SJason Molenda using namespace lldb_private;
292fd83355SJason Molenda 
30b9c1b51eSKate Stone const char *AppleGetPendingItemsHandler::g_get_pending_items_function_name =
31b9c1b51eSKate Stone     "__lldb_backtrace_recording_get_pending_items";
32b9c1b51eSKate Stone const char *AppleGetPendingItemsHandler::g_get_pending_items_function_code =
33b9c1b51eSKate Stone     "                                  \n\
342fd83355SJason Molenda extern \"C\"                                                                                                    \n\
352fd83355SJason Molenda {                                                                                                               \n\
362fd83355SJason Molenda     /*                                                                                                          \n\
372fd83355SJason Molenda      * mach defines                                                                                             \n\
382fd83355SJason Molenda      */                                                                                                         \n\
392fd83355SJason Molenda                                                                                                                 \n\
402fd83355SJason Molenda     typedef unsigned int uint32_t;                                                                              \n\
412fd83355SJason Molenda     typedef unsigned long long uint64_t;                                                                        \n\
422fd83355SJason Molenda     typedef uint32_t mach_port_t;                                                                               \n\
432fd83355SJason Molenda     typedef mach_port_t vm_map_t;                                                                               \n\
442fd83355SJason Molenda     typedef int kern_return_t;                                                                                  \n\
452fd83355SJason Molenda     typedef uint64_t mach_vm_address_t;                                                                         \n\
462fd83355SJason Molenda     typedef uint64_t mach_vm_size_t;                                                                            \n\
472fd83355SJason Molenda                                                                                                                 \n\
482fd83355SJason Molenda     mach_port_t mach_task_self ();                                                                              \n\
492fd83355SJason Molenda     kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size);         \n\
502fd83355SJason Molenda                                                                                                                 \n\
512fd83355SJason Molenda     /*                                                                                                          \n\
522fd83355SJason Molenda      * libBacktraceRecording defines                                                                            \n\
532fd83355SJason Molenda      */                                                                                                         \n\
542fd83355SJason Molenda                                                                                                                 \n\
552fd83355SJason Molenda     typedef uint32_t queue_list_scope_t;                                                                        \n\
562fd83355SJason Molenda     typedef void *dispatch_queue_t;                                                                             \n\
572fd83355SJason Molenda     typedef void *introspection_dispatch_queue_info_t;                                                          \n\
582fd83355SJason Molenda     typedef void *introspection_dispatch_item_info_ref;                                                         \n\
592fd83355SJason Molenda                                                                                                                 \n\
602fd83355SJason Molenda     extern uint64_t __introspection_dispatch_queue_get_pending_items (dispatch_queue_t queue,                   \n\
612fd83355SJason Molenda                                                  introspection_dispatch_item_info_ref *returned_queues_buffer,  \n\
622fd83355SJason Molenda                                                  uint64_t *returned_queues_buffer_size);                        \n\
632fd83355SJason Molenda     extern int printf(const char *format, ...);                                                                 \n\
642fd83355SJason Molenda                                                                                                                 \n\
652fd83355SJason Molenda     /*                                                                                                          \n\
662fd83355SJason Molenda      * return type define                                                                                       \n\
672fd83355SJason Molenda      */                                                                                                         \n\
682fd83355SJason Molenda                                                                                                                 \n\
692fd83355SJason Molenda     struct get_pending_items_return_values                                                                      \n\
702fd83355SJason Molenda     {                                                                                                           \n\
712fd83355SJason Molenda         uint64_t pending_items_buffer_ptr;    /* the address of the items buffer from libBacktraceRecording */  \n\
722fd83355SJason Molenda         uint64_t pending_items_buffer_size;   /* the size of the items buffer from libBacktraceRecording */     \n\
732fd83355SJason Molenda         uint64_t count;                /* the number of items included in the queues buffer */                  \n\
742fd83355SJason Molenda     };                                                                                                          \n\
752fd83355SJason Molenda                                                                                                                 \n\
762fd83355SJason Molenda     void  __lldb_backtrace_recording_get_pending_items                                                          \n\
772fd83355SJason Molenda                                                (struct get_pending_items_return_values *return_buffer,          \n\
782fd83355SJason Molenda                                                 int debug,                                                      \n\
792fd83355SJason Molenda                                                 uint64_t /* dispatch_queue_t */ queue,                          \n\
802fd83355SJason Molenda                                                 void *page_to_free,                                             \n\
812fd83355SJason Molenda                                                 uint64_t page_to_free_size)                                     \n\
822fd83355SJason Molenda {                                                                                                               \n\
832fd83355SJason Molenda     if (debug)                                                                                                  \n\
842fd83355SJason Molenda       printf (\"entering get_pending_items with args return_buffer == %p, debug == %d, queue == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, queue, page_to_free, page_to_free_size); \n\
852fd83355SJason Molenda     if (page_to_free != 0)                                                                                      \n\
862fd83355SJason Molenda     {                                                                                                           \n\
872fd83355SJason Molenda         mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
882fd83355SJason Molenda     }                                                                                                           \n\
892fd83355SJason Molenda                                                                                                                 \n\
902fd83355SJason Molenda     return_buffer->count = __introspection_dispatch_queue_get_pending_items (                                   \n\
912fd83355SJason Molenda                                                       (void*) queue,                                            \n\
922fd83355SJason Molenda                                                       (void**)&return_buffer->pending_items_buffer_ptr,         \n\
932fd83355SJason Molenda                                                       &return_buffer->pending_items_buffer_size);               \n\
942fd83355SJason Molenda     if (debug)                                                                                                  \n\
952fd83355SJason Molenda         printf(\"result was count %lld\\n\", return_buffer->count);                                             \n\
962fd83355SJason Molenda }                                                                                                               \n\
972fd83355SJason Molenda }                                                                                                               \n\
982fd83355SJason Molenda ";
992fd83355SJason Molenda 
AppleGetPendingItemsHandler(Process * process)10016ff8604SSaleem Abdulrasool AppleGetPendingItemsHandler::AppleGetPendingItemsHandler(Process *process)
101b9c1b51eSKate Stone     : m_process(process), m_get_pending_items_impl_code(),
1022fd83355SJason Molenda       m_get_pending_items_function_mutex(),
1032fd83355SJason Molenda       m_get_pending_items_return_buffer_addr(LLDB_INVALID_ADDRESS),
104b9c1b51eSKate Stone       m_get_pending_items_retbuffer_mutex() {}
1052fd83355SJason Molenda 
106fd2433e1SJonas Devlieghere AppleGetPendingItemsHandler::~AppleGetPendingItemsHandler() = default;
1072fd83355SJason Molenda 
Detach()108b9c1b51eSKate Stone void AppleGetPendingItemsHandler::Detach() {
109b9c1b51eSKate Stone   if (m_process && m_process->IsAlive() &&
110b9c1b51eSKate Stone       m_get_pending_items_return_buffer_addr != LLDB_INVALID_ADDRESS) {
111b9c1b51eSKate Stone     std::unique_lock<std::mutex> lock(m_get_pending_items_retbuffer_mutex,
112b9c1b51eSKate Stone                                       std::defer_lock);
1130c86dfb8SReid Kleckner     (void)lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
1142fd83355SJason Molenda     m_process->DeallocateMemory(m_get_pending_items_return_buffer_addr);
1152fd83355SJason Molenda   }
1162fd83355SJason Molenda }
1172fd83355SJason Molenda 
11805097246SAdrian Prantl // Compile our __lldb_backtrace_recording_get_pending_items() function (from
11905097246SAdrian Prantl // the source above in g_get_pending_items_function_code) if we don't find that
12005097246SAdrian Prantl // function in the inferior already with USE_BUILTIN_FUNCTION defined.  (e.g.
12105097246SAdrian Prantl // this would be the case for testing.)
1222fd83355SJason Molenda //
123b9c1b51eSKate Stone // Insert the __lldb_backtrace_recording_get_pending_items into the inferior
124b9c1b51eSKate Stone // process if needed.
1252fd83355SJason Molenda //
126b9c1b51eSKate Stone // Write the get_pending_items_arglist into the inferior's memory space to
127b9c1b51eSKate Stone // prepare for the call.
1282fd83355SJason Molenda //
129b9c1b51eSKate Stone // Returns the address of the arguments written down in the inferior process,
13005097246SAdrian Prantl // which can be used to make the function call.
1312fd83355SJason Molenda 
SetupGetPendingItemsFunction(Thread & thread,ValueList & get_pending_items_arglist)132b9c1b51eSKate Stone lldb::addr_t AppleGetPendingItemsHandler::SetupGetPendingItemsFunction(
133b9c1b51eSKate Stone     Thread &thread, ValueList &get_pending_items_arglist) {
1346896b355SJim Ingham   ThreadSP thread_sp(thread.shared_from_this());
1356896b355SJim Ingham   ExecutionContext exe_ctx(thread_sp);
136579e70c9SSean Callanan   DiagnosticManager diagnostics;
137a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::SystemRuntime);
1386896b355SJim Ingham 
1392fd83355SJason Molenda   lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
140151c032cSJim Ingham   FunctionCaller *get_pending_items_caller = nullptr;
1412fd83355SJason Molenda 
1422fd83355SJason Molenda   // Scope for mutex locker:
1432fd83355SJason Molenda   {
14416ff8604SSaleem Abdulrasool     std::lock_guard<std::mutex> guard(m_get_pending_items_function_mutex);
1452fd83355SJason Molenda 
1462fd83355SJason Molenda     // First stage is to make the ClangUtility to hold our injected function:
1472fd83355SJason Molenda 
14870355aceSJonas Devlieghere     if (!m_get_pending_items_impl_code) {
149248a1305SKonrad Kleine       if (g_get_pending_items_function_code != nullptr) {
150de346cf2SJonas Devlieghere         auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
151de346cf2SJonas Devlieghere             g_get_pending_items_function_code,
152de346cf2SJonas Devlieghere             g_get_pending_items_function_name, eLanguageTypeC, exe_ctx);
153de346cf2SJonas Devlieghere         if (!utility_fn_or_error) {
154de346cf2SJonas Devlieghere           LLDB_LOG_ERROR(log, utility_fn_or_error.takeError(),
155de346cf2SJonas Devlieghere                          "Failed to create UtilityFunction for pending-items "
156de346cf2SJonas Devlieghere                          "introspection: {0}.");
157151c032cSJim Ingham           return args_addr;
158151c032cSJim Ingham         }
159de346cf2SJonas Devlieghere         m_get_pending_items_impl_code = std::move(*utility_fn_or_error);
160b9c1b51eSKate Stone       } else {
16163e5fb76SJonas Devlieghere         LLDB_LOGF(log, "No pending-items introspection code found.");
1622fd83355SJason Molenda         return LLDB_INVALID_ADDRESS;
1632fd83355SJason Molenda       }
1642fd83355SJason Molenda 
1652fd83355SJason Molenda       // Next make the runner function for our implementation utility function.
16697206d57SZachary Turner       Status error;
167594308c7SRaphael Isemann       TypeSystemClang *clang_ast_context = ScratchTypeSystemClang::GetForTarget(
168594308c7SRaphael Isemann           thread.GetProcess()->GetTarget());
169b9c1b51eSKate Stone       CompilerType get_pending_items_return_type =
170b9c1b51eSKate Stone           clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
171b9c1b51eSKate Stone       get_pending_items_caller =
172b9c1b51eSKate Stone           m_get_pending_items_impl_code->MakeFunctionCaller(
173b9c1b51eSKate Stone               get_pending_items_return_type, get_pending_items_arglist,
174b9c1b51eSKate Stone               thread_sp, error);
175ed2977faSJason Molenda       if (error.Fail() || get_pending_items_caller == nullptr) {
17663e5fb76SJonas Devlieghere         LLDB_LOGF(log,
17763e5fb76SJonas Devlieghere                   "Failed to install pending-items introspection function "
178b9c1b51eSKate Stone                   "caller: %s.",
179b9c1b51eSKate Stone                   error.AsCString());
180151c032cSJim Ingham         m_get_pending_items_impl_code.reset();
1812fd83355SJason Molenda         return args_addr;
1822fd83355SJason Molenda       }
1832fd83355SJason Molenda     }
1842fd83355SJason Molenda   }
1852fd83355SJason Molenda 
186579e70c9SSean Callanan   diagnostics.Clear();
1872fd83355SJason Molenda 
188b9c1b51eSKate Stone   if (get_pending_items_caller == nullptr) {
18963e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Failed to get get_pending_items_caller.");
190d76b2a42SJason Molenda     return LLDB_INVALID_ADDRESS;
191d76b2a42SJason Molenda   }
192d76b2a42SJason 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_pending_items_caller->WriteFunctionArguments(
199b9c1b51eSKate Stone           exe_ctx, args_addr, get_pending_items_arglist, diagnostics)) {
200b9c1b51eSKate Stone     if (log) {
20163e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Error writing pending-items 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 AppleGetPendingItemsHandler::GetPendingItemsReturnInfo
GetPendingItems(Thread & thread,addr_t queue,addr_t page_to_free,uint64_t page_to_free_size,Status & error)212b9c1b51eSKate Stone AppleGetPendingItemsHandler::GetPendingItems(Thread &thread, addr_t queue,
213b9c1b51eSKate Stone                                              addr_t page_to_free,
214b9c1b51eSKate Stone                                              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);
221a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::SystemRuntime);
2222fd83355SJason Molenda 
2232fd83355SJason Molenda   GetPendingItemsReturnInfo return_value;
2242fd83355SJason Molenda   return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
2252fd83355SJason Molenda   return_value.items_buffer_size = 0;
2262fd83355SJason Molenda   return_value.count = 0;
2272fd83355SJason Molenda 
2282fd83355SJason Molenda   error.Clear();
2292fd83355SJason Molenda 
230a6682a41SJonas Devlieghere   if (!thread.SafeToCallFunctions()) {
23163e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Not safe to call functions on thread 0x%" PRIx64,
232b9c1b51eSKate Stone               thread.GetID());
233b4892cd2SJason Molenda     error.SetErrorString("Not safe to call functions on this thread.");
234b4892cd2SJason Molenda     return return_value;
235b4892cd2SJason Molenda   }
236b4892cd2SJason Molenda 
2372fd83355SJason Molenda   // Set up the arguments for a call to
2382fd83355SJason Molenda 
2392fd83355SJason Molenda   // struct get_pending_items_return_values
2402fd83355SJason Molenda   // {
241b9c1b51eSKate Stone   //     uint64_t pending_items_buffer_ptr;    /* the address of the items
242b9c1b51eSKate Stone   //     buffer from libBacktraceRecording */
243b9c1b51eSKate Stone   //     uint64_t pending_items_buffer_size;   /* the size of the items buffer
244b9c1b51eSKate Stone   //     from libBacktraceRecording */
245b9c1b51eSKate Stone   //     uint64_t count;                /* the number of items included in the
246b9c1b51eSKate Stone   //     queues buffer */
2472fd83355SJason Molenda   // };
2482fd83355SJason Molenda   //
2492fd83355SJason Molenda   // void  __lldb_backtrace_recording_get_pending_items
250b9c1b51eSKate Stone   //                                            (struct
251b9c1b51eSKate Stone   //                                            get_pending_items_return_values
252b9c1b51eSKate Stone   //                                            *return_buffer,
2532fd83355SJason Molenda   //                                             int debug,
254b9c1b51eSKate Stone   //                                             uint64_t /* dispatch_queue_t */
255b9c1b51eSKate Stone   //                                             queue
2562fd83355SJason Molenda   //                                             void *page_to_free,
2572fd83355SJason Molenda   //                                             uint64_t page_to_free_size)
2582fd83355SJason Molenda 
259b9c1b51eSKate Stone   // Where the return_buffer argument points to a 24 byte region of memory
26005097246SAdrian Prantl   // already allocated by lldb in the inferior process.
2612fd83355SJason Molenda 
262b9c1b51eSKate Stone   CompilerType clang_void_ptr_type =
263b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
2642fd83355SJason Molenda   Value return_buffer_ptr_value;
265057efa99SAdrian Prantl   return_buffer_ptr_value.SetValueType(Value::ValueType::Scalar);
26699558cc4SGreg Clayton   return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
2672fd83355SJason Molenda 
268a1e5dc86SGreg Clayton   CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
2692fd83355SJason Molenda   Value debug_value;
270057efa99SAdrian Prantl   debug_value.SetValueType(Value::ValueType::Scalar);
27199558cc4SGreg Clayton   debug_value.SetCompilerType(clang_int_type);
2722fd83355SJason Molenda 
273b9c1b51eSKate Stone   CompilerType clang_uint64_type =
274b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
2752fd83355SJason Molenda   Value queue_value;
276057efa99SAdrian Prantl   queue_value.SetValueType(Value::ValueType::Scalar);
27799558cc4SGreg Clayton   queue_value.SetCompilerType(clang_uint64_type);
2782fd83355SJason Molenda 
2792fd83355SJason Molenda   Value page_to_free_value;
280057efa99SAdrian Prantl   page_to_free_value.SetValueType(Value::ValueType::Scalar);
28199558cc4SGreg Clayton   page_to_free_value.SetCompilerType(clang_void_ptr_type);
2822fd83355SJason Molenda 
2832fd83355SJason Molenda   Value page_to_free_size_value;
284057efa99SAdrian Prantl   page_to_free_size_value.SetValueType(Value::ValueType::Scalar);
28599558cc4SGreg Clayton   page_to_free_size_value.SetCompilerType(clang_uint64_type);
2862fd83355SJason Molenda 
28716ff8604SSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_get_pending_items_retbuffer_mutex);
288b9c1b51eSKate Stone   if (m_get_pending_items_return_buffer_addr == LLDB_INVALID_ADDRESS) {
289b9c1b51eSKate Stone     addr_t bufaddr = process_sp->AllocateMemory(
290b9c1b51eSKate Stone         32, ePermissionsReadable | ePermissionsWritable, error);
291b9c1b51eSKate Stone     if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
29263e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Failed to allocate memory for return buffer for get "
293b9c1b51eSKate Stone                      "current queues func call");
2942fd83355SJason Molenda       return return_value;
2952fd83355SJason Molenda     }
2962fd83355SJason Molenda     m_get_pending_items_return_buffer_addr = bufaddr;
2972fd83355SJason Molenda   }
2982fd83355SJason Molenda 
2992fd83355SJason Molenda   ValueList argument_values;
3002fd83355SJason Molenda 
3012fd83355SJason Molenda   return_buffer_ptr_value.GetScalar() = m_get_pending_items_return_buffer_addr;
3022fd83355SJason Molenda   argument_values.PushValue(return_buffer_ptr_value);
3032fd83355SJason Molenda 
3042fd83355SJason Molenda   debug_value.GetScalar() = 0;
3052fd83355SJason Molenda   argument_values.PushValue(debug_value);
3062fd83355SJason Molenda 
3072fd83355SJason Molenda   queue_value.GetScalar() = queue;
3082fd83355SJason Molenda   argument_values.PushValue(queue_value);
3092fd83355SJason Molenda 
3102fd83355SJason Molenda   if (page_to_free != LLDB_INVALID_ADDRESS)
3112fd83355SJason Molenda     page_to_free_value.GetScalar() = page_to_free;
3122fd83355SJason Molenda   else
3132fd83355SJason Molenda     page_to_free_value.GetScalar() = 0;
3142fd83355SJason Molenda   argument_values.PushValue(page_to_free_value);
3152fd83355SJason Molenda 
3162fd83355SJason Molenda   page_to_free_size_value.GetScalar() = page_to_free_size;
3172fd83355SJason Molenda   argument_values.PushValue(page_to_free_size_value);
3182fd83355SJason Molenda 
3192fd83355SJason Molenda   addr_t args_addr = SetupGetPendingItemsFunction(thread, argument_values);
3202fd83355SJason Molenda 
321579e70c9SSean Callanan   DiagnosticManager diagnostics;
3222fd83355SJason Molenda   ExecutionContext exe_ctx;
323b9c1b51eSKate Stone   FunctionCaller *get_pending_items_caller =
324b9c1b51eSKate Stone       m_get_pending_items_impl_code->GetFunctionCaller();
325151c032cSJim Ingham 
3262fd83355SJason Molenda   EvaluateExpressionOptions options;
3272fd83355SJason Molenda   options.SetUnwindOnError(true);
3282fd83355SJason Molenda   options.SetIgnoreBreakpoints(true);
3292fd83355SJason Molenda   options.SetStopOthers(true);
3305b0a687dSJason Molenda #if __has_feature(address_sanitizer)
3314c03ea14SAdrian Prantl   options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
3325b0a687dSJason Molenda #else
3335b0a687dSJason Molenda   options.SetTimeout(std::chrono::milliseconds(500));
3345b0a687dSJason Molenda #endif
335a0560152SJason Molenda   options.SetTryAllThreads(false);
336c01783a8SRaphael Isemann   options.SetIsForUtilityExpr(true);
3372fd83355SJason Molenda   thread.CalculateExecutionContext(exe_ctx);
3382fd83355SJason Molenda 
339248a1305SKonrad Kleine   if (get_pending_items_caller == nullptr) {
340b9c1b51eSKate Stone     error.SetErrorString("Unable to compile function to call "
341b9c1b51eSKate Stone                          "__introspection_dispatch_queue_get_pending_items");
342fa4286eaSJason Molenda     return return_value;
3432fd83355SJason Molenda   }
3442fd83355SJason Molenda 
3451624a2d3SJim Ingham   ExpressionResults func_call_ret;
3462fd83355SJason Molenda   Value results;
347b9c1b51eSKate Stone   func_call_ret = get_pending_items_caller->ExecuteFunction(
348b9c1b51eSKate Stone       exe_ctx, &args_addr, options, diagnostics, results);
349b9c1b51eSKate Stone   if (func_call_ret != eExpressionCompleted || !error.Success()) {
35063e5fb76SJonas Devlieghere     LLDB_LOGF(log,
35163e5fb76SJonas Devlieghere               "Unable to call "
352b9c1b51eSKate Stone               "__introspection_dispatch_queue_get_pending_items(), got "
353b9c1b51eSKate Stone               "ExpressionResults %d, error contains %s",
354b9c1b51eSKate Stone               func_call_ret, error.AsCString(""));
355b9c1b51eSKate Stone     error.SetErrorString("Unable to call "
356b9c1b51eSKate Stone                          "__introspection_dispatch_queue_get_pending_items() "
357b9c1b51eSKate Stone                          "for list of queues");
3582fd83355SJason Molenda     return return_value;
3592fd83355SJason Molenda   }
3602fd83355SJason Molenda 
361b9c1b51eSKate Stone   return_value.items_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
362b9c1b51eSKate Stone       m_get_pending_items_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
363b9c1b51eSKate Stone   if (!error.Success() ||
364b9c1b51eSKate Stone       return_value.items_buffer_ptr == LLDB_INVALID_ADDRESS) {
3652fd83355SJason Molenda     return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
3662fd83355SJason Molenda     return return_value;
3672fd83355SJason Molenda   }
3682fd83355SJason Molenda 
369b9c1b51eSKate Stone   return_value.items_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
370b9c1b51eSKate Stone       m_get_pending_items_return_buffer_addr + 8, 8, 0, error);
3712fd83355SJason Molenda 
372b9c1b51eSKate Stone   if (!error.Success()) {
3732fd83355SJason Molenda     return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
3742fd83355SJason Molenda     return return_value;
3752fd83355SJason Molenda   }
3762fd83355SJason Molenda 
377b9c1b51eSKate Stone   return_value.count = m_process->ReadUnsignedIntegerFromMemory(
378b9c1b51eSKate Stone       m_get_pending_items_return_buffer_addr + 16, 8, 0, error);
379b9c1b51eSKate Stone   if (!error.Success()) {
3802fd83355SJason Molenda     return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
3812fd83355SJason Molenda     return return_value;
3822fd83355SJason Molenda   }
3832fd83355SJason Molenda 
38463e5fb76SJonas Devlieghere   LLDB_LOGF(log,
38563e5fb76SJonas Devlieghere             "AppleGetPendingItemsHandler called "
386b9c1b51eSKate Stone             "__introspection_dispatch_queue_get_pending_items "
387b9c1b51eSKate Stone             "(page_to_free == 0x%" PRIx64 ", size = %" PRId64
388b9c1b51eSKate Stone             "), returned page is at 0x%" PRIx64 ", size %" PRId64
389b9c1b51eSKate Stone             ", count = %" PRId64,
390b9c1b51eSKate Stone             page_to_free, page_to_free_size, return_value.items_buffer_ptr,
391b9c1b51eSKate Stone             return_value.items_buffer_size, return_value.count);
3922a6c252dSJason Molenda 
3932fd83355SJason Molenda   return return_value;
3942fd83355SJason Molenda }
395