-
Notifications
You must be signed in to change notification settings - Fork 334
/
MddBootstrapTracelogging.cpp
156 lines (142 loc) · 6.96 KB
/
MddBootstrapTracelogging.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#pragma once
#include "pch.h"
void MddBootstrap_StopActivity(
const std::string failureType,
const bool isActivityRunning,
const GUID *activityId,
const WindowsAppRuntime::MddBootstrap::Activity::Context& activityContext,
const wil::FailureInfo& failure)
{
MddBootstrap_WriteEventWithActivity(*failureType.c_str(), activityId);
if (isActivityRunning)
{
PWSTR initializationFrameworkPackageFullName{};
auto initializationCount{ WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetInitializeData(initializationFrameworkPackageFullName) };
if (activityContext.GetMddBootstrapAPI() == WindowsAppRuntime::MddBootstrap::Activity::MddBootstrapAPI::Initialize)
{
WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetInitializeActivity().StopWithResult(
failure.hr,
static_cast<UINT32>(initializationCount),
static_cast<UINT32>(WindowsAppRuntime::MddBootstrap::Activity::Context::GetIntegrityFlags()),
initializationFrameworkPackageFullName,
static_cast<UINT32>(activityContext.GetLastFailure().type),
activityContext.GetLastFailure().file.c_str(),
activityContext.GetLastFailure().lineNumer,
activityContext.GetLastFailure().message.c_str(),
activityContext.GetLastFailure().module.c_str());
}
else if (activityContext.GetMddBootstrapAPI() == WindowsAppRuntime::MddBootstrap::Activity::MddBootstrapAPI::Shutdown)
{
WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetShutdownActivity().StopWithResult(
failure.hr,
static_cast<UINT32>(initializationCount),
static_cast<UINT32>(activityContext.GetLastFailure().type),
activityContext.GetLastFailure().file.c_str(),
activityContext.GetLastFailure().lineNumer,
activityContext.GetLastFailure().message.c_str(),
activityContext.GetLastFailure().module.c_str());
}
}
}
// Set thread callback for wil error handlers.
// If a process-wide callback is set, then that will also be called after thread callback is called.
bool __stdcall wilResultLoggingThreadCallback(const wil::FailureInfo& failure) noexcept
{
if (WindowsAppRuntimeBootstrap_TraceLogger::IsEnabled())
{
auto IsActivityRunning = []()
{
if (WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetMddBootstrapAPI() ==
WindowsAppRuntime::MddBootstrap::Activity::MddBootstrapAPI::Initialize)
{
return WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetInitializeActivity().IsRunning();
}
else if (WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetMddBootstrapAPI() ==
WindowsAppRuntime::MddBootstrap::Activity::MddBootstrapAPI::Shutdown)
{
return WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetShutdownActivity().IsRunning();
}
return false;
};
auto isActivityRunning = IsActivityRunning();
auto GetActivityId = [isActivityRunning]()
{
if (WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetMddBootstrapAPI() ==
WindowsAppRuntime::MddBootstrap::Activity::MddBootstrapAPI::Initialize)
{
if (isActivityRunning)
{
return WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetInitializeActivity().Id();
}
else
{
return WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetSavedInitializeActivityId();
}
}
else if (WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetMddBootstrapAPI() ==
WindowsAppRuntime::MddBootstrap::Activity::MddBootstrapAPI::Shutdown)
{
if (isActivityRunning)
{
return WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetShutdownActivity().Id();
}
else
{
return WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetSavedShutdownActivityId();
}
}
return static_cast<const GUID*>(nullptr);
};
auto activityId = GetActivityId();
if (failure.type == wil::FailureType::Log)
{
MddBootstrap_WriteEventWithActivity("Log", activityId);
}
else if (failure.type == wil::FailureType::Exception)
{
// Bootstrap shutdown API is a best effort API. Hence, don't stop bootstrap activity on an Exception
if (WindowsAppRuntime::MddBootstrap::Activity::Context::Get().GetMddBootstrapAPI() ==
WindowsAppRuntime::MddBootstrap::Activity::MddBootstrapAPI::Shutdown)
{
MddBootstrap_WriteEventWithActivity("Exception", activityId);
}
else
{
MddBootstrap_StopActivity("Exception", isActivityRunning, activityId, WindowsAppRuntime::MddBootstrap::Activity::Context::Get(), failure);
}
}
else if (failure.type == wil::FailureType::FailFast)
{
MddBootstrap_StopActivity("FailFast", isActivityRunning, activityId, WindowsAppRuntime::MddBootstrap::Activity::Context::Get(), failure);
}
else if (failure.type == wil::FailureType::Return)
{
// For the RETURN_ macro that returns from the Bootstrap API, WindowsAppRuntime::MddBootstrap::Activity::Context::
// m_stopActivityForWilReturnHR need to be set to indicate to this callback that the activity should be stopped.
// When it is set, stop the activity.
if (WindowsAppRuntime::MddBootstrap::Activity::Context::Get().ShouldStopActivityForWilReturnHR())
{
MddBootstrap_StopActivity("Return", isActivityRunning, activityId, WindowsAppRuntime::MddBootstrap::Activity::Context::Get(), failure);
WindowsAppRuntime::MddBootstrap::Activity::Context::Get().StopActivityForWilReturnHR(false);
}
else
{
MddBootstrap_WriteEventWithActivity("Return", activityId);
WindowsAppRuntime::MddBootstrap::Activity::Context::Get().SetLastFailure(failure);
}
}
}
// Returning true indicates to wil that the error is reported in Telemetry by this callback.
return true;
}
GUID& GetLifetimeActivityId() noexcept
{
static GUID lifetimeActivityId{};
if (IsEqualGUID(lifetimeActivityId, GUID_NULL))
{
std::ignore = CoCreateGuid(&lifetimeActivityId);
}
return lifetimeActivityId;
}