summaryrefslogtreecommitdiff
path: root/src/debug/di/shimevents.cpp
blob: 2a1430176e448164fa0a94df101b86d21ed78689 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//*****************************************************************************
// File: ShimEvents.cpp
// 

//
// The V3 ICD debugging APIs have a lower abstraction level than V2.
// This provides V2 ICD debugging functionality on top of the V3 debugger object.
//*****************************************************************************

#include "stdafx.h"

#include "safewrap.h"
#include "check.h" 

#include <limits.h>
#include "shimpriv.h"

//---------------------------------------------------------------------------------------
// Need virtual dtor since this is a base class.
// Derived classes will do real work
//---------------------------------------------------------------------------------------
ManagedEvent::~ManagedEvent()
{
}

#ifdef _DEBUG
//---------------------------------------------------------------------------------------
// For debugging, get a pointer value that can identify the type of this event.
// 
// Returns:
//    persistent pointer value that can be used as cookie to identify this event type.
//---------------------------------------------------------------------------------------
void * ManagedEvent::GetDebugCookie()
{
    // Return vtable, first void* in the structure.
    return *(reinterpret_cast<void**> (this));
}
#endif

//---------------------------------------------------------------------------------------
// Ctor for DispatchArgs
// 
// Arguments:
//    pCallback1 - 1st callback, for debug events in V1.0, V1.1
//    pCallback2 - 2nd callback, for debug events added in V2
//
// Notes:
//    We'll have a lot of derived classes of ManagedEvent, and so encapsulating the arguments
//    for the Dispatch() function lets us juggle them around easily without hitting every signature.
//---------------------------------------------------------------------------------------
ManagedEvent::DispatchArgs::DispatchArgs(ICorDebugManagedCallback * pCallback1, ICorDebugManagedCallback2 * pCallback2, ICorDebugManagedCallback3 * pCallback3)
{
    m_pCallback1 = pCallback1;
    m_pCallback2 = pCallback2;
    m_pCallback3 = pCallback3;
}


// trivial accessor to get Callback 1
ICorDebugManagedCallback * ManagedEvent::DispatchArgs::GetCallback1()
{
    return m_pCallback1;
}

// trivial accessor to get callback 2
ICorDebugManagedCallback2 * ManagedEvent::DispatchArgs::GetCallback2()
{
    return m_pCallback2;
}

// trivial accessor to get callback 3
ICorDebugManagedCallback3 * ManagedEvent::DispatchArgs::GetCallback3()
{
    return m_pCallback3;
}

// Returns OS Thread Id that this event occured on, 0 if no thread affinity.
DWORD ManagedEvent::GetOSTid()
{
    return m_dwThreadId;
}

//---------------------------------------------------------------------------------------
// Constructore for events with thread affinity
// 
// Arguments:
//     pThread -  thread that this event is associated with. 
//
// Notes:
//     Thread affinity is used with code:ManagedEventQueue::HasQueuedCallbacks
//     This includes event callbacks that have a thread parameter
//---------------------------------------------------------------------------------------
ManagedEvent::ManagedEvent(ICorDebugThread * pThread)
{
    m_dwThreadId = 0;
    if (pThread != NULL)
    {
        pThread->GetID(&m_dwThreadId);
    }        
    
    m_pNext = NULL;
}

//---------------------------------------------------------------------------------------
// Constructor for events with no thread affinity
//---------------------------------------------------------------------------------------
ManagedEvent::ManagedEvent()
{
    m_dwThreadId = 0;
    m_pNext = NULL;
}

    




// Ctor
ManagedEventQueue::ManagedEventQueue()
{
    m_pFirstEvent = NULL;
    m_pLastEvent = NULL;
    m_pLock = NULL;
}

//---------------------------------------------------------------------------------------
// Initialize
//
// Arguments:
//    pLock - lock that protects this event queue. This takes a weak ref to the lock,
//            so caller ensures lock stays alive for lifespan of this object
// 
// Notes:
//    Event queue locks itself using this lock.
//    Only call this once. 
//---------------------------------------------------------------------------------------
void ManagedEventQueue::Init(RSLock * pLock)
{
    _ASSERTE(m_pLock == NULL);
    m_pLock = pLock;
}

//---------------------------------------------------------------------------------------    
// Remove event from the top. 
//
// Returns: 
//    Event that was just dequeued. 
//
// Notes:
//    Caller then takes ownership of Event and will call Delete on it.
//    If IsEmpty() function returns NULL.
//    
//    It is an error to call Dequeue when the only elements in the queue are suspended.
//    Suspending the queue implies there are going to be new events added which should come before
//    the elements that are suspended.  Trying to deqeue when there are only suspended elements
//    left is error-prone - if it were allowed, the order may be non-deterministic.
//    In practice we could probably ban calling Dequeue at all when any elements are suspended,
//    but this seems overly restrictive - there is nothing wrong with allowing these "new"
//    events to be dequeued since we know they come first (you can't nest suspensions).
//---------------------------------------------------------------------------------------
ManagedEvent * ManagedEventQueue::Dequeue()
{
    RSLockHolder lockHolder(m_pLock);
    if (m_pFirstEvent == NULL)
    {
        return NULL;
    }
    
    ManagedEvent * pEvent = m_pFirstEvent;
    m_pFirstEvent = m_pFirstEvent->m_pNext;
    if (m_pFirstEvent == NULL)
    {
        m_pLastEvent = NULL;
    }

    pEvent->m_pNext = NULL;
    return pEvent;
}

//---------------------------------------------------------------------------------------
// Append the event to the end of the queue.
// Queue owns the event and will delete it (unless it's dequeued first).
// 
// Note that this can be called when a suspended queue is active.  Events are pushed onto 
// the currently active queue (ahead of the suspended queue).
//
// Arguments:
//     pEvent - event to queue.
//
//---------------------------------------------------------------------------------------
void ManagedEventQueue::QueueEvent(ManagedEvent * pEvent)
{
    RSLockHolder lockHolder(m_pLock);
    _ASSERTE(pEvent != NULL);
    _ASSERTE(pEvent->m_pNext == NULL);
    
    if (m_pLastEvent == NULL)
    {
        _ASSERTE(m_pFirstEvent == NULL);
        m_pFirstEvent = m_pLastEvent = pEvent;
    }
    else
    {
        m_pLastEvent->m_pNext = pEvent;
        m_pLastEvent = pEvent;
    }
}


//---------------------------------------------------------------------------------------
// Returns true iff the event queue is empty (including any suspended queue elements)
//---------------------------------------------------------------------------------------
bool ManagedEventQueue::IsEmpty()
{
    RSLockHolder lockHolder(m_pLock);
    if (m_pFirstEvent != NULL)
    {
        _ASSERTE(m_pLastEvent != NULL);
        return false;
    }

    _ASSERTE(m_pLastEvent == NULL);
    return true;
}


//---------------------------------------------------------------------------------------
// Delete all events and empty the queue (including any suspended queue elements)
//
// Notes:
//    This is like calling { while(!IsEmpty()) delete Dequeue(); }
//---------------------------------------------------------------------------------------
void ManagedEventQueue::DeleteAll()
{
    RSLockHolder lockHolder(m_pLock);

    while (m_pFirstEvent != NULL)
    {
        // verify that the last event in the queue is actually the one stored as the last event
        _ASSERTE( m_pFirstEvent->m_pNext != NULL || m_pFirstEvent == m_pLastEvent );

        ManagedEvent * pNext = m_pFirstEvent->m_pNext;
        delete m_pFirstEvent;
        m_pFirstEvent = pNext;
    }
    m_pLastEvent = NULL;

    _ASSERTE(IsEmpty());
};

//---------------------------------------------------------------------------------------
// Worker to implement ICorDebugProcess::HasQueuedCallbacks for shim
//---------------------------------------------------------------------------------------
BOOL ManagedEventQueue::HasQueuedCallbacks(ICorDebugThread * pThread)
{
    // This is from the public paths of ICorDebugProcess::HasQueuedCallbacks.
    // In V2, this would fail in cases, notably including if the process is not synchronized.
    // In arrowhead, it always succeeds.

    // No thread - look process wide.
    if (pThread == NULL)
    {
        return !IsEmpty();
    }

    // If we have a thread, look for events with thread affinity.
    DWORD dwThreadID = 0;
    HRESULT hr = pThread->GetID(&dwThreadID);
    (void)hr; //prevent "unused variable" error from GCC
    SIMPLIFYING_ASSUMPTION(SUCCEEDED(hr));

    // Don't take lock until after we don't call any ICorDebug APIs.
    RSLockHolder lockHolder(m_pLock);

    ManagedEvent * pCurrent = m_pFirstEvent;
    while (pCurrent != NULL)
    {
        if (pCurrent->GetOSTid() == dwThreadID)
        {
            return true;
        }
        pCurrent = pCurrent->m_pNext;
    }
    return false;
}