1 //===-- GDBRemoteCommunicationServer.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_GDBRemoteCommunicationServer_h_
11 #define liblldb_GDBRemoteCommunicationServer_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <vector>
16 #include <set>
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Host/Mutex.h"
20 #include "lldb/Target/Process.h"
21 #include "GDBRemoteCommunication.h"
22 
23 class ProcessGDBRemote;
24 class StringExtractorGDBRemote;
25 
26 class GDBRemoteCommunicationServer : public GDBRemoteCommunication
27 {
28 public:
29     enum
30     {
31         eBroadcastBitRunPacketSent = kLoUserBroadcastBit
32     };
33     //------------------------------------------------------------------
34     // Constructors and Destructors
35     //------------------------------------------------------------------
36     GDBRemoteCommunicationServer(bool is_platform);
37 
38     virtual
39     ~GDBRemoteCommunicationServer();
40 
41     bool
42     GetPacketAndSendResponse (uint32_t timeout_usec,
43                               lldb_private::Error &error,
44                               bool &interrupt,
45                               bool &quit);
46 
47     virtual bool
48     GetThreadSuffixSupported ()
49     {
50         return true;
51     }
52 
53     // After connecting, do a little handshake with the client to make sure
54     // we are at least communicating
55     bool
56     HandshakeWithClient (lldb_private::Error *error_ptr);
57 
58     // Set both ports to zero to let the platform automatically bind to
59     // a port chosen by the OS.
60     void
61     SetPortRange (uint16_t lo_port_num, uint16_t hi_port_num)
62     {
63         m_lo_port_num = lo_port_num;
64         m_hi_port_num = hi_port_num;
65         m_next_port = m_lo_port_num;
66         m_use_port_range = true;
67     }
68 
69     // If we are using a port range, get and update the next port to be used variable.
70     // Otherwise, just return 0.
71     uint16_t
72     GetAndUpdateNextPort ()
73     {
74         if (!m_use_port_range)
75             return 0;
76         uint16_t val = m_next_port;
77         if (++m_next_port > m_hi_port_num)
78             m_next_port = m_lo_port_num;
79         return val;
80     }
81 
82 protected:
83     //typedef std::map<uint16_t, lldb::pid_t> PortToPIDMap;
84 
85     lldb::thread_t m_async_thread;
86     lldb_private::ProcessLaunchInfo m_process_launch_info;
87     lldb_private::Error m_process_launch_error;
88     std::set<lldb::pid_t> m_spawned_pids;
89     lldb_private::Mutex m_spawned_pids_mutex;
90     lldb_private::ProcessInstanceInfoList m_proc_infos;
91     uint32_t m_proc_infos_index;
92     uint16_t m_lo_port_num;
93     uint16_t m_hi_port_num;
94     //PortToPIDMap m_port_to_pid_map;
95     uint16_t m_next_port;
96     bool m_use_port_range;
97 
98 
99     size_t
100     SendUnimplementedResponse (const char *packet);
101 
102     size_t
103     SendErrorResponse (uint8_t error);
104 
105     size_t
106     SendOKResponse ();
107 
108     bool
109     Handle_A (StringExtractorGDBRemote &packet);
110 
111     bool
112     Handle_qLaunchSuccess (StringExtractorGDBRemote &packet);
113 
114     bool
115     Handle_qHostInfo (StringExtractorGDBRemote &packet);
116 
117     bool
118     Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet);
119 
120     bool
121     Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet);
122 
123     bool
124     Handle_qPlatform_IO_MkDir (StringExtractorGDBRemote &packet);
125 
126     bool
127     Handle_qProcessInfoPID (StringExtractorGDBRemote &packet);
128 
129     bool
130     Handle_qfProcessInfo (StringExtractorGDBRemote &packet);
131 
132     bool
133     Handle_qsProcessInfo (StringExtractorGDBRemote &packet);
134 
135     bool
136     Handle_qC (StringExtractorGDBRemote &packet);
137 
138     bool
139     Handle_qUserName (StringExtractorGDBRemote &packet);
140 
141     bool
142     Handle_qGroupName (StringExtractorGDBRemote &packet);
143 
144     bool
145     Handle_qSpeedTest (StringExtractorGDBRemote &packet);
146 
147     bool
148     Handle_QEnvironment  (StringExtractorGDBRemote &packet);
149 
150     bool
151     Handle_QLaunchArch (StringExtractorGDBRemote &packet);
152 
153     bool
154     Handle_QSetDisableASLR (StringExtractorGDBRemote &packet);
155 
156     bool
157     Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
158 
159     bool
160     Handle_QStartNoAckMode (StringExtractorGDBRemote &packet);
161 
162     bool
163     Handle_QSetSTDIN (StringExtractorGDBRemote &packet);
164 
165     bool
166     Handle_QSetSTDOUT (StringExtractorGDBRemote &packet);
167 
168     bool
169     Handle_QSetSTDERR (StringExtractorGDBRemote &packet);
170 
171     bool
172     Handle_vFile_Open (StringExtractorGDBRemote &packet);
173 
174     bool
175     Handle_vFile_Close (StringExtractorGDBRemote &packet);
176 
177     bool
178     Handle_vFile_pRead (StringExtractorGDBRemote &packet);
179 
180     bool
181     Handle_vFile_pWrite (StringExtractorGDBRemote &packet);
182 
183     bool
184     Handle_vFile_Size (StringExtractorGDBRemote &packet);
185 
186     bool
187     Handle_vFile_Mode (StringExtractorGDBRemote &packet);
188 
189     bool
190     Handle_vFile_Exists (StringExtractorGDBRemote &packet);
191 
192     bool
193     Handle_vFile_Stat (StringExtractorGDBRemote &packet);
194 
195     bool
196     Handle_vFile_MD5 (StringExtractorGDBRemote &packet);
197 
198     bool
199     Handle_qPlatform_RunCommand (StringExtractorGDBRemote &packet);
200 
201 private:
202     bool
203     DebugserverProcessReaped (lldb::pid_t pid);
204 
205     static bool
206     ReapDebugserverProcess (void *callback_baton,
207                             lldb::pid_t pid,
208                             bool exited,
209                             int signal,
210                             int status);
211 
212     //------------------------------------------------------------------
213     // For GDBRemoteCommunicationServer only
214     //------------------------------------------------------------------
215     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer);
216 };
217 
218 #endif  // liblldb_GDBRemoteCommunicationServer_h_
219