1 //===-- AppleGetPendingItemsHandler.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_AppleGetPendingItemsHandler_h_
11 #define lldb_AppleGetPendingItemsHandler_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <vector>
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/lldb-public.h"
20 #include "lldb/Core/Error.h"
21 #include "lldb/Expression/ClangFunction.h"
22 #include "lldb/Host/Mutex.h"
23 #include "lldb/Symbol/ClangASTType.h"
24 
25 // This class will insert a ClangUtilityFunction into the inferior process for
26 // calling libBacktraceRecording's __introspection_dispatch_queue_get_pending_items()
27 // function.  The function in the inferior will return a struct by value
28 // with these members:
29 //
30 //     struct get_pending_items_return_values
31 //     {
32 //         introspection_dispatch_item_info_ref *items_buffer;
33 //         uint64_t items_buffer_size;
34 //         uint64_t count;
35 //     };
36 //
37 // The items_buffer pointer is an address in the inferior program's address
38 // space (items_buffer_size in size) which must be mach_vm_deallocate'd by
39 // lldb.  count is the number of items that were stored in the buffer.
40 //
41 // The AppleGetPendingItemsHandler object should persist so that the ClangUtilityFunction
42 // can be reused multiple times.
43 
44 namespace lldb_private
45 {
46 
47 class AppleGetPendingItemsHandler {
48 public:
49 
50     AppleGetPendingItemsHandler (lldb_private::Process *process);
51 
52     ~AppleGetPendingItemsHandler();
53 
54     struct GetPendingItemsReturnInfo
55     {
56         lldb::addr_t    items_buffer_ptr;  /* the address of the pending items buffer from libBacktraceRecording */
57         lldb::addr_t    items_buffer_size; /* the size of the pending items buffer from libBacktraceRecording */
58         uint64_t        count;              /* the number of pending items included in the buffer */
59 
60         GetPendingItemsReturnInfo () :
61             items_buffer_ptr(LLDB_INVALID_ADDRESS),
62             items_buffer_size(0),
63             count(0)
64         {}
65     };
66 
67     //----------------------------------------------------------
68     /// Get the list of pending items for a given queue via a call to
69     /// __introspection_dispatch_queue_get_pending_items.  If there's a page of
70     /// memory that needs to be freed, pass in the address and size and it will
71     /// be freed before getting the list of queues.
72     ///
73     /// @param [in] thread
74     ///     The thread to run this plan on.
75     ///
76     /// @param [in] queue
77     ///     The dispatch_queue_t value for the queue of interest.
78     ///
79     /// @param [in] page_to_free
80     ///     An address of an inferior process vm page that needs to be deallocated,
81     ///     LLDB_INVALID_ADDRESS if this is not needed.
82     ///
83     /// @param [in] page_to_free_size
84     ///     The size of the vm page that needs to be deallocated if an address was
85     ///     passed in to page_to_free.
86     ///
87     /// @param [out] error
88     ///     This object will be updated with the error status / error string from any failures encountered.
89     ///
90     /// @returns
91     ///     The result of the inferior function call execution.  If there was a failure of any kind while getting
92     ///     the information, the items_buffer_ptr value will be LLDB_INVALID_ADDRESS.
93     //----------------------------------------------------------
94     GetPendingItemsReturnInfo
95     GetPendingItems (Thread &thread, lldb::addr_t queue, lldb::addr_t page_to_free, uint64_t page_to_free_size, lldb_private::Error &error);
96 
97 
98     void
99     Detach ();
100 
101 private:
102 
103     lldb::addr_t
104     SetupGetPendingItemsFunction (Thread &thread, ValueList &get_pending_items_arglist);
105 
106     static const char *g_get_pending_items_function_name;
107     static const char *g_get_pending_items_function_code;
108 
109     lldb_private::Process *m_process;
110     std::unique_ptr<ClangFunction> m_get_pending_items_function;
111     std::unique_ptr<ClangUtilityFunction> m_get_pending_items_impl_code;
112     Mutex m_get_pending_items_function_mutex;
113 
114     lldb::addr_t m_get_pending_items_return_buffer_addr;
115     Mutex m_get_pending_items_retbuffer_mutex;
116 
117 };
118 
119 }  // using namespace lldb_private
120 
121 #endif	// lldb_AppleGetPendingItemsHandler_h_
122