Path: news.mitre.org!blanket.mitre.org!philabs!newsjunkie.ans.net!newsfeeds.ans.net!news-was.dfn.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!logbridge.uoregon.edu!zdc!szdc!super.zippo.com!newsp.zippo.com!sdrn
From: lexis@super.zippo.com
Newsgroups: comp.os.ms-windows.programmer.nt.kernel-mode
Subject: Re: kernel debugger extensions (help!)
Date: 6 Dec 1997 12:52:57 -0800
Organization: Super Zippo News Service [http://super.zippo.com]
Lines: 162
Message-ID: <66cdv9$j3m@sdrn.zippo.com>
References: <01bd0205$96fdcf30$c7b38686@eguadalu-desk>
NNTP-Posting-Host: p-960.newsdawg.com

In article <01bd0205$96fdcf30$c7b38686@eguadalu-desk>, "eric says...
>
>Hi,
>
>Anyone ever successfully create a custom debugger extension dll?
>I've been looking through the DDK, SDK, VC5, and the Windbg help
>file and all four seem to show different ways of implementing
>extension functions.  Unfortunately, none of them work.  I tried
>compiling the kdexts example from the 4.0 DDK but windbg gave an
>error saying the dll caused an exception.
>
>Is there some kind of versioning system I'm missing?  Would anyone 
>with a successful experience doing this please, *PLEASE*, tell me
>how you did it?
>
>Thanks,
>Eric

Go back and try \MSTOOLS\samples\win32\wdbgexts.  The examples in 
\DDK\src\krnldbg are errant B.S. published by Microsoft four times
a year to trap the unwary.  

I will attach the DLL interface part of my extensions below.  Note that
it was necessary to separate the DLL interface from the application
code due to conflicts between wdbgexts.h and ndis.h.  Also, I never get
consistent results when calling ReadMemory for more than 1K bytes.  Also, you
may have to kick windbag pretty hard to load symbols when not at a
breakpoint, etc.  The extension mechanism seems no less buggy than windbag
itself, but I still like having extensions.  

#include <stdio.h>
#include <wtypes.h>
#include <wdbgexts.h>
#include <stdarg.h>

WINDBG_EXTENSION_APIS  ExtensionApis;

#define API_VERSION_NUMBER 4

typedef struct API_VERSION {
    USHORT  MajorVersion;
    USHORT  MinorVersion;
    USHORT  Revision;
    USHORT  Reserved;
} API_VERSION, *LPAPI_VERSION;

API_VERSION            ApiVersion = { 3, 5, API_VERSION_NUMBER, 0 };
USHORT                 SavedMajorVersion;
USHORT                 SavedMinorVersion;


VOID
WinDbgExtensionDllInit(
    PWINDBG_EXTENSION_APIS lpExtensionApis,
    USHORT MajorVersion,
    USHORT MinorVersion
    )
{
    ExtensionApis = *lpExtensionApis;

    SavedMajorVersion = MajorVersion;

    SavedMinorVersion = MinorVersion;

    dprintf("MajorVersion %d, MinorVersion %d\n", MajorVersion, MinorVersion);
    return;
}

VOID
CheckVersion(
    VOID
    )
{
}

LPAPI_VERSION
ExtensionApiVersion(
    VOID
    )
{
}

LPAPI_VERSION
ExtensionApiVersion(
    VOID
    )
{
    return &ApiVersion;
}

unsigned long _GetExpression(char *expression)
{
    return (GetExpression(expression));
}

void _GetSymbol(LPVOID address, PUCHAR buffer, LPDWORD lpdwDisplacement)
{
     GetSymbol(address, buffer, lpdwDisplacement);
}

int _CheckControlC(VOID)
{
    return (CheckControlC());
}

int _ReadMemory(DWORD offset, LPVOID lpBuffer, DWORD cb, LPDWORD 
lpcbBytesRead)
{
        int i;
        DWORD Count;
        DWORD Actual = 0;
        DWORD Temp;

        while (cb) {
                Count = MIN(1024, cb);
        if((ReadMemory(offset, lpBuffer, Count, &Temp)) == 0)
                return 0;
        cb -= Count;
        offset += Count;
        (PUCHAR)lpBuffer += Count;
        Actual += Temp;
    }
    *lpcbBytesRead = Actual;
    return 1;
}

int _WriteMemory(DWORD offset, LPVOID lpBuffer, DWORD cb, LPDWORD 
lpcbBytesWritt
en)
{
    return(WriteMemory(offset, lpBuffer, cb, lpcbBytesWritten));
}

DECLARE_API(lecdump)
{
     _lecdump(args);
}

DECLARE_API(lec)
{
     _lec(args);
}

DECLARE_API(macdump)
{
     _macdump(args);
}

DECLARE_API(sigdump)
{
     _sigdump(args);
}

DECLARE_API(vers)
{
     dprintf("%s%s\n", PKG_VERSION_STR, PKG_BUILDTAG);
}

DECLARE_API(dbg)
{
     _dbg(args);
}
