1 //===-- AppleGetThreadItemInfoHandler.h ----------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef lldb_AppleGetThreadItemInfoHandler_h_
12 #define lldb_AppleGetThreadItemInfoHandler_h_
13 
14 // C Includes
15 // C++ Includes
16 #include <map>
17 #include <mutex>
18 #include <vector>
19 
20 // Other libraries and framework includes
21 // Project includes
22 #include "lldb/Core/Error.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_thread_get_item_info()
29 // function.  The function in the inferior will return a struct by value
30 // with these members:
31 //
32 //     struct get_thread_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 AppleGetThreadItemInfoHandler object should persist so that the
43 // UtilityFunction
44 // can be reused multiple times.
45 
46 namespace lldb_private {
47 
48 class AppleGetThreadItemInfoHandler {
49 public:
50   AppleGetThreadItemInfoHandler(lldb_private::Process *process);
51 
52   ~AppleGetThreadItemInfoHandler();
53 
54   struct GetThreadItemInfoReturnInfo {
55     lldb::addr_t item_buffer_ptr;  /* the address of the item buffer from
56                                       libBacktraceRecording */
57     lldb::addr_t item_buffer_size; /* the size of the item buffer from
58                                       libBacktraceRecording */
59 
60     GetThreadItemInfoReturnInfo()
61         : item_buffer_ptr(LLDB_INVALID_ADDRESS), item_buffer_size(0) {}
62   };
63 
64   //----------------------------------------------------------
65   /// Get the information about a work item by calling
66   /// __introspection_dispatch_thread_get_item_info.  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_id
71   ///     The thread to get the extended backtrace for.
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 item_buffer_ptr value will be
90   ///     LLDB_INVALID_ADDRESS.
91   //----------------------------------------------------------
92   GetThreadItemInfoReturnInfo GetThreadItemInfo(Thread &thread,
93                                                 lldb::tid_t thread_id,
94                                                 lldb::addr_t page_to_free,
95                                                 uint64_t page_to_free_size,
96                                                 lldb_private::Error &error);
97 
98   void Detach();
99 
100 private:
101   lldb::addr_t
102   SetupGetThreadItemInfoFunction(Thread &thread,
103                                  ValueList &get_thread_item_info_arglist);
104 
105   static const char *g_get_thread_item_info_function_name;
106   static const char *g_get_thread_item_info_function_code;
107 
108   lldb_private::Process *m_process;
109   std::unique_ptr<UtilityFunction> m_get_thread_item_info_impl_code;
110   std::mutex m_get_thread_item_info_function_mutex;
111 
112   lldb::addr_t m_get_thread_item_info_return_buffer_addr;
113   std::mutex m_get_thread_item_info_retbuffer_mutex;
114 };
115 
116 } // using namespace lldb_private
117 
118 #endif // lldb_AppleGetThreadItemInfoHandler_h_
119