1. 程式人生 > >開發驅動時用到的核心列印函式KdPrint 的使用方法

開發驅動時用到的核心列印函式KdPrint 的使用方法

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類似。

  1. ULONG
  2.                                     DbgPrint(  
  3.                                       IN PCHAR  Format,  
  4.                                       . . . .  [arguments]  
  5.                                       );  

   1、直接輸出字串,輸出的字串是以NULL結尾的字串(CHAR型別),如:

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

2、指定格式輸出字串,輸出得字串可以是以NULL結尾的ASNI字串,也可以是寬字串(WCHAR型別),如:

  1. invoke DbgPrint,$CTA0("%s"),$CTA0("The Driver has Unloaded.")   ;輸出ASNI字串  
  2.       invoke DbgPrint,$CTA0("%ws"),$CTW0("The Driver has Unloaded.")     ;輸出wchar型別字串  
  3.       invoke DbgPrint,$CTA0("%S"),$CTW0("The Driver has Unloaded.")     ;輸出wchar型別字串(注意是大寫的S)   

3、UNICODE_STRING結構的串的輸出,如:

  1. ucstShow    UNICODE_STRING    <?>             ;定義一個UNICODE_STRING的結構  
  2.         invoke RtlInitUnicodeString,addr ucstShow,$CTW0("This is the fifth debug Information.")     ;初始化  
  3.         invoke DbgPrint,$CTA0("%wZ"),addr ucstShow  

4、混合拼接資訊輸出,如:

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

實際上就是printf,sprintf,wsprintf的用法,很簡單~~

         還有很多輸出方式,如下表(網上找的):

以下是隨便寫的測試程式碼:

  1. ;/**  
  2. ; *************************************************************************  
  3. ; * 檔名稱: Driver.asm  
  4. ; * 版      本:  
  5. ; * 描      述: 學習DbgPrint的用法  
  6. ; * 作      者: zzydog  
  7. ; * 建立日期: 2010  
  8. ; *************************************************************************  
  9. ; */  
  10. .386  
  11. .model flat, stdcall  
  12. option casemap:none  
  13. include Strings.mac  
  14. include w2k\ntstatus.inc  
  15. include w2k\ntddk.inc  
  16. include w2k\ntoskrnl.inc  
  17. includelib ntoskrnl.lib  
  18. includelib ntdll.lib  
  19. ;************************************************************************************  
  20. ;函式定義  
  21. DriverEntry proto pDriverObject:PDRIVER_OBJECT,pusRegistryPath:PUNICODE_STRING      
  22. DirverUnload proto pDriverObject:PDRIVER_OBJECT  
  23. ;************************************************************************************  
  24. .data  
  25.     ucstShow    UNICODE_STRING    <?>  
  26.     szShowLoad        db            "The Dirver has been loaded!",NULL  
  27.     szShowUnLoad    db            "The Driver has been Unloaded!",NULL  
  28.     dwShow            dd            1990h  
  29. .code  
  30. DriverEntry proc pDriverObject:PDRIVER_OBJECT,pusRegistryPath:PUNICODE_STRING  
  31.     invoke DbgPrint,addr szShowLoad  
  32.     invoke DbgPrint,$CTA0("This is the first debug Information.")  
  33.     invoke DbgPrint,$CTA0("%s"),$CTA0("This is the second debug Information.")  
  34.     invoke DbgPrint,$CTA0("%ws"),$CTW0("This is the third debug Information.")  
  35.     invoke DbgPrint,$CTA0("%S"),$CTW0("This is the forth debug Information.")  
  36.     invoke RtlInitUnicodeString,addr ucstShow,$CTW0("This is the fifth debug Information.")  
  37.     invoke DbgPrint,$CTA0("%wZ"),addr ucstShow  
  38.     invoke RtlInitUnicodeString,addr ucstShow,$CTW0("hello,I was born in")  
  39.     invoke DbgPrint,$CTA0("%wZ %x"),addr ucstShow,dwShow  
  40.     assume edx:ptr DRIVER_OBJECT  
  41.     mov edx,[pDriverObject]  
  42.     mov [edx].DriverUnload,offset DriverUnload  
  43.     mov eax,STATUS_SUCCESS  
  44.     ret  
  45. DriverEntry endp  
  46. DriverUnload proc pDriverObject:PDRIVER_OBJECT  
  47.     invoke DbgPrint,$CTA0("%s"),addr szShowUnLoad  
  48.     mov eax,STATUS_SUCCESS  
  49.     ret  
  50. DriverUnload endp  
  51. end DriverEntry