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

转载自: 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类似。

  ULONG
                                      DbgPrint(
                                        IN PCHAR  Format,
                                        . . . .  [arguments]
                                        );

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

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

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

   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结构的串的输出,如:

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

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

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

invoke RtlInitUnicodeString,addr ucstShow,$CTW0("hello,I was born in")
         invoke DbgPrint,$CTA0("%wZ %x"),addr ucstShow,dwShow

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

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

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

;/**
; *************************************************************************
; * 文件名称: 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


 

 

 



 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值