1 //===-- AppleGetQueuesHandler.h ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_SYSTEMRUNTIME_MACOSX_APPLEGETQUEUESHANDLER_H
10 #define LLDB_SOURCE_PLUGINS_SYSTEMRUNTIME_MACOSX_APPLEGETQUEUESHANDLER_H
11 
12 #include <map>
13 #include <mutex>
14 #include <vector>
15 
16 #include "lldb/Symbol/CompilerType.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/lldb-public.h"
19 
20 // This class will insert a UtilityFunction into the inferior process for
21 // calling libBacktraceRecording's introspection_get_dispatch_queues()
22 // function.  The function in the inferior will return a struct by value
23 // with these members:
24 //
25 //     struct get_current_queues_return_values
26 //     {
27 //         introspection_dispatch_queue_info_t *queues_buffer;
28 //         uint64_t queues_buffer_size;
29 //         uint64_t count;
30 //     };
31 //
32 // The queues_buffer pointer is an address in the inferior program's address
33 // space (queues_buffer_size in size) which must be mach_vm_deallocate'd by
34 // lldb.  count is the number of queues that were stored in the buffer.
35 //
36 // The AppleGetQueuesHandler object should persist so that the UtilityFunction
37 // can be reused multiple times.
38 
39 namespace lldb_private {
40 
41 class AppleGetQueuesHandler {
42 public:
43   AppleGetQueuesHandler(lldb_private::Process *process);
44 
45   ~AppleGetQueuesHandler();
46 
47   struct GetQueuesReturnInfo {
48     lldb::addr_t queues_buffer_ptr =
49         LLDB_INVALID_ADDRESS; /* the address of the queues buffer from
50           libBacktraceRecording */
51     lldb::addr_t queues_buffer_size = 0; /* the size of the queues buffer from
52                                         libBacktraceRecording */
53     uint64_t count = 0; /* the number of queues included in the queues buffer */
54 
55     GetQueuesReturnInfo() = default;
56   };
57 
58   /// Get the list of queues that exist (with any active or pending items) via
59   /// a call to introspection_get_dispatch_queues().  If there's a page of
60   /// memory that needs to be freed, pass in the address and size and it will
61   /// be freed before getting the list of queues.
62   ///
63   /// \param [in] thread
64   ///     The thread to run this plan on.
65   ///
66   /// \param [in] page_to_free
67   ///     An address of an inferior process vm page that needs to be
68   ///     deallocated,
69   ///     LLDB_INVALID_ADDRESS if this is not needed.
70   ///
71   /// \param [in] page_to_free_size
72   ///     The size of the vm page that needs to be deallocated if an address was
73   ///     passed in to page_to_free.
74   ///
75   /// \param [out] error
76   ///     This object will be updated with the error status / error string from
77   ///     any failures encountered.
78   ///
79   /// \returns
80   ///     The result of the inferior function call execution.  If there was a
81   ///     failure of any kind while getting
82   ///     the information, the queues_buffer_ptr value will be
83   ///     LLDB_INVALID_ADDRESS.
84   GetQueuesReturnInfo GetCurrentQueues(Thread &thread,
85                                        lldb::addr_t page_to_free,
86                                        uint64_t page_to_free_size,
87                                        lldb_private::Status &error);
88 
89   void Detach();
90 
91 private:
92   lldb::addr_t SetupGetQueuesFunction(Thread &thread,
93                                       ValueList &get_queues_arglist);
94 
95   static const char *g_get_current_queues_function_name;
96   static const char *g_get_current_queues_function_code;
97 
98   lldb_private::Process *m_process;
99   std::unique_ptr<UtilityFunction> m_get_queues_impl_code_up;
100   std::mutex m_get_queues_function_mutex;
101 
102   lldb::addr_t m_get_queues_return_buffer_addr;
103   std::mutex m_get_queues_retbuffer_mutex;
104 };
105 
106 } // using namespace lldb_private
107 
108 #endif // LLDB_SOURCE_PLUGINS_SYSTEMRUNTIME_MACOSX_APPLEGETQUEUESHANDLER_H
109