-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathdebugutil.cpp
465 lines (383 loc) · 12.3 KB
/
debugutil.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
#include "debugutil.h"
#include <array>
#include <string>
#include "dbgutil.h"
#include "stringu.h"
#include "stringa.h"
#include "Environment.h"
#include "SRWExclusiveLock.h"
#include "exceptions.h"
#include "atlbase.h"
#include "config_utility.h"
#include "StringHelpers.h"
#include "aspnetcore_msg.h"
#include "EventLog.h"
inline HANDLE g_logFile = INVALID_HANDLE_VALUE;
inline HMODULE g_hModule;
inline SRWLOCK g_logFileLock;
inline HANDLE g_stdOutHandle = INVALID_HANDLE_VALUE;
std::wstring GetDateTime()
{
std::chrono::milliseconds milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(milliseconds);
milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(milliseconds - seconds);
time_t t = seconds.count();
tm time;
// convert time to utc
gmtime_s(&time, &t);
constexpr auto size = sizeof("2019-11-23T13:23:02.000Z");
wchar_t timeString[size];
// format string to ISO8601 with additional space for 3 digits of millisecond precision
std::wcsftime(timeString, size, L"%FT%T.000Z", &time);
// add millisecond part
// 5 = 3 digits of millisecond precision + 'Z' + null character ending
swprintf(timeString + size - 5, 5, L"%03dZ", (int)milliseconds.count());
return std::wstring(timeString);
}
HRESULT
PrintDebugHeader()
{
// Major, minor are stored in dwFileVersionMS field and patch, build in dwFileVersionLS field as pair of 32 bit numbers
LOG_INFOF(L"Initializing logs for '%ls'. %ls %ls.",
GetModuleName().c_str(),
GetProcessIdString().c_str(),
GetVersionInfoString().c_str()
);
return S_OK;
}
std::wstring
GetProcessIdString()
{
return format(L"Process Id: %u.", GetCurrentProcessId());
}
std::wstring
GetVersionInfoString()
{
auto func = [](std::wstring& res)
{
DWORD verHandle = 0;
UINT size = 0;
LPVOID lpBuffer = NULL;
auto path = GetModuleName();
DWORD verSize = GetFileVersionInfoSize(path.c_str(), &verHandle);
RETURN_LAST_ERROR_IF(verSize == 0);
// Allocate memory to hold data structure returned by GetFileVersionInfo
std::vector<BYTE> verData(verSize);
RETURN_LAST_ERROR_IF(!GetFileVersionInfo(path.c_str(), verHandle, verSize, verData.data()));
RETURN_LAST_ERROR_IF(!VerQueryValue(verData.data(), L"\\", &lpBuffer, &size));
auto verInfo = reinterpret_cast<VS_FIXEDFILEINFO *>(lpBuffer);
if (verInfo->dwSignature != VS_FFI_SIGNATURE)
{
RETURN_IF_FAILED(E_FAIL);
}
LPVOID pvProductName = NULL;
unsigned int iProductNameLen = 0;
RETURN_LAST_ERROR_IF(!VerQueryValue(verData.data(), _T("\\StringFileInfo\\040904b0\\FileDescription"), &pvProductName, &iProductNameLen));
res = format(L"File Version: %d.%d.%d.%d. Description: %s",
(verInfo->dwFileVersionMS >> 16) & 0xffff,
(verInfo->dwFileVersionMS >> 0) & 0xffff,
(verInfo->dwFileVersionLS >> 16) & 0xffff,
(verInfo->dwFileVersionLS >> 0) & 0xffff,
pvProductName);
return S_OK;
};
std::wstring versionInfoString;
return func(versionInfoString) == S_OK ? versionInfoString : L"";
}
std::wstring
GetModuleName()
{
WCHAR path[MAX_PATH];
LOG_LAST_ERROR_IF(!GetModuleFileName(g_hModule, path, sizeof(path) / sizeof(WCHAR)));
return path;
}
void SetDebugFlags(const std::wstring &debugValue)
{
try
{
std::wstringstream stringStream(debugValue);
std::wstring flag;
while (std::getline(stringStream, flag, L','))
{
if (_wcsnicmp(flag.c_str(), L"error", wcslen(L"error")) == 0) DEBUG_FLAGS_VAR |= DEBUG_FLAGS_ERROR;
if (_wcsnicmp(flag.c_str(), L"warning", wcslen(L"warning")) == 0) DEBUG_FLAGS_VAR |= DEBUG_FLAGS_WARN;
if (_wcsnicmp(flag.c_str(), L"info", wcslen(L"info")) == 0) DEBUG_FLAGS_VAR |= DEBUG_FLAGS_INFO;
if (_wcsnicmp(flag.c_str(), L"trace", wcslen(L"trace")) == 0) DEBUG_FLAGS_VAR |= DEBUG_FLAGS_TRACE;
if (_wcsnicmp(flag.c_str(), L"console", wcslen(L"console")) == 0) DEBUG_FLAGS_VAR |= ASPNETCORE_DEBUG_FLAG_CONSOLE;
if (_wcsnicmp(flag.c_str(), L"file", wcslen(L"file")) == 0) DEBUG_FLAGS_VAR |= ASPNETCORE_DEBUG_FLAG_FILE;
if (_wcsnicmp(flag.c_str(), L"eventlog", wcslen(L"eventlog")) == 0) DEBUG_FLAGS_VAR |= ASPNETCORE_DEBUG_FLAG_EVENTLOG;
}
// If file or console is enabled but level is not set, enable levels up to info
if (DEBUG_FLAGS_VAR != 0 && (DEBUG_FLAGS_VAR & DEBUG_FLAGS_ANY) == 0)
{
DEBUG_FLAGS_VAR |= DEBUG_FLAGS_INFO;
}
}
catch (...)
{
// ignore
}
}
bool CreateDebugLogFile(const std::filesystem::path &debugOutputFile)
{
try
{
if (!debugOutputFile.empty())
{
if (g_logFile != INVALID_HANDLE_VALUE)
{
LOG_INFOF(L"Switching debug log files to '%ls'", debugOutputFile.c_str());
}
SRWExclusiveLock lock(g_logFileLock);
if (g_logFile != INVALID_HANDLE_VALUE)
{
CloseHandle(g_logFile);
g_logFile = INVALID_HANDLE_VALUE;
}
// ignore errors
std::error_code ec;
create_directories(debugOutputFile.parent_path(), ec);
g_logFile = CreateFileW(debugOutputFile.c_str(),
(GENERIC_READ | GENERIC_WRITE),
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
nullptr,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
return true;
}
}
catch (...)
{
// ignore
}
return false;
}
VOID
DebugInitialize(HMODULE hModule)
{
g_hModule = hModule;
DuplicateHandle(
/* hSourceProcessHandle*/ GetCurrentProcess(),
/* hSourceHandle */ GetStdHandle(STD_OUTPUT_HANDLE),
/* hTargetProcessHandle */ GetCurrentProcess(),
/* lpTargetHandle */&g_stdOutHandle,
/* dwDesiredAccess */ 0, // dwDesired is ignored if DUPLICATE_SAME_ACCESS is specified
/* bInheritHandle */ TRUE,
/* dwOptions */ DUPLICATE_SAME_ACCESS);
HKEY hKey;
InitializeSRWLock(&g_logFileLock);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\IIS Extensions\\IIS AspNetCore Module V2\\Parameters",
0,
KEY_READ,
&hKey) == NO_ERROR)
{
DWORD dwType;
DWORD dwData;
DWORD cbData;
cbData = sizeof(dwData);
if ((RegQueryValueEx(hKey,
L"DebugFlags",
NULL,
&dwType,
(LPBYTE)&dwData,
&cbData) == NO_ERROR) &&
(dwType == REG_DWORD))
{
DEBUG_FLAGS_VAR = dwData;
}
RegCloseKey(hKey);
}
try
{
SetDebugFlags(Environment::GetEnvironmentVariableValue(L"ASPNETCORE_MODULE_DEBUG").value_or(L""));
}
catch (...)
{
// ignore
}
try
{
const auto debugOutputFile = Environment::GetEnvironmentVariableValue(L"ASPNETCORE_MODULE_DEBUG_FILE");
CreateDebugLogFile(debugOutputFile.value_or(L""));
}
catch (...)
{
// ignore
}
if (IsDebuggerPresent())
{
DEBUG_FLAGS_VAR |= DEBUG_FLAGS_INFO;
}
PrintDebugHeader();
}
HRESULT
DebugInitializeFromConfig(IHttpServer& pHttpServer, IHttpApplication& pHttpApplication)
{
auto oldFlags = DEBUG_FLAGS_VAR;
CComPtr<IAppHostElement> pAspNetCoreElement;
const CComBSTR bstrAspNetCoreSection = L"system.webServer/aspNetCore";
CComBSTR bstrConfigPath = pHttpApplication.GetAppConfigPath();
RETURN_IF_FAILED(pHttpServer.GetAdminManager()->GetAdminSection(bstrAspNetCoreSection,
bstrConfigPath,
&pAspNetCoreElement));
STRU debugFile;
RETURN_IF_FAILED(ConfigUtility::FindDebugFile(pAspNetCoreElement, debugFile));
STRU debugValue;
RETURN_IF_FAILED(ConfigUtility::FindDebugLevel(pAspNetCoreElement, debugValue));
SetDebugFlags(debugValue.QueryStr());
if (debugFile.QueryCCH() == 0 && IsEnabled(ASPNETCORE_DEBUG_FLAG_FILE))
{
debugFile.Append(L".\\aspnetcore-debug.log");
}
std::filesystem::path filePath = std::filesystem::path(debugFile.QueryStr());
if (!filePath.empty() && filePath.is_relative())
{
filePath = std::filesystem::path(pHttpApplication.GetApplicationPhysicalPath()) / filePath;
}
const auto reopenedFile = CreateDebugLogFile(filePath);
// Print header if flags changed
if (oldFlags != DEBUG_FLAGS_VAR || reopenedFile)
{
PrintDebugHeader();
}
return S_OK;
}
VOID
DebugStop()
{
if (g_logFile != INVALID_HANDLE_VALUE)
{
CloseHandle(g_logFile);
}
if (g_stdOutHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(g_stdOutHandle);
}
}
BOOL
IsEnabled(
DWORD dwFlag
)
{
return ( dwFlag & DEBUG_FLAGS_VAR );
}
void WriteFileEncoded(UINT codePage, HANDLE hFile, const LPCWSTR szString)
{
DWORD nBytesWritten = 0;
auto const encodedByteCount = WideCharToMultiByte(codePage, 0, szString, -1, nullptr, 0, nullptr, nullptr);
auto encodedBytes = std::shared_ptr<CHAR[]>(new CHAR[encodedByteCount]);
WideCharToMultiByte(codePage, 0, szString, -1, encodedBytes.get(), encodedByteCount, nullptr, nullptr);
WriteFile(hFile, encodedBytes.get(), encodedByteCount - 1, &nBytesWritten, nullptr);
}
VOID
DebugPrintW(
DWORD dwFlag,
const LPCWSTR szString
)
{
STACK_STRU (strOutput, 256);
HRESULT hr = S_OK;
if ( IsEnabled( dwFlag ) )
{
auto time = GetDateTime();
hr = strOutput.SafeSnwprintf(
L"[%s, PID: %u] [%S] %s\r\n",
time.c_str(), GetCurrentProcessId(), DEBUG_LABEL_VAR, szString );
if (FAILED (hr))
{
return;
}
OutputDebugString( strOutput.QueryStr() );
if (IsEnabled(ASPNETCORE_DEBUG_FLAG_CONSOLE))
{
WriteFileEncoded(GetConsoleOutputCP(), g_stdOutHandle, strOutput.QueryStr());
}
if (g_logFile != INVALID_HANDLE_VALUE)
{
SRWExclusiveLock lock(g_logFileLock);
SetFilePointer(g_logFile, 0, nullptr, FILE_END);
WriteFileEncoded(CP_UTF8, g_logFile, strOutput.QueryStr());
FlushFileBuffers(g_logFile);
}
if (IsEnabled(ASPNETCORE_DEBUG_FLAG_EVENTLOG))
{
WORD eventType;
switch (dwFlag)
{
case ASPNETCORE_DEBUG_FLAG_ERROR:
eventType = EVENTLOG_ERROR_TYPE;
break;
case ASPNETCORE_DEBUG_FLAG_WARNING:
eventType = EVENTLOG_WARNING_TYPE;
break;
default:
eventType = EVENTLOG_INFORMATION_TYPE;
break;
}
EventLog::LogEventNoTrace(eventType, ASPNETCORE_EVENT_DEBUG_LOG, strOutput.QueryStr());
}
}
}
VOID
DebugPrintfW(
DWORD dwFlag,
const LPCWSTR szFormat,
...
)
{
STACK_STRU (strCooked,256);
va_list args;
HRESULT hr = S_OK;
if ( IsEnabled( dwFlag ) )
{
va_start( args, szFormat );
hr = strCooked.SafeVsnwprintf(szFormat, args );
va_end( args );
if (FAILED (hr))
{
return;
}
DebugPrintW( dwFlag, strCooked.QueryStr() );
}
}
VOID
DebugPrint(
DWORD dwFlag,
const LPCSTR szString
)
{
STACK_STRU (strOutput, 256);
if ( IsEnabled( dwFlag ) )
{
strOutput.CopyA(szString);
DebugPrintW(dwFlag, strOutput.QueryStr());
}
}
VOID
DebugPrintf(
DWORD dwFlag,
const LPCSTR szFormat,
...
)
{
STACK_STRA (strCooked,256);
va_list args;
HRESULT hr = S_OK;
if ( IsEnabled( dwFlag ) )
{
va_start( args, szFormat );
hr = strCooked.SafeVsnprintf(szFormat, args );
va_end( args );
if (FAILED (hr))
{
return;
}
DebugPrint( dwFlag, strCooked.QueryStr() );
}
}