/*++
*******************************************************************************
* Copyright (c) 1999-2000 Digi International Inc., All Rights Reserved        *
*                                                                             *
* This software contains proprietary and confidential information of Digi     *
* International Inc.  By accepting transfer of this copy, Recipient agrees    *
* to retain this software in confidence, to prevent disclosure to others,     *
* and to make no use of this software other than that for which it was        *
* delivered.  This is an unpublished copyrighted work of Digi International   *
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly   *
* prohibited.                                                                 *
*                                                                             *
* Restricted Rights Legend                                                    *
*                                                                             *
* Use, duplication, or disclosure by the Government is subject to             *
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in         *
* Technical Data and Computer Software clause at DFARS 252.227-7031 or        *
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -          *
* Restricted Rights at 48 CFR 52.227-19, as applicable.                       *
*                                                                             *
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343          *
*                                                                             *
*******************************************************************************

Module Name:

	kdlib.c

Abstract:

	Wrapper functions for kd APIs.  These can be called from a ddk-centric module, 
	avoiding an attempt to #include usermode windbg headers alongside ddk headers.

	Trying to include both NT4 DDK headers and new (e.g. win2k) Platform SDK windbg
	headers is disasterous due to changes for 64-bit windows in the new sdk.

Author:

	Michael Zarns

--*/

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

#include "kdlib.h"

#include <stdio.h>
#include <stdlib.h>


#ifndef STATIC
#define STATIC static
#endif


///////////////////////////////////////////////////////////////////////////////
//
// Globals and constants
//

#define MAJOR_FREE		0x0f
#define MAJOR_CHECKED	0x0c

#if DBG
#define KDDXB_MAJOR_VER		MAJOR_CHECKED
#define KDDXB_BUILD			"Checked"
#else
#define KDDXB_MAJOR_VER		MAJOR_FREE
#define KDDXB_BUILD			"Free"
#endif


#define KDDXB_API_VERSION	1
EXT_API_VERSION ApiVersion = { EXT_API_VERSION_NUMBER, 0, KDDXB_API_VERSION, 0 };

WINDBG_EXTENSION_APIS ExtensionApis;
USHORT gMajorVersion;
USHORT gMinorVersion;


///////////////////////////////////////////////////////////////////////////////
//
// Standard Windbg Ext Export Routines
//

LPEXT_API_VERSION 
KdextsApiVersion( VOID )
{
    return &ApiVersion;
}


BOOL 
DllInit( 
	HANDLE hModule,
	ULONG  dwReason,
	ULONG  dwReserved )
{
   return TRUE;
}


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

    gMajorVersion = MajorVersion;
    gMinorVersion = MinorVersion;

    return;
}


ULONG WDBGAPI CheckVersion()
{
   if( (gMajorVersion != KDDXB_MAJOR_VER) )
   {
      dprintf("\r\n*** Extension DLL (%s) does not match target system(%d %s)\r\n\r\n",
              KDDXB_BUILD, 
              gMinorVersion, gMajorVersion == MAJOR_FREE ? "Free" : "Checked" );
   }

	return FALSE;
}


DECLARE_API( version )
{
   dprintf( "%s Extension dll for debugging %s kernel for Build %d\n",
            KDDXB_BUILD, gMajorVersion == MAJOR_CHECKED ? "Checked" : "Free",
            gMinorVersion );
}


DECLARE_API( Bang )
{
	// Previously printed help strings, although I don't know how we ever used it.
	// Normally, user types !help which calls the Help function, maybe ! by itself
	// calls this function?
}


///////////////////////////////////////////////////////////////////////////////
//
// Wrapper routines for kd APIs.  Keep all kernel code in other modules.  kd APIs 
// are wrapped up inside this module allows us to avoid needing to #include both 
// kernel headers and SDK headers in a single source file.  Newer SDKs (e.g. win2k)
// behave badly when combined with the older NT4 DDK headers because many things 
// were moved/changed/redefined resulting in LOTS of compile errors.
//

#define BUFFER_MAX (32*1024)
char OutputBuf[BUFFER_MAX];


void __cdecl WdbgPrint( PCSTR pFormat, ... )
{
	va_list ArgList;
	va_start( ArgList, pFormat );
	_vsnprintf( OutputBuf, BUFFER_MAX, pFormat, ArgList );
	va_end( ArgList );

	dprintf( "%s", OutputBuf );
}


ULONG_PTR
WdbgGetExpression( PCSTR pExpression )
{
	return GetExpression( pExpression );
}


ULONG WdbgReadMemory( ULONG_PTR Offset, PVOID pBuffer, ULONG cb, PULONG pcbRead )
{
	return ReadMemory( Offset, pBuffer, cb, pcbRead );
}


ULONG
WdbgCheckControlC( VOID )
{
	return CheckControlC();
}

