Skip to content

Commit ed1fa44

Browse files
authored
Add diagnostics to hostfxrutil (#951)
1 parent 7977793 commit ed1fa44

File tree

12 files changed

+181
-209
lines changed

12 files changed

+181
-209
lines changed

src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp

+13-25
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,22 @@ APPLICATION_INFO::EnsureApplicationCreated(
191191
IHttpContext *pHttpContext
192192
)
193193
{
194-
HRESULT hr = S_OK;
195194
IAPPLICATION *pApplication = NULL;
196195
STRU struExeLocation;
197-
STACK_STRU(struFileName, 300); // >MAX_PATH
198196
STRU struHostFxrDllLocation;
197+
STACK_STRU(struFileName, 300); // >MAX_PATH
199198

200199
if (m_pApplication != NULL)
201200
{
202-
goto Finished;
201+
return S_OK;
203202
}
204203

205204
if (m_pApplication == NULL)
206205
{
207206
SRWExclusiveLock lock(m_srwLock);
208207
if (m_pApplication != NULL)
209208
{
210-
goto Finished;
209+
return S_OK;
211210
}
212211

213212
//
@@ -221,27 +220,19 @@ APPLICATION_INFO::EnsureApplicationCreated(
221220
// FindRequestHandlerAssembly obtains a global lock, but after releasing the lock,
222221
// there is a period where we could call
223222

224-
hr = FindRequestHandlerAssembly(struExeLocation);
225-
if (FAILED(hr))
226-
{
227-
goto Finished;
228-
}
223+
RETURN_IF_FAILED(FindRequestHandlerAssembly(struExeLocation));
229224

230225
if (m_pfnAspNetCoreCreateApplication == NULL)
231226
{
232-
hr = HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION);
233-
goto Finished;
227+
RETURN_IF_FAILED(HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION));
234228
}
235229

236-
hr = m_pfnAspNetCoreCreateApplication(m_pServer, pHttpContext, struExeLocation.QueryStr(), &pApplication);
237-
230+
RETURN_IF_FAILED(m_pfnAspNetCoreCreateApplication(m_pServer, pHttpContext, struExeLocation.QueryStr(), &pApplication));
238231
m_pApplication = pApplication;
239232
}
240233
}
241234

242-
Finished:
243-
244-
return hr;
235+
return S_OK;
245236
}
246237

247238
HRESULT
@@ -288,20 +279,17 @@ APPLICATION_INFO::FindRequestHandlerAssembly(STRU& location)
288279
{
289280
std::unique_ptr<HOSTFXR_OPTIONS> options;
290281

291-
if (FAILED(hr = HOSTFXR_OPTIONS::Create(
282+
FINISHED_IF_FAILED(HOSTFXR_OPTIONS::Create(
292283
NULL,
293284
m_pConfiguration->QueryProcessPath()->QueryStr(),
294285
m_pConfiguration->QueryApplicationPhysicalPath()->QueryStr(),
295286
m_pConfiguration->QueryArguments()->QueryStr(),
296287
g_hEventLog,
297-
options)))
298-
{
299-
goto Finished;
300-
}
288+
options));
301289

302-
location.Copy(options->GetExeLocation());
290+
FINISHED_IF_FAILED(location.Copy(options->GetExeLocation()));
303291

304-
if (FAILED(hr = FindNativeAssemblyFromHostfxr(options.get(), pstrHandlerDllName, &struFileName)))
292+
if (FAILED_LOG(hr = FindNativeAssemblyFromHostfxr(options.get(), pstrHandlerDllName, &struFileName)))
305293
{
306294
UTILITY::LogEventF(g_hEventLog,
307295
EVENTLOG_ERROR_TYPE,
@@ -314,7 +302,7 @@ APPLICATION_INFO::FindRequestHandlerAssembly(STRU& location)
314302
}
315303
else
316304
{
317-
if (FAILED(hr = FindNativeAssemblyFromGlobalLocation(pstrHandlerDllName, &struFileName)))
305+
if (FAILED_LOG(hr = FindNativeAssemblyFromGlobalLocation(pstrHandlerDllName, &struFileName)))
318306
{
319307
UTILITY::LogEventF(g_hEventLog,
320308
EVENTLOG_ERROR_TYPE,
@@ -326,7 +314,7 @@ APPLICATION_INFO::FindRequestHandlerAssembly(STRU& location)
326314
}
327315
}
328316

329-
WDebugPrintf(ASPNETCORE_DEBUG_FLAG_INFO, L"Loading request handler: %s", struFileName.QueryStr());
317+
WLOG_INFOF(L"Loading request handler: %s", struFileName.QueryStr());
330318

331319
g_hAspnetCoreRH = LoadLibraryW(struFileName.QueryStr());
332320

src/AspNetCoreModuleV2/AspNetCore/src/proxymodule.cxx

+4-5
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ ASPNET_CORE_PROXY_MODULE::OnExecuteRequestHandler(
9797
&m_pApplicationInfo);
9898
if (FAILED(hr))
9999
{
100-
goto Finished;
101-
}
100+
goto Finished;
101+
}
102102

103103
if (!m_pApplicationInfo->QueryAllowStart())
104104
{
@@ -172,7 +172,7 @@ ASPNET_CORE_PROXY_MODULE::OnExecuteRequestHandler(
172172
}
173173
catch (...)
174174
{
175-
hr = CaughtExceptionHResult();
175+
hr = OBSERVE_CAUGHT_EXCEPTION();
176176
}
177177

178178
Finished:
@@ -214,8 +214,7 @@ ASPNET_CORE_PROXY_MODULE::OnAsyncCompletion(
214214
}
215215
catch (...)
216216
{
217-
// Log exception
218-
CaughtExceptionHResult();
217+
OBSERVE_CAUGHT_EXCEPTION();
219218
return RQ_NOTIFICATION_FINISH_REQUEST;
220219
}
221220
}

src/AspNetCoreModuleV2/CommonLib/config_utility.h

+16-68
Original file line numberDiff line numberDiff line change
@@ -17,90 +17,38 @@ class ConfigUtility
1717
HRESULT
1818
FindHandlerVersion(IAppHostElement* pElement, STRU* strHandlerVersionValue)
1919
{
20-
HRESULT hr = S_OK;
21-
IAppHostElement *pHandlerSettings = NULL;
22-
IAppHostElementCollection *pHandlerSettingsCollection = NULL;
23-
ENUM_INDEX index;
24-
IAppHostElement *pHandlerVar = NULL;
20+
HRESULT hr;
21+
CComPtr<IAppHostElement> pHandlerSettings = nullptr;
22+
CComPtr<IAppHostElementCollection> pHandlerSettingsCollection = nullptr;
23+
CComPtr<IAppHostElement> pHandlerVar = nullptr;
24+
ENUM_INDEX index {};
2525
STRU strHandlerName;
2626
STRU strHandlerValue;
2727

28-
hr = GetElementChildByName(pElement,
29-
CS_ASPNETCORE_HANDLER_SETTINGS,
30-
&pHandlerSettings);
31-
if (FAILED(hr))
32-
{
33-
goto Finished;
34-
}
28+
RETURN_IF_FAILED(GetElementChildByName(pElement, CS_ASPNETCORE_HANDLER_SETTINGS,&pHandlerSettings));
29+
RETURN_IF_FAILED(pHandlerSettings->get_Collection(&pHandlerSettingsCollection));
3530

36-
hr = pHandlerSettings->get_Collection(&pHandlerSettingsCollection);
37-
if (FAILED(hr))
38-
{
39-
goto Finished;
40-
}
31+
RETURN_IF_FAILED(hr = FindFirstElement(pHandlerSettingsCollection, &index, &pHandlerVar));
4132

42-
for (hr = FindFirstElement(pHandlerSettingsCollection, &index, &pHandlerVar);
43-
SUCCEEDED(hr);
44-
hr = FindNextElement(pHandlerSettingsCollection, &index, &pHandlerVar))
33+
while (hr != S_FALSE)
4534
{
46-
if (hr == S_FALSE)
47-
{
48-
hr = S_OK;
49-
break;
50-
}
51-
52-
hr = GetElementStringProperty(pHandlerVar,
53-
CS_ASPNETCORE_HANDLER_SETTINGS_NAME,
54-
&strHandlerName);
55-
56-
if (FAILED(hr))
57-
{
58-
goto Finished;
59-
}
60-
61-
hr = GetElementStringProperty(pHandlerVar,
62-
CS_ASPNETCORE_HANDLER_SETTINGS_VALUE,
63-
&strHandlerValue);
64-
65-
if (FAILED(hr))
66-
{
67-
goto Finished;
68-
69-
}
35+
RETURN_IF_FAILED(GetElementStringProperty(pHandlerVar, CS_ASPNETCORE_HANDLER_SETTINGS_NAME, &strHandlerName));
36+
RETURN_IF_FAILED(GetElementStringProperty(pHandlerVar, CS_ASPNETCORE_HANDLER_SETTINGS_VALUE, &strHandlerValue));
7037

7138
if (strHandlerName.Equals(CS_ASPNETCORE_HANDLER_VERSION, TRUE))
7239
{
73-
hr = strHandlerVersionValue->Copy(strHandlerValue);
74-
goto Finished;
40+
RETURN_IF_FAILED(strHandlerVersionValue->Copy(strHandlerValue));
41+
break;
7542
}
7643

7744
strHandlerName.Reset();
7845
strHandlerValue.Reset();
46+
pHandlerVar.Release();
7947

80-
pHandlerVar->Release();
81-
pHandlerVar = NULL;
82-
}
83-
Finished:
84-
85-
if (pHandlerVar != NULL)
86-
{
87-
pHandlerVar->Release();
88-
pHandlerVar = NULL;
89-
}
90-
91-
if (pHandlerSettingsCollection != NULL)
92-
{
93-
pHandlerSettingsCollection->Release();
94-
pHandlerSettingsCollection = NULL;
95-
}
96-
97-
if (pHandlerSettings != NULL)
98-
{
99-
pHandlerSettings->Release();
100-
pHandlerSettings = NULL;
48+
RETURN_IF_FAILED(hr = FindNextElement(pHandlerSettingsCollection, &index, &pHandlerVar));
10149
}
10250

103-
return hr;
51+
return S_OK;
10452
}
10553
};
10654

src/AspNetCoreModuleV2/CommonLib/debugutil.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ DebugPrintf(
132132
VOID
133133
WDebugPrintf(
134134
DWORD dwFlag,
135-
LPWSTR szFormat,
135+
LPCWSTR szFormat,
136136
...
137137
)
138138
{

src/AspNetCoreModuleV2/CommonLib/debugutil.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
#define ASPNETCORE_DEBUG_FLAG_ERROR DEBUG_FLAG_ERROR
1111
#define ASPNETCORE_DEBUG_FLAG_CONSOLE 0x00000008
1212

13+
#define LOG_INFO(...) DebugPrint(ASPNETCORE_DEBUG_FLAG_INFO, __VA_ARGS__)
14+
#define LOG_INFOF(...) DebugPrintf(ASPNETCORE_DEBUG_FLAG_INFO, __VA_ARGS__)
15+
#define WLOG_INFOF(...) WDebugPrintf(ASPNETCORE_DEBUG_FLAG_INFO, __VA_ARGS__)
16+
17+
#define LOG_WARN(...) DebugPrint(ASPNETCORE_DEBUG_FLAG_WARNING, __VA_ARGS__)
18+
#define LOG_WARNF(...) DebugPrintf(ASPNETCORE_DEBUG_FLAG_WARNING, __VA_ARGS__)
19+
#define WLOG_WARNF(...) WDebugPrintf(ASPNETCORE_DEBUG_FLAG_WARNING, __VA_ARGS__)
20+
21+
#define LOG_ERROR(...) DebugPrint(ASPNETCORE_DEBUG_FLAG_ERROR, __VA_ARGS__)
22+
#define LOG_ERRORF(...) DebugPrintf(ASPNETCORE_DEBUG_FLAG_ERROR, __VA_ARGS__)
23+
#define WLOG_ERRORF(...) WDebugPrintf(ASPNETCORE_DEBUG_FLAG_ERROR, __VA_ARGS__)
24+
1325
VOID
1426
DebugInitialize();
1527

@@ -34,6 +46,6 @@ DebugPrintf(
3446
VOID
3547
WDebugPrintf(
3648
DWORD dwFlag,
37-
LPWSTR szFormat,
49+
LPCWSTR szFormat,
3850
...
3951
);

src/AspNetCoreModuleV2/CommonLib/exceptions.h

+45-13
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,54 @@
77

88
#include "debugutil.h"
99

10-
inline VOID ReportUntypedException()
10+
#define LOCATION_INFO_ENABLED TRUE
11+
12+
#if LOCATION_INFO_ENABLED
13+
#define LOCATION_FORMAT "%s:%d "
14+
#define LOCATION_ARGUMENTS_ONLY _In_opt_ PCSTR fileName, unsigned int lineNumber
15+
#define LOCATION_ARGUMENTS LOCATION_ARGUMENTS_ONLY,
16+
#define LOCATION_CALL_ONLY fileName, lineNumber
17+
#define LOCATION_CALL LOCATION_CALL_ONLY,
18+
#define LOCATION_INFO __FILE__, __LINE__
19+
#else
20+
#define LOCATION_FORMAT
21+
#define LOCATION_ARGUMENTS_ONLY
22+
#define LOCATION_ARGUMENTS
23+
#define LOCATION_CALL_ONLY
24+
#define LOCATION_CALL
25+
#define LOCATION_INFO
26+
#endif
27+
28+
#define OBSERVE_CAUGHT_EXCEPTION() CaughtExceptionHResult(LOCATION_INFO);
29+
#define RETURN_CAUGHT_EXCEPTION() return CaughtExceptionHResult(LOCATION_INFO);
30+
#define CATCH_RETURN() catch (...) { RETURN_CAUGHT_EXCEPTION(); }
31+
#define THROW_IF_NULL_ALLOC(ptr) Throw_IfNullAlloc(ptr)
32+
#define RETURN_IF_FAILED(hr) do { HRESULT __hrRet = hr; if (FAILED(__hrRet)) { LogHResultFailed(LOCATION_INFO, __hrRet); return __hrRet; }} while (0, 0)
33+
#define FINISHED_IF_FAILED(hrr) do { HRESULT __hrRet = hrr; if (FAILED(__hrRet)) { LogHResultFailed(LOCATION_INFO, __hrRet); hr = __hrRet; goto Finished; }} while (0, 0)
34+
#define LOG_IF_FAILED(hr) LogHResultFailed(LOCATION_INFO, hr)
35+
#define SUCCEEDED_LOG(hr) SUCCEEDED(LOG_IF_FAILED(hr))
36+
#define FAILED_LOG(hr) FAILED(LOG_IF_FAILED(hr))
37+
38+
__declspec(noinline) inline VOID ReportUntypedException(LOCATION_ARGUMENTS_ONLY)
39+
{
40+
DebugPrintf(ASPNETCORE_DEBUG_FLAG_ERROR, LOCATION_FORMAT "Unhandled non-standard exception", LOCATION_CALL_ONLY);
41+
}
42+
43+
__declspec(noinline) inline VOID ReportException(LOCATION_ARGUMENTS std::exception& exception)
1144
{
12-
DebugPrint(ASPNETCORE_DEBUG_FLAG_ERROR, "Unhandled non-standard exception");
45+
DebugPrintf(ASPNETCORE_DEBUG_FLAG_ERROR, LOCATION_FORMAT "Unhandled exception: %s", LOCATION_CALL exception.what());
1346
}
1447

15-
inline VOID ReportException(std::exception& exception)
48+
__declspec(noinline) inline HRESULT LogHResultFailed(LOCATION_ARGUMENTS HRESULT hr)
1649
{
17-
DebugPrintf(ASPNETCORE_DEBUG_FLAG_ERROR, "Unhandled exception: %s", exception.what());
50+
if (FAILED(hr))
51+
{
52+
DebugPrintf(ASPNETCORE_DEBUG_FLAG_ERROR, "Failed HRESULT returned: 0x%x at " LOCATION_FORMAT, hr, LOCATION_CALL_ONLY);
53+
}
54+
return hr;
1855
}
1956

20-
inline __declspec(noinline) HRESULT CaughtExceptionHResult()
57+
__declspec(noinline) inline HRESULT CaughtExceptionHResult(LOCATION_ARGUMENTS_ONLY)
2158
{
2259
try
2360
{
@@ -29,17 +66,17 @@ inline __declspec(noinline) HRESULT CaughtExceptionHResult()
2966
}
3067
catch (std::system_error& exception)
3168
{
32-
ReportException(exception);
69+
ReportException(LOCATION_CALL exception);
3370
return exception.code().value();
3471
}
3572
catch (std::exception& exception)
3673
{
37-
ReportException(exception);
74+
ReportException(LOCATION_CALL exception);
3875
return HRESULT_FROM_WIN32(ERROR_UNHANDLED_EXCEPTION);
3976
}
4077
catch (...)
4178
{
42-
ReportUntypedException();
79+
ReportUntypedException(LOCATION_CALL_ONLY);
4380
return HRESULT_FROM_WIN32(ERROR_UNHANDLED_EXCEPTION);
4481
}
4582
}
@@ -52,8 +89,3 @@ template <typename PointerT> auto Throw_IfNullAlloc(PointerT pointer)
5289
}
5390
return pointer;
5491
}
55-
56-
57-
#define RETURN_CAUGHT_EXCEPTION() return CaughtExceptionHResult();
58-
#define CATCH_RETURN() catch (...) { RETURN_CAUGHT_EXCEPTION(); }
59-
#define THROW_IF_NULL_ALLOC(ptr) Throw_IfNullAlloc(ptr)

0 commit comments

Comments
 (0)