From 42bc54ce13680e15ec6149082e2a0d0067ef83fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger?= Date: Sat, 12 May 2018 14:31:07 +0200 Subject: [PATCH] Implemented WPP tracing boilerplate --- .gitignore | 8 +++++ sys/Driver.c | 54 ++++++++++++++++++++++++++++--- sys/ViGEmBus.vcxproj | 19 +++++++++++ sys/ViGEmBus.vcxproj.filters | 3 ++ sys/busenum.c | 1 + sys/busenum.h | 3 ++ sys/trace.h | 61 ++++++++++++++++++++++++++++++++++++ 7 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 sys/trace.h diff --git a/.gitignore b/.gitignore index 799ae30..5caa9df 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,11 @@ /Release (static) /build/bin/Debug /build/obj/Debug +/bin +/sys/x64/Debug +/x64/Debug +/x64/Debug (static) +*.user +/.vs/config +/sys/RCa21300 +/sys/RCb21300 diff --git a/sys/Driver.c b/sys/Driver.c index 493522c..8a8887a 100644 --- a/sys/Driver.c +++ b/sys/Driver.c @@ -25,6 +25,7 @@ SOFTWARE. #include "busenum.h" #include +#include "driver.tmh" #ifdef ALLOC_PRAGMA #pragma alloc_text (INIT, DriverEntry) @@ -40,18 +41,31 @@ SOFTWARE. // NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) { - WDF_DRIVER_CONFIG config; - NTSTATUS status; - WDFDRIVER driver; + WDF_DRIVER_CONFIG config; + NTSTATUS status; + WDFDRIVER driver; + WDF_OBJECT_ATTRIBUTES attributes; KdPrint((DRIVERNAME "Virtual Gamepad Emulation Bus Driver [built: %s %s]\n", __DATE__, __TIME__)); + // + // Initialize WPP Tracing + // + WPP_INIT_TRACING(DriverObject, RegistryPath); + + // + // Register cleanup callback + // + WDF_OBJECT_ATTRIBUTES_INIT(&attributes); + attributes.EvtCleanupCallback = Bus_EvtDriverContextCleanup; + WDF_DRIVER_CONFIG_INIT(&config, Bus_EvtDeviceAdd); - status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, &driver); + status = WdfDriverCreate(DriverObject, RegistryPath, &attributes, &config, &driver); if (!NT_SUCCESS(status)) { + WPP_CLEANUP(DriverObject); KdPrint((DRIVERNAME "WdfDriverCreate failed with status 0x%x\n", status)); } @@ -382,6 +396,38 @@ Bus_FileClose( WdfChildListEndIteration(list, &iterator); } +VOID +Bus_EvtDriverContextCleanup( + _In_ WDFOBJECT DriverObject + ) +/*++ +Routine Description: + + Free all the resources allocated in DriverEntry. + +Arguments: + + DriverObject - handle to a WDF Driver object. + +Return Value: + + VOID. + +--*/ +{ + UNREFERENCED_PARAMETER(DriverObject); + + PAGED_CODE (); + + TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + + // + // Stop WPP Tracing + // + WPP_CLEANUP( WdfDriverWdmGetDriverObject( (WDFDRIVER) DriverObject) ); + +} + // // Called by PDO when a boot-up stage has been completed // diff --git a/sys/ViGEmBus.vcxproj b/sys/ViGEmBus.vcxproj index 2ad467c..bb36a47 100644 --- a/sys/ViGEmBus.vcxproj +++ b/sys/ViGEmBus.vcxproj @@ -149,6 +149,7 @@ DbgengKernelDebugger $(SolutionDir)Include;$(IncludePath) true + false DbgengKernelDebugger @@ -161,6 +162,7 @@ DbgengKernelDebugger $(SolutionDir)Include;$(IncludePath) true + false DbgengKernelDebugger @@ -212,6 +214,11 @@ $(DDK_LIB_PATH)ntstrsafe.lib;$(DDK_LIB_PATH)wdmsec.lib;%(AdditionalDependencies) + + true + true + trace.h + @@ -225,6 +232,9 @@ true + true + true + trace.h @@ -250,6 +260,11 @@ $(DDK_LIB_PATH)ntstrsafe.lib;$(DDK_LIB_PATH)wdmsec.lib;%(AdditionalDependencies) + + true + true + trace.h + @@ -263,6 +278,9 @@ true + true + true + trace.h @@ -281,6 +299,7 @@ + diff --git a/sys/ViGEmBus.vcxproj.filters b/sys/ViGEmBus.vcxproj.filters index a589514..cb0c79e 100644 --- a/sys/ViGEmBus.vcxproj.filters +++ b/sys/ViGEmBus.vcxproj.filters @@ -66,6 +66,9 @@ Header Files + + Header Files + diff --git a/sys/busenum.c b/sys/busenum.c index 2894fba..d401ee3 100644 --- a/sys/busenum.c +++ b/sys/busenum.c @@ -26,6 +26,7 @@ SOFTWARE. #include "busenum.h" #include #include +#include "busenum.tmh" #ifdef ALLOC_PRAGMA #pragma alloc_text (PAGE, Bus_PlugInDevice) diff --git a/sys/busenum.h b/sys/busenum.h index 5c5b813..dea6a0c 100644 --- a/sys/busenum.h +++ b/sys/busenum.h @@ -25,6 +25,7 @@ SOFTWARE. #pragma once +#include "trace.h" #include #include #define NTSTRSAFE_LIB @@ -99,6 +100,8 @@ EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL Pdo_EvtIoInternalDeviceControl; EVT_WDF_TIMER Xgip_SysInitTimerFunc; +EVT_WDF_OBJECT_CONTEXT_CLEANUP Bus_EvtDriverContextCleanup; + #pragma endregion #pragma region Bus enumeration-specific functions diff --git a/sys/trace.h b/sys/trace.h new file mode 100644 index 0000000..d67e89f --- /dev/null +++ b/sys/trace.h @@ -0,0 +1,61 @@ +/*++ + +Module Name: + + Trace.h + +Abstract: + + Header file for the debug tracing related function defintions and macros. + +Environment: + + Kernel mode + +--*/ + +// +// Define the tracing flags. +// +// Tracing GUID - c5ce18fe-27bd-4049-b0b4-8a47cab1dcd9 +// + +#define WPP_CONTROL_GUIDS \ + WPP_DEFINE_CONTROL_GUID( \ + ViGEmBusTraceGuid, (c5ce18fe,27bd,4049,b0b4,8a47cab1dcd9), \ + \ + WPP_DEFINE_BIT(MYDRIVER_ALL_INFO) \ + WPP_DEFINE_BIT(TRACE_BUSENUM) \ + WPP_DEFINE_BIT(TRACE_BUSPDO) \ + WPP_DEFINE_BIT(TRACE_BYTEARRAY) \ + WPP_DEFINE_BIT(TRACE_DRIVER) \ + WPP_DEFINE_BIT(TRACE_DS4) \ + WPP_DEFINE_BIT(TRACE_QUEUE) \ + WPP_DEFINE_BIT(TRACE_USBPDO) \ + WPP_DEFINE_BIT(TRACE_UTIL) \ + WPP_DEFINE_BIT(TRACE_XGIP) \ + WPP_DEFINE_BIT(TRACE_XUSB) \ + ) + +#define WPP_FLAG_LEVEL_LOGGER(flag, level) \ + WPP_LEVEL_LOGGER(flag) + +#define WPP_FLAG_LEVEL_ENABLED(flag, level) \ + (WPP_LEVEL_ENABLED(flag) && \ + WPP_CONTROL(WPP_BIT_ ## flag).Level >= level) + +#define WPP_LEVEL_FLAGS_LOGGER(lvl,flags) \ + WPP_LEVEL_LOGGER(flags) + +#define WPP_LEVEL_FLAGS_ENABLED(lvl, flags) \ + (WPP_LEVEL_ENABLED(flags) && WPP_CONTROL(WPP_BIT_ ## flags).Level >= lvl) + +// +// This comment block is scanned by the trace preprocessor to define our +// Trace function. +// +// begin_wpp config +// FUNC Trace{FLAG=MYDRIVER_ALL_INFO}(LEVEL, MSG, ...); +// FUNC TraceEvents(LEVEL, FLAGS, MSG, ...); +// end_wpp +//