基于UMDF2将 WPP 软件跟踪添加到 Windows 驱动程序

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");

4.附录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人在路上……

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值