1 //===-- ConnectionMachPort.cpp ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "lldb/Core/ConnectionMachPort.h"
14 #include <servers/bootstrap.h>
17 // Other libraries and framework includes
19 #include "lldb/lldb-private-log.h"
20 #include "lldb/Core/Communication.h"
21 #include "lldb/Core/Log.h"
24 using namespace lldb_private;
28 mach_msg_header_t head;
29 ConnectionMachPort::PayloadType payload;
34 ConnectionMachPort::ConnectionMachPort () :
36 m_task(mach_task_self()),
37 m_port(MACH_PORT_TYPE_NONE)
41 ConnectionMachPort::~ConnectionMachPort ()
47 ConnectionMachPort::IsConnected () const
49 return m_port != MACH_PORT_TYPE_NONE;
53 ConnectionMachPort::Connect (const char *s, Error *error_ptr)
58 error_ptr->SetErrorString ("already connected");
59 return eConnectionStatusError;
62 if (s == NULL || s[0] == '\0')
65 error_ptr->SetErrorString ("empty connect URL");
66 return eConnectionStatusError;
69 ConnectionStatus status = eConnectionStatusError;
71 if (strncmp (s, "bootstrap-checkin://", strlen("bootstrap-checkin://")))
73 s += strlen("bootstrap-checkin://");
77 status = BootstrapCheckIn (s, error_ptr);
82 error_ptr->SetErrorString ("bootstrap port name is empty");
85 else if (strncmp (s, "bootstrap-lookup://", strlen("bootstrap-lookup://")))
87 s += strlen("bootstrap-lookup://");
90 status = BootstrapLookup (s, error_ptr);
95 error_ptr->SetErrorString ("bootstrap port name is empty");
101 error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s);
105 if (status == eConnectionStatusSuccess)
119 ConnectionMachPort::BootstrapCheckIn (const char *port, Error *error_ptr)
121 mach_port_t bootstrap_port = MACH_PORT_TYPE_NONE;
123 /* Getting bootstrap server port */
124 kern_return_t kret = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
125 if (kret == KERN_SUCCESS)
128 int len = snprintf(port_name, sizeof(port_name), "%s", port);
129 if (len < sizeof(port_name))
131 kret = ::bootstrap_check_in (bootstrap_port,
139 error_ptr->SetErrorString ("bootstrap is too long");
140 return eConnectionStatusError;
144 if (kret != KERN_SUCCESS)
148 error_ptr->SetError (kret, eErrorTypeMachKernel);
149 return eConnectionStatusError;
151 return eConnectionStatusSuccess;
154 lldb::ConnectionStatus
155 ConnectionMachPort::BootstrapLookup (const char *port,
162 if (::snprintf (port_name, sizeof (port_name), "%s", port) >= sizeof (port_name))
165 error_ptr->SetErrorString ("port netname is too long");
166 return eConnectionStatusError;
172 error_ptr->SetErrorString ("empty port netname");
173 return eConnectionStatusError;
176 mach_port_t bootstrap_port = MACH_PORT_TYPE_NONE;
178 /* Getting bootstrap server port */
179 kern_return_t kret = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
180 if (kret == KERN_SUCCESS)
182 kret = ::bootstrap_look_up (bootstrap_port,
187 if (kret != KERN_SUCCESS)
190 error_ptr->SetError (kret, eErrorTypeMachKernel);
191 return eConnectionStatusError;
194 return eConnectionStatusSuccess;
198 ConnectionMachPort::Disconnect (Error *error_ptr)
202 // TODO: verify if we need to netname_check_out for
204 if (m_port != MACH_PORT_TYPE_NONE)
206 kret = ::mach_port_deallocate (m_task, m_port);
208 error_ptr->SetError (kret, eErrorTypeMachKernel);
209 m_port = MACH_PORT_TYPE_NONE;
212 return eConnectionStatusSuccess;
216 ConnectionMachPort::Read (void *dst,
218 uint32_t timeout_usec,
219 ConnectionStatus &status,
224 kern_return_t kret = Receive (payload);
225 if (kret == KERN_SUCCESS)
227 memcpy (dst, payload.data, payload.data_length);
228 status = eConnectionStatusSuccess;
229 return payload.data_length;
233 error_ptr->SetError (kret, eErrorTypeMachKernel);
234 status = eConnectionStatusError;
239 ConnectionMachPort::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
243 payload.data_length = src_len;
244 const size_t max_payload_size = sizeof(payload.data);
245 if (src_len > max_payload_size)
246 payload.data_length = max_payload_size;
247 memcpy (payload.data, src, payload.data_length);
249 if (Send (payload) == KERN_SUCCESS)
251 status = eConnectionStatusSuccess;
252 return payload.data_length;
254 status = eConnectionStatusError;
259 ConnectionMachPort::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
261 return eConnectionStatusLostConnection;
265 ConnectionMachPort::Send (const PayloadType &payload)
267 struct MessageType message;
269 /* (i) Form the message : */
271 /* (i.a) Fill the header fields : */
272 message.head.msgh_bits = MACH_MSGH_BITS_REMOTE (MACH_MSG_TYPE_MAKE_SEND) |
273 MACH_MSGH_BITS_OTHER (MACH_MSGH_BITS_COMPLEX);
274 message.head.msgh_size = sizeof(MessageType);
275 message.head.msgh_local_port = MACH_PORT_NULL;
276 message.head.msgh_remote_port = m_port;
278 /* (i.b) Explain the message type ( an integer ) */
279 // message.type.msgt_name = MACH_MSG_TYPE_INTEGER_32;
280 // message.type.msgt_size = 32;
281 // message.type.msgt_number = 1;
282 // message.type.msgt_inline = TRUE;
283 // message.type.msgt_longform = FALSE;
284 // message.type.msgt_deallocate = FALSE;
285 /* message.type.msgt_unused = 0; */ /* not needed, I think */
287 /* (i.c) Fill the message with the given integer : */
288 message.payload = payload;
290 /* (ii) Send the message : */
291 kern_return_t kret = ::mach_msg (&message.head,
293 message.head.msgh_size,
296 MACH_MSG_TIMEOUT_NONE,
303 ConnectionMachPort::Receive (PayloadType &payload)
306 message.head.msgh_size = sizeof(MessageType);
308 kern_return_t kret = ::mach_msg (&message.head,
313 MACH_MSG_TIMEOUT_NONE,
316 if (kret == KERN_SUCCESS)
317 payload = message.payload;
323 #endif // #if defined(__APPLE__)