1. 背景
作为一位标准的 WINDOWN 驱动开发人员最痛苦并快乐着就是构建驱动框架时都喜欢构架自己的LOG框架,
直接调用系统的接口函数显得太生涩和苦逼了;而 window 驱动的原生态log API 调用又繁琐且麻烦。
因此,基于此基础上,就有了本文的撰写目的。
2. 概要
2.1 环境约定
运行环境:WIN10/WIN11
开发工具:VS2019
SDK版本:wdksetup.exe
驱动框架:UMDF2
2.2 参考文档 `
[1] https://docs.microsoft.com/zh-cn/windows-hardware/drivers/devtest/adding-wpp-software-tracing-to-a-windows-driver#step-4-ad
3. 操作
3.1 UMDF2 项目创建
A.打开 Visual Studio 2019;
B.创建新项目
C.基于UMDF2 框架创建项目
D.配置基于UMDF2 框架的项目
3.2 向项目添加 LOG 相关的文件
A.添加 LogMsg.c 文件
B.添加 LogMsg.h 文件
3.3 代码修改
LogMsg.c
+++ b/ LogMsg.c
@@ -0,0 +1,23 @@
+#include " LogMsg.h"
+#include " LogMsg.tmh"
+
+
+#define DBG_LEN 512
+#define WHERESTR "[%2d][%4d][%20s] "
+int pt_log(uint32_t level, int line, const char* fn, const char* fmt, ...)
+{
+ va_list ap;
+ char db_buf[DBG_LEN];
+ char info_buf[DBG_LEN];
+ memset(db_buf, 0, DBG_LEN);
+ memset(info_buf, 0, DBG_LEN);
+ do {
+ va_start(ap, fmt);
+ sprintf(db_buf, WHERESTR, level, line, fn);
+ vsprintf(info_buf, fmt, ap);
+ strcat(db_buf, info_buf);
+ TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%s", db_buf);
+ va_end(ap);
+ } while (0);
+ return 0;
+}
LogMsg.h:
@@ -0,0 +1,39 @@
+#pragma once
+#include <stdlib.h>
+#include <stdio.h>
+#include <windows.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <stdarg.h>
+#include "string.h"
+#include "stdint.h"
+#include "stdbool.h"
+#include <wdf.h>
+#include "Trace.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define PT_DBG PT_VBS
+typedef enum level_t {
+ PT_NONE,
+ PT_FATAL, // the same as CRITICAL,
+ PT_ERR,
+ PT_WRN,
+ PT_INF,
+ PT_VBS
+} level_t;
+
+int pt_log(uint32_t level, int line, const char* fn, const char* fmt, ...);
+#ifdef __cplusplus
+}
+#endif
+
+#define PL(x, ...) pt_log(x, __LINE__, __FUNCTION__, ##__VA_ARGS__)
+#define PLV(...) PL(PT_VBS, ##__VA_ARGS__)
+#define PLD(...) PL(PT_DBG, ##__VA_ARGS__)
+#define PLI(...) PL(PT_INF, ##__VA_ARGS__)
+#define PLW(...) PL(PT_WRN, ##__VA_ARGS__)
+#define PLE(...) PL(PT_ERR, ##__VA_ARGS__)
Driver.c:
+++ b/Driver.c
@@ -16,6 +16,7 @@ Environment:
#include "driver.h"
#include "driver.tmh"
+#include "LogMesg.h"
NTSTATUS
DriverEntry(
@@ -61,7 +62,10 @@ Return Value:
WPP_INIT_TRACING( DriverObject, RegistryPath );
#endif
- TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");
+ //TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");
+ PLI("UMDF2 Driver version : 0X0001");
+ PLI("%!FUNC! Entry");