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

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

    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
    {
        THROWS;
        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(CommonEventFields &commonFields, SString &message, StackContents &stackContents)
{
    STANDARD_VM_CONTRACT;

    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(commonFields.TimeStamp.QuadPart != m_fileOpenTimeStamp.QuadPart)
    {
        LARGE_INTEGER elapsedNanoseconds;
        elapsedNanoseconds.QuadPart = commonFields.TimeStamp.QuadPart - m_fileOpenTimeStamp.QuadPart;
        millisecondsSinceTraceStart = elapsedNanoseconds.QuadPart / 1000000.0;
    }

    StackScratchBuffer scratch;
    SString threadFrame;
    threadFrame.Printf("Thread (%d)", commonFields.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
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    StackScratchBuffer scratch;
    const char * charStr = str.GetANSI(scratch);
    ULONG inCount = str.GetCount();
    ULONG outCount;
    m_pFileStream->Write(charStr, inCount, &outCount);

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

void EventPipeJsonFile::FormatCallStack(StackContents &stackContents, SString &resultStr)
{
    STANDARD_VM_CONTRACT;

    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);
    }
}