Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Writing a kd/WinDbg Debugger Extension DLL


December 1998/Writing a kd/WinDbg Debugger Extension DLL/Listing 2

Listing 2: wdjext.h — Header file for building debugger extensions

// Include file for WinDbg kernel debugger extensions.
// Note that you will typically NOT want to simply include
// <wdbgexts.h> in your projects because of dependancies
// on some Windows definitions.  Include this file instead.
//
#ifndef _wdjEXTS_H
#define _wdjEXTS_H

// Include global DDK header
#include <ntddk.h>

// Include base definitions required by WDBGEXTS.H
#include <windef.h>

// Some definitions from <winbase.h> that <wdbgexts.h> depends on
#define WINBASEAPI DECLSPEC_IMPORT
WINBASEAPI HLOCAL WINAPI LocalAlloc(UINT uFlags, UINT uBytes);
WINBASEAPI HLOCAL WINAPI LocalFree(HLOCAL hMem);
#define LMEM_FIXED          0x0000
#define LMEM_ZEROINIT       0x0040
#define LPTR                (LMEM_FIXED | LMEM_ZEROINIT)

#define CopyMemory RtlCopyMemory
#define ZeroMemory RtlZeroMemory

// Include <wdbgexts.h> itself
#include <wdbgexts.h>

// The following four variables are defined in wdjexts.c
extern WINDBG_EXTENSION_APIS ExtensionApis;
extern USHORT MjVersion;
extern USHORT MnVersion;
extern EXT_API_VERSION Version;

// The following structures are taken from the source file
// ddk\src\krnldbg\kdapis\windbgkd.h.  That file could be included
// here, but it's hard to specify on the INCLUDE path.
//
// KPROCESSOR_STATE is the structure of memory accessible with the
// ReadControlSpace() API.  DBGKD_GET_VERSION is the structure 
// returned by the IG_GET_KERNEL_VERSION Ioctl().
typedef struct _DESCRIPTOR {
    WORD    Pad;
    WORD    Limit;
    DWORD   Base;
} KDESCRIPTOR, *PKDESCRIPTOR;
typedef struct _KSPECIAL_REGISTERS {
    DWORD Cr0;
    DWORD Cr2;
    DWORD Cr3;
    DWORD Cr4;
    DWORD KernelDr0;
    DWORD KernelDr1;
    DWORD KernelDr2;
    DWORD KernelDr3;
    DWORD KernelDr6;
    DWORD KernelDr7;
    KDESCRIPTOR Gdtr;
    KDESCRIPTOR Idtr;
    WORD   Tr;
    WORD   Ldtr;
    DWORD Reserved[6];
} KSPECIAL_REGISTERS, *PKSPECIAL_REGISTERS;
typedef struct _KPROCESSOR_STATE {
    struct _CONTEXT ContextFrame;
    struct _KSPECIAL_REGISTERS SpecialRegisters;
} KPROCESSOR_STATE, *PKPROCESSOR_STATE;

typedef struct _DBGKD_GET_VERSION {
    WORD    MajorVersion;   // 0xF == Free, 0xC == Checked
    WORD    MinorVersion;   // 1381 for NT4.0, 1057 for NT3.51
    WORD    ProtocolVersion;
    WORD    Flags;          // DBGKD_VERS_FLAG_XXX
    DWORD   KernBase;       // load address of ntoskrnl.exe
    DWORD   PsLoadedModuleList;
    WORD    MachineType;
    WORD    ThCallbackStack;
    WORD    NextCallback;
    WORD    FramePointer;
    DWORD   KiCallUserMode;
    DWORD   KeUserCallbackDispatcher;
    DWORD   BreakpointWithStatus;
    DWORD   Reserved4;
} DBGKD_GET_VERSION, *PDBGKD_GET_VERSION;
#define DBGKD_VERS_FLAG_MP      0x0001

// wdjGetContext() and wdjGetSpecialRegisters() provide access
// to the information held in control space.
__inline VOID
wdjGetContext(USHORT Processor, PCONTEXT Context)
{
    // Read the CONTEXT out of control space.  
    ReadControlSpace(
        Processor,
        FIELD_OFFSET(KPROCESSOR_STATE, ContextFrame),
        Context, 
        sizeof(CONTEXT) 
        );
}

__inline VOID
wdjGetSpecialRegisters( USHORT Processor, 
                        PKSPECIAL_REGISTERS Registers)
{
    // Read the KSPECIAL_REGISTERS out of control space.  
    ReadControlSpace(
        Processor,
        FIELD_OFFSET(KPROCESSOR_STATE, SpecialRegisters),
        Registers, 
        sizeof(KSPECIAL_REGISTERS)      
        );
}

#if EXT_API_VERSION_NUMBER >= 5
// IG_GET_KERNEL_VERSION is not defined in the version
// of WDBGEXTS.H that shipped with VC++ 5.0.
__inline ULONG
wdjGetKernelVersion(PDBGKD_GET_VERSION VersionInfo)
{
    return Ioctl(IG_GET_KERNEL_VERSION, &VersionInfo, 
                 sizeof(DBGKD_GET_VERSION) );
}
#endif

__inline VOID
wdjWriteControlSpace(USHORT processor, ULONG address, 
                     PVOID buf, ULONG size)
{

    PREADCONTROLSPACE prc;
    prc = (PREADCONTROLSPACE)LocalAlloc(LPTR, sizeof(*prc) + size);
    CopyMemory( prc->Buf, buf, size );
    prc->Processor = processor;
    prc->Address = (ULONG)address;
    prc->BufLen = size;
    Ioctl(IG_WRITE_CONTROL_SPACE, (PVOID)prc, sizeof(*prc) + size);
    LocalFree( prc );
}
#endif // _wdjEXT_H
//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.