1 //===-- AppleGetThreadItemInfoHandler.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_AppleGetThreadItemInfoHandler_h_
11 #define lldb_AppleGetThreadItemInfoHandler_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_thread_get_item_info()
27 // function.  The function in the inferior will return a struct by value
28 // with these members:
29 //
30 //     struct get_thread_item_info_return_values
31 //     {
32 //         introspection_dispatch_item_info_ref *item_buffer;
33 //         uint64_t item_buffer_size;
34 //     };
35 //
36 // The item_buffer pointer is an address in the inferior program's address
37 // space (item_buffer_size in size) which must be mach_vm_deallocate'd by
38 // lldb.
39 //
40 // The AppleGetThreadItemInfoHandler object should persist so that the ClangUtilityFunction
41 // can be reused multiple times.
42 
43 namespace lldb_private
44 {
45 
46 class AppleGetThreadItemInfoHandler {
47 public:
48 
49     AppleGetThreadItemInfoHandler (lldb_private::Process *process);
50 
51     ~AppleGetThreadItemInfoHandler();
52 
53     struct GetThreadItemInfoReturnInfo
54     {
55         lldb::addr_t    item_buffer_ptr;  /* the address of the item buffer from libBacktraceRecording */
56         lldb::addr_t    item_buffer_size; /* the size of the item buffer from libBacktraceRecording */
57 
58         GetThreadItemInfoReturnInfo() :
59             item_buffer_ptr(LLDB_INVALID_ADDRESS),
60             item_buffer_size(0)
61         {}
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 deallocated,
75     ///     LLDB_INVALID_ADDRESS if this is not needed.
76     ///
77     /// @param [in] page_to_free_size
78     ///     The size of the vm page that needs to be deallocated if an address was
79     ///     passed in to page_to_free.
80     ///
81     /// @param [out] error
82     ///     This object will be updated with the error status / error string from any failures encountered.
83     ///
84     /// @returns
85     ///     The result of the inferior function call execution.  If there was a failure of any kind while getting
86     ///     the information, the item_buffer_ptr value will be LLDB_INVALID_ADDRESS.
87     //----------------------------------------------------------
88     GetThreadItemInfoReturnInfo
89     GetThreadItemInfo (Thread &thread, lldb::tid_t thread_id, lldb::addr_t page_to_free, uint64_t page_to_free_size, lldb_private::Error &error);
90 
91 
92     void
93     Detach ();
94 
95 private:
96 
97     lldb::addr_t
98     SetupGetThreadItemInfoFunction (Thread &thread, ValueList &get_thread_item_info_arglist);
99 
100     static const char *g_get_thread_item_info_function_name;
101     static const char *g_get_thread_item_info_function_code;
102 
103     lldb_private::Process *m_process;
104     std::unique_ptr<ClangFunction> m_get_thread_item_info_function;
105     std::unique_ptr<ClangUtilityFunction> m_get_thread_item_info_impl_code;
106     Mutex m_get_thread_item_info_function_mutex;
107 
108     lldb::addr_t m_get_thread_item_info_return_buffer_addr;
109     Mutex m_get_thread_item_info_retbuffer_mutex;
110 
111 };
112 
113 }  // using namespace lldb_private
114 
115 #endif	// lldb_AppleGetThreadItemInfoHandler_h_
116