summaryrefslogtreecommitdiff
path: root/src/vm/eventpipejsonfile.cpp
blob: aa60df7b070c83a9fe057265409e1457d69fb3fc (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
// 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.

#include "common.h"
#include "eventpipejsonfile.h"
#include "typestring.h"

#ifdef _DEBUG
#ifdef FEATURE_PERFTRACING

EventPipeJsonFile::EventPipeJsonFile(SString &outFilePath)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    m_writeErrorEncountered = false;
    m_pFileStream = new CFileStream();
    if(FAILED(m_pFileStream->OpenForWrite(outFilePath)))
    {
        delete(m_pFileStream);
        m_pFileStream = NULL;
        return;
    }

    QueryPerformanceCounter(&m_fileOpenTimeStamp);

    SString fileStart(W("{\n\"StackSource\" : {\n\"Samples\" : [\n"));
    Write(fileStart);
}

EventPipeJsonFile::~EventPipeJsonFile()
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    if(m_pFileStream != NULL)
    {
        if(!m_writeErrorEncountered)
        {
            SString closingString(W("]}}"));
            Write(closingString);
        }

        delete(m_pFileStream);
        m_pFileStream = NULL;
    }
}

void EventPipeJsonFile::WriteEvent(EventPipeEventInstance &instance)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END; 

    instance.SerializeToJsonFile(this);
}

void EventPipeJsonFile::WriteEvent(LARGE_INTEGER timeStamp, DWORD threadID, SString &message, StackContents &stackContents)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END; 

    if(m_pFileStream == NULL || m_writeErrorEncountered)
    {
        return;
    }

    // Format the call stack.
    SString strCallStack;
    FormatCallStack(stackContents, strCallStack);

    // Convert the timestamp from a QPC value to a trace-relative timestamp.
    double millisecondsSinceTraceStart = 0.0;
    if(timeStamp.QuadPart != m_fileOpenTimeStamp.QuadPart)
    {
        LARGE_INTEGER elapsedNanoseconds;
        elapsedNanoseconds.QuadPart = timeStamp.QuadPart - m_fileOpenTimeStamp.QuadPart;
        millisecondsSinceTraceStart = elapsedNanoseconds.QuadPart / 1000000.0;
    }

    StackScratchBuffer scratch;
    SString threadFrame;
    threadFrame.Printf("Thread (%d)", threadID);
    SString event;
    event.Printf("{\"Time\" : \"%f\", \"Metric\" : \"1\",\n\"Stack\": [\n\"%s\",\n%s\"%s\"]},", millisecondsSinceTraceStart, message.GetANSI(scratch), strCallStack.GetANSI(scratch), threadFrame.GetANSI(scratch));
    Write(event);
}

void EventPipeJsonFile::Write(SString &str)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    StackScratchBuffer scratch;
    const char * charStr = str.GetANSI(scratch);

    EX_TRY
    {
        ULONG inCount = str.GetCount();
        ULONG outCount;

        m_pFileStream->Write(charStr, inCount, &outCount);

        if(inCount != outCount)
        {
            m_writeErrorEncountered = true;
        }
    }
    EX_CATCH
    {
        m_writeErrorEncountered = true;
    }
    EX_END_CATCH(SwallowAllExceptions);
}

void EventPipeJsonFile::FormatCallStack(StackContents &stackContents, SString &resultStr)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END; 

    StackScratchBuffer scratch;
    SString frameStr;
    
    for(unsigned int i=0; i<stackContents.GetLength(); i++)
    {
        // Get the method declaration string.
        MethodDesc *pMethod = stackContents.GetMethod(i);
        _ASSERTE(pMethod != NULL);

        SString mAssemblyName;
        mAssemblyName.SetUTF8(pMethod->GetLoaderModule()->GetAssembly()->GetSimpleName());
        SString fullName;
        TypeString::AppendMethodInternal(
            fullName,
            pMethod,
            TypeString::FormatNamespace | TypeString::FormatSignature);

        frameStr.Printf("\"%s!%s\",\n", mAssemblyName.GetANSI(scratch), fullName.GetANSI(scratch));
        resultStr.Append(frameStr);
    }
}

#endif // _DEBUG
#endif // FEATURE_PERFTRACING