您的位置:首页 > 其它

开发驱动时用到的内核打印函数KdPrint 的使用方法

2017-01-09 14:21 651 查看
转载自: http://blog.csdn.net/wjcsharp/article/details/6161712

DbgPrint会发送一个消息给内核调试器。

DbgPrint and DbgPrintEx can be called at IRQL<=DIRQL. However, Unicode format codes (%wc and %ws) can be used only at IRQL PASSIVE_LEVEL. Also, because the debugger uses interprocess
interrupts (IPIs) to communicate with other processors, callingDbgPrint at IRQL>DIRQL can cause deadlocks.

仅能在内核模式下使用DbgPrint函数。如果想在用户模式下使用打印到windbg上查看,得用OutPutDebugString。

In Windows Vista and later versions of Windows, DbgPrint sends a message only if certain conditions apply. Specifically, it behaves like theDbgPrintEx routine
with the DEFAULT component and a message importance level of DPFLTR_INFO_LEVEL. In other words, the following two function calls are identical:

KdPrint使用方法类似printf,注意KdPrint((" ", ));使用的是双括号。

用KdPrint(())来代替printf 输出信息。这些信息可以在DbgView 中看到。KdPrint(())自身是一个宏,

为了完整传入参数所以使用了两重括弧。这个比DbgPrint 调用要稍好。因为在free 版不被编译。

DebugPrint格式说明符



二、

几天一直在做那些无聊的实验,把驱动的学习耽误到现在。幸好,把那些无聊的实验写完。

话说回来,驱动编程真的比在RING3下麻烦很多呢,在字符串的使用都需要做很多的初始化,搞到我头都大了,如果是用C就很好理解,但是我用的是汇编~~~。今天,就看了看关于DbgPrint的用法,顺便做点笔记。

DbgPrintf,是输出Debug信息的,用法跟printf,sprintf,wsprintf类似。

[cpp] view
plain copy

ULONG

DbgPrint(

IN PCHAR Format,

. . . . [arguments]

);

1、直接输出字符串,输出的字符串是以NULL结尾的字符串(CHAR类型),如:

[cpp] view
plain copy

invoke DbgPrint,$CTA0("the Driver has loaded.")

2、指定格式输出字符串,输出得字符串可以是以NULL结尾的ASNI字符串,也可以是宽字符串(WCHAR类型),如:

[cpp] view
plain copy

invoke DbgPrint,$CTA0("%s"),$CTA0("The Driver has Unloaded.") ;输出ASNI字符串

invoke DbgPrint,$CTA0("%ws"),$CTW0("The Driver has Unloaded.") ;输出wchar类型字符串

invoke DbgPrint,$CTA0("%S"),$CTW0("The Driver has Unloaded.") ;输出wchar类型字符串(注意是大写的S)

3、UNICODE_STRING结构的串的输出,如:

[cpp] view
plain copy

ucstShow UNICODE_STRING <?> ;定义一个UNICODE_STRING的结构

invoke RtlInitUnicodeString,addr ucstShow,$CTW0("This is the fifth debug Information.") ;初始化

invoke DbgPrint,$CTA0("%wZ"),addr ucstShow

4、混合拼接信息输出,如:

[cpp] view
plain copy

invoke RtlInitUnicodeString,addr ucstShow,$CTW0("hello,I was born in")

invoke DbgPrint,$CTA0("%wZ %x"),addr ucstShow,dwShow

实际上就是printf,sprintf,wsprintf的用法,很简单~~

还有很多输出方式,如下表(网上找的):



以下是随便写的测试代码:

[plain] view
plain copy

;/**

; *************************************************************************

; * 文件名称: Driver.asm

; * 版 本:

; * 描 述: 学习DbgPrint的用法

; * 作 者: zzydog

; * 创建日期: 2010

; *************************************************************************

; */

.386

.model flat, stdcall

option casemap:none

include Strings.mac

include w2k\ntstatus.inc

include w2k\ntddk.inc

include w2k\ntoskrnl.inc

includelib ntoskrnl.lib

includelib ntdll.lib

;************************************************************************************

;函数定义

DriverEntry proto pDriverObject:PDRIVER_OBJECT,pusRegistryPath:PUNICODE_STRING

DirverUnload proto pDriverObject:PDRIVER_OBJECT

;************************************************************************************

.data

ucstShow UNICODE_STRING <?>

szShowLoad db "The Dirver has been loaded!",NULL

szShowUnLoad db "The Driver has been Unloaded!",NULL

dwShow dd 1990h

.code

DriverEntry proc pDriverObject:PDRIVER_OBJECT,pusRegistryPath:PUNICODE_STRING

invoke DbgPrint,addr szShowLoad

invoke DbgPrint,$CTA0("This is the first debug Information.")

invoke DbgPrint,$CTA0("%s"),$CTA0("This is the second debug Information.")

invoke DbgPrint,$CTA0("%ws"),$CTW0("This is the third debug Information.")

invoke DbgPrint,$CTA0("%S"),$CTW0("This is the forth debug Information.")

invoke RtlInitUnicodeString,addr ucstShow,$CTW0("This is the fifth debug Information.")

invoke DbgPrint,$CTA0("%wZ"),addr ucstShow

invoke RtlInitUnicodeString,addr ucstShow,$CTW0("hello,I was born in")

invoke DbgPrint,$CTA0("%wZ %x"),addr ucstShow,dwShow

assume edx:ptr DRIVER_OBJECT

mov edx,[pDriverObject]

mov [edx].DriverUnload,offset DriverUnload

mov eax,STATUS_SUCCESS

ret

DriverEntry endp

DriverUnload proc pDriverObject:PDRIVER_OBJECT

invoke DbgPrint,$CTA0("%s"),addr szShowUnLoad

mov eax,STATUS_SUCCESS

ret

DriverUnload endp

end DriverEntry
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: