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     /// @param[in] args
156     ///     The command line to launch.
157     ///
158     /// @param[in] argc
159     ///     The number of elements in the args array of cstring pointers.
160     ///
161     /// @return
162     ///     An Error object indicating the success or failure of making
163     ///     the setting.
164     //------------------------------------------------------------------
165     lldb_private::Error
166     SetLaunchArguments (const char *const args[], int argc);
167 
168     //------------------------------------------------------------------
169     /// Specify the launch flags for the process.
170     ///
171     /// @param[in] launch_flags
172     ///     The launch flags to use when launching this process.
173     ///
174     /// @return
175     ///     An Error object indicating the success or failure of making
176     ///     the setting.
177     //------------------------------------------------------------------
178     lldb_private::Error
179     SetLaunchFlags (unsigned int launch_flags);
180 
181     //------------------------------------------------------------------
182     /// Launch a process with the current launch settings.
183     ///
184     /// This method supports running an lldb-gdbserver or similar
185     /// server in a situation where the startup code has been provided
186     /// with all the information for a child process to be launched.
187     ///
188     /// @return
189     ///     An Error object indicating the success or failure of the
190     ///     launch.
191     //------------------------------------------------------------------
192     lldb_private::Error
193     LaunchProcess ();
194 
195     //------------------------------------------------------------------
196     /// Attach to a process.
197     ///
198     /// This method supports attaching llgs to a process accessible via the
199     /// configured Platform.
200     ///
201     /// @return
202     ///     An Error object indicating the success or failure of the
203     ///     attach operation.
204     //------------------------------------------------------------------
205     lldb_private::Error
206     AttachToProcess (lldb::pid_t pid);
207 
208     //------------------------------------------------------------------
209     // NativeProcessProtocol::NativeDelegate overrides
210     //------------------------------------------------------------------
211     void
212     InitializeDelegate (lldb_private::NativeProcessProtocol *process) override;
213 
214     void
215     ProcessStateChanged (lldb_private::NativeProcessProtocol *process, lldb::StateType state) override;
216 
217     void
218     DidExec (lldb_private::NativeProcessProtocol *process) override;
219 
220 protected:
221     lldb::PlatformSP m_platform_sp;
222     lldb::thread_t m_async_thread;
223     lldb_private::ProcessLaunchInfo m_process_launch_info;
224     lldb_private::Error m_process_launch_error;
225     std::set<lldb::pid_t> m_spawned_pids;
226     lldb_private::Mutex m_spawned_pids_mutex;
227     lldb_private::ProcessInstanceInfoList m_proc_infos;
228     uint32_t m_proc_infos_index;
229     PortMap m_port_map;
230     uint16_t m_port_offset;
231     lldb::tid_t m_current_tid;
232     lldb::tid_t m_continue_tid;
233     lldb_private::Mutex m_debugged_process_mutex;
234     lldb_private::NativeProcessProtocolSP m_debugged_process_sp;
235     lldb::DebuggerSP m_debugger_sp;
236     Communication m_stdio_communication;
237     bool m_exit_now; // use in asynchronous handling to indicate process should exit.
238     lldb::StateType m_inferior_prev_state;
239     bool m_thread_suffix_supported;
240     bool m_list_threads_in_stop_reply;
241     lldb::DataBufferSP m_active_auxv_buffer_sp;
242     lldb_private::Mutex m_saved_registers_mutex;
243     std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
244     uint32_t m_next_saved_registers_id;
245 
246     PacketResult
247     SendUnimplementedResponse (const char *packet);
248 
249     PacketResult
250     SendErrorResponse (uint8_t error);
251 
252     PacketResult
253     SendIllFormedResponse (const StringExtractorGDBRemote &packet, const char *error_message);
254 
255     PacketResult
256     SendOKResponse ();
257 
258     PacketResult
259     SendONotification (const char *buffer, uint32_t len);
260 
261     PacketResult
262     SendWResponse (lldb_private::NativeProcessProtocol *process);
263 
264     PacketResult
265     SendStopReplyPacketForThread (lldb::tid_t tid);
266 
267     PacketResult
268     SendStopReasonForState (lldb::StateType process_state, bool flush_on_exit);
269 
270     PacketResult
271     Handle_A (StringExtractorGDBRemote &packet);
272 
273     PacketResult
274     Handle_qLaunchSuccess (StringExtractorGDBRemote &packet);
275 
276     PacketResult
277     Handle_qHostInfo (StringExtractorGDBRemote &packet);
278 
279     PacketResult
280     Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet);
281 
282     PacketResult
283     Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet);
284 
285     PacketResult
286     Handle_k (StringExtractorGDBRemote &packet);
287 
288     PacketResult
289     Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet);
290 
291     PacketResult
292     Handle_qPlatform_chmod (StringExtractorGDBRemote &packet);
293 
294     PacketResult
295     Handle_qProcessInfo (StringExtractorGDBRemote &packet);
296 
297     PacketResult
298     Handle_qProcessInfoPID (StringExtractorGDBRemote &packet);
299 
300     PacketResult
301     Handle_qfProcessInfo (StringExtractorGDBRemote &packet);
302 
303     PacketResult
304     Handle_qsProcessInfo (StringExtractorGDBRemote &packet);
305 
306     PacketResult
307     Handle_qC (StringExtractorGDBRemote &packet);
308 
309     PacketResult
310     Handle_qUserName (StringExtractorGDBRemote &packet);
311 
312     PacketResult
313     Handle_qGroupName (StringExtractorGDBRemote &packet);
314 
315     PacketResult
316     Handle_qSpeedTest (StringExtractorGDBRemote &packet);
317 
318     PacketResult
319     Handle_QEnvironment  (StringExtractorGDBRemote &packet);
320 
321     PacketResult
322     Handle_QLaunchArch (StringExtractorGDBRemote &packet);
323 
324     PacketResult
325     Handle_QSetDisableASLR (StringExtractorGDBRemote &packet);
326 
327     PacketResult
328     Handle_QSetDetachOnError (StringExtractorGDBRemote &packet);
329 
330     PacketResult
331     Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
332 
333     PacketResult
334     Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
335 
336     PacketResult
337     Handle_QStartNoAckMode (StringExtractorGDBRemote &packet);
338 
339     PacketResult
340     Handle_QSetSTDIN (StringExtractorGDBRemote &packet);
341 
342     PacketResult
343     Handle_QSetSTDOUT (StringExtractorGDBRemote &packet);
344 
345     PacketResult
346     Handle_QSetSTDERR (StringExtractorGDBRemote &packet);
347 
348     PacketResult
349     Handle_C (StringExtractorGDBRemote &packet);
350 
351     PacketResult
352     Handle_c (StringExtractorGDBRemote &packet, bool skip_file_pos_adjustment = false);
353 
354     PacketResult
355     Handle_vCont (StringExtractorGDBRemote &packet);
356 
357     PacketResult
358     Handle_vCont_actions (StringExtractorGDBRemote &packet);
359 
360     PacketResult
361     Handle_stop_reason (StringExtractorGDBRemote &packet);
362 
363     PacketResult
364     Handle_vFile_Open (StringExtractorGDBRemote &packet);
365 
366     PacketResult
367     Handle_vFile_Close (StringExtractorGDBRemote &packet);
368 
369     PacketResult
370     Handle_vFile_pRead (StringExtractorGDBRemote &packet);
371 
372     PacketResult
373     Handle_vFile_pWrite (StringExtractorGDBRemote &packet);
374 
375     PacketResult
376     Handle_vFile_Size (StringExtractorGDBRemote &packet);
377 
378     PacketResult
379     Handle_vFile_Mode (StringExtractorGDBRemote &packet);
380 
381     PacketResult
382     Handle_vFile_Exists (StringExtractorGDBRemote &packet);
383 
384     PacketResult
385     Handle_vFile_symlink (StringExtractorGDBRemote &packet);
386 
387     PacketResult
388     Handle_vFile_unlink (StringExtractorGDBRemote &packet);
389 
390     PacketResult
391     Handle_vFile_Stat (StringExtractorGDBRemote &packet);
392 
393     PacketResult
394     Handle_vFile_MD5 (StringExtractorGDBRemote &packet);
395 
396     PacketResult
397     Handle_qPlatform_shell (StringExtractorGDBRemote &packet);
398 
399     PacketResult
400     Handle_qRegisterInfo (StringExtractorGDBRemote &packet);
401 
402     PacketResult
403     Handle_qfThreadInfo (StringExtractorGDBRemote &packet);
404 
405     PacketResult
406     Handle_qsThreadInfo (StringExtractorGDBRemote &packet);
407 
408     PacketResult
409     Handle_p (StringExtractorGDBRemote &packet);
410 
411     PacketResult
412     Handle_P (StringExtractorGDBRemote &packet);
413 
414     PacketResult
415     Handle_H (StringExtractorGDBRemote &packet);
416 
417     PacketResult
418     Handle_interrupt (StringExtractorGDBRemote &packet);
419 
420     PacketResult
421     Handle_m (StringExtractorGDBRemote &packet);
422 
423     PacketResult
424     Handle_M (StringExtractorGDBRemote &packet);
425 
426     PacketResult
427     Handle_qMemoryRegionInfoSupported (StringExtractorGDBRemote &packet);
428 
429     PacketResult
430     Handle_qMemoryRegionInfo (StringExtractorGDBRemote &packet);
431 
432     PacketResult
433     Handle_Z (StringExtractorGDBRemote &packet);
434 
435     PacketResult
436     Handle_z (StringExtractorGDBRemote &packet);
437 
438     PacketResult
439     Handle_s (StringExtractorGDBRemote &packet);
440 
441     PacketResult
442     Handle_qSupported (StringExtractorGDBRemote &packet);
443 
444     PacketResult
445     Handle_QThreadSuffixSupported (StringExtractorGDBRemote &packet);
446 
447     PacketResult
448     Handle_QListThreadsInStopReply (StringExtractorGDBRemote &packet);
449 
450     PacketResult
451     Handle_qXfer_auxv_read (StringExtractorGDBRemote &packet);
452 
453     PacketResult
454     Handle_QSaveRegisterState (StringExtractorGDBRemote &packet);
455 
456     PacketResult
457     Handle_QRestoreRegisterState (StringExtractorGDBRemote &packet);
458 
459     PacketResult
460     Handle_vAttach (StringExtractorGDBRemote &packet);
461 
462     PacketResult
463     Handle_qThreadStopInfo (StringExtractorGDBRemote &packet);
464 
465     void
466     SetCurrentThreadID (lldb::tid_t tid);
467 
468     lldb::tid_t
469     GetCurrentThreadID () const;
470 
471     void
472     SetContinueThreadID (lldb::tid_t tid);
473 
474     lldb::tid_t
475     GetContinueThreadID () const { return m_continue_tid; }
476 
477     lldb_private::Error
478     SetSTDIOFileDescriptor (int fd);
479 
480     static void
481     STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
482 
483 private:
484     bool
485     DebugserverProcessReaped (lldb::pid_t pid);
486 
487     static bool
488     ReapDebugserverProcess (void *callback_baton,
489                             lldb::pid_t pid,
490                             bool exited,
491                             int signal,
492                             int status);
493 
494     bool
495     DebuggedProcessReaped (lldb::pid_t pid);
496 
497     static bool
498     ReapDebuggedProcess (void *callback_baton,
499                          lldb::pid_t pid,
500                          bool exited,
501                          int signal,
502                          int status);
503 
504     bool
505     KillSpawnedProcess (lldb::pid_t pid);
506 
507     bool
508     IsGdbServer ()
509     {
510         return !m_is_platform;
511     }
512 
513     /// Launch an inferior process from lldb-gdbserver
514     lldb_private::Error
515     LaunchProcessForDebugging ();
516 
517     /// Launch a process from lldb-platform
518     lldb_private::Error
519     LaunchPlatformProcess ();
520 
521     void
522     HandleInferiorState_Exited (lldb_private::NativeProcessProtocol *process);
523 
524     void
525     HandleInferiorState_Stopped (lldb_private::NativeProcessProtocol *process);
526 
527     void
528     FlushInferiorOutput ();
529 
530     lldb_private::NativeThreadProtocolSP
531     GetThreadFromSuffix (StringExtractorGDBRemote &packet);
532 
533     uint32_t
534     GetNextSavedRegistersID ();
535 
536     void
537     MaybeCloseInferiorTerminalConnection ();
538 
539     void
540     ClearProcessSpecificData ();
541 
542     bool
543     ShouldRedirectInferiorOutputOverGdbRemote (const lldb_private::ProcessLaunchInfo &launch_info) const;
544 
545     //------------------------------------------------------------------
546     // For GDBRemoteCommunicationServer only
547     //------------------------------------------------------------------
548     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer);
549 };
550 
551 #endif  // liblldb_GDBRemoteCommunicationServer_h_
552