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 #include <unordered_map>
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-private-forward.h"
21 #include "lldb/Core/Communication.h"
22 #include "lldb/Host/Mutex.h"
23 #include "lldb/Target/Process.h"
24 #include "GDBRemoteCommunication.h"
25 
26 #include "../../../Host/common/NativeProcessProtocol.h"
27 
28 class ProcessGDBRemote;
29 class StringExtractorGDBRemote;
30 
31 class GDBRemoteCommunicationServer :
32     public GDBRemoteCommunication,
33     public lldb_private::NativeProcessProtocol::NativeDelegate
34 {
35 public:
36     typedef std::map<uint16_t, lldb::pid_t> PortMap;
37 
38     enum
39     {
40         eBroadcastBitRunPacketSent = kLoUserBroadcastBit
41     };
42     //------------------------------------------------------------------
43     // Constructors and Destructors
44     //------------------------------------------------------------------
45     GDBRemoteCommunicationServer(bool is_platform);
46 
47     GDBRemoteCommunicationServer(bool is_platform,
48                                  const lldb::PlatformSP& platform_sp,
49                                  lldb::DebuggerSP& debugger_sp);
50 
51     virtual
52     ~GDBRemoteCommunicationServer();
53 
54     PacketResult
55     GetPacketAndSendResponse (uint32_t timeout_usec,
56                               lldb_private::Error &error,
57                               bool &interrupt,
58                               bool &quit);
59 
60     virtual bool
61     GetThreadSuffixSupported ()
62     {
63         return true;
64     }
65 
66     // After connecting, do a little handshake with the client to make sure
67     // we are at least communicating
68     bool
69     HandshakeWithClient (lldb_private::Error *error_ptr);
70 
71     // Set both ports to zero to let the platform automatically bind to
72     // a port chosen by the OS.
73     void
74     SetPortMap (PortMap &&port_map)
75     {
76         m_port_map = port_map;
77     }
78 
79     //----------------------------------------------------------------------
80     // If we are using a port map where we can only use certain ports,
81     // get the next available port.
82     //
83     // If we are using a port map and we are out of ports, return UINT16_MAX
84     //
85     // If we aren't using a port map, return 0 to indicate we should bind to
86     // port 0 and then figure out which port we used.
87     //----------------------------------------------------------------------
88     uint16_t
89     GetNextAvailablePort ()
90     {
91         if (m_port_map.empty())
92             return 0; // Bind to port zero and get a port, we didn't have any limitations
93 
94         for (auto &pair : m_port_map)
95         {
96             if (pair.second == LLDB_INVALID_PROCESS_ID)
97             {
98                 pair.second = ~(lldb::pid_t)LLDB_INVALID_PROCESS_ID;
99                 return pair.first;
100             }
101         }
102         return UINT16_MAX;
103     }
104 
105     bool
106     AssociatePortWithProcess (uint16_t port, lldb::pid_t pid)
107     {
108         PortMap::iterator pos = m_port_map.find(port);
109         if (pos != m_port_map.end())
110         {
111             pos->second = pid;
112             return true;
113         }
114         return false;
115     }
116 
117     bool
118     FreePort (uint16_t port)
119     {
120         PortMap::iterator pos = m_port_map.find(port);
121         if (pos != m_port_map.end())
122         {
123             pos->second = LLDB_INVALID_PROCESS_ID;
124             return true;
125         }
126         return false;
127     }
128 
129     bool
130     FreePortForProcess (lldb::pid_t pid)
131     {
132         if (!m_port_map.empty())
133         {
134             for (auto &pair : m_port_map)
135             {
136                 if (pair.second == pid)
137                 {
138                     pair.second = LLDB_INVALID_PROCESS_ID;
139                     return true;
140                 }
141             }
142         }
143         return false;
144     }
145 
146     void
147     SetPortOffset (uint16_t port_offset)
148     {
149         m_port_offset = port_offset;
150     }
151 
152     //------------------------------------------------------------------
153     /// Specify the program to launch and its arguments.
154     ///
155     /// The LaunchProcess () command can be executed to do the lauching.
156     ///
157     /// @param[in] args
158     ///     The command line to launch.
159     ///
160     /// @param[in] argc
161     ///     The number of elements in the args array of cstring pointers.
162     ///
163     /// @return
164     ///     An Error object indicating the success or failure of making
165     ///     the setting.
166     //------------------------------------------------------------------
167     lldb_private::Error
168     SetLaunchArguments (const char *const args[], int argc);
169 
170     //------------------------------------------------------------------
171     /// Specify the launch flags for the process.
172     ///
173     /// The LaunchProcess () command can be executed to do the lauching.
174     ///
175     /// @param[in] launch_flags
176     ///     The launch flags to use when launching this process.
177     ///
178     /// @return
179     ///     An Error object indicating the success or failure of making
180     ///     the setting.
181     //------------------------------------------------------------------
182     lldb_private::Error
183     SetLaunchFlags (unsigned int launch_flags);
184 
185     //------------------------------------------------------------------
186     /// Launch a process with the current launch settings.
187     ///
188     /// This method supports running an lldb-gdbserver or similar
189     /// server in a situation where the startup code has been provided
190     /// with all the information for a child process to be launched.
191     ///
192     /// @return
193     ///     An Error object indicating the success or failure of the
194     ///     launch.
195     //------------------------------------------------------------------
196     lldb_private::Error
197     LaunchProcess ();
198 
199     //------------------------------------------------------------------
200     /// Attach to a process.
201     ///
202     /// This method supports attaching llgs to a process accessible via the
203     /// configured Platform.
204     ///
205     /// @return
206     ///     An Error object indicating the success or failure of the
207     ///     attach operation.
208     //------------------------------------------------------------------
209     lldb_private::Error
210     AttachToProcess (lldb::pid_t pid);
211 
212     //------------------------------------------------------------------
213     // NativeProcessProtocol::NativeDelegate overrides
214     //------------------------------------------------------------------
215     void
216     InitializeDelegate (lldb_private::NativeProcessProtocol *process) override;
217 
218     void
219     ProcessStateChanged (lldb_private::NativeProcessProtocol *process, lldb::StateType state) override;
220 
221     void
222     DidExec (lldb_private::NativeProcessProtocol *process) override;
223 
224 protected:
225     lldb::PlatformSP m_platform_sp;
226     lldb::thread_t m_async_thread;
227     lldb_private::ProcessLaunchInfo m_process_launch_info;
228     lldb_private::Error m_process_launch_error;
229     std::set<lldb::pid_t> m_spawned_pids;
230     lldb_private::Mutex m_spawned_pids_mutex;
231     lldb_private::ProcessInstanceInfoList m_proc_infos;
232     uint32_t m_proc_infos_index;
233     PortMap m_port_map;
234     uint16_t m_port_offset;
235     lldb::tid_t m_current_tid;
236     lldb::tid_t m_continue_tid;
237     lldb_private::Mutex m_debugged_process_mutex;
238     lldb_private::NativeProcessProtocolSP m_debugged_process_sp;
239     lldb::DebuggerSP m_debugger_sp;
240     Communication m_stdio_communication;
241     bool m_exit_now; // use in asynchronous handling to indicate process should exit.
242     lldb::StateType m_inferior_prev_state;
243     bool m_thread_suffix_supported;
244     bool m_list_threads_in_stop_reply;
245     lldb::DataBufferSP m_active_auxv_buffer_sp;
246     lldb_private::Mutex m_saved_registers_mutex;
247     std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
248     uint32_t m_next_saved_registers_id;
249 
250     PacketResult
251     SendUnimplementedResponse (const char *packet);
252 
253     PacketResult
254     SendErrorResponse (uint8_t error);
255 
256     PacketResult
257     SendIllFormedResponse (const StringExtractorGDBRemote &packet, const char *error_message);
258 
259     PacketResult
260     SendOKResponse ();
261 
262     PacketResult
263     SendONotification (const char *buffer, uint32_t len);
264 
265     PacketResult
266     SendWResponse (lldb_private::NativeProcessProtocol *process);
267 
268     PacketResult
269     SendStopReplyPacketForThread (lldb::tid_t tid);
270 
271     PacketResult
272     SendStopReasonForState (lldb::StateType process_state, bool flush_on_exit);
273 
274     PacketResult
275     Handle_A (StringExtractorGDBRemote &packet);
276 
277     PacketResult
278     Handle_qLaunchSuccess (StringExtractorGDBRemote &packet);
279 
280     PacketResult
281     Handle_qHostInfo (StringExtractorGDBRemote &packet);
282 
283     PacketResult
284     Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet);
285 
286     PacketResult
287     Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet);
288 
289     PacketResult
290     Handle_k (StringExtractorGDBRemote &packet);
291 
292     PacketResult
293     Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet);
294 
295     PacketResult
296     Handle_qPlatform_chmod (StringExtractorGDBRemote &packet);
297 
298     PacketResult
299     Handle_qProcessInfo (StringExtractorGDBRemote &packet);
300 
301     PacketResult
302     Handle_qProcessInfoPID (StringExtractorGDBRemote &packet);
303 
304     PacketResult
305     Handle_qfProcessInfo (StringExtractorGDBRemote &packet);
306 
307     PacketResult
308     Handle_qsProcessInfo (StringExtractorGDBRemote &packet);
309 
310     PacketResult
311     Handle_qC (StringExtractorGDBRemote &packet);
312 
313     PacketResult
314     Handle_qUserName (StringExtractorGDBRemote &packet);
315 
316     PacketResult
317     Handle_qGroupName (StringExtractorGDBRemote &packet);
318 
319     PacketResult
320     Handle_qSpeedTest (StringExtractorGDBRemote &packet);
321 
322     PacketResult
323     Handle_QEnvironment  (StringExtractorGDBRemote &packet);
324 
325     PacketResult
326     Handle_QLaunchArch (StringExtractorGDBRemote &packet);
327 
328     PacketResult
329     Handle_QSetDisableASLR (StringExtractorGDBRemote &packet);
330 
331     PacketResult
332     Handle_QSetDetachOnError (StringExtractorGDBRemote &packet);
333 
334     PacketResult
335     Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
336 
337     PacketResult
338     Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
339 
340     PacketResult
341     Handle_QStartNoAckMode (StringExtractorGDBRemote &packet);
342 
343     PacketResult
344     Handle_QSetSTDIN (StringExtractorGDBRemote &packet);
345 
346     PacketResult
347     Handle_QSetSTDOUT (StringExtractorGDBRemote &packet);
348 
349     PacketResult
350     Handle_QSetSTDERR (StringExtractorGDBRemote &packet);
351 
352     PacketResult
353     Handle_C (StringExtractorGDBRemote &packet);
354 
355     PacketResult
356     Handle_c (StringExtractorGDBRemote &packet, bool skip_file_pos_adjustment = false);
357 
358     PacketResult
359     Handle_vCont (StringExtractorGDBRemote &packet);
360 
361     PacketResult
362     Handle_vCont_actions (StringExtractorGDBRemote &packet);
363 
364     PacketResult
365     Handle_stop_reason (StringExtractorGDBRemote &packet);
366 
367     PacketResult
368     Handle_vFile_Open (StringExtractorGDBRemote &packet);
369 
370     PacketResult
371     Handle_vFile_Close (StringExtractorGDBRemote &packet);
372 
373     PacketResult
374     Handle_vFile_pRead (StringExtractorGDBRemote &packet);
375 
376     PacketResult
377     Handle_vFile_pWrite (StringExtractorGDBRemote &packet);
378 
379     PacketResult
380     Handle_vFile_Size (StringExtractorGDBRemote &packet);
381 
382     PacketResult
383     Handle_vFile_Mode (StringExtractorGDBRemote &packet);
384 
385     PacketResult
386     Handle_vFile_Exists (StringExtractorGDBRemote &packet);
387 
388     PacketResult
389     Handle_vFile_symlink (StringExtractorGDBRemote &packet);
390 
391     PacketResult
392     Handle_vFile_unlink (StringExtractorGDBRemote &packet);
393 
394     PacketResult
395     Handle_vFile_Stat (StringExtractorGDBRemote &packet);
396 
397     PacketResult
398     Handle_vFile_MD5 (StringExtractorGDBRemote &packet);
399 
400     PacketResult
401     Handle_qPlatform_shell (StringExtractorGDBRemote &packet);
402 
403     PacketResult
404     Handle_qRegisterInfo (StringExtractorGDBRemote &packet);
405 
406     PacketResult
407     Handle_qfThreadInfo (StringExtractorGDBRemote &packet);
408 
409     PacketResult
410     Handle_qsThreadInfo (StringExtractorGDBRemote &packet);
411 
412     PacketResult
413     Handle_p (StringExtractorGDBRemote &packet);
414 
415     PacketResult
416     Handle_P (StringExtractorGDBRemote &packet);
417 
418     PacketResult
419     Handle_H (StringExtractorGDBRemote &packet);
420 
421     PacketResult
422     Handle_interrupt (StringExtractorGDBRemote &packet);
423 
424     PacketResult
425     Handle_m (StringExtractorGDBRemote &packet);
426 
427     PacketResult
428     Handle_M (StringExtractorGDBRemote &packet);
429 
430     PacketResult
431     Handle_qMemoryRegionInfoSupported (StringExtractorGDBRemote &packet);
432 
433     PacketResult
434     Handle_qMemoryRegionInfo (StringExtractorGDBRemote &packet);
435 
436     PacketResult
437     Handle_Z (StringExtractorGDBRemote &packet);
438 
439     PacketResult
440     Handle_z (StringExtractorGDBRemote &packet);
441 
442     PacketResult
443     Handle_s (StringExtractorGDBRemote &packet);
444 
445     PacketResult
446     Handle_qSupported (StringExtractorGDBRemote &packet);
447 
448     PacketResult
449     Handle_QThreadSuffixSupported (StringExtractorGDBRemote &packet);
450 
451     PacketResult
452     Handle_QListThreadsInStopReply (StringExtractorGDBRemote &packet);
453 
454     PacketResult
455     Handle_qXfer_auxv_read (StringExtractorGDBRemote &packet);
456 
457     PacketResult
458     Handle_QSaveRegisterState (StringExtractorGDBRemote &packet);
459 
460     PacketResult
461     Handle_QRestoreRegisterState (StringExtractorGDBRemote &packet);
462 
463     PacketResult
464     Handle_vAttach (StringExtractorGDBRemote &packet);
465 
466     void
467     SetCurrentThreadID (lldb::tid_t tid);
468 
469     lldb::tid_t
470     GetCurrentThreadID () const;
471 
472     void
473     SetContinueThreadID (lldb::tid_t tid);
474 
475     lldb::tid_t
476     GetContinueThreadID () const { return m_continue_tid; }
477 
478     lldb_private::Error
479     SetSTDIOFileDescriptor (int fd);
480 
481     static void
482     STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
483 
484 private:
485     bool
486     DebugserverProcessReaped (lldb::pid_t pid);
487 
488     static bool
489     ReapDebugserverProcess (void *callback_baton,
490                             lldb::pid_t pid,
491                             bool exited,
492                             int signal,
493                             int status);
494 
495     bool
496     DebuggedProcessReaped (lldb::pid_t pid);
497 
498     static bool
499     ReapDebuggedProcess (void *callback_baton,
500                          lldb::pid_t pid,
501                          bool exited,
502                          int signal,
503                          int status);
504 
505     bool
506     KillSpawnedProcess (lldb::pid_t pid);
507 
508     bool
509     IsGdbServer ()
510     {
511         return !m_is_platform;
512     }
513 
514     /// Launch a process from lldb-gdbserver
515     lldb_private::Error
516     LaunchDebugServerProcess ();
517 
518     /// Launch a process from lldb-platform
519     lldb_private::Error
520     LaunchPlatformProcess ();
521 
522     void
523     HandleInferiorState_Exited (lldb_private::NativeProcessProtocol *process);
524 
525     void
526     HandleInferiorState_Stopped (lldb_private::NativeProcessProtocol *process);
527 
528     void
529     FlushInferiorOutput ();
530 
531     lldb_private::NativeThreadProtocolSP
532     GetThreadFromSuffix (StringExtractorGDBRemote &packet);
533 
534     uint32_t
535     GetNextSavedRegistersID ();
536 
537     void
538     MaybeCloseInferiorTerminalConnection ();
539 
540     void
541     ClearProcessSpecificData ();
542 
543     //------------------------------------------------------------------
544     // For GDBRemoteCommunicationServer only
545     //------------------------------------------------------------------
546     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer);
547 };
548 
549 #endif  // liblldb_GDBRemoteCommunicationServer_h_
550