1 //===-- SystemRuntimeMacOSX.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 liblldb_SystemRuntimeMacOSX_h_
11 #define liblldb_SystemRuntimeMacOSX_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <vector>
17 #include <string>
18 
19 // Other libraries and framework includes
20 #include "llvm/Support/MachO.h"
21 
22 #include "lldb/Target/SystemRuntime.h"
23 #include "lldb/Host/FileSpec.h"
24 #include "lldb/Core/ConstString.h"
25 #include "lldb/Core/ModuleList.h"
26 #include "lldb/Core/UUID.h"
27 #include "lldb/Host/Mutex.h"
28 #include "lldb/Target/Process.h"
29 
30 class SystemRuntimeMacOSX : public lldb_private::SystemRuntime
31 {
32 public:
33     //------------------------------------------------------------------
34     // Static Functions
35     //------------------------------------------------------------------
36     static void
37     Initialize();
38 
39     static void
40     Terminate();
41 
42     static lldb_private::ConstString
43     GetPluginNameStatic();
44 
45     static const char *
46     GetPluginDescriptionStatic();
47 
48     static lldb_private::SystemRuntime *
49     CreateInstance (lldb_private::Process *process);
50 
51     SystemRuntimeMacOSX (lldb_private::Process *process);
52 
53     virtual
54     ~SystemRuntimeMacOSX ();
55 
56     void
57     Clear (bool clear_process);
58 
59     void
60     DidAttach ();
61 
62     void
63     DidLaunch();
64 
65     void
66     ModulesDidLoad (lldb_private::ModuleList &module_list);
67 
68     const std::vector<lldb_private::ConstString> &
69     GetExtendedBacktraceTypes ();
70 
71     lldb::ThreadSP
72     GetExtendedBacktraceThread (lldb::ThreadSP thread, lldb_private::ConstString type);
73 
74     // REMOVE THE FOLLOWING 4
75     bool
76     SetItemEnqueuedBreakpoint ();
77 
78     bool
79     DidSetItemEnqueuedBreakpoint () const;
80 
81     static bool
82     ItemEnqueuedCallback (void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
83 
84     bool
85     ItemEnqueuedBreakpointHit (lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
86 
87     //------------------------------------------------------------------
88     // PluginInterface protocol
89     //------------------------------------------------------------------
90     virtual lldb_private::ConstString
91     GetPluginName();
92 
93     virtual uint32_t
94     GetPluginVersion();
95 
96 private:
97     struct ArchivedBacktrace {
98         uint32_t stop_id;
99         bool stop_id_is_valid;
100         lldb::queue_id_t libdispatch_queue_id;   // LLDB_INVALID_QUEUE_ID if unavailable
101         std::vector<lldb::addr_t> pcs;
102     };
103 
104     SystemRuntimeMacOSX::ArchivedBacktrace
105     GetLibdispatchExtendedBacktrace (lldb::ThreadSP thread);
106 
107     void
108     PopulateQueueList (lldb_private::QueueList &queue_list);
109 
110 protected:
111     lldb::user_id_t m_break_id;
112     mutable lldb_private::Mutex m_mutex;
113 
114 private:
115 
116     void
117     ParseLdiHeaders ();
118 
119     bool
120     LdiHeadersInitialized ();
121 
122     lldb::addr_t
123     GetQueuesHead ();
124 
125     lldb::addr_t
126     GetItemsHead ();
127 
128     lldb::addr_t
129     GetThreadCreatorItem (lldb::ThreadSP thread);
130 
131     lldb::tid_t
132     GetNewThreadUniqueThreadID (lldb::ThreadSP original_thread_sp);
133 
134     void
135     SetNewThreadThreadName (lldb::ThreadSP original_thread_sp, lldb::ThreadSP new_extended_thread_sp);
136 
137     void
138     SetNewThreadQueueName (lldb::ThreadSP original_thread_sp, lldb::ThreadSP new_extended_thread_sp);
139 
140     void
141     SetNewThreadExtendedBacktraceToken (lldb::ThreadSP original_thread_sp, lldb::ThreadSP new_extended_thread_sp);
142 
143     void
144     SetNewThreadQueueID (lldb::ThreadSP original_thread_sp, lldb::ThreadSP new_extended_thread_sp);
145 
146     struct ldi_queue_offsets {
147         uint16_t next;
148         uint16_t prev;
149         uint16_t queue_id;
150         uint16_t current_item_ptr;
151     };
152 
153     struct ldi_item_offsets {
154         uint16_t next;
155         uint16_t prev;
156         uint16_t type;
157         uint16_t identifier;
158         uint16_t stop_id;
159         uint16_t backtrace_length;
160         uint16_t backtrace_ptr;
161         uint16_t thread_name_ptr;
162         uint16_t queue_name_ptr;
163         uint16_t unique_thread_id;
164         uint16_t pthread_id;
165         uint16_t enqueueing_thread_dispatch_queue_t;
166         uint16_t enqueueing_thread_dispatch_block_ptr;
167         uint16_t queue_id_from_thread_info;
168     };
169 
170     struct ldi_header {
171         uint16_t                    version;
172         uint16_t                    ldi_header_size;
173         uint16_t                    initialized;        // 0 means uninitialized
174         uint16_t                    queue_size;
175         uint16_t                    item_size;
176         uint64_t                    queues_head_ptr_address;  // Address of queues head structure
177         uint64_t                    items_head_ptr_address;   // Address of items_head
178         struct ldi_queue_offsets    queue_offsets;
179         struct ldi_item_offsets     item_offsets;
180     };
181 
182     struct ldi_header   m_ldi_header;
183 
184     DISALLOW_COPY_AND_ASSIGN (SystemRuntimeMacOSX);
185 };
186 
187 #endif  // liblldb_SystemRuntimeMacOSX_h_
188