Event tracing support added

This commit is contained in:
Simon Rozman 2016-05-18 21:35:51 +02:00
parent bdef5db62c
commit f610d4183f
9 changed files with 184 additions and 4 deletions

View File

@ -8,8 +8,11 @@
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup> <ItemDefinitionGroup>
<ClCompile> <ClCompile>
<AdditionalIncludeDirectories>..\include;..\..\lib\WinStd\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(IntDir);..\include;..\..\lib\WinStd\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<ResourceCompile>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup /> <ItemGroup />
</Project> </Project>

View File

@ -7,6 +7,9 @@
<PropertyGroup /> <PropertyGroup />
<ItemDefinitionGroup> <ItemDefinitionGroup>
<ResourceCompile /> <ResourceCompile />
<ClCompile>
<PreprocessorDefinitions>EAPMETHOD_TYPE=21;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup /> <ItemGroup />
</Project> </Project>

Binary file not shown.

View File

@ -98,6 +98,23 @@
<Project>{47399d91-7eb9-41de-b521-514ba5db0c43}</Project> <Project>{47399d91-7eb9-41de-b521-514ba5db0c43}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<CustomBuild Include="..\res\EAPMethod.man">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)."</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)."</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)."</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)."</Command>
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Compiling manifest...</Message>
<Message Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Compiling manifest...</Message>
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Compiling manifest...</Message>
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Compiling manifest...</Message>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs)</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs)</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs)</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs)</Outputs>
</CustomBuild>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -32,4 +32,9 @@
<Filter>Resource Files</Filter> <Filter>Resource Files</Filter>
</ResourceCompile> </ResourceCompile>
</ItemGroup> </ItemGroup>
<ItemGroup>
<CustomBuild Include="..\res\EAPMethod.man">
<Filter>Resource Files</Filter>
</CustomBuild>
</ItemGroup>
</Project> </Project>

View File

@ -20,6 +20,12 @@
#pragma once #pragma once
#include <WinStd/ETW.h>
#include <WinStd/Win.h> #include <WinStd/Win.h>
#include <eaptypes.h>
#include <tchar.h>
#include <Windows.h> #include <Windows.h>
#include <EAPMethodETW.h>

Binary file not shown.

View File

@ -20,15 +20,161 @@
#include <StdAfx.h> #include <StdAfx.h>
using namespace std;
using namespace winstd;
#define ETW_FN_VOID event_fn_auto < &EAPMETHOD_TRACE_EVT_FN_CALL, &EAPMETHOD_TRACE_EVT_FN_RETURN > _event_auto(*g_ep, __FUNCTION__)
#define ETW_FN_DWORD(res) event_fn_auto_ret<DWORD , &EAPMETHOD_TRACE_EVT_FN_CALL, &EAPMETHOD_TRACE_EVT_FN_RETURN_DWORD > _event_auto(*g_ep, __FUNCTION__, res)
#define ETW_FN_HRESULT(res) event_fn_auto_ret<HRESULT, &EAPMETHOD_TRACE_EVT_FN_CALL, &EAPMETHOD_TRACE_EVT_FN_RETURN_HRESULT> _event_auto(*g_ep, __FUNCTION__, res)
event_provider *g_ep = NULL;
// event_fn_auto actually and winstd::event_auto_res<> do not need an assignment operator actually, so the C4512 warning is safely ignored.
#pragma warning(push)
#pragma warning(disable: 4512)
///
/// Helper class to write an event on entry/exit of scope.
///
/// It writes one string event at creation and another at destruction.
///
template <const EVENT_DESCRIPTOR *event_cons, const EVENT_DESCRIPTOR *event_dest>
class event_fn_auto
{
public:
inline event_fn_auto(_In_ event_provider &ep, _In_z_ LPCSTR pszFnName) : m_ep(ep)
{
EventDataDescCreate(&m_fn_name, pszFnName, (ULONG)(strlen(pszFnName) + 1)*sizeof(*pszFnName));
m_ep.write(event_cons, 1, &m_fn_name);
}
inline ~event_fn_auto()
{
m_ep.write(event_dest, 1, &m_fn_name);
}
protected:
event_provider &m_ep; ///< Reference to event provider in use
EVENT_DATA_DESCRIPTOR m_fn_name; ///< Function name
};
///
/// Helper template to write an event on entry/exit of scope with one parameter (typically result).
///
/// It writes one string event at creation and another at destruction, with allowing one sprintf type parameter for string event at destruction.
///
template<class T, const EVENT_DESCRIPTOR *event_cons, const EVENT_DESCRIPTOR *event_dest>
class event_fn_auto_ret
{
public:
inline event_fn_auto_ret(_In_ event_provider &ep, _In_z_ LPCSTR pszFnName, T &result) : m_ep(ep), m_result(result)
{
EventDataDescCreate(m_desc + 0, pszFnName, (ULONG)(strlen(pszFnName) + 1)*sizeof(*pszFnName));
m_ep.write(event_cons, 1, m_desc);
}
inline ~event_fn_auto_ret()
{
EventDataDescCreate(m_desc + 1, &m_result, sizeof(T));
m_ep.write(event_dest, 2, m_desc);
}
protected:
event_provider &m_ep; ///< Reference to event provider in use
T &m_result; ///< Function result
EVENT_DATA_DESCRIPTOR m_desc[2]; ///< Function name and return value
};
#pragma warning(pop)
BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved)
{ {
UNREFERENCED_PARAMETER(hinstDLL);
UNREFERENCED_PARAMETER(lpvReserved); UNREFERENCED_PARAMETER(lpvReserved);
if (fdwReason == DLL_PROCESS_ATTACH) { if (fdwReason == DLL_PROCESS_ATTACH) {
winstd::tstring filename; g_ep = new event_provider();
GetModuleFileName(hinstDLL, filename); assert(g_ep);
g_ep->create(&EAPMETHOD_TRACE_EVENT_PROVIDER);
g_ep->write<unsigned int>(&EAPMETHOD_TRACE_EVT_MODULE_LOAD, EAPMETHOD_TYPE);
} else if (fdwReason == DLL_PROCESS_DETACH) {
assert(g_ep);
g_ep->write<unsigned int>(&EAPMETHOD_TRACE_EVT_MODULE_UNLOAD, EAPMETHOD_TYPE);
delete g_ep;
assert(!_CrtDumpMemoryLeaks());
} }
return TRUE; return TRUE;
} }
//#pragma comment(linker, "/EXPORT:DllRegisterServer,PRIVATE")
//#pragma comment(linker, "/EXPORT:DllUnregisterServer,PRIVATE")
extern "C"
{
/////
///// Registers the EAP method.
/////
//HRESULT STDAPICALLTYPE DllRegisterServer()
//{
// HRESULT hr = S_OK;
// ETW_FN_HRESULT(hr);
// return hr;
//}
/////
///// Unregisters the EAP method.
/////
//HRESULT STDAPICALLTYPE DllUnregisterServer()
//{
// HRESULT hr = S_OK;
// ETW_FN_HRESULT(hr);
// return hr;
//}
///
/// Releases all memory associated with an opaque user interface context data buffer.
///
/// \sa [EapPeerFreeMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363606.aspx)
///
__declspec(dllexport) VOID WINAPI EapPeerFreeMemory(_In_ void *pUIContextData)
{
ETW_FN_VOID;
if (pUIContextData) {
// Since we do security here and some of the BLOBs contain credentials, sanitize every memory block before freeing.
HANDLE hHeap = GetProcessHeap();
SecureZeroMemory(pUIContextData, HeapSize(hHeap, 0, pUIContextData));
HeapFree(hHeap, 0, pUIContextData);
}
}
///
/// Releases error-specific memory allocated by the EAP peer method.
///
/// \sa [EapPeerFreeErrorMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363605.aspx)
///
__declspec(dllexport) VOID WINAPI EapPeerFreeErrorMemory(_In_ EAP_ERROR *ppEapError)
{
ETW_FN_VOID;
if (ppEapError) {
// pRootCauseString and pRepairString always trail the ppEapError to reduce number of (de)allocations.
HeapFree(GetProcessHeap(), 0, ppEapError);
}
}
};

@ -1 +1 @@
Subproject commit f6029b2f04f52ae2b692980cb4d5734031568d91 Subproject commit 9be7e7eafb62f815155a9635764ce7770a5daa25