summaryrefslogtreecommitdiff
path: root/src/vm/perfinfo.cpp
blob: 7075137010f00fc2c0055a05088c1384015b1658 (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
// 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: perfinfo.cpp
//

#include "common.h"

#if defined(FEATURE_PERFMAP) && !defined(DACCESS_COMPILE)
#include "perfinfo.h"
#include "pal.h"

PerfInfo::PerfInfo(int pid)
  : m_Stream(nullptr)
{
    LIMITED_METHOD_CONTRACT;

    SString tempPath;
    if (!WszGetTempPath(tempPath))
    {
        return;
    }

    SString path;
    path.Printf("%Sperfinfo-%d.map", tempPath.GetUnicode(), pid);
    OpenFile(path);
}

// Logs image loads into the process' perfinfo-%d.map file
void PerfInfo::LogImage(PEFile* pFile, WCHAR* guid)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_PREEMPTIVE;
        PRECONDITION(pFile != nullptr);
        PRECONDITION(guid != nullptr);
    } CONTRACTL_END;

    SString value;
    const SString& path = pFile->GetPath();
    PEImageLayout *pLoadedLayout = pFile->GetLoaded();
    SIZE_T baseAddr = (SIZE_T)pLoadedLayout->GetBase();
    value.Printf("%S%c%S%c%p", path.GetUnicode(), sDelimiter, guid, sDelimiter, baseAddr);

    SString command;
    command.Printf("%s", "ImageLoad");
    WriteLine(command, value);

}

// Writes a command line, with "type" being the type of command, with "value" as the command's corresponding instructions/values. This is to be used to log specific information, e.g. LogImage
void PerfInfo::WriteLine(SString& type, SString& value)
{

    CONTRACTL    
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_PREEMPTIVE;
    } CONTRACTL_END;

    if (m_Stream == nullptr)
    {
        return;
    }

    SString line;
    line.Printf("%S%c%S%c\n",
            type.GetUnicode(), sDelimiter, value.GetUnicode(), sDelimiter);

    EX_TRY
    {
        StackScratchBuffer scratch;
        const char* strLine = line.GetANSI(scratch);
        ULONG inCount = line.GetCount();
        ULONG outCount;

        m_Stream->Write(strLine, inCount, &outCount);

        if (inCount != outCount)
        {
            // error encountered
        }
    }
    EX_CATCH{} EX_END_CATCH(SwallowAllExceptions);
}

// Opens a file ready to be written in.
void PerfInfo::OpenFile(SString& path)
{
    STANDARD_VM_CONTRACT;

    m_Stream = new (nothrow) CFileStream();

    if (m_Stream != nullptr)
    {
        HRESULT hr = m_Stream->OpenForWrite(path.GetUnicode());
        if (FAILED(hr))
        {
            delete m_Stream;
            m_Stream = nullptr;
        }
    }
}

PerfInfo::~PerfInfo()
{
    LIMITED_METHOD_CONTRACT;

    delete m_Stream;
    m_Stream = nullptr;
}


#endif