看流星社区

 找回密码
 注册账号
查看: 2061|回复: 0

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

[复制链接]

该用户从未签到

发表于 2017-6-1 17:26:40 | 显示全部楼层 |阅读模式
转载自:
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 WindowsVista 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&#26684;式说明符





二、
几天一直在做那些无聊的实验,把驱动的学习耽误到现在。幸好,把那些无聊的实验写完。
话说回来,驱动编程真的比在RING3下麻烦很多呢,在字符串的使用都需要做很多的初始化,搞到我头都大了,如果是用C就很好理解,但是我用的是汇编~~~。今天,就看了看关于DbgPrint的用法,顺便做点笔记。

DbgPrintf,是输出Debug信息的,用法跟printf,sprintf,wsprintf类似。
  ULONG
                                      DbgPrint(
                                        IN PCHAR  Format,
                                        . . . .  [arguments]
                                        );

[/code]
1、直接输出字符串,输出的字符串是以NULL结尾的字符串(CHAR类型),如:
    invoke DbgPrint,$CTA0("the Driver has loaded.")[/code]
2、指定&#26684;式输出字符串,输出得字符串可以是以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)
[/code]
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

[/code]
4、混合拼接信息输出,如:
invoke RtlInitUnicodeString,addr ucstShow,$CTW0("hello,I was born in")
         invoke DbgPrint,$CTA0("%wZ %x"),addr ucstShow,dwShow

[/code]
实际上就是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 pDriverObjectDRIVER_OBJECT,pusRegistryPathUNICODE_STRING   
DirverUnload proto pDriverObjectDRIVER_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 pDriverObjectDRIVER_OBJECT,pusRegistryPathUNICODE_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 pDriverObjectDRIVER_OBJECT
    invoke DbgPrint,$CTA0("%s"),addr szShowUnLoad
    mov eax,STATUS_SUCCESS
    ret
DriverUnload endp

end DriverEntry
[/code]
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-29 14:39

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表