1 //===-- AppleGetItemInfoHandler.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_AppleGetItemInfoHandler_h_
11 #define lldb_AppleGetItemInfoHandler_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/Expression/UtilityFunction.h"
23 #include "lldb/Symbol/CompilerType.h"
24 #include "lldb/lldb-public.h"
25 
26 // This class will insert a UtilityFunction into the inferior process for
27 // calling libBacktraceRecording's
28 // __introspection_dispatch_queue_item_get_info()
29 // function.  The function in the inferior will return a struct by value
30 // with these members:
31 //
32 //     struct get_item_info_return_values
33 //     {
34 //         introspection_dispatch_item_info_ref *item_buffer;
35 //         uint64_t item_buffer_size;
36 //     };
37 //
38 // The item_buffer pointer is an address in the inferior program's address
39 // space (item_buffer_size in size) which must be mach_vm_deallocate'd by
40 // lldb.
41 //
42 // The AppleGetItemInfoHandler object should persist so that the UtilityFunction
43 // can be reused multiple times.
44 
45 namespace lldb_private {
46 
47 class AppleGetItemInfoHandler {
48 public:
49   AppleGetItemInfoHandler(lldb_private::Process *process);
50 
51   ~AppleGetItemInfoHandler();
52 
53   struct GetItemInfoReturnInfo {
54     lldb::addr_t item_buffer_ptr;  /* the address of the item buffer from
55                                       libBacktraceRecording */
56     lldb::addr_t item_buffer_size; /* the size of the item buffer from
57                                       libBacktraceRecording */
58 
59     GetItemInfoReturnInfo()
60         : item_buffer_ptr(LLDB_INVALID_ADDRESS), item_buffer_size(0) {}
61   };
62 
63   //----------------------------------------------------------
64   /// Get the information about a work item by calling
65   /// __introspection_dispatch_queue_item_get_info.  If there's a page of
66   /// memory that needs to be freed, pass in the address and size and it will
67   /// be freed before getting the list of queues.
68   ///
69   /// @param [in] thread
70   ///     The thread to run this plan on.
71   ///
72   /// @param [in] item
73   ///     The introspection_dispatch_item_info_ref value for the item of
74   ///     interest.
75   ///
76   /// @param [in] page_to_free
77   ///     An address of an inferior process vm page that needs to be
78   ///     deallocated,
79   ///     LLDB_INVALID_ADDRESS if this is not needed.
80   ///
81   /// @param [in] page_to_free_size
82   ///     The size of the vm page that needs to be deallocated if an address was
83   ///     passed in to page_to_free.
84   ///
85   /// @param [out] error
86   ///     This object will be updated with the error status / error string from
87   ///     any failures encountered.
88   ///
89   /// @returns
90   ///     The result of the inferior function call execution.  If there was a
91   ///     failure of any kind while getting
92   ///     the information, the item_buffer_ptr value will be
93   ///     LLDB_INVALID_ADDRESS.
94   //----------------------------------------------------------
95   GetItemInfoReturnInfo GetItemInfo(Thread &thread, lldb::addr_t item,
96                                     lldb::addr_t page_to_free,
97                                     uint64_t page_to_free_size,
98                                     lldb_private::Error &error);
99 
100   void Detach();
101 
102 private:
103   lldb::addr_t SetupGetItemInfoFunction(Thread &thread,
104                                         ValueList &get_item_info_arglist);
105 
106   static const char *g_get_item_info_function_name;
107   static const char *g_get_item_info_function_code;
108 
109   lldb_private::Process *m_process;
110   std::unique_ptr<UtilityFunction> m_get_item_info_impl_code;
111   std::mutex m_get_item_info_function_mutex;
112 
113   lldb::addr_t m_get_item_info_return_buffer_addr;
114   std::mutex m_get_item_info_retbuffer_mutex;
115 };
116 
117 } // using namespace lldb_private
118 
119 #endif // lldb_AppleGetItemInfoHandler_h_
120