-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
perfinfo.cpp
133 lines (99 loc) · 2.71 KB
/
perfinfo.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ===========================================================================
// File: perfinfo.cpp
//
#include "common.h"
#if defined(FEATURE_PERFMAP) && !defined(DACCESS_COMPILE)
#include "perfinfo.h"
#include "pal.h"
PerfInfo::PerfInfo(int pid, const char* basePath)
: m_Stream(nullptr)
{
LIMITED_METHOD_CONTRACT;
SString path;
path.Printf("%s/perfinfo-%d.map", basePath, pid);
OpenFile(path);
}
// Logs image loads into the process' perfinfo-%d.map file
void PerfInfo::LogImage(PEAssembly* pPEAssembly, CHAR* guid)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
PRECONDITION(pPEAssembly != nullptr);
PRECONDITION(guid != nullptr);
} CONTRACTL_END;
SString value;
const SString& path = pPEAssembly->GetPath();
if (path.IsEmpty())
{
return;
}
SIZE_T baseAddr = 0;
if (pPEAssembly->IsReadyToRun())
{
PEImageLayout *pLoadedLayout = pPEAssembly->GetLoadedLayout();
if (pLoadedLayout)
{
baseAddr = (SIZE_T)pLoadedLayout->GetBase();
}
}
value.Printf("%s%c%s%c%p", path.GetUTF8(), 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.GetUTF8(), sDelimiter, value.GetUTF8(), sDelimiter);
EX_TRY
{
const char* strLine = line.GetUTF8();
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