forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.ps1
249 lines (197 loc) · 8.12 KB
/
log.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<#
.SYNOPSIS
This script provides helpers for starting, stopping and canceling log collection.
.PARAMETER Start
Starts the logs being collected with the given profile.
.PARAMETER Profile
The name of the profile to use for log collection.
.PARAMETER Cancel
Stops the logs from being collected and discards any collected so far.
.PARAMETER Stop
Stops the logs from being collected and saves them to the -Output location.
.PARAMETER Output
The output file name or directory for the logs.
.PARAMETER RawLogOnly
Does not convert the output logs to text. Only keeps raw files.
.PARAMETER InstanceName
A unique name for the logging instance.
.PARAMETER ProfileInScriptDirectory
Flag for if the MsQuic wprp file is in the same directory as the script.
.EXAMPLE
logs.ps1 -Start -Profile Basic.Light
.EXAMPLE
logs.ps1 -Cancel
.EXAMPLE
logs.ps1 -Stop -Output .\quic
#>
param (
[Parameter(Mandatory = $false, ParameterSetName='Start')]
[switch]$Start = $false,
[Parameter(Mandatory = $false, ParameterSetName='Start')]
[switch]$Stream = $false,
[Parameter(Mandatory = $false, ParameterSetName='Start')]
[ValidateSet("Basic.Light", "Datapath.Light", "Datapath.Verbose", "Stacks.Light", "Performance.Light", "Basic.Verbose", "Performance.Light", "Performance.Verbose", "Full.Light", "Full.Verbose", "SpinQuic.Light")]
[string]$Profile = "Full.Light",
[Parameter(Mandatory = $false, ParameterSetName='Cancel')]
[switch]$Cancel = $false,
[Parameter(Mandatory = $false, ParameterSetName='Stop')]
[switch]$Stop = $false,
[Parameter(Mandatory = $true, ParameterSetName='Stop')]
[string]$OutputPath = "",
[Parameter(Mandatory = $false, ParameterSetName='Stop')]
[switch]$RawLogOnly = $false,
[Parameter(Mandatory = $false, ParameterSetName='Stop')]
[string]$TmfPath = "",
[Parameter(Mandatory = $false, ParameterSetName='Decode')]
[switch]$Decode = $false,
[Parameter(Mandatory = $true, ParameterSetName='Decode')]
[string]$LogFile,
[Parameter(Mandatory = $true, ParameterSetName='Decode')]
[string]$WorkingDirectory,
[Parameter(Mandatory = $false)]
[string]$InstanceName = "msquic",
[Parameter(Mandatory = $false)]
[switch]$ProfileInScriptDirectory = $false
)
Set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
# Root directory of the project.
$RootDir = Split-Path $PSScriptRoot -Parent
# Path for the WPR profile.
$WprpFile = $RootDir + "\src\manifest\msquic.wprp"
if ($ProfileInScriptDirectory) {
$WprpFile = Join-Path $PSScriptRoot MsQuic.wprp
}
$SideCar = Join-Path $RootDir "src/manifest/clog.sidecar"
$Clog2Text_lttng = "$HOME/.dotnet/tools/clog2text_lttng"
$TempDir = $null
if ($IsLinux) {
$InstanceName = $InstanceName.Replace(".", "_")
$TempDir = Join-Path $HOME "QUICLogs" $InstanceName
}
# Start log collection.
function Log-Start {
if ($IsWindows) {
wpr.exe -start "$($WprpFile)!$($Profile)" -filemode -instancename $InstanceName 2>&1
} elseif ($IsMacOS) {
} else {
if (Test-Path $TempDir) {
Write-Error "LTTng session ($InstanceName) already running! ($TempDir)"
}
try {
if ($Stream) {
lttng -q create msquiclive --live
} else {
New-Item -Path $TempDir -ItemType Directory -Force | Out-Null
$Command = "lttng create $InstanceName -o=$TempDir"
Invoke-Expression $Command | Write-Debug
}
lttng enable-event --userspace CLOG_* | Write-Debug
lttng add-context --userspace --type=vpid --type=vtid | Write-Debug
lttng start | Write-Debug
if ($Stream) {
lttng list | Write-Debug
babeltrace -i lttng-live net://localhost | Write-Debug
Write-Host "Now decoding LTTng events in realtime...`n"
$args = "babeltrace --names all -i lttng-live net://localhost/host/$env:NAME/msquiclive | $Clog2Text_lttng -s $SideCar --showTimestamp --showCpuInfo"
Write-Host $args
Invoke-Expression $args
}
} finally {
if ($Stream) {
Invoke-Expression "lttng destroy msquiclive" | Write-Debug
}
}
}
}
# Cancels log collection, discarding any logs.
function Log-Cancel {
if ($IsWindows) {
try {
wpr.exe -cancel -instancename $InstanceName 2>&1
} catch {
}
} elseif ($IsMacOS) {
} else {
if (!(Test-Path $TempDir)) {
Write-Debug "LTTng session ($InstanceName) not currently running"
} else {
Invoke-Expression "lttng destroy -n $InstanceName" | Write-Debug
Remove-Item -Path $TempDir -Recurse -Force | Out-Null
Write-Debug "Destroyed LTTng session ($InstanceName) and deleted $TempDir"
}
}
}
# Stops log collection, keeping the logs.
function Log-Stop {
if ($IsWindows) {
$EtlPath = $OutputPath + ".etl"
wpr.exe -stop $EtlPath -instancename $InstanceName 2>&1
if (!$RawLogOnly) {
$LogPath = $OutputPath + ".log"
$Command = "netsh trace convert $($EtlPath) output=$($LogPath) overwrite=yes report=no"
if ($TmfPath -ne "" -and (Test-Path $TmfPath)) {
$Command += " tmfpath=$TmfPath"
}
Invoke-Expression $Command
}
} elseif ($IsMacOS) {
} else {
$ClogOutputDecodeFile = $OutputPath + ".log"
if (!(Test-Path $OutputPath)) {
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
}
if (!(Test-Path $TempDir)) {
Write-Error "LTTng session ($InstanceName) not currently running!"
}
Invoke-Expression "lttng stop $InstanceName" | Write-Debug
$LTTNGTarFile = $OutputPath + ".tgz"
$BableTraceFile = $OutputPath + ".babel.txt"
Write-Host "tar/gzip LTTng log files: $LTTNGTarFile"
tar -cvzf $LTTNGTarFile $TempDir | Write-Debug
if (!$RawLogOnly) {
Write-Debug "Decoding LTTng into BabelTrace format ($BableTraceFile)"
babeltrace --names all $TempDir/* > $BableTraceFile
Write-Host "Decoding into human-readable text: $ClogOutputDecodeFile"
$Command = "$Clog2Text_lttng -i $BableTraceFile -s $SideCar -o $ClogOutputDecodeFile --showTimestamp --showCpuInfo"
Write-Host $Command
Invoke-Expression $Command | Write-Debug
Remove-Item -Path $BableTraceFile -Force | Out-Null
}
Invoke-Expression "lttng destroy $InstanceName" | Write-Debug
Remove-Item -Path $TempDir -Recurse -Force | Out-Null
Write-Debug "Destroyed LTTng session ($InstanceName) and deleted $TempDir"
}
}
# Decodes a log file.
function Log-Decode {
if (!(Test-Path $WorkingDirectory)) {
New-Item -Path $WorkingDirectory -ItemType Directory -Force | Out-Null
}
if ($IsWindows) {
Write-Error "Not supported on Windows"
} elseif ($IsMacOS) {
} else {
Write-Host $LogFile
$DecompressedLogs = Join-Path $WorkingDirectory "DecompressedLogs"
$ClogOutputDecodeFile = Join-Path $WorkingDirectory "clog_decode.txt"
$BableTraceFile = Join-Path $WorkingDirectory "decoded_babeltrace.txt"
mkdir $WorkingDirectory
mkdir $DecompressedLogs
Write-Host "Decompressing $Logfile into $DecompressedLogs"
tar xvfz $Logfile -C $DecompressedLogs
Write-Host "Decoding LTTng into BabelTrace format ($BableTraceFile)"
babeltrace --names all $DecompressedLogs/* > $BableTraceFile
Write-Host "Decoding Babeltrace into human text using CLOG"
$Command = "$Clog2Text_lttng -i $BableTraceFile -s $SideCar -o $ClogOutputDecodeFile"
Write-Host $Command
Invoke-Expression $Command
}
}
##############################################################
# Main Execution #
##############################################################
if ($Start) { Log-Start }
if ($Cancel) { Log-Cancel }
if ($Stop) { Log-Stop }
if ($Decode) { Log-Decode }