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

// Common cross-platform behavior of reg sets.
// Platform specific stuff is in CordbRegisterSet.cpp located in
// the platform sub-dir.
//
//*****************************************************************************
#include "stdafx.h"
#include "primitives.h"

/* ------------------------------------------------------------------------- *
 * Common (cross-platform) Register-Set stuff
 * ------------------------------------------------------------------------- */


CordbRegisterSet::CordbRegisterSet( 
    DebuggerREGDISPLAY * pRegDisplay, 
    CordbThread *        pThread,
    bool fActive, 
    bool fQuickUnwind,
    bool fTakeOwnershipOfDRD /*= false*/)
  : CordbBase(pThread->GetProcess(), 0, enumCordbRegisterSet)
{
    _ASSERTE( pRegDisplay != NULL );
    _ASSERTE( pThread != NULL );
    m_rd          = pRegDisplay;
    m_thread      = pThread;
    m_active      = fActive;
    m_quickUnwind = fQuickUnwind;

    m_fTakeOwnershipOfDRD = fTakeOwnershipOfDRD;

    // Add to our parent thread's neuter list.
    
    HRESULT hr = S_OK;
    EX_TRY
    {
        pThread->GetRefreshStackNeuterList()->Add(GetProcess(), this);
    } 
    EX_CATCH_HRESULT(hr);
    SetUnrecoverableIfFailed(GetProcess(), hr);
}

void CordbRegisterSet::Neuter()
{
    m_thread = NULL;
    if (m_fTakeOwnershipOfDRD)
    {
        delete m_rd;
    }
    m_rd = NULL;
    
    CordbBase::Neuter();
}

CordbRegisterSet::~CordbRegisterSet()
{
    _ASSERTE(this->IsNeutered());
}


HRESULT CordbRegisterSet::QueryInterface(REFIID riid, void **ppInterface)
{
    // <NOTE>
    // This is an exception to the rule that a QI for a higher version API should fail if
    // the debugger does not support that version of the API.  The reasoning is that
    // while higher versions of other APIs support enhanced functionality and are not
    // required, this particular API is required on IA64.  An example scenario is when an
    // Everett debuggger is ported to Whidbey and the user wants to use the debugger on IA64.
    // The user should not be required to implement the ICorDebugManagedCallback2 API, as would
    // be the case if we make the versioning check like other higher version APIs.
    // </NOTE>
    if (riid == IID_ICorDebugRegisterSet)
    {
        *ppInterface = static_cast<ICorDebugRegisterSet*>(this);
    }
    else if (riid == IID_ICorDebugRegisterSet2)
    {
        *ppInterface = static_cast<ICorDebugRegisterSet2*>(this);
    }
    else if (riid == IID_IUnknown)
    {
        *ppInterface = static_cast<IUnknown*>(static_cast<ICorDebugRegisterSet*>(this));
    }
    else
    {
        *ppInterface = NULL;
        return E_NOINTERFACE;
    }

    ExternalAddRef();
    return S_OK;
}

//-----------------------------------------------------------------------------
// This is just a convenience function to convert a regdisplay into a Context.
// Since a context has more info than a regdisplay, the conversion isn't perfect
// and the context can't be fully accurate.
//
// Inputs:
//    contextSize - sizeof incoming context buffer  in bytes
//    context - buffer to copy this regdisplay's OS CONTEXT structure into.
//
// Returns S_OK on success. 
//-----------------------------------------------------------------------------
HRESULT CordbRegisterSet::GetThreadContext(ULONG32 contextSize, BYTE context[])
{
    PUBLIC_REENTRANT_API_ENTRY(this);
    FAIL_IF_NEUTERED(this);
    ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());

    HRESULT hr = S_OK;
    EX_TRY
    {
        _ASSERTE( m_thread != NULL );
        if( contextSize < sizeof( DT_CONTEXT ))
        {
            ThrowHR(E_INVALIDARG);
        }        

        ValidateOrThrow(context);

        DT_CONTEXT *pInputContext = reinterpret_cast<DT_CONTEXT *> (context);
        
        // Just to be safe, zero out the buffer we got in while preserving the ContextFlags.  
        // On X64 the ContextFlags field is not the first 4 bytes of the DT_CONTEXT.
        DWORD dwContextFlags = pInputContext->ContextFlags;
        ZeroMemory(context, contextSize);
        pInputContext->ContextFlags = dwContextFlags;

        // Augment the leafmost (active) register w/ information from the current context.
        DT_CONTEXT * pLeafContext = NULL;
        if (m_active)
        {
            EX_TRY
            {
                // This may fail, but it is not a disastrous failure in this case.  All we care is whether
                // pLeafContext is updated to a non-NULL value.
                m_thread->GetManagedContext( &pLeafContext);
            }
            EX_CATCH
            {
            }
            EX_END_CATCH(SwallowAllExceptions)

            if (pLeafContext != NULL)
            {
                // @todo - shouldn't this be a context-flags sensitive copy?
                memmove( pInputContext, pLeafContext, sizeof( DT_CONTEXT) );
            }
        }


        // Now update the registers based on the current frame.
        // This is a very platform specific action.
        InternalCopyRDToContext(pInputContext);
    }
    EX_CATCH_HRESULT(hr);
    return hr;
}

//-----------------------------------------------------------------------------
// Helpers to impl IRegSet2 on top of original IRegSet.
// These are useful on platforms that don't need IRegSet2 (like x86 + amd64). 
// See CorDebug.idl for details.
//
// Inputs:
//   regCount - size of pAvailable buffer in bytes
//   pAvailable - buffer to hold bitvector of available registers. 
//                On success, bit at position CorDebugRegister is 1 iff that
//                register is available.
// Returns S_OK on success.
//-----------------------------------------------------------------------------
HRESULT CordbRegisterSet::GetRegistersAvailableAdapter(
    ULONG32 regCount, 
    BYTE    pAvailable[])
{
    // Defer to call on v1.0 interface
    HRESULT hr = S_OK;

    if (regCount < sizeof(ULONG64))
    {
        return E_INVALIDARG;
    }

    _ASSERTE(pAvailable != NULL);

    ULONG64 availRegs;
    hr = this->GetRegistersAvailable(&availRegs);
    if (FAILED(hr))
    {
        return hr;
    }

    // Nor marshal our 64-bit value into the outgoing byte array.
    for(int iBit = 0; iBit < (int) sizeof(availRegs) * 8; iBit++)
    {
        ULONG64 test = SETBITULONG64(iBit);
        if (availRegs & test)
        {
            SET_BIT_MASK(pAvailable, iBit);
        }
        else
        {
            RESET_BIT_MASK(pAvailable, iBit);
        }
    }
    return S_OK;
}

//-----------------------------------------------------------------------------
// Helpers to impl IRegSet2 on top of original IRegSet.
// These are useful on platforms that don't need IRegSet2 (like x86 + amd64). 
// See CorDebug.idl for details.
//
// Inputs:
//  maskCount - size of mask buffer in bytes.
//  mask - input buffer specifying registers to request
//  regCount - size of regBuffer in bytes
//  regBuffer - output buffer, regBuffer[n] = value of register at n-th active
//              bit in mask.
// Returns S_OK on success.
//-----------------------------------------------------------------------------

// mask input requrest registers, which get written to regCount buffer.
HRESULT CordbRegisterSet::GetRegistersAdapter(
    ULONG32 maskCount, BYTE mask[], 
    ULONG32 regCount, CORDB_REGISTER regBuffer[])
{
    // Convert input mask to orig mask.
    ULONG64 maskOrig = 0;

    for(UINT iBit = 0; iBit < maskCount * 8; iBit++)
    {
        if (IS_SET_BIT_MASK(mask, iBit))
        {
            maskOrig |= SETBITULONG64(iBit);
        }
    }
    
    return this->GetRegisters(maskOrig, 
        regCount, regBuffer);
}