180814287SRaphael Isemann //===-- AppleGetQueuesHandler.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 "AppleGetQueuesHandler.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"
20cfe7d6c4SBruce Mitchener #include "lldb/Target/Target.h"
21cfe7d6c4SBruce Mitchener #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 *AppleGetQueuesHandler::g_get_current_queues_function_name =
31b9c1b51eSKate Stone     "__lldb_backtrace_recording_get_current_queues";
32b9c1b51eSKate Stone const char *AppleGetQueuesHandler::g_get_current_queues_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 *introspection_dispatch_queue_info_t;                                                          \n\
572fd83355SJason Molenda                                                                                                                 \n\
582fd83355SJason Molenda     extern uint64_t __introspection_dispatch_get_queues (queue_list_scope_t scope,                              \n\
592fd83355SJason Molenda                                                  introspection_dispatch_queue_info_t *returned_queues_buffer,   \n\
602fd83355SJason Molenda                                                  uint64_t *returned_queues_buffer_size);                        \n\
612fd83355SJason Molenda     extern int printf(const char *format, ...);                                                                 \n\
622fd83355SJason Molenda                                                                                                                 \n\
632fd83355SJason Molenda     /*                                                                                                          \n\
642fd83355SJason Molenda      * return type define                                                                                       \n\
652fd83355SJason Molenda      */                                                                                                         \n\
662fd83355SJason Molenda                                                                                                                 \n\
672fd83355SJason Molenda     struct get_current_queues_return_values                                                                     \n\
682fd83355SJason Molenda     {                                                                                                           \n\
692fd83355SJason Molenda         uint64_t queues_buffer_ptr;    /* the address of the queues buffer from libBacktraceRecording */        \n\
702fd83355SJason Molenda         uint64_t queues_buffer_size;   /* the size of the queues buffer from libBacktraceRecording */           \n\
712fd83355SJason Molenda         uint64_t count;                /* the number of queues included in the queues buffer */                 \n\
722fd83355SJason Molenda     };                                                                                                          \n\
732fd83355SJason Molenda                                                                                                                 \n\
742fd83355SJason Molenda     void  __lldb_backtrace_recording_get_current_queues                                                         \n\
752fd83355SJason Molenda                                                (struct get_current_queues_return_values *return_buffer,         \n\
762fd83355SJason Molenda                                                 int debug,                                                      \n\
772fd83355SJason Molenda                                                 void *page_to_free,                                             \n\
782fd83355SJason Molenda                                                 uint64_t page_to_free_size)                                     \n\
792fd83355SJason Molenda {                                                                                                               \n\
802fd83355SJason Molenda     if (debug)                                                                                                  \n\
812fd83355SJason Molenda       printf (\"entering get_current_queues with args %p, %d, 0x%p, 0x%llx\\n\", return_buffer, debug, page_to_free, page_to_free_size); \n\
822fd83355SJason Molenda     if (page_to_free != 0)                                                                                      \n\
832fd83355SJason Molenda     {                                                                                                           \n\
842fd83355SJason Molenda         mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
852fd83355SJason Molenda     }                                                                                                           \n\
862fd83355SJason Molenda                                                                                                                 \n\
872fd83355SJason Molenda     return_buffer->count = __introspection_dispatch_get_queues (                                                \n\
882fd83355SJason Molenda                                                       /* QUEUES_WITH_ANY_ITEMS */ 2,                            \n\
892fd83355SJason Molenda                                                       (void**)&return_buffer->queues_buffer_ptr,                \n\
902fd83355SJason Molenda                                                       &return_buffer->queues_buffer_size);                      \n\
912fd83355SJason Molenda     if (debug)                                                                                                  \n\
922fd83355SJason Molenda         printf(\"result was count %lld\\n\", return_buffer->count);                                             \n\
932fd83355SJason Molenda }                                                                                                               \n\
942fd83355SJason Molenda }                                                                                                               \n\
952fd83355SJason Molenda ";
962fd83355SJason Molenda 
AppleGetQueuesHandler(Process * process)9716ff8604SSaleem Abdulrasool AppleGetQueuesHandler::AppleGetQueuesHandler(Process *process)
98b9c1b51eSKate Stone     : m_process(process), m_get_queues_impl_code_up(),
992fd83355SJason Molenda       m_get_queues_function_mutex(),
1002fd83355SJason Molenda       m_get_queues_return_buffer_addr(LLDB_INVALID_ADDRESS),
101b9c1b51eSKate Stone       m_get_queues_retbuffer_mutex() {}
1022fd83355SJason Molenda 
103fd2433e1SJonas Devlieghere AppleGetQueuesHandler::~AppleGetQueuesHandler() = default;
1042fd83355SJason Molenda 
Detach()105b9c1b51eSKate Stone void AppleGetQueuesHandler::Detach() {
1062fd83355SJason Molenda 
107b9c1b51eSKate Stone   if (m_process && m_process->IsAlive() &&
108b9c1b51eSKate Stone       m_get_queues_return_buffer_addr != LLDB_INVALID_ADDRESS) {
109b9c1b51eSKate Stone     std::unique_lock<std::mutex> lock(m_get_queues_retbuffer_mutex,
110b9c1b51eSKate Stone                                       std::defer_lock);
1110c86dfb8SReid Kleckner     (void)lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
1122fd83355SJason Molenda     m_process->DeallocateMemory(m_get_queues_return_buffer_addr);
1132fd83355SJason Molenda   }
1142fd83355SJason Molenda }
1152fd83355SJason Molenda 
116b9c1b51eSKate Stone // Construct a CompilerType for the structure that
11705097246SAdrian Prantl // g_get_current_queues_function_code will return by value so we can extract
11805097246SAdrian Prantl // the fields after performing the function call. i.e. we are getting this
11905097246SAdrian Prantl // struct returned to us:
1202fd83355SJason Molenda //
1212fd83355SJason Molenda //    struct get_current_queues_return_values
1222fd83355SJason Molenda //    {
1232fd83355SJason Molenda //        introspection_dispatch_queue_info_t *queues_buffer;
1242fd83355SJason Molenda //        uint64_t queues_buffer_size;
1252fd83355SJason Molenda //        uint64_t count;
1262fd83355SJason Molenda //    };
1272fd83355SJason Molenda 
128b9c1b51eSKate Stone // Compile our __lldb_backtrace_recording_get_current_queues() function (from
12905097246SAdrian Prantl // the source above in g_get_current_queues_function_code) if we don't find
13005097246SAdrian Prantl // that function in the inferior already with USE_BUILTIN_FUNCTION defined.
13105097246SAdrian Prantl // (e.g. this would be the case for testing.)
1322fd83355SJason Molenda //
133b9c1b51eSKate Stone // Insert the __lldb_backtrace_recording_get_current_queues into the inferior
134b9c1b51eSKate Stone // process if needed.
1352fd83355SJason Molenda //
136b9c1b51eSKate Stone // Write the get_queues_arglist into the inferior's memory space to prepare for
137b9c1b51eSKate Stone // the call.
1382fd83355SJason Molenda //
139b9c1b51eSKate Stone // Returns the address of the arguments written down in the inferior process,
14005097246SAdrian Prantl // which can be used to make the function call.
1412fd83355SJason Molenda 
1422fd83355SJason Molenda lldb::addr_t
SetupGetQueuesFunction(Thread & thread,ValueList & get_queues_arglist)143b9c1b51eSKate Stone AppleGetQueuesHandler::SetupGetQueuesFunction(Thread &thread,
144b9c1b51eSKate Stone                                               ValueList &get_queues_arglist) {
1456896b355SJim Ingham   ThreadSP thread_sp(thread.shared_from_this());
1466896b355SJim Ingham   ExecutionContext exe_ctx(thread_sp);
1476896b355SJim Ingham 
1482fd83355SJason Molenda   Address impl_code_address;
149579e70c9SSean Callanan   DiagnosticManager diagnostics;
150a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::SystemRuntime);
1512fd83355SJason Molenda   lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
1522fd83355SJason Molenda 
153151c032cSJim Ingham   FunctionCaller *get_queues_caller = nullptr;
154151c032cSJim Ingham 
1552fd83355SJason Molenda   // Scope for mutex locker:
1562fd83355SJason Molenda   {
15716ff8604SSaleem Abdulrasool     std::lock_guard<std::mutex> guard(m_get_queues_function_mutex);
1582fd83355SJason Molenda 
1592fd83355SJason Molenda     // First stage is to make the ClangUtility to hold our injected function:
1602fd83355SJason Molenda 
16170355aceSJonas Devlieghere     if (!m_get_queues_impl_code_up) {
162248a1305SKonrad Kleine       if (g_get_current_queues_function_code != nullptr) {
163de346cf2SJonas Devlieghere         auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
164de346cf2SJonas Devlieghere             g_get_current_queues_function_code,
165de346cf2SJonas Devlieghere             g_get_current_queues_function_name, eLanguageTypeC, exe_ctx);
166de346cf2SJonas Devlieghere         if (!utility_fn_or_error) {
167de346cf2SJonas Devlieghere           LLDB_LOG_ERROR(log, utility_fn_or_error.takeError(),
168de346cf2SJonas Devlieghere                          "Failed to create UtilityFunction for queues "
169de346cf2SJonas Devlieghere                          "introspection: {0}.");
170151c032cSJim Ingham           return args_addr;
171151c032cSJim Ingham         }
172de346cf2SJonas Devlieghere         m_get_queues_impl_code_up = std::move(*utility_fn_or_error);
173b9c1b51eSKate Stone       } else {
174b9c1b51eSKate Stone         if (log) {
17563e5fb76SJonas Devlieghere           LLDB_LOGF(log, "No queues introspection code found.");
176579e70c9SSean Callanan           diagnostics.Dump(log);
177579e70c9SSean Callanan         }
1782fd83355SJason Molenda         return LLDB_INVALID_ADDRESS;
1792fd83355SJason Molenda       }
1802fd83355SJason Molenda     }
1812fd83355SJason Molenda 
1822fd83355SJason Molenda     // Next make the runner function for our implementation utility function.
1836e3b0cc2SRaphael Isemann     TypeSystemClang *clang_ast_context =
184594308c7SRaphael Isemann         ScratchTypeSystemClang::GetForTarget(thread.GetProcess()->GetTarget());
185b9c1b51eSKate Stone     CompilerType get_queues_return_type =
186b9c1b51eSKate Stone         clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
18797206d57SZachary Turner     Status error;
188b9c1b51eSKate Stone     get_queues_caller = m_get_queues_impl_code_up->MakeFunctionCaller(
189b9c1b51eSKate Stone         get_queues_return_type, get_queues_arglist, thread_sp, error);
190ed2977faSJason Molenda     if (error.Fail() || get_queues_caller == nullptr) {
19163e5fb76SJonas Devlieghere       LLDB_LOGF(log,
192b9c1b51eSKate Stone                 "Could not get function caller for get-queues function: %s.",
193b9c1b51eSKate Stone                 error.AsCString());
1942fd83355SJason Molenda       return args_addr;
1952fd83355SJason Molenda     }
1962fd83355SJason Molenda   }
1972fd83355SJason Molenda 
198579e70c9SSean Callanan   diagnostics.Clear();
1992fd83355SJason Molenda 
200b9c1b51eSKate Stone   // Now write down the argument values for this particular call.  This looks
20105097246SAdrian Prantl   // like it might be a race condition if other threads were calling into here,
20205097246SAdrian Prantl   // but actually it isn't because we allocate a new args structure for this
20305097246SAdrian Prantl   // call by passing args_addr = LLDB_INVALID_ADDRESS...
2042fd83355SJason Molenda 
205b9c1b51eSKate Stone   if (!get_queues_caller->WriteFunctionArguments(
206b9c1b51eSKate Stone           exe_ctx, args_addr, get_queues_arglist, diagnostics)) {
207b9c1b51eSKate Stone     if (log) {
20863e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Error writing get-queues function arguments.");
209579e70c9SSean Callanan       diagnostics.Dump(log);
210579e70c9SSean Callanan     }
2112fd83355SJason Molenda     return args_addr;
2122fd83355SJason Molenda   }
2132fd83355SJason Molenda 
2142fd83355SJason Molenda   return args_addr;
2152fd83355SJason Molenda }
2162fd83355SJason Molenda 
2172fd83355SJason Molenda AppleGetQueuesHandler::GetQueuesReturnInfo
GetCurrentQueues(Thread & thread,addr_t page_to_free,uint64_t page_to_free_size,Status & error)218b9c1b51eSKate Stone AppleGetQueuesHandler::GetCurrentQueues(Thread &thread, addr_t page_to_free,
219b9c1b51eSKate Stone                                         uint64_t page_to_free_size,
22097206d57SZachary Turner                                         Status &error) {
2212fd83355SJason Molenda   lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
2222fd83355SJason Molenda   ProcessSP process_sp(thread.CalculateProcess());
2232fd83355SJason Molenda   TargetSP target_sp(thread.CalculateTarget());
224594308c7SRaphael Isemann   TypeSystemClang *clang_ast_context =
225594308c7SRaphael Isemann       ScratchTypeSystemClang::GetForTarget(*target_sp);
226a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::SystemRuntime);
2272fd83355SJason Molenda 
2282fd83355SJason Molenda   GetQueuesReturnInfo return_value;
2292fd83355SJason Molenda   return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
2302fd83355SJason Molenda   return_value.queues_buffer_size = 0;
2312fd83355SJason Molenda   return_value.count = 0;
2322fd83355SJason Molenda 
2332fd83355SJason Molenda   error.Clear();
2342fd83355SJason Molenda 
235a6682a41SJonas Devlieghere   if (!thread.SafeToCallFunctions()) {
23663e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Not safe to call functions on thread 0x%" PRIx64,
237b9c1b51eSKate Stone               thread.GetID());
238b4892cd2SJason Molenda     error.SetErrorString("Not safe to call functions on this thread.");
239b4892cd2SJason Molenda     return return_value;
240b4892cd2SJason Molenda   }
241b4892cd2SJason Molenda 
2422fd83355SJason Molenda   // Set up the arguments for a call to
2432fd83355SJason Molenda 
2442fd83355SJason Molenda   // struct get_current_queues_return_values
2452fd83355SJason Molenda   // {
246b9c1b51eSKate Stone   //    uint64_t queues_buffer_ptr;    /* the address of the queues buffer from
247b9c1b51eSKate Stone   //    libBacktraceRecording */
248b9c1b51eSKate Stone   //    uint64_t queues_buffer_size;   /* the size of the queues buffer from
249b9c1b51eSKate Stone   //    libBacktraceRecording */
250b9c1b51eSKate Stone   //    uint64_t count;                /* the number of queues included in the
251b9c1b51eSKate Stone   //    queues buffer */
2522fd83355SJason Molenda   // };
2532fd83355SJason Molenda   //
2542fd83355SJason Molenda   //  void
2552fd83355SJason Molenda   //    __lldb_backtrace_recording_get_current_queues
256b9c1b51eSKate Stone   //                                         (struct
257b9c1b51eSKate Stone   //                                         get_current_queues_return_values
258b9c1b51eSKate Stone   //                                         *return_buffer,
2592fd83355SJason Molenda   //                                          void *page_to_free,
2602fd83355SJason Molenda   //                                          uint64_t page_to_free_size);
2612fd83355SJason Molenda 
262b9c1b51eSKate Stone   // Where the return_buffer argument points to a 24 byte region of memory
26305097246SAdrian Prantl   // already allocated by lldb in the inferior process.
2642fd83355SJason Molenda 
265b9c1b51eSKate Stone   CompilerType clang_void_ptr_type =
266b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
2672fd83355SJason Molenda   Value return_buffer_ptr_value;
268057efa99SAdrian Prantl   return_buffer_ptr_value.SetValueType(Value::ValueType::Scalar);
26999558cc4SGreg Clayton   return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
2702fd83355SJason Molenda 
271a1e5dc86SGreg Clayton   CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
2722fd83355SJason Molenda   Value debug_value;
273057efa99SAdrian Prantl   debug_value.SetValueType(Value::ValueType::Scalar);
27499558cc4SGreg Clayton   debug_value.SetCompilerType(clang_int_type);
2752fd83355SJason Molenda 
2762fd83355SJason Molenda   Value page_to_free_value;
277057efa99SAdrian Prantl   page_to_free_value.SetValueType(Value::ValueType::Scalar);
27899558cc4SGreg Clayton   page_to_free_value.SetCompilerType(clang_void_ptr_type);
2792fd83355SJason Molenda 
280b9c1b51eSKate Stone   CompilerType clang_uint64_type =
281b9c1b51eSKate Stone       clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
2822fd83355SJason Molenda   Value page_to_free_size_value;
283057efa99SAdrian Prantl   page_to_free_size_value.SetValueType(Value::ValueType::Scalar);
28499558cc4SGreg Clayton   page_to_free_size_value.SetCompilerType(clang_uint64_type);
2852fd83355SJason Molenda 
28616ff8604SSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_get_queues_retbuffer_mutex);
287b9c1b51eSKate Stone   if (m_get_queues_return_buffer_addr == LLDB_INVALID_ADDRESS) {
288b9c1b51eSKate Stone     addr_t bufaddr = process_sp->AllocateMemory(
289b9c1b51eSKate Stone         32, ePermissionsReadable | ePermissionsWritable, error);
290b9c1b51eSKate Stone     if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
29163e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Failed to allocate memory for return buffer for get "
292b9c1b51eSKate Stone                      "current queues func call");
2932fd83355SJason Molenda       return return_value;
2942fd83355SJason Molenda     }
2952fd83355SJason Molenda     m_get_queues_return_buffer_addr = bufaddr;
2962fd83355SJason Molenda   }
2972fd83355SJason Molenda 
2982fd83355SJason Molenda   ValueList argument_values;
2992fd83355SJason Molenda 
3002fd83355SJason Molenda   return_buffer_ptr_value.GetScalar() = m_get_queues_return_buffer_addr;
3012fd83355SJason Molenda   argument_values.PushValue(return_buffer_ptr_value);
3022fd83355SJason Molenda 
3032fd83355SJason Molenda   debug_value.GetScalar() = 0;
3042fd83355SJason Molenda   argument_values.PushValue(debug_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 = SetupGetQueuesFunction(thread, argument_values);
3162fd83355SJason Molenda 
317b9c1b51eSKate Stone   if (!m_get_queues_impl_code_up) {
318b9c1b51eSKate Stone     error.SetErrorString(
319b9c1b51eSKate Stone         "Unable to compile __introspection_dispatch_get_queues.");
320151c032cSJim Ingham     return return_value;
321151c032cSJim Ingham   }
322151c032cSJim Ingham 
323b9c1b51eSKate Stone   FunctionCaller *get_queues_caller =
324b9c1b51eSKate Stone       m_get_queues_impl_code_up->GetFunctionCaller();
325151c032cSJim Ingham 
326248a1305SKonrad Kleine   if (get_queues_caller == nullptr) {
327b9c1b51eSKate Stone     error.SetErrorString(
328b9c1b51eSKate Stone         "Unable to get caller for call __introspection_dispatch_get_queues");
329fa4286eaSJason Molenda     return return_value;
3302fd83355SJason Molenda   }
3312fd83355SJason Molenda 
332579e70c9SSean Callanan   DiagnosticManager diagnostics;
3332fd83355SJason Molenda   ExecutionContext exe_ctx;
3342fd83355SJason Molenda   EvaluateExpressionOptions options;
3352fd83355SJason Molenda   options.SetUnwindOnError(true);
3362fd83355SJason Molenda   options.SetIgnoreBreakpoints(true);
3372fd83355SJason Molenda   options.SetStopOthers(true);
3385b0a687dSJason Molenda #if __has_feature(address_sanitizer)
3394c03ea14SAdrian Prantl   options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
3405b0a687dSJason Molenda #else
3415b0a687dSJason Molenda   options.SetTimeout(std::chrono::milliseconds(500));
3425b0a687dSJason Molenda #endif
343a0560152SJason Molenda   options.SetTryAllThreads(false);
344c01783a8SRaphael Isemann   options.SetIsForUtilityExpr(true);
3452fd83355SJason Molenda   thread.CalculateExecutionContext(exe_ctx);
3462fd83355SJason Molenda 
3471624a2d3SJim Ingham   ExpressionResults func_call_ret;
3482fd83355SJason Molenda   Value results;
349b9c1b51eSKate Stone   func_call_ret = get_queues_caller->ExecuteFunction(
350b9c1b51eSKate Stone       exe_ctx, &args_addr, options, diagnostics, results);
351b9c1b51eSKate Stone   if (func_call_ret != eExpressionCompleted || !error.Success()) {
35263e5fb76SJonas Devlieghere     LLDB_LOGF(log,
35363e5fb76SJonas Devlieghere               "Unable to call introspection_get_dispatch_queues(), got "
354b9c1b51eSKate Stone               "ExpressionResults %d, error contains %s",
355b9c1b51eSKate Stone               func_call_ret, error.AsCString(""));
356b9c1b51eSKate Stone     error.SetErrorString("Unable to call introspection_get_dispatch_queues() "
357b9c1b51eSKate Stone                          "for list of queues");
3582fd83355SJason Molenda     return return_value;
3592fd83355SJason Molenda   }
3602fd83355SJason Molenda 
361b9c1b51eSKate Stone   return_value.queues_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
362b9c1b51eSKate Stone       m_get_queues_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
363b9c1b51eSKate Stone   if (!error.Success() ||
364b9c1b51eSKate Stone       return_value.queues_buffer_ptr == LLDB_INVALID_ADDRESS) {
3652fd83355SJason Molenda     return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
3662fd83355SJason Molenda     return return_value;
3672fd83355SJason Molenda   }
3682fd83355SJason Molenda 
369b9c1b51eSKate Stone   return_value.queues_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
370b9c1b51eSKate Stone       m_get_queues_return_buffer_addr + 8, 8, 0, error);
3712fd83355SJason Molenda 
372b9c1b51eSKate Stone   if (!error.Success()) {
3732fd83355SJason Molenda     return_value.queues_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_queues_return_buffer_addr + 16, 8, 0, error);
379b9c1b51eSKate Stone   if (!error.Success()) {
3802fd83355SJason Molenda     return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
3812fd83355SJason Molenda     return return_value;
3822fd83355SJason Molenda   }
3832fd83355SJason Molenda 
38463e5fb76SJonas Devlieghere   LLDB_LOGF(log,
38563e5fb76SJonas Devlieghere             "AppleGetQueuesHandler called "
386b9c1b51eSKate Stone             "__introspection_dispatch_get_queues (page_to_free == "
38763e5fb76SJonas Devlieghere             "0x%" PRIx64 ", size = %" PRId64 "), returned page is at 0x%" PRIx64
38863e5fb76SJonas Devlieghere             ", size %" PRId64 ", count = %" PRId64,
389b9c1b51eSKate Stone             page_to_free, page_to_free_size, return_value.queues_buffer_ptr,
390b9c1b51eSKate Stone             return_value.queues_buffer_size, return_value.count);
3912a6c252dSJason Molenda 
3922fd83355SJason Molenda   return return_value;
3932fd83355SJason Molenda }
394