summaryrefslogtreecommitdiff
path: root/src/debug/di/remoteeventchannel.cpp
blob: ab810008a3dc8c659d858e953001b60f00cd8a76 (plain)
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
333
334
335
336
337
338
339
340
341
342
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//*****************************************************************************
// File: RemoteEventChannel.cpp
// 

//
// Implements the old-style event channel between two remote processes.
//*****************************************************************************

#include "stdafx.h"
#include "eventchannel.h"

#include "dbgtransportsession.h"
#include "dbgtransportmanager.h"


//---------------------------------------------------------------------------------------
// Class serves as a connector to win32 native-debugging API.
class RemoteEventChannel : public IEventChannel
{
public:
    RemoteEventChannel(DebuggerIPCControlBlock * pDCBBuffer,
                       DbgTransportTarget *      pProxy, 
                       DbgTransportSession *     pTransport);

    virtual ~RemoteEventChannel() {}

    // Inititalize the event channel.
    virtual HRESULT Init(HANDLE hTargetProc);

    // Called when the debugger is detaching.
    virtual void Detach();

    // Delete the event channel and clean up all the resources it owns.  This function can only be called once.
    virtual void Delete();



    // Update a single field with a value stored in the RS copy of the DCB.
    virtual HRESULT UpdateLeftSideDCBField(void *rsFieldAddr, SIZE_T size);

    // Update the entire RS copy of the debugger control block by reading the LS copy.
    virtual HRESULT UpdateRightSideDCB();

    // Get the pointer to the RS DCB.
    virtual DebuggerIPCControlBlock * GetDCB();



    // Check whether we need to wait for an acknowledgement from the LS after sending an IPC event.
    virtual BOOL NeedToWaitForAck(DebuggerIPCEvent * pEvent);

    // Get a handle to wait on after sending an IPC event to the LS.  The caller should call NeedToWaitForAck()
    virtual HANDLE GetRightSideEventAckHandle();

    // Clean up the state if the wait for an acknowledgement is unsuccessful.
    virtual void   ClearEventForLeftSide();



    // Send an IPC event to the LS.
    virtual HRESULT SendEventToLeftSide(DebuggerIPCEvent * pEvent, SIZE_T eventSize);

    // Get the reply from the LS for a previously sent IPC event.
    virtual HRESULT GetReplyFromLeftSide(DebuggerIPCEvent * pReplyEvent, SIZE_T eventSize);



    // Save an IPC event from the LS.  
    // Used for transferring an IPC event from the native pipeline to the IPC event channel.
    virtual HRESULT SaveEventFromLeftSide(DebuggerIPCEvent * pEventFromLeftSide);

    // Get a saved IPC event from the LS.  
    // Used for transferring an IPC event from the native pipeline to the IPC event channel.
    virtual HRESULT GetEventFromLeftSide(DebuggerIPCEvent * pLocalManagedEvent);

private:
    DebuggerIPCControlBlock * m_pDCBBuffer;     // local buffer for the DCB on the RS
    DbgTransportTarget *      m_pProxy;         // connection to the debugger proxy
    DbgTransportSession *     m_pTransport;     // connection to the debuggee process

    // The next two fields are used for storing an IPC event from the native pipeline 
    // for the IPC event channel.
    BYTE m_rgbLeftSideEventBuffer[CorDBIPC_BUFFER_SIZE];
    BOOL m_fLeftSideEventAvailable;
};

// Allocate and return an old-style event channel object for this target platform.
HRESULT NewEventChannelForThisPlatform(CORDB_ADDRESS pLeftSideDCB, 
                                       ICorDebugMutableDataTarget * pMutableDataTarget,
                                       const ProcessDescriptor * pProcessDescriptor,
                                       MachineInfo machineInfo,
                                       IEventChannel ** ppEventChannel)
{
    // @dbgtodo  Mac - Consider moving all of the transport logic to one place.
    // Perhaps add a new function on DbgTransportManager.
    HandleHolder hDummy;
    HRESULT hr = E_FAIL;

    RemoteEventChannel *      pEventChannel = NULL;
    DebuggerIPCControlBlock * pDCBBuffer    = NULL;

    DbgTransportTarget *   pProxy     = g_pDbgTransportTarget;
    DbgTransportSession *  pTransport = NULL;

    hr = pProxy->GetTransportForProcess(pProcessDescriptor, &pTransport, &hDummy);
    if (FAILED(hr))
    {
        goto Label_Exit;
    }

    if (!pTransport->WaitForSessionToOpen(10000))
    {
        hr = CORDBG_E_TIMEOUT;
        goto Label_Exit;
    }

    pDCBBuffer = new (nothrow) DebuggerIPCControlBlock;
    if (pDCBBuffer == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto Label_Exit;
    }

    pEventChannel = new (nothrow) RemoteEventChannel(pDCBBuffer, pProxy, pTransport);
    if (pEventChannel == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto Label_Exit;
    }

    _ASSERTE(SUCCEEDED(hr));
    *ppEventChannel = pEventChannel;

Label_Exit:
    if (FAILED(hr))
    {
        if (pEventChannel != NULL)
        {
            // The IEventChannel has ownership of the proxy and the transport, 
            // so we don't need to clean them up here.
            delete pEventChannel;
        }
        else
        {
            if (pTransport != NULL)
            {
                pProxy->ReleaseTransport(pTransport);
            }
            if (pDCBBuffer != NULL)
            {
                delete pDCBBuffer;
            }
        }
    }
    return hr;
}

//-----------------------------------------------------------------------------
//
// This is the constructor.
//
// Arguments:
//    pLeftSideDCB       - target address of the DCB on the LS
//    pDCBBuffer         - local buffer for storing the DCB on the RS; the memory is owned by this class
//    pMutableDataTarget - data target for reading from and writing to the target process's address space
//

RemoteEventChannel::RemoteEventChannel(DebuggerIPCControlBlock * pDCBBuffer,
                                       DbgTransportTarget *      pProxy, 
                                       DbgTransportSession *     pTransport)
{
    m_pDCBBuffer = pDCBBuffer;
    m_pProxy = pProxy;
    m_pTransport = pTransport;
    m_fLeftSideEventAvailable = FALSE;
}

// Inititalize the event channel.
//
// virtual
HRESULT RemoteEventChannel::Init(HANDLE hTargetProc)
{
    return S_OK;
}

// Called when the debugger is detaching.
//
// virtual
void RemoteEventChannel::Detach()
{
    // This is a nop for Mac debugging because we don't use RSEA/RSER.
    return;
}

// Delete the event channel and clean up all the resources it owns.  This function can only be called once.
// 
// virtual
void RemoteEventChannel::Delete()
{
    if (m_pDCBBuffer != NULL)
    {
        delete m_pDCBBuffer;
        m_pDCBBuffer = NULL;
    }

    if (m_pTransport != NULL)
    {
        m_pProxy->ReleaseTransport(m_pTransport);
    }

    delete this;
}

// Update a single field with a value stored in the RS copy of the DCB.
//
// virtual 
HRESULT RemoteEventChannel::UpdateLeftSideDCBField(void * rsFieldAddr, SIZE_T size)
{
    _ASSERTE(m_pDCBBuffer != NULL);

    // Ask the transport to update the LS DCB.
    return m_pTransport->SetDCB(m_pDCBBuffer);
}

// Update the entire RS copy of the debugger control block by reading the LS copy.
//
// virtual 
HRESULT RemoteEventChannel::UpdateRightSideDCB()
{
    _ASSERTE(m_pDCBBuffer != NULL);

    // Ask the transport to read the DCB from the Ls.
    return m_pTransport->GetDCB(m_pDCBBuffer);
}

// Get the pointer to the RS DCB.
//
// virtual 
DebuggerIPCControlBlock * RemoteEventChannel::GetDCB()
{
    return m_pDCBBuffer;
}

// Check whether we need to wait for an acknowledgement from the LS after sending an IPC event.
//
// virtual 
BOOL RemoteEventChannel::NeedToWaitForAck(DebuggerIPCEvent * pEvent)
{
    // There are three cases to consider when sending an event over the transport:
    //
    // 1) asynchronous
    //      - the LS can just send the event and continue
    //
    // 2) synchronous, but no reply
    //      - This is different than Windows.  We don't wait for an acknowledgement.  
    //        Needless to say this is a semantical difference, but none of our code actually expects 
    //        this type of IPC events to be synchronized.
    //
    // 3) synchronous, reply required: 
    //      - This is the only case we need to wait for an acknowledgement in the Mac debugging case.
    return (!pEvent->asyncSend && pEvent->replyRequired);
}

// Get a handle to wait on after sending an IPC event to the LS.  The caller should call NeedToWaitForAck()
//
// virtual 
HANDLE RemoteEventChannel::GetRightSideEventAckHandle()
{
    // Delegate to the transport which does the real work.
    return m_pTransport->GetIPCEventReadyEvent();
}

// Clean up the state if the wait for an acknowledgement is unsuccessful.
//
// virtual 
void RemoteEventChannel::ClearEventForLeftSide()
{
    // This is a nop for Mac debugging because we don't use RSEA/RSER.
    return;
}

// Send an IPC event to the LS.
//
// virtual 
HRESULT RemoteEventChannel::SendEventToLeftSide(DebuggerIPCEvent * pEvent, SIZE_T eventSize)
{
    _ASSERTE(eventSize <= CorDBIPC_BUFFER_SIZE);

    // Delegate to the transport.  The event size is ignored.
    return m_pTransport->SendEvent(pEvent);
}

// Get the reply from the LS for a previously sent IPC event.
//
// virtual 
HRESULT RemoteEventChannel::GetReplyFromLeftSide(DebuggerIPCEvent * pReplyEvent, SIZE_T eventSize)
{
    // Delegate to the transport.
    m_pTransport->GetNextEvent(pReplyEvent, (DWORD)eventSize);
    return S_OK;
}

// Save an IPC event from the LS.  
// Used for transferring an IPC event from the native pipeline to the IPC event channel.
//
// virtual
HRESULT RemoteEventChannel::SaveEventFromLeftSide(DebuggerIPCEvent * pEventFromLeftSide)
{
    if (m_fLeftSideEventAvailable)
    {
        // We should only be saving one event at a time.
        return E_FAIL;
    }
    else
    {
        memcpy(m_rgbLeftSideEventBuffer, reinterpret_cast<BYTE *>(pEventFromLeftSide), CorDBIPC_BUFFER_SIZE);
        m_fLeftSideEventAvailable = TRUE;
        return S_OK;
    }
}

// Get a saved IPC event from the LS.  
// Used for transferring an IPC event from the native pipeline to the IPC event channel.
//
// virtual
HRESULT RemoteEventChannel::GetEventFromLeftSide(DebuggerIPCEvent * pLocalManagedEvent)
{
    if (m_fLeftSideEventAvailable)
    {
        memcpy(reinterpret_cast<BYTE *>(pLocalManagedEvent), m_rgbLeftSideEventBuffer, CorDBIPC_BUFFER_SIZE);
        m_fLeftSideEventAvailable = FALSE;
        return S_OK;
    }
    else
    {
        // We have not saved any event.
        return E_FAIL;
    }
}