blob: c0c7b08ccf7a5e9f55a98d19489f2c3e041265c0 [file] [log] [blame]
Greg Clayton269f91e2011-07-15 18:02:58 +00001//===-- CommunicationKDP.h --------------------------------------*- C++ -*-===//
Greg Clayton363be3f2011-07-15 03:27:12 +00002//
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_CommunicationKDP_h_
11#define liblldb_CommunicationKDP_h_
12
13// C Includes
14// C++ Includes
15#include <list>
16#include <string>
17
18// Other libraries and framework includes
19// Project includes
20#include "lldb/lldb-private.h"
21#include "lldb/Core/Communication.h"
22#include "lldb/Core/Listener.h"
23#include "lldb/Host/Mutex.h"
24#include "lldb/Host/Predicate.h"
25#include "lldb/Host/TimeValue.h"
26
27class StringExtractor;
28
29class CommunicationKDP : public lldb_private::Communication
30{
31public:
32 enum
33 {
34 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
35 };
Greg Clayton1e5b0212011-07-15 16:31:38 +000036
37 const static uint32_t kMaxPacketSize = 1200;
38 const static uint32_t kMaxDataSize = 1024;
39
40 typedef enum
41 {
42 eRequestTypeConnect = 0u,
43 eRequestTypeDisconnect,
44 eRequestTypeHostInfo,
45 eRequestTypeVersion,
46 eRequestTypeMaxBytes,
47 eRequestTypeReadMemory,
48 eRequestTypeWriteMemory,
49 eRequestTypeReadRegisters,
50 eRequestTypeWriteRegisters,
51 eRequestTypeLoad,
52 eRequestTypeImagePath,
53 eRequestTypeSuspend,
54 eRequestTypeResume,
55 eRequestTypeException,
56 eRequestTypeTermination,
57 eRequestTypeBreakpointSet,
58 eRequestTypeBreakpointRemove,
59 eRequestTypeRegions,
60 eRequestTypeReattach,
61 eRequestTypeHostReboot,
62 eRequestTypeReadMemory64,
63 eRequestTypeWriteMemory64,
64 eRequestTypeBreakpointSet64,
65 eRequestTypeBreakpointRemove64,
66 eRequestTypeKernelVersion
67 } RequestType;
68
69 typedef enum
70 {
71 eErrorSuccess = 0,
72 eErrorAlreadyConnected,
73 eErrorPacketToBig,
74 eErrorInvalidRegisterFlavor,
75 eErrorUnimplemented
76 } ErrorType;
77
78 typedef enum
79 {
80 ePacketTypeRequest = 0u,
81 ePacketTypeReply = 1u
82 } PacketType;
Greg Clayton363be3f2011-07-15 03:27:12 +000083 //------------------------------------------------------------------
84 // Constructors and Destructors
85 //------------------------------------------------------------------
86 CommunicationKDP (const char *comm_name);
87
88 virtual
89 ~CommunicationKDP();
90
Greg Clayton1e5b0212011-07-15 16:31:38 +000091 bool
92 SendRequestPacket (const lldb_private::StreamString &request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +000093
94 // Wait for a packet within 'nsec' seconds
95 size_t
96 WaitForPacketWithTimeoutMicroSeconds (StringExtractor &response,
97 uint32_t usec);
98
Greg Clayton363be3f2011-07-15 03:27:12 +000099 bool
100 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
101
102 bool
103 CheckForPacket (const uint8_t *src,
104 size_t src_len,
105 StringExtractor &packet);
106 bool
107 IsRunning() const
108 {
109 return m_public_is_running.GetValue();
110 }
111
Greg Clayton363be3f2011-07-15 03:27:12 +0000112 //------------------------------------------------------------------
113 // Set the global packet timeout.
114 //
115 // For clients, this is the timeout that gets used when sending
116 // packets and waiting for responses. For servers, this might not
117 // get used, and if it doesn't this should be moved to the
118 // CommunicationKDPClient.
119 //------------------------------------------------------------------
120 uint32_t
121 SetPacketTimeout (uint32_t packet_timeout)
122 {
123 const uint32_t old_packet_timeout = m_packet_timeout;
124 m_packet_timeout = packet_timeout;
125 return old_packet_timeout;
126 }
127
128 uint32_t
129 GetPacketTimeoutInMicroSeconds () const
130 {
131 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
132 }
133 //------------------------------------------------------------------
134 // Start a debugserver instance on the current host using the
135 // supplied connection URL.
136 //------------------------------------------------------------------
137 lldb_private::Error
138 StartDebugserverProcess (const char *connect_url,
139 const char *unix_socket_name,
140 lldb_private::ProcessLaunchInfo &launch_info);
141
142
Greg Clayton1e5b0212011-07-15 16:31:38 +0000143 ErrorType
144 Connect (uint16_t reply_port,
145 uint16_t exc_port,
146 const char *greeting);
147
148 ErrorType
149 Disconnect ();
150
Greg Clayton363be3f2011-07-15 03:27:12 +0000151protected:
152 typedef std::list<std::string> packet_collection;
153
Greg Clayton1e5b0212011-07-15 16:31:38 +0000154 bool
155 SendRequestPacketNoLock (const lldb_private::StreamString &request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000156
157 size_t
158 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractor &response,
159 uint32_t timeout_usec);
160
161 bool
162 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
163
Greg Clayton1e5b0212011-07-15 16:31:38 +0000164 void
165 MakeRequestPacketHeader (RequestType request_type,
166 lldb_private::StreamString &request_packet);
167
Greg Clayton363be3f2011-07-15 03:27:12 +0000168 //------------------------------------------------------------------
169 // Classes that inherit from CommunicationKDP can see and modify these
170 //------------------------------------------------------------------
171 uint32_t m_packet_timeout;
172 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
173 lldb_private::Predicate<bool> m_public_is_running;
174 lldb_private::Predicate<bool> m_private_is_running;
Greg Clayton1e5b0212011-07-15 16:31:38 +0000175 uint32_t m_session_key;
176 uint8_t m_request_sequence_id;
177 uint8_t m_exception_sequence_id;
Greg Clayton363be3f2011-07-15 03:27:12 +0000178private:
179 //------------------------------------------------------------------
180 // For CommunicationKDP only
181 //------------------------------------------------------------------
182 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
183};
184
185#endif // liblldb_CommunicationKDP_h_