Skip to content

Commit

Permalink
Hook up the WIL fallback error tracer in Terminal (#7864)
Browse files Browse the repository at this point in the history
This pull request introduces (a very, very stripped-down copy of) the
WIL fallback error reporter.

It emits error records, usually immediately before the application
implodes, into the event stream.

This should improve diagnosability of issues that take Terminal down,
and allow us to give out a .wprp file to gather traces from users.

(cherry picked from commit cd76893)
  • Loading branch information
DHowett committed Oct 19, 2020
1 parent 62d22c2 commit ef5198e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/actions/spell-check/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ hpj
hpp
HPR
HPROPSHEETPAGE
HProvider
HREDRAW
hresult
HRSRC
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include <LibraryResources.h>
#include <WilErrorReporting.h>

// Note: Generate GUID using TlgGuid.exe tool
TRACELOGGING_DEFINE_PROVIDER(
Expand All @@ -19,6 +20,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD reason, LPVOID /*reserved*/)
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hInstDll);
TraceLoggingRegister(g_hTerminalAppProvider);
Microsoft::Console::ErrorReporting::EnableFallbackFailureReporting(g_hTerminalAppProvider);
break;
case DLL_PROCESS_DETACH:
if (g_hTerminalAppProvider)
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalConnection/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include <LibraryResources.h>
#include <WilErrorReporting.h>

// Note: Generate GUID using TlgGuid.exe tool
#pragma warning(suppress : 26477) // One of the macros uses 0/NULL. We don't have control to make it nullptr.
Expand All @@ -21,6 +22,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD reason, LPVOID /*reserved*/)
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hInstDll);
TraceLoggingRegister(g_hTerminalConnectionProvider);
Microsoft::Console::ErrorReporting::EnableFallbackFailureReporting(g_hTerminalConnectionProvider);
break;
case DLL_PROCESS_DETACH:
if (g_hTerminalConnectionProvider)
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include <LibraryResources.h>
#include <WilErrorReporting.h>

// Note: Generate GUID using TlgGuid.exe tool
TRACELOGGING_DEFINE_PROVIDER(
Expand All @@ -19,6 +20,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD reason, LPVOID /*reserved*/)
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hInstDll);
TraceLoggingRegister(g_hTerminalControlProvider);
Microsoft::Console::ErrorReporting::EnableFallbackFailureReporting(g_hTerminalControlProvider);
break;
case DLL_PROCESS_DETACH:
if (g_hTerminalControlProvider)
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/WindowsTerminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "AppHost.h"
#include "resource.h"
#include "../types/inc/User32Utils.hpp"
#include <WilErrorReporting.h>

using namespace winrt;
using namespace winrt::Windows::UI;
Expand Down Expand Up @@ -91,6 +92,7 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
TraceLoggingDescription("Event emitted immediately on startup"),
TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance));
::Microsoft::Console::ErrorReporting::EnableFallbackFailureReporting(g_hWindowsTerminalProvider);

// If Terminal is spawned by a shortcut that requests that it run in a new process group
// while attached to a console session, that request is nonsense. That request will, however,
Expand Down
61 changes: 61 additions & 0 deletions src/inc/WilErrorReporting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- WilErrorReporting.h
--*/
#pragma once

#include <winmeta.h>
#include <wil/common.h>

#define CONSOLE_WIL_TRACELOGGING_COMMON_FAILURE_PARAMS(failure) \
TraceLoggingUInt32((failure).hr, "hresult", "Failure error code"), \
TraceLoggingString((failure).pszFile, "fileName", "Source code file name where the error occurred"), \
TraceLoggingUInt32((failure).uLineNumber, "lineNumber", "Line number within the source code file where the error occurred"), \
TraceLoggingString((failure).pszModule, "module", "Name of the binary where the error occurred"), \
TraceLoggingUInt32(static_cast<DWORD>((failure).type), "failureType", "Indicates what type of failure was observed (exception, returned error, logged error or fail fast"), \
TraceLoggingWideString((failure).pszMessage, "message", "Custom message associated with the failure (if any)"), \
TraceLoggingUInt32((failure).threadId, "threadId", "Identifier of the thread the error occurred on"), \
TraceLoggingString((failure).pszCallContext, "callContext", "List of containing this error"), \
TraceLoggingUInt32((failure).callContextOriginating.contextId, "originatingContextId", "Identifier for the oldest activity containing this error"), \
TraceLoggingString((failure).callContextOriginating.contextName, "originatingContextName", "Name of the oldest activity containing this error"), \
TraceLoggingWideString((failure).callContextOriginating.contextMessage, "originatingContextMessage", "Custom message associated with the oldest activity containing this error (if any)"), \
TraceLoggingUInt32((failure).callContextCurrent.contextId, "currentContextId", "Identifier for the newest activity containing this error"), \
TraceLoggingString((failure).callContextCurrent.contextName, "currentContextName", "Name of the newest activity containing this error"), \
TraceLoggingWideString((failure).callContextCurrent.contextMessage, "currentContextMessage", "Custom message associated with the newest activity containing this error (if any)")

#define CONSOLE_WIL_TRACELOGGING_FAILURE_PARAMS(failure) \
TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance), \
TraceLoggingStruct(14, "wilResult"), \
CONSOLE_WIL_TRACELOGGING_COMMON_FAILURE_PARAMS(failure)

namespace Microsoft::Console::ErrorReporting
{
__declspec(selectany) TraceLoggingHProvider FallbackProvider;
__declspec(noinline) inline void WINAPI ReportFailureToFallbackProvider(bool alreadyReported, const wil::FailureInfo& failure) noexcept
try
{
if (!alreadyReported && FallbackProvider)
{
#pragma warning(suppress : 26477 26485 26494 26482 26446) // We don't control TraceLoggingWrite
TraceLoggingWrite(FallbackProvider, "FallbackError", TraceLoggingKeyword(MICROSOFT_KEYWORD_TELEMETRY), TraceLoggingLevel(WINEVENT_LEVEL_ERROR), CONSOLE_WIL_TRACELOGGING_FAILURE_PARAMS(failure));
}
}
catch (...)
{
// Don't log anything. We just failed to trace, where will we go now?
}

__declspec(noinline) inline void EnableFallbackFailureReporting(TraceLoggingHProvider provider) noexcept
try
{
FallbackProvider = provider;
::wil::SetResultTelemetryFallback(::Microsoft::Console::ErrorReporting::ReportFailureToFallbackProvider);
}
catch (...)
{
// Don't log anything. We just failed to set up WIL -- how are we going to log anything?
}
}

0 comments on commit ef5198e

Please sign in to comment.