123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002 |
- /***********************************************************************
- SRVDBG.CPP
- Server Debug Support
- Copyright (C) 1995 Microsoft Corporation
- All rights reserved.
- Created on: 05/25/95
- ************************************************************************/
- //#include "stdafx.h"
- #ifndef _WINDOWS_
- #define WIN32_LEAN_AND_MEAN
- #define STRICT 1
- #include <windows.h>
- #endif
- #include "srvdbg.h"
- #include "dbgutil.h"
- static HANDLE g_hEventLog = NULL;
- BOOL g_fWantInt3 = FALSE;
- char g_szDbgModPath[MAX_PATH] = "<Path unknown>";
- /***********************************************************************
- FUNCTION: SRVDBG_Init
- Initialize Server Debugging Support
- If NULL is passed for the path, this routine will attempt to
- determine the path through GetModuleFileName, which may not be
- accurate for DLLs, depending on the calling context of the call
- to this routine (won't work for DLLs unless called from ProcessAttach
- notification).
- Created on: 05/26/95
- ************************************************************************/
- BOOL SRVDBG_Init(
- LPSTR lpszSource,
- LPSTR lpszPath)
- {
- char szKey[ 128 ];
- HKEY hk = NULL;
- g_fWantInt3 = FALSE;
- //
- // Check the registry for whether or not we want override the passed in fWantInt3 flag. Stored at DWORD key at:
- // HKLM\\System\\CurrentControlSet\Services\\Sentinal\\WantInt3
- //
- // If a key is missing, it isn't used (i.e. if there's no key in the registry, default to False.
- //
- wsprintf(szKey, "System\\CurrentControlSet\\Services\\Sentinal");
- if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, szKey, &hk))
- {
- BOOL f;
- DWORD dw = sizeof(f);
- if (ERROR_SUCCESS == RegQueryValueEx(hk, "fWantInt3", NULL, NULL, (LPBYTE) &f, &dw))
- {
- g_fWantInt3 = f;
- }
- RegCloseKey(hk);
- }
- if ( !lpszSource )
- return ( FALSE );
- if (!lpszPath)
- GetModuleFileName(NULL, g_szDbgModPath, sizeof(g_szDbgModPath));
- else
- lstrcpy(g_szDbgModPath, lpszPath);
-
- Assert(!g_hEventLog);
- g_hEventLog = RegisterEventSource( NULL, lpszSource );
- return ( g_hEventLog ? TRUE : FALSE );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Terminate
- Terminate Server Debugging Support
- Created on: 05/26/95
- ************************************************************************/
- BOOL SRVDBG_Terminate( void )
- {
- if ( !g_hEventLog )
- return ( FALSE );
- BOOL fDeregisterEventSource = DeregisterEventSource( g_hEventLog );
- Assert(fDeregisterEventSource);
- g_hEventLog = NULL;
- return fDeregisterEventSource;
- }
- /***********************************************************************
- FUNCTION: SRVDBG_ClearLog
- Clear debug event log. SRVDBG_Init must be called first.
- Created on: 05/26/95
- ************************************************************************/
- BOOL SRVDBG_ClearLog( void )
- {
- if ( !g_hEventLog )
- return ( FALSE );
- return ( ClearEventLog( g_hEventLog, NULL ) );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Error
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Error( LPCSTR lpsz )
- {
- SRVDBG_ReportEvent( SRVDBG_ERROR_TYPE, SRVDBG_ERROR, lpsz, NULL, NULL );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Error2
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Error2( LPCSTR lpsz, LPCSTR lpsz2)
- {
- SRVDBG_ReportEvent( SRVDBG_ERROR_TYPE, SRVDBG_ERROR, lpsz, lpsz2, NULL );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Error3
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Error3( LPCSTR lpsz, LPCSTR lpsz2, LPCSTR lpsz3)
- {
- SRVDBG_ReportEvent( SRVDBG_ERROR_TYPE, SRVDBG_ERROR, lpsz, lpsz2, lpsz3 );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Info
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Info( LPCSTR lpsz )
- {
- SRVDBG_ReportEvent( SRVDBG_INFO_TYPE, SRVDBG_INFO, lpsz, NULL, NULL );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Info2
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Info2( LPCSTR lpsz, LPCSTR lpsz2 )
- {
- SRVDBG_ReportEvent( SRVDBG_INFO_TYPE, SRVDBG_INFO, lpsz, lpsz2, NULL );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Info3
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Info3( LPCSTR lpsz, LPCSTR lpsz2, LPCSTR lpsz3 )
- {
- SRVDBG_ReportEvent( SRVDBG_INFO_TYPE, SRVDBG_INFO, lpsz, lpsz2, lpsz3 );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Warning
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Warning( LPCSTR lpsz )
- {
- SRVDBG_ReportEvent( SRVDBG_WARNING_TYPE, SRVDBG_WARNING, lpsz, NULL, NULL );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Warning2
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Warning2( LPCSTR lpsz, LPCSTR lpsz2 )
- {
- SRVDBG_ReportEvent( SRVDBG_WARNING_TYPE, SRVDBG_WARNING, lpsz, lpsz2, NULL );
- }
- /***********************************************************************
- FUNCTION: SRVDBG_Warning3
- Definition: Log error event SRVDBG_Init must be called first.
- Created on: 12/09/97
- Comment: This was a macro before.
- ************************************************************************/
- void SRVDBG_Warning3( LPCSTR lpsz, LPCSTR lpsz2, LPCSTR lpsz3 )
- {
- SRVDBG_ReportEvent( SRVDBG_WARNING_TYPE, SRVDBG_WARNING, lpsz, lpsz2, lpsz3 );
- }
- void SRVDBG_ErrorEvt( DWORD IDEvent, LPCSTR lpsz )
- {
- SRVDBG_ReportEvent( SRVDBG_ERROR_TYPE, IDEvent, lpsz, NULL, NULL );
- }
- void SRVDBG_WarningEvt( DWORD IDEvent, LPCSTR lpsz )
- {
- SRVDBG_ReportEvent( SRVDBG_WARNING_TYPE, IDEvent, lpsz, NULL, NULL );
- }
- void SRVDBG_InfoEvt( DWORD IDEvent, LPCSTR lpsz )
- {
- SRVDBG_ReportEvent( SRVDBG_INFO_TYPE, IDEvent, lpsz, NULL, NULL );
- }
- void SRVDBG_Assert( LPCSTR lpsz )
- {
- SRVDBG_ErrorEvt( SRVDBG_ASSERT, lpsz );
- }
- /***********************************************************************
- FUNCTION: SBRDBG_ReportEvent
- Report error, warning, or information. SRVDBG_Init must be called first.
- History
- Created on: 05/26/95
- 06/05/96 cmason Added special support for getting Assert's logged
- into the QScript Thread Local Storage trave buffer
- so that on Aborts and Exceptions, the Assert will
- show up in the dump of the trace buffer.
- ************************************************************************/
- BOOL SRVDBG_ReportEvent(
- WORD fwEventType,
- DWORD IDEvent,
- LPCSTR lpsz,
- LPCSTR lpsz2,
- LPCSTR lpsz3)
- {
- LPCSTR plpszT[5];
- WORD cStrings;
- BOOL fSuccess;
- cStrings = 5;
- plpszT[0] = g_szDbgModPath;
- plpszT[1] = ""; // was product id
- plpszT[2] = lpsz ? lpsz : "";
- plpszT[3] = lpsz2 ? lpsz2 : "";
- plpszT[4] = lpsz3 ? lpsz3 : "";
- #if 0
- // go ahead and send the event to anyone watching debug output
- for (int i=0; i<cStrings; i++)
- {
- if (*plpszT[i])
- {
- if( SRVDBG_ASSERT == IDEvent )
- {
- TraceN1( "%s ", plpszT[i] );
- }
- else
- {
- ODS(plpszT[i]);
- ODS(" ");
- }
- }
- }
- if( SRVDBG_ASSERT == IDEvent )
- {
- Trace0( "\r\n" );
- }
- else
- {
- ODS("\r\n");
- }
- #endif
- switch ( fwEventType )
- {
- case SRVDBG_INFO_TYPE:
- case SRVDBG_WARNING_TYPE:
- break;
- case SRVDBG_ERROR_TYPE:
- if (g_fWantInt3)
- {
- //
- // 06/13/96 cmason Will only break into a debugger if one is
- // running. If not running under a debugger, does nothing.
- //
- __try
- {
- _asm { int 3 };
- }
- __except (EXCEPTION_EXECUTE_HANDLER)
- {
- }
- }
- //ExitProcess(1);
- break;
- }
- //
- // 05/29/96 cmason Moved check to bottom of function so that Asserts still work even it
- // SRVDBG could not init the event log.
- // 09/08/96 garrettm If no event source, we try to create one on the fly...
- //
- if ( !g_hEventLog )
- {
- HANDLE hLog = RegisterEventSource(NULL, "UNKNOWN");
- if (hLog)
- {
- ReportEvent( hLog, fwEventType, 0, IDEvent, NULL, cStrings, 0, plpszT, NULL );
- DeregisterEventSource( hLog );
- }
- return ( FALSE );
- }
- fSuccess = ReportEvent( g_hEventLog, fwEventType, 0, IDEvent, NULL,
- cStrings, 0, plpszT, NULL );
- return ( fSuccess );
- }
- void SRVDBG_ErrorBinary(LPCSTR lpsz, LPCSTR lpsz2, LPCSTR lpsz3, DWORD dwDataSize, LPVOID lpRawData )
- {
- SRVDBG_ReportEventBinary(SRVDBG_ERROR_TYPE, SRVDBG_ERROR, lpsz, lpsz2, lpsz3, dwDataSize, (LPVOID)lpRawData);
- }
- void SRVDBG_WarningBinary( LPCSTR lpsz, LPCSTR lpsz2, LPCSTR lpsz3, DWORD dwDataSize, LPVOID lpRawData )
- {
- SRVDBG_ReportEventBinary( SRVDBG_WARNING_TYPE, SRVDBG_WARNING, lpsz, lpsz2, lpsz3, dwDataSize, (LPVOID)lpRawData );
- }
- void SRVDBG_InfoBinary( LPCSTR lpsz, LPCSTR lpsz2, LPCSTR lpsz3, DWORD dwDataSize, LPVOID lpRawData )
- {
- SRVDBG_ReportEventBinary( SRVDBG_INFO_TYPE, SRVDBG_INFO, lpsz, lpsz2, lpsz3, dwDataSize, (LPVOID)lpRawData );
- }
- void SRVDBG_RebootMachine()
- {
- SRVDBG_ReportEvent( SRVDBG_ERROR_TYPE, SRVDBG_ERROR, "Reboot Machine", NULL, NULL );
- }
- void SRVDBG_RestartService( LPCSTR lpsz )
- {
- SRVDBG_ReportEvent( SRVDBG_ERROR_TYPE, SRVDBG_ERROR, "Restart Service", lpsz, NULL );
- }
- /***********************************************************************
- FUNCTION: SBRDBG_ReportEventBinary
- Report error, warning, or information in binary form.
-
- SRVDBG_Init must be called first.
- History
- 04/14/97 cmason Created to allow us to log larger pieces of information
- than allowed in the Event Log with strings.
- ************************************************************************/
- BOOL SRVDBG_ReportEventBinary
- (
- WORD fwEventType,
- DWORD IDEvent,
- LPCSTR lpsz,
- LPCSTR lpsz2,
- LPCSTR lpsz3,
- DWORD dwDataSize,
- LPVOID lpRawData
- )
- {
- const WORD cStrings = 5;
- LPCSTR plpszT[cStrings];
- BOOL fSuccess;
- plpszT[0] = g_szDbgModPath;
- plpszT[1] = ""; // was productid
- plpszT[2] = lpsz ? lpsz : "";
- plpszT[3] = lpsz2 ? lpsz2 : "";
- plpszT[4] = lpsz3 ? lpsz3 : "";
- // go ahead and send the event to anyone watching debug output
- for (int i=0; i < cStrings; i++)
- {
- if (*plpszT[i])
- {
- if( SRVDBG_ASSERT == IDEvent )
- {
- TraceN1( "%s ", plpszT[i] );
- }
- else
- {
- ODS(plpszT[i]);
- ODS(" ");
- }
- }
- }
- if( SRVDBG_ASSERT == IDEvent )
- {
- Trace0( "\r\n" );
- }
- else
- {
- ODS("\r\n");
- }
- switch ( fwEventType )
- {
- case SRVDBG_INFO_TYPE:
- case SRVDBG_WARNING_TYPE:
- break;
- case SRVDBG_ERROR_TYPE:
- if (g_fWantInt3)
- {
- //
- // 06/13/96 cmason Will only break into a debugger if one is
- // running. If not running under a debugger, does nothing.
- //
- __try
- {
- _asm { int 3 };
- }
- __except (EXCEPTION_EXECUTE_HANDLER)
- {
- }
- }
- break;
- }
- //
- // 05/29/96 cmason Moved check to bottom of function so that Asserts still work even it
- // SRVDBG could not init the event log.
- // 09/08/96 garrettm If no event source, we try to create one on the fly...
- //
- if ( !g_hEventLog )
- {
- HANDLE hLog = RegisterEventSource(NULL, "UNKNOWN");
- if (hLog)
- {
- ReportEvent( hLog, fwEventType, 0, IDEvent, NULL, cStrings, dwDataSize, plpszT, lpRawData );
- DeregisterEventSource( hLog );
- }
- return ( FALSE );
- }
- fSuccess = ReportEvent( g_hEventLog, fwEventType, 0, IDEvent, NULL,
- cStrings, dwDataSize, plpszT, lpRawData );
- return ( fSuccess );
- }
- /***********************************************************************
- ***********************************************************************/
- void SRVDBG_DoAssert(
- LPCSTR szFile,
- int iLine,
- LPCSTR szAssert )
- {
- char szBuf[ 2048 ];
- wsprintf( szBuf, "File: %s at line %d\r\n%s", szFile, iLine, szAssert );
- SRVDBG_Assert( szBuf );
- }
-
- /***********************************************************************
- FUNCTION: SRVDBG_LogSystemID
- Place an event of some sort (by message ID) into the NT event log.
- Created 29-Mar-96 LeoN
- ************************************************************************/
- BOOL SRVDBG_LogSystemID (
- WORD type, // error, warning, info
- DWORD messageID, // id of message in module
- LPCSTR g_module, // module containing the message
- ...
- ) {
- va_list args;
- HMODULE hMod;
- char * psz;
-
- hMod = GetModuleHandle (g_module);
- if (hMod)
- {
- DWORD id;
- switch (type)
- {
- case 0:
- return TRUE;
- case SRVDBG_INFO_TYPE:
- id = SRVDBG_INFO;
- break;
- case SRVDBG_WARNING_TYPE:
- id = SRVDBG_WARNING;
- break;
- default:
- case SRVDBG_ERROR_TYPE:
- id = SRVDBG_ERROR;
- break;
- }
- va_start(args, g_module);
- if (0 == FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
- | FORMAT_MESSAGE_FROM_HMODULE
- | FORMAT_MESSAGE_FROM_SYSTEM,
- hMod, messageID, 0,
- (char *)(&psz), 16, &args))
- {
- char sz[200];
- wsprintf(sz, "Couldn't find error for id %d", messageID);
- SRVDBG_ReportEvent (SRVDBG_ERROR_TYPE, id, sz, g_module, NULL);
- }
- else
- {
- SRVDBG_ReportEvent (type, id, psz, NULL, NULL);
- LocalFree(psz);
- }
- va_end(args);
- }
- else
- {
- SRVDBG_ReportEvent (SRVDBG_ERROR_TYPE, SRVDBG_ERROR, "Failure Logging Message", g_module, NULL);
- }
- return TRUE;
- }
- void LogSystemSZ(WORD type, LPCSTR szMessage )
- {
- SRVDBG_LogSystemSZ((WORD)type, szMessage);
- }
- /***********************************************************************
- FUNCTION: SRVDBG_LogSystemSZ
- Place an event of some sort (by string) into the NT event log.
- Created 29-Mar-96 LeoN
- ************************************************************************/
- BOOL SRVDBG_LogSystemSZ (
- WORD type, // error, warning, info
- LPCSTR szMessage
- ) {
- DWORD id;
- switch (type)
- {
- case 0:
- return TRUE;
- case SRVDBG_INFO_TYPE:
- id = SRVDBG_INFO;
- break;
- case SRVDBG_WARNING_TYPE:
- id = SRVDBG_WARNING;
- break;
- default:
- case SRVDBG_ERROR_TYPE:
- id = SRVDBG_ERROR;
- break;
- }
- return SRVDBG_ReportEvent (type, id, szMessage, NULL, NULL);
- }
- extern "C" void DoAssert(
- LPCSTR szFile,
- int iLine,
- LPCSTR szAssert )
- {
- char szBuf[ 2048 ];
- wsprintf( szBuf, "File: %s at line %d\r\n%s", szFile, iLine, szAssert );
- SRVDBG_Assert( szBuf );
- }
- #ifdef DEBUG
- // Force Debug initialization early
- // #pragma init_seg(compiler)
- DebugInfo g_dbi = {
- true, // Show trace
- -1, // No TLS trace buffer
- NULL, // No special trace function
- true, // Show asserts
- 0, // Assert count
- false, // Assert on alloc failure
- false, // Stop on Assert
- };
- /*---------------------------------------------------------------------------
- PrivateDbgTraceV
- Trace string with va_list arguments. Handle indenting.
- Author: kennt
- ---------------------------------------------------------------------------*/
- static void PrivateDbgTraceV(LPCSTR szFormat, va_list pvargs)
- {
- char szBuffer[2048+16];
- char *psz = szBuffer;
- #ifdef CORP_BUILD
- psz += wsprintf( psz, "tid(%x): ", GetCurrentThreadId());
- #endif
- wvsprintf(psz, szFormat, pvargs);
- OutputTrace(szBuffer);
- }
- /*!--------------------------------------------------------------------------
- DbgTraceV
- Trace string with va_list arguments. Handle indenting.
- Author: GaryBu
- ---------------------------------------------------------------------------*/
- DBG_API(void) DbgTraceV(LPCSTR szFormat, va_list pvargs)
- {
- PrivateDbgTraceV(szFormat, pvargs);
- }
- /*!--------------------------------------------------------------------------
- DbgTrace
- Trace string with args.
- Author: suryanr
- ---------------------------------------------------------------------------*/
- DBG_APIV(void) DbgTrace(LPSTR szFormat, ...)
- {
- va_list args;
- if (g_dbi.fDoTrace)
- {
- va_start(args, szFormat);
- DbgTraceV(szFormat, args);
- va_end(args);
- }
- }
- /*---------------------------------------------------------------------------
- OutputTrace
- OutputDebugString + any local handling
- Author: MarkGo
- ---------------------------------------------------------------------------*/
- DBG_API(void) OutputTrace(LPCTSTR lpBuffer)
- {
- DbgTraceBuf *ptb;
-
- if (g_dbi.pfnTrace)
- g_dbi.pfnTrace(lpBuffer);
- else
- ODS(lpBuffer);
- if (g_dbi.itlsTraceRGB != (DWORD) -1
- && (ptb = (DbgTraceBuf *)TlsGetValue(g_dbi.itlsTraceRGB))!=NULL )
- {
- DWORD cbOut = lstrlen(lpBuffer)+1;
- // Buffer the trace output
- // Is output larger than buf?
- if (cbOut >= ptb->cchBuf)
- {
- // Copy *last* cchBuf chars
- memcpy(ptb->rgch, lpBuffer + (cbOut - ptb->cchBuf), ptb->cchBuf);
- ptb->ichStart = 0;
- ptb->ichEnd = ptb->cchBuf-1;
- }
- else
- {
- // Is output larger than remaining contig space?
- if (cbOut > ptb->cchBuf - ptb->ichEnd)
- {
- memcpy(&(ptb->rgch[ptb->ichEnd]), lpBuffer,
- ptb->cchBuf - ptb->ichEnd);
- memcpy(ptb->rgch, lpBuffer+(ptb->cchBuf-ptb->ichEnd),
- cbOut-(ptb->cchBuf-ptb->ichEnd));
- ptb->ichStart = cbOut-(ptb->cchBuf-ptb->ichEnd);
- ptb->ichEnd = ptb->ichStart-1;
- }
- else
- {
- memcpy(&ptb->rgch[ptb->ichEnd], lpBuffer, cbOut);
- if (ptb->ichStart > ptb->ichEnd)
- {
- ptb->ichEnd += cbOut-1;
- ptb->ichStart = ptb->ichEnd+1;
- }
- else
- ptb->ichEnd += cbOut-1;
- }
-
- }
- }
- }
- #ifndef FDbgFalse
- /*!--------------------------------------------------------------------------
- FDbgFalse
- Returns false, but insures that debug code page is in memory.
- Also makes a handy breakpoint.
- Author: MarkGo
- ---------------------------------------------------------------------------*/
- DBG_API(BOOL) FDbgFalse()
- {
- return false;
- }
- #endif
- #ifdef __AFX_H__
- /*!--------------------------------------------------------------------------
- AfxAssertFailedLine
- Overrides Afx's assertion handling
- Author: GaryBu, kennt, MarkGo
- ---------------------------------------------------------------------------*/
- BOOL AFXAPI AfxAssertFailedLine(LPCSTR lpszFileName, int nLine)
- {
- DbgAssert(lpszFileName, nLine, "AFX Assert");
- return false;
- }
- #endif
- /*!--------------------------------------------------------------------------
- DbgAssert
- Display assert dialog.
- Author: GaryBu, kennt, MarkGo
- ---------------------------------------------------------------------------*/
- DBG_APIV(void) DbgAssert(LPCSTR szFile, int iLine, LPCSTR szFmt, ...)
- {
- va_list arg;
- char sz[MAX_PATH*2];
- char *pch = sz;
- va_start(arg, szFmt);
- pch += wvsprintf(sz, szFmt, arg);
- va_end(arg);
- // Remove trailing newlines...
- while (*(pch-1) == '\n' || *(pch-1) == '\r')
- --pch;
- *pch = 0;
- if (g_dbi.fShowAsserts)
- {
- //
- // 03/27/96 cmason Added doing trace output as well as the Event Viewer
- // entry for asserts.
- // 04/04/96 cmason Removed redundancy of tracing out the assert twice, once
- // here and agian inside the SRVDBG code called by DoAssert. The code here
- // was first, but the the SRVDBG code traces out more events than just asserts.
- //
- DbgTrace( "Assert failed: ");
- //
- // This loggs to the Event Viewer
- //
- DoAssert(szFile, iLine, sz);
- //
- // 03/27/96 cmason Added breaking into the debugger if requested.
- //
- if( g_dbi.fStopOnAssert )
- {
- //
- // This does an Int 3 and is protected by byt __try/__except so
- // that nothing happens if a debugger isn't running.
- //
- DbgStop();
- }
- }
- else
- DbgTrace(
- "Skipped Assert (ShowAsserts is FALSE):\r\n%s (file %s, line %d)\r\n",
- sz, szFile, iLine);
- }
- /*!--------------------------------------------------------------------------
- DbgStop -- causes a break into the debugger if the debugger is running.
- -
- Author: afx
- ---------------------------------------------------------------------------*/
- #pragma optimize("qgel", off) // assembler cannot be globally optimized
- DBG_API(void) DbgStop()
- {
- __try
- {
- _asm { int 3 };
- }
- __except (EXCEPTION_EXECUTE_HANDLER)
- {
- }
- }
- DBG_API(DWORD) DbgSetThreadTraceBuffer(DWORD itlsTrace)
- {
- DWORD itlsPrev = g_dbi.itlsTraceRGB;
- g_dbi.itlsTraceRGB = itlsTrace;
- return itlsPrev;
- }
- DBG_API(const char *) DbgGetTraceBuffer(BOOL fSecondChunk)
- {
- DbgTraceBuf *ptb;
-
- if (g_dbi.itlsTraceRGB == (DWORD)-1)
- return fSecondChunk ? "" : "<No trace information available>\r\n";
-
- if (NULL==(ptb = (DbgTraceBuf *)TlsGetValue(g_dbi.itlsTraceRGB)))
- return fSecondChunk ? "" : "<No trace buffer set>\r\n";
- if (!fSecondChunk)
- {
- // Return initial chunk, regardless of wraparound.
- return &(ptb->rgch[ptb->ichStart]);
- }
-
- // We only have a second chunk in wraparound
- if (ptb->ichStart > ptb->ichEnd)
- return ptb->rgch;
- return "";
- }
- DBG_API(BOOL) DbgSetTraceBuffer(void *rgbTrace, int cbTrace)
- {
- DbgTraceBuf *ptb;
-
- if (g_dbi.itlsTraceRGB == (DWORD) -1
- || (rgbTrace && cbTrace <= sizeof(DbgTraceBuf)))
- return false;
- ptb = (DbgTraceBuf *)rgbTrace;
- if (ptb)
- {
- ptb->cchBuf = cbTrace - sizeof(DbgTraceBuf);
- ptb->ichStart = ptb->ichEnd = 0;
- ptb->rgch[ptb->ichEnd] = '\0';
- ptb->rgch[ptb->cchBuf] = '\0';
- }
-
- TlsSetValue(g_dbi.itlsTraceRGB, ptb);
- return true;
- }
- #pragma optimize("", on)
- #endif //DEBUG
- /***********************************************************************
- FUNCTION: LoadMsg
- Loads a string from the message table and returns it in an allocated
- string buffer.
- History
- Created on: 06/01/95
- 04/29/96 cmason Trav#336: Added FORMAT_MESSAGE_MAX_WIDTH_MASK
- so that CRLF's must be explicitly specified.
- ************************************************************************/
- char *LoadMsg(UINT idMsg,
- HMODULE hmod) // May be null, in which case app's msg table is used
- {
- void *pv;
- char *pszRet;
- DWORD cch;
- cch = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
- | FORMAT_MESSAGE_IGNORE_INSERTS
- | FORMAT_MESSAGE_FROM_HMODULE
- | FORMAT_MESSAGE_FROM_SYSTEM
- | FORMAT_MESSAGE_MAX_WIDTH_MASK,
- hmod, idMsg, 0 /*Default language*/,
- (char *)(&pv), 16, NULL);
- if ( !cch )
- return NULL;
- //
- // 04/30/96 cmason #336: Removing the trailing space.
- //
- Assert( ' ' == ((char *)pv)[cch - 1]);
- ((char *)pv)[cch - 1] = '\0';
- pszRet = new char[cch];
- memcpy(pszRet, pv, cch);
- LocalFree(pv);
- return pszRet;
- }
|