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 #include <map>
15 #include <mutex>
16 #include <vector>
17 
18 #include "lldb/Symbol/CompilerType.h"
19 #include "lldb/Utility/Status.h"
20 #include "lldb/lldb-public.h"
21 
22 // This class will insert a UtilityFunction into the inferior process for
23 // calling libBacktraceRecording's
24 // __introspection_dispatch_thread_get_item_info()
25 // function.  The function in the inferior will return a struct by value
26 // with these members:
27 //
28 //     struct get_thread_item_info_return_values
29 //     {
30 //         introspection_dispatch_item_info_ref *item_buffer;
31 //         uint64_t item_buffer_size;
32 //     };
33 //
34 // The item_buffer pointer is an address in the inferior program's address
35 // space (item_buffer_size in size) which must be mach_vm_deallocate'd by
36 // lldb.
37 //
38 // The AppleGetThreadItemInfoHandler object should persist so that the
39 // UtilityFunction
40 // can be reused multiple times.
41 
42 namespace lldb_private {
43 
44 class AppleGetThreadItemInfoHandler {
45 public:
46   AppleGetThreadItemInfoHandler(lldb_private::Process *process);
47 
48   ~AppleGetThreadItemInfoHandler();
49 
50   struct GetThreadItemInfoReturnInfo {
51     lldb::addr_t item_buffer_ptr;  /* the address of the item buffer from
52                                       libBacktraceRecording */
53     lldb::addr_t item_buffer_size; /* the size of the item buffer from
54                                       libBacktraceRecording */
55 
56     GetThreadItemInfoReturnInfo()
57         : item_buffer_ptr(LLDB_INVALID_ADDRESS), item_buffer_size(0) {}
58   };
59 
60   //----------------------------------------------------------
61   /// Get the information about a work item by calling
62   /// __introspection_dispatch_thread_get_item_info.  If there's a page of
63   /// memory that needs to be freed, pass in the address and size and it will
64   /// be freed before getting the list of queues.
65   ///
66   /// @param [in] thread_id
67   ///     The thread to get the extended backtrace for.
68   ///
69   /// @param [in] page_to_free
70   ///     An address of an inferior process vm page that needs to be
71   ///     deallocated,
72   ///     LLDB_INVALID_ADDRESS if this is not needed.
73   ///
74   /// @param [in] page_to_free_size
75   ///     The size of the vm page that needs to be deallocated if an address was
76   ///     passed in to page_to_free.
77   ///
78   /// @param [out] error
79   ///     This object will be updated with the error status / error string from
80   ///     any failures encountered.
81   ///
82   /// @returns
83   ///     The result of the inferior function call execution.  If there was a
84   ///     failure of any kind while getting
85   ///     the information, the item_buffer_ptr value will be
86   ///     LLDB_INVALID_ADDRESS.
87   //----------------------------------------------------------
88   GetThreadItemInfoReturnInfo GetThreadItemInfo(Thread &thread,
89                                                 lldb::tid_t thread_id,
90                                                 lldb::addr_t page_to_free,
91                                                 uint64_t page_to_free_size,
92                                                 lldb_private::Status &error);
93 
94   void Detach();
95 
96 private:
97   lldb::addr_t
98   SetupGetThreadItemInfoFunction(Thread &thread,
99                                  ValueList &get_thread_item_info_arglist);
100 
101   static const char *g_get_thread_item_info_function_name;
102   static const char *g_get_thread_item_info_function_code;
103 
104   lldb_private::Process *m_process;
105   std::unique_ptr<UtilityFunction> m_get_thread_item_info_impl_code;
106   std::mutex m_get_thread_item_info_function_mutex;
107 
108   lldb::addr_t m_get_thread_item_info_return_buffer_addr;
109   std::mutex m_get_thread_item_info_retbuffer_mutex;
110 };
111 
112 } // using namespace lldb_private
113 
114 #endif // lldb_AppleGetThreadItemInfoHandler_h_
115