aboutsummaryrefslogtreecommitdiff
path: root/source/Core/ConnectionMachPort.cpp
blob: fe29814be42025d2036e3b08c27147184aa45565 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//===-- ConnectionMachPort.cpp ----------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#if defined(__APPLE__)

#include "lldb/Core/ConnectionMachPort.h"

// C Includes
#include <mach/mach.h>
#include <servers/bootstrap.h>

// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/lldb-private-log.h"
#include "lldb/Core/Communication.h"
#include "lldb/Core/Log.h"

using namespace lldb;
using namespace lldb_private;

struct MessageType
{
    mach_msg_header_t head;
    ConnectionMachPort::PayloadType payload;
};



ConnectionMachPort::ConnectionMachPort () :
    Connection(),
    m_task(mach_task_self()),
    m_port(MACH_PORT_TYPE_NONE)
{
}

ConnectionMachPort::~ConnectionMachPort ()
{
    Disconnect (NULL);
}

bool
ConnectionMachPort::IsConnected () const
{
    return  m_port != MACH_PORT_TYPE_NONE;
}

ConnectionStatus
ConnectionMachPort::Connect (const char *s, Error *error_ptr)
{
    if (IsConnected())
    {
        if (error_ptr)
            error_ptr->SetErrorString ("already connected");
        return eConnectionStatusError;
    }
    
    if (s == NULL || s[0] == '\0')
    {
        if (error_ptr)
            error_ptr->SetErrorString ("empty connect URL");
        return eConnectionStatusError;
    }
    
    ConnectionStatus status = eConnectionStatusError;
    
    if (strncmp (s, "bootstrap-checkin://", strlen("bootstrap-checkin://")))
    {
        s += strlen("bootstrap-checkin://");
        
        if (*s)
        {
            status = BootstrapCheckIn (s, error_ptr);
        }
        else
        {
            if (error_ptr)
                error_ptr->SetErrorString ("bootstrap port name is empty");
        }
    }
    else if (strncmp (s, "bootstrap-lookup://", strlen("bootstrap-lookup://")))
    {
        s += strlen("bootstrap-lookup://");
        if (*s)
        {
            status = BootstrapLookup (s, error_ptr);
        }
        else
        {
            if (error_ptr)
                error_ptr->SetErrorString ("bootstrap port name is empty");
        }
    }
    else
    {
        if (error_ptr)
            error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s);   
    }


    if (status == eConnectionStatusSuccess)
    {
        if (error_ptr)
            error_ptr->Clear();
        m_uri.assign(s);
    }
    else
    {
        Disconnect(NULL);
    }
        
    return status;
}

ConnectionStatus
ConnectionMachPort::BootstrapCheckIn (const char *port, Error *error_ptr)
{
    mach_port_t bootstrap_port = MACH_PORT_TYPE_NONE;
    
    /* Getting bootstrap server port */
    kern_return_t kret = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
    if (kret == KERN_SUCCESS) 
    {
        name_t port_name;
        int len = snprintf(port_name, sizeof(port_name), "%s", port);
        if (static_cast<size_t>(len) < sizeof(port_name))
        {
            kret = ::bootstrap_check_in (bootstrap_port, 
                                         port_name, 
                                         &m_port);
        }
        else
        {
            Disconnect(NULL);
            if (error_ptr)
                error_ptr->SetErrorString ("bootstrap is too long");
            return eConnectionStatusError;
        }
    }
    
    if (kret != KERN_SUCCESS)
    {
        Disconnect(NULL);
        if (error_ptr)
            error_ptr->SetError (kret, eErrorTypeMachKernel);
        return eConnectionStatusError;
    }
    return eConnectionStatusSuccess;
}

lldb::ConnectionStatus
ConnectionMachPort::BootstrapLookup (const char *port, 
                                     Error *error_ptr)
{
    name_t port_name;

    if (port && port[0])
    {
        if (static_cast<size_t>(::snprintf (port_name, sizeof (port_name), "%s", port)) >= sizeof (port_name))
        {
            if (error_ptr)
                error_ptr->SetErrorString ("port netname is too long");
            return eConnectionStatusError;
        }
    }
    else
    {
        if (error_ptr)
            error_ptr->SetErrorString ("empty port netname");
        return eConnectionStatusError;
    }

    mach_port_t bootstrap_port = MACH_PORT_TYPE_NONE;
    
    /* Getting bootstrap server port */
    kern_return_t kret = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
    if (kret == KERN_SUCCESS) 
    {
        kret = ::bootstrap_look_up (bootstrap_port, 
                                    port_name, 
                                    &m_port);
    }

    if (kret != KERN_SUCCESS)
    {
        if (error_ptr)
            error_ptr->SetError (kret, eErrorTypeMachKernel);
        return eConnectionStatusError;
    }

    return eConnectionStatusSuccess;
}

ConnectionStatus
ConnectionMachPort::Disconnect (Error *error_ptr)
{
    kern_return_t kret;
    
    // TODO: verify if we need to netname_check_out for
    // either or both 
    if (m_port != MACH_PORT_TYPE_NONE)
    {
        kret = ::mach_port_deallocate (m_task, m_port);       
        if (error_ptr)
            error_ptr->SetError (kret, eErrorTypeMachKernel);
        m_port = MACH_PORT_TYPE_NONE;
    }
    m_uri.clear();

    return eConnectionStatusSuccess;
}

size_t
ConnectionMachPort::Read (void *dst, 
                          size_t dst_len, 
                          uint32_t timeout_usec,
                          ConnectionStatus &status, 
                          Error *error_ptr)
{
    PayloadType payload;
    
    kern_return_t kret = Receive (payload);
    if (kret == KERN_SUCCESS)
    {
        memcpy (dst, payload.data, payload.data_length);
        status = eConnectionStatusSuccess;
        return payload.data_length;
    }
    
    if (error_ptr)
        error_ptr->SetError (kret, eErrorTypeMachKernel);
    status = eConnectionStatusError;
    return 0;
}

size_t
ConnectionMachPort::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
{
    PayloadType payload;
    payload.command = 0;
    payload.data_length = src_len;
    const size_t max_payload_size = sizeof(payload.data);
    if (src_len > max_payload_size)
        payload.data_length = max_payload_size;
    memcpy (payload.data, src, payload.data_length);
    
    if (Send (payload) == KERN_SUCCESS)
    {
        status = eConnectionStatusSuccess;
        return payload.data_length;
    }
    status = eConnectionStatusError;
    return 0;
}

std::string
ConnectionMachPort::GetURI()
{
    return m_uri;
}

ConnectionStatus
ConnectionMachPort::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
{
    return eConnectionStatusLostConnection;
}

kern_return_t 
ConnectionMachPort::Send (const PayloadType &payload)
{
	struct MessageType message;
    
	/* (i) Form the message : */
    
	/* (i.a) Fill the header fields : */
	message.head.msgh_bits = MACH_MSGH_BITS_REMOTE (MACH_MSG_TYPE_MAKE_SEND) | 
                             MACH_MSGH_BITS_OTHER (MACH_MSGH_BITS_COMPLEX);
	message.head.msgh_size = sizeof(MessageType);
	message.head.msgh_local_port = MACH_PORT_NULL;
	message.head.msgh_remote_port = m_port;
    
	/* (i.b) Explain the message type ( an integer ) */
    //	message.type.msgt_name = MACH_MSG_TYPE_INTEGER_32;
    //	message.type.msgt_size = 32;
    //	message.type.msgt_number = 1;
    //	message.type.msgt_inline = TRUE;
    //	message.type.msgt_longform = FALSE;
    //	message.type.msgt_deallocate = FALSE;
	/* message.type.msgt_unused = 0; */ /* not needed, I think */
    
	/* (i.c) Fill the message with the given integer : */
	message.payload = payload;
    
	/* (ii) Send the message : */
	kern_return_t kret = ::mach_msg (&message.head, 
                                     MACH_SEND_MSG, 
                                     message.head.msgh_size, 
                                     0, 
                                     MACH_PORT_NULL, 
                                     MACH_MSG_TIMEOUT_NONE, 
                                     MACH_PORT_NULL);
    
	return kret;
}

kern_return_t 
ConnectionMachPort::Receive (PayloadType &payload)
{
	MessageType message;
	message.head.msgh_size = sizeof(MessageType);
    
	kern_return_t kret = ::mach_msg (&message.head, 
                                     MACH_RCV_MSG, 
                                     0, 
                                     sizeof(MessageType), 
                                     m_port,
                                     MACH_MSG_TIMEOUT_NONE, 
                                     MACH_PORT_NULL);
    
    if (kret == KERN_SUCCESS)
        payload = message.payload;

	return kret;
}


#endif // #if defined(__APPLE__)