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