summaryrefslogtreecommitdiff
path: root/src/vm/i386/profiler.cpp
blob: 11d4247aefb004910252c8c4a760e33e245ec092 (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
// 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: profiler.cpp
//

// 

// 
// ======================================================================================

#include "common.h"

#ifdef PROFILING_SUPPORTED
#include "proftoeeinterfaceimpl.h"

//
// The following structure is the format on x86 builds of the data
// being passed in plaformSpecificHandle for ProfileEnter/Leave/Tailcall
//
typedef struct _PROFILE_PLATFORM_SPECIFIC_DATA
{
    FunctionID functionId;
    DWORD    doubleBuffer1;
    DWORD    doubleBuffer2;
    DWORD    floatBuffer;
    DWORD    floatingPointValuePresent;
    UINT_PTR eax; // eax and edx must be continuous in this structure to make getting 64 bit return values easier.
    UINT_PTR edx;
    UINT_PTR ecx;
    UINT_PTR esp;
    UINT_PTR ip;
} PROFILE_PLATFORM_SPECIFIC_DATA, *PPROFILE_PLATFORM_SPECIFIC_DATA;


/*
 * ProfileGetIPFromPlatformSpecificHandle
 *
 * This routine takes the platformSpecificHandle and retrieves from it the
 * IP value.
 *
 * Parameters:
 *    handle - the platformSpecificHandle passed to ProfileEnter/Leave/Tailcall
 *
 * Returns:
 *    The IP value stored in the handle.
 */
UINT_PTR ProfileGetIPFromPlatformSpecificHandle(void *handle)
{
    LIMITED_METHOD_CONTRACT;

    return ((PROFILE_PLATFORM_SPECIFIC_DATA *)handle)->ip;
}


/*
 * ProfileSetFunctionIDInPlatformSpecificHandle
 *
 * This routine takes the platformSpecificHandle and functionID, and assign 
 * functionID to functionID field of platformSpecificHandle.
 *
 * Parameters:
 *    pPlatformSpecificHandle - the platformSpecificHandle passed to ProfileEnter/Leave/Tailcall
 *    functionID - the FunctionID to be assigned
 *
 * Returns:
 *    None
 */
void ProfileSetFunctionIDInPlatformSpecificHandle(void * pPlatformSpecificHandle, FunctionID functionID)
{
    LIMITED_METHOD_CONTRACT;

    _ASSERTE(pPlatformSpecificHandle != NULL);
    _ASSERTE(functionID != NULL);

    PROFILE_PLATFORM_SPECIFIC_DATA * pData = reinterpret_cast<PROFILE_PLATFORM_SPECIFIC_DATA *>(pPlatformSpecificHandle);
    pData->functionId = functionID;   
}

/*
 * ProfileArgIterator::ProfileArgIterator
 *
 * Constructor. Initializes for arg iteration.
 *
 * Parameters:
 *    pMetaSig - The signature of the method we are going iterate over
 *    platformSpecificHandle - the value passed to ProfileEnter/Leave/Tailcall
 *
 * Returns:
 *    None.
 */
ProfileArgIterator::ProfileArgIterator(MetaSig * pMetaSig, void * platformSpecificHandle):
    m_argIterator(pMetaSig)
{
    //
    // It would be really nice to contract this, but the underlying functions are convolutedly
    // contracted.  Basically everything should be loaded by the time the profiler gets a call
    // back, so everything is NOTHROW/NOTRIGGER, but there is not mechanism for saying that the
    // contracts in called functions should be for the best case, not the worst case, now.
    //
    WRAPPER_NO_CONTRACT;

    m_handle = platformSpecificHandle;
}

/*
 * ProfileArgIterator::~ProfileArgIterator
 *
 * Destructor, releases all resources.
 *
 */
ProfileArgIterator::~ProfileArgIterator()
{
    LIMITED_METHOD_CONTRACT;
}

/*
 * ProfileArgIterator::GetNextArgAddr
 *
 * After initialization, this method is called repeatedly until it
 * returns NULL to get the address of each arg.  Note: this address
 * could be anywhere on the stack.
 *
 * Returns:
 *    Address of the argument, or NULL if iteration is complete.
 */
LPVOID ProfileArgIterator::GetNextArgAddr()
{
    //
    // It would be really nice to contract this, but the underlying functions are convolutedly
    // contracted.  Basically everything should be loaded by the time the profiler gets a call
    // back, so everything is NOTHROW/NOTRIGGER, but there is not mechanism for saying that the
    // contracts in called functions should be for the best case, not the worst case, now.
    //
    WRAPPER_NO_CONTRACT;

    int argOffset = m_argIterator.GetNextOffset();

    //
    // Value is enregistered, figure out where and return that.
    //
    PROFILE_PLATFORM_SPECIFIC_DATA *pData = (PROFILE_PLATFORM_SPECIFIC_DATA *)m_handle;

    //
    // Zero indicates the end of the args.
    //
    if (argOffset == TransitionBlock::InvalidOffset)
    {
        return NULL;
    }

    if (pData == NULL)
    {
        //
        // Something wrong.
        //
        _ASSERTE(!"Why do we have a NULL data pointer here?");
        return NULL;
    }

    //
    // If this is not enregistered, return the value
    //
    if (TransitionBlock::IsStackArgumentOffset(argOffset))
    {
        return ((LPBYTE)pData->esp) + (argOffset - TransitionBlock::GetOffsetOfArgs());
    }

    switch (argOffset - TransitionBlock::GetOffsetOfArgumentRegisters())
    {
    case offsetof(ArgumentRegisters, ECX):
        return &(pData->ecx);
    case offsetof(ArgumentRegisters, EDX):
        return &(pData->edx);
    }

    _ASSERTE(!"Arg is an unsaved register!");
    return NULL;
}

/*
 * ProfileArgIterator::GetHiddenArgValue
 *
 * Called after initialization, any number of times, to retrieve any
 * hidden argument, so that resolution for Generics can be done.
 *
 * Parameters:
 *    None.
 *
 * Returns:
 *    Value of the hidden parameter, or NULL if none exists.
 */
LPVOID ProfileArgIterator::GetHiddenArgValue(void)
{
    //
    // It would be really nice to contract this, but the underlying functions are convolutedly
    // contracted.  Basically everything should be loaded by the time the profiler gets a call
    // back, so everything is NOTHROW/NOTRIGGER, but there is not mechanism for saying that the
    // contracts in called functions should be for the best case, not the worst case, now.
    //
    WRAPPER_NO_CONTRACT;

    PROFILE_PLATFORM_SPECIFIC_DATA *pData = (PROFILE_PLATFORM_SPECIFIC_DATA *)m_handle;

    MethodDesc *pMethodDesc = FunctionIdToMethodDesc(pData->functionId);

    if (!pMethodDesc->RequiresInstArg())
    {
        return NULL;
    }

    //
    // The ArgIterator::GetParamTypeOffset() can only be called after calling GetNextOffset until the
    // entire signature has been walked, but *before* GetNextOffset returns TransitionBlock::InvalidOffset 
    // - indicating the end.
    //

    //
    // Get the offset of the hidden arg
    //
    int argOffset = m_argIterator.GetParamTypeArgOffset();

    //
    // If this is not enregistered, return the value
    //
    if (TransitionBlock::IsStackArgumentOffset(argOffset))
    {
        return *(LPVOID *)(((LPBYTE)pData->esp) + (argOffset - TransitionBlock::GetOffsetOfArgs()));
    }

    switch (argOffset - TransitionBlock::GetOffsetOfArgumentRegisters())
    {
    case offsetof(ArgumentRegisters, ECX):
        return (LPVOID)(pData->ecx);
    case offsetof(ArgumentRegisters, EDX):
        return (LPVOID)(pData->edx);
    }

    _ASSERTE(!"Arg is an unsaved register!");
    return NULL;
}

/*
 * ProfileArgIterator::GetThis
 *
 * Called after initialization, any number of times, to retrieve the
 * value of 'this'.
 *
 * Parameters:
 *    None.
 *
 * Returns:
 *    value of the 'this' parameter, or NULL if none exists.
 */
LPVOID ProfileArgIterator::GetThis(void)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    PROFILE_PLATFORM_SPECIFIC_DATA *pData = (PROFILE_PLATFORM_SPECIFIC_DATA *)m_handle;

    if (pData->ip == 0)
    {
        return NULL;
    }

    if (!m_argIterator.HasThis())
    {
        return NULL;
    }

    switch (offsetof(ArgumentRegisters, THIS_REG))
    {
    case offsetof(ArgumentRegisters, ECX):
        return (LPVOID)pData->ecx;

    case offsetof(ArgumentRegisters, EDX):
        return (LPVOID)pData->edx;
    }

    _ASSERTE(!"This is an unsaved register!");
    return NULL;
}



/*
 * ProfileArgIterator::GetReturnBufferAddr
 *
 * Called after initialization, any number of times, to retrieve the
 * address of the return buffer.  NULL indicates no return value.
 *
 * Parameters:
 *    None.
 *
 * Returns:
 *    Address of the return buffer, or NULL if none exists.
 */
LPVOID ProfileArgIterator::GetReturnBufferAddr(void)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    PROFILE_PLATFORM_SPECIFIC_DATA *pData = (PROFILE_PLATFORM_SPECIFIC_DATA *)m_handle;

    if (m_argIterator.HasRetBuffArg())
    {
        return (void *)(pData->eax);
    }

    switch (m_argIterator.GetSig()->GetReturnType())
    {
    case ELEMENT_TYPE_R8:
        _ASSERTE(pData->floatingPointValuePresent);
        return (void *)(&(pData->doubleBuffer1));

    case ELEMENT_TYPE_R4:
        _ASSERTE(pData->floatingPointValuePresent);
        return (void *)(&(pData->floatBuffer));

    default:
        return &(pData->eax);
    }
}

#endif // PROFILING_SUPPORTED