-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathWindowsLocalTest.ps1
206 lines (185 loc) · 7.05 KB
/
WindowsLocalTest.ps1
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
<#
.SYNOPSIS
This script spawns a process with the COR_PROFILER environment variables set. This script assumes the ClrInstrumentationEngine msi is installed.
NOTE: The bitness of the application will dictate whether to use 32-bit or 64-bit for the profiler, instrumentation methods, and raw profiler hook.
.PARAMETER ApplicationPath
Optional, the application to run with the profiler set. By default, will spawn another PowerShell process.
.PARAMETER InstrumentationMethodConfigPath
Required for ParameterSet 'InstrumentationMethod'
Optional for ParameterSet 'RawProfilerHook'
Path to the config file defining the InstrumentationMethod to load.
NOTE: This config file will be set for both the 32-bit and 64-bit environment variable.
.PARAMETER RawProfilerHookGuid
Required for ParameterSet 'RawProfilerHook'
Optional for ParameterSet 'InstrumentationMethod'
The COR_PROFILER guid of a raw profiler.
.PARAMETER RawProfilerHookPath
Required for ParameterSet 'RawProfilerHook'
Optional for ParameterSet 'InstrumentationMethod'
The COR_PROFILER_PATH string for the raw profiler dll.
NOTE: This config file will be set for both the 32-bit and 64-bit environment variable.
.PARAMETER DebugWait
Optional, sets MicrosoftInstrumentationEngine_DebugWait which causes the ClrInstrumentationEngine to wait for a debugger to attach.
.PARAMETER DisableSignatureValidation
Optional, sets MicrosoftInstrumentationEngine_DisableCodeSignatureValidation which allows CLRIE to use unsigned Instrumentation Methods.
.PARAMETER ProxyUseDebug
Optional, forces the CLRIE ProfilerProxy to look at CLRIE versions with "_Debug" suffix.
.PARAMETER ProxyUsePreview
Optional, allows the CLRIE ProfilerProxy to look at both release and preview (with "-build#####" suffix) versions.
#>
[CmdletBinding(DefaultParameterSetName='None')]
param(
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Test-Path $_ })]
[string]
$ApplicationPath = 'powershell',
[Parameter(ParameterSetName='InstrumentationMethod', Mandatory = $true)]
[Parameter(ParameterSetName='RawProfilerHook', Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Test-Path $_ })]
[string]
$InstrumentationMethodConfigPath,
[Parameter(ParameterSetName='InstrumentationMethod', Mandatory = $false)]
[Parameter(ParameterSetName='RawProfilerHook', Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$RawProfilerHookGuid,
[Parameter(ParameterSetName='InstrumentationMethod', Mandatory = $false)]
[Parameter(ParameterSetName='RawProfilerHook', Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Test-Path $_ })]
[string]
$RawProfilerHookPath,
[Parameter(Mandatory = $false)]
[switch]
$DebugWait,
[Parameter(Mandatory = $false)]
[switch]
$DisableSignatureValidation,
[Parameter(Mandatory = $false)]
[switch]
$ProxyUseDebug,
[Parameter(Mandatory = $false)]
[switch]
$ProxyUsePreview
)
# CLRIE configuration variables
$configurationEnvironmentVariables = @(
# COR_PROFILER
@{
name = 'COR_ENABLE_PROFILING'
value = 1
enable = $true
}
@{
name = 'COR_PROFILER'
value = '{324F817A-7420-4E6D-B3C1-143FBED6D855}'
enable = $true
}
@{
name = 'COR_PROFILER_PATH_32'
value = "${env:ProgramFiles(x86)}\Microsoft CLR Instrumentation Engine\Proxy\v1\InstrumentationEngine.ProfilerProxy_x86.dll"
enable = $true
}
@{
name = 'COR_PROFILER_PATH_64'
value = "$env:ProgramFiles\Microsoft CLR Instrumentation Engine\Proxy\v1\InstrumentationEngine.ProfilerProxy_x64.dll"
enable = $true
}
@{
name = 'CORECLR_ENABLE_PROFILING'
value = 1
enable = $true
}
@{
name = 'CORECLR_PROFILER'
value = '{324F817A-7420-4E6D-B3C1-143FBED6D855}'
enable = $true
}
@{
name = 'CORECLR_PROFILER_PATH_32'
value = "${env:ProgramFiles(x86)}\Microsoft CLR Instrumentation Engine\Proxy\v1\InstrumentationEngine.ProfilerProxy_x86.dll"
enable = $true
}
@{
name = 'CORECLR_PROFILER_PATH_64'
value = "$env:ProgramFiles\Microsoft CLR Instrumentation Engine\Proxy\v1\InstrumentationEngine.ProfilerProxy_x64.dll"
enable = $true
}
# CLRIE-specific
@{
name = "MicrosoftInstrumentationEngine_DebugWait"
value = 1
enable = $DebugWait
}
@{
name = 'MicrosoftInstrumentationEngine_DisableCodeSignatureValidation'
value = 1
enable = $DisableSignatureValidation
}
@{
name = 'MicrosoftInstrumentationEngine_LogLevel'
value = 'Errors'
enable = $true
}
# Proxy-specific
@{
name = 'InstrumentationEngineProxy_UseDebug'
value = 1
enable = $ProxyUseDebug
}
@{
name = 'InstrumentationEngineProxy_UsePreview'
value = 1
enable = $ProxyUsePreview
}
# Instrumentation Method
@{
name = 'MicrosoftInstrumentationEngine_ConfigPath32_TestMethod'
value = $InstrumentationMethodConfigPath
enable = ($PSCmdlet.ParameterSetName -ieq 'InstrumentationMethod')
}
@{
name = 'MicrosoftInstrumentationEngine_ConfigPath64_TestMethod'
value = $InstrumentationMethodConfigPath
enable = ($PSCmdlet.ParameterSetName -ieq 'InstrumentationMethod')
}
# Raw Profiler Hook
@{
name = 'MicrosoftInstrumentationEngine_RawProfilerHook'
value = $RawProfilerHookGuid
enable = ($PSCmdlet.ParameterSetName -ieq 'InstrumentationMethod' -or $PSCmdlet.ParameterSetName -ieq 'RawProfilerHook') -and $RawProfilerHookGuid
}
@{
name = 'MicrosoftInstrumentationEngine_RawProfilerHookPath_32'
value = $RawProfilerHookPath
enable = ($PSCmdlet.ParameterSetName -ieq 'InstrumentationMethod' -or $PSCmdlet.ParameterSetName -ieq 'RawProfilerHook') -and $RawProfilerHookPath
}
@{
name = 'MicrosoftInstrumentationEngine_RawProfilerHookPath_64'
value = $RawProfilerHookPath
enable = ($PSCmdlet.ParameterSetName -ieq 'InstrumentationMethod' -or $PSCmdlet.ParameterSetName -ieq 'RawProfilerHook') -and $RawProfilerHookPath
}
)
$configurationEnvironmentVariables | ForEach-Object {
$envVarPath = "Env:\$($_.name)"
if ($_.enable) {
Write-Verbose "Set $($_.name) = $($_.value)"
New-Item -Path $envVarPath -Value $_.value -Force | Out-Null
} elseif (Test-Path $envVarPath) {
Write-Verbose "Removed $($_.name)"
Remove-Item -Path $envVarPath
}
}
Start-Process $ApplicationPath
# This reverts the state of the caller to not be profiled
$configurationEnvironmentVariables | ForEach-Object {
$envVarPath = "Env:\$($_.name)"
if (Test-Path $envVarPath) {
Write-Verbose "Removed $($_.name)"
Remove-Item -Path $envVarPath
}
}