📄 wxdebug.cpp
字号:
TCHAR szInfo[iDEBUGINFO]; // Constructs key names
HKEY hModuleKey; // Module key handle
/* Construct the base key name */
wsprintf(szInfo,TEXT("%s\\%s"),pBaseKey,m_ModuleName);
/* Create or open the key for this module */
lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
szInfo, // Address of subkey name
(DWORD) 0, // Reserved value
NULL, // Address of class name
(DWORD) 0, // Special options flags
KEY_ALL_ACCESS, // Desired security access
NULL, // Key security descriptor
&hModuleKey, // Opened handle buffer
NULL); // What really happened
if (lReturn != ERROR_SUCCESS) {
DbgLog((LOG_ERROR,0,TEXT("Could not access module key")));
return;
}
DbgInitLogTo(hModuleKey);
DbgInitKeyLevels(hModuleKey, fTakeMax);
RegCloseKey(hModuleKey);
}
/* Initialise the module file name */
void WINAPI DbgInitModuleName()
{
TCHAR FullName[iDEBUGINFO]; // Load the full path and module name
TCHAR *pName; // Searches from the end for a backslash
GetModuleFileName(m_hInst,FullName,iDEBUGINFO);
pName = _tcsrchr(FullName,'\\');
if (pName == NULL) {
pName = FullName;
} else {
pName++;
}
lstrcpy(m_ModuleName,pName);
}
struct MsgBoxMsg
{
HWND hwnd;
TCHAR *szTitle;
TCHAR *szMessage;
DWORD dwFlags;
INT iResult;
};
//
// create a thread to call MessageBox(). calling MessageBox() on
// random threads at bad times can confuse the host (eg IE).
//
DWORD WINAPI MsgBoxThread(
LPVOID lpParameter // thread data
)
{
MsgBoxMsg *pmsg = (MsgBoxMsg *)lpParameter;
pmsg->iResult = MessageBox(
pmsg->hwnd,
pmsg->szTitle,
pmsg->szMessage,
pmsg->dwFlags);
return 0;
}
INT MessageBoxOtherThread(
HWND hwnd,
TCHAR *szTitle,
TCHAR *szMessage,
DWORD dwFlags)
{
if(g_fDbgInDllEntryPoint)
{
// can't wait on another thread because we have the loader
// lock held in the dll entry point.
return MessageBox(hwnd, szTitle, szMessage, dwFlags);
}
else
{
MsgBoxMsg msg = {hwnd, szTitle, szMessage, dwFlags, 0};
DWORD dwid;
HANDLE hThread = CreateThread(
0, // security
0, // stack size
MsgBoxThread,
(void *)&msg, // arg
0, // flags
&dwid);
if(hThread)
{
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return msg.iResult;
}
// break into debugger on failure.
return IDCANCEL;
}
}
/* Displays a message box if the condition evaluated to FALSE */
void WINAPI DbgAssert(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine)
{
if(g_fUseKASSERT)
{
DbgKernelAssert(pCondition, pFileName, iLine);
}
else
{
TCHAR szInfo[iDEBUGINFO];
wsprintf(szInfo, TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
pCondition, iLine, pFileName);
INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"),
MB_SYSTEMMODAL |
MB_ICONHAND |
MB_YESNOCANCEL |
MB_SETFOREGROUND);
switch (MsgId)
{
case IDNO: /* Kill the application */
FatalAppExit(FALSE, TEXT("Application terminated"));
break;
case IDCANCEL: /* Break into the debugger */
DebugBreak();
break;
case IDYES: /* Ignore assertion continue execution */
break;
}
}
}
/* Displays a message box at a break point */
void WINAPI DbgBreakPoint(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine)
{
if(g_fUseKASSERT)
{
DbgKernelAssert(pCondition, pFileName, iLine);
}
else
{
TCHAR szInfo[iDEBUGINFO];
wsprintf(szInfo, TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
pCondition, iLine, pFileName);
INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"),
MB_SYSTEMMODAL |
MB_ICONHAND |
MB_YESNOCANCEL |
MB_SETFOREGROUND);
switch (MsgId)
{
case IDNO: /* Kill the application */
FatalAppExit(FALSE, TEXT("Application terminated"));
break;
case IDCANCEL: /* Break into the debugger */
DebugBreak();
break;
case IDYES: /* Ignore break point continue execution */
break;
}
}
}
void WINAPI DbgBreakPoint(const TCHAR *pFileName,INT iLine,const TCHAR* szFormatString,...)
{
// A debug break point message can have at most 2000 characters if
// ANSI or UNICODE characters are being used. A debug break point message
// can have between 1000 and 2000 double byte characters in it. If a
// particular message needs more characters, then the value of this constant
// should be increased.
const DWORD MAX_BREAK_POINT_MESSAGE_SIZE = 2000;
TCHAR szBreakPointMessage[MAX_BREAK_POINT_MESSAGE_SIZE];
const DWORD MAX_CHARS_IN_BREAK_POINT_MESSAGE = sizeof(szBreakPointMessage) / sizeof(TCHAR);
va_list va;
va_start( va, szFormatString );
int nReturnValue = _vsntprintf( szBreakPointMessage, MAX_CHARS_IN_BREAK_POINT_MESSAGE, szFormatString, va );
va_end(va);
// _vsnprintf() returns -1 if an error occurs.
if( -1 == nReturnValue ) {
DbgBreak( "ERROR in DbgBreakPoint(). The variable length debug message could not be displayed because _vsnprintf() failed." );
return;
}
::DbgBreakPoint( szBreakPointMessage, pFileName, iLine );
}
/* When we initialised the library we stored in the m_Levels array the current
debug output level for this module for each of the five categories. When
some debug logging is sent to us it can be sent with a combination of the
categories (if it is applicable to many for example) in which case we map
the type's categories into their current debug levels and see if any of
them can be accepted. The function looks at each bit position in turn from
the input type field and then compares it's debug level with the modules.
A level of 0 means that output is always sent to the debugger. This is
due to producing output if the input level is <= m_Levels.
*/
BOOL WINAPI DbgCheckModuleLevel(DWORD Type,DWORD Level)
{
if(g_fAutoRefreshLevels)
{
// re-read the registry every second. We cannot use RegNotify() to
// notice registry changes because it's not available on win9x.
static DWORD g_dwLastRefresh = 0;
DWORD dwTime = timeGetTime();
if(dwTime - g_dwLastRefresh > 1000) {
g_dwLastRefresh = dwTime;
// there's a race condition: multiple threads could update the
// values. plus read and write not synchronized. no harm
// though.
DbgInitModuleSettings(false);
}
}
DWORD Mask = 0x01;
// If no valid bits are set return FALSE
if ((Type & ((1<<iMAXLEVELS)-1))) {
// speed up unconditional output.
if (0==Level)
return(TRUE);
for (LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
if (Type & Mask) {
if (Level <= (m_Levels[lKeyPos] & ~LOG_FORCIBLY_SET)) {
return TRUE;
}
}
Mask <<= 1;
}
}
return FALSE;
}
/* Set debug levels to a given value */
void WINAPI DbgSetModuleLevel(DWORD Type, DWORD Level)
{
DWORD Mask = 0x01;
for (LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
if (Type & Mask) {
m_Levels[lKeyPos] = Level | LOG_FORCIBLY_SET;
}
Mask <<= 1;
}
}
/* whether to check registry values periodically. this isn't turned
automatically because of the potential performance hit. */
void WINAPI DbgSetAutoRefreshLevels(bool fAuto)
{
g_fAutoRefreshLevels = fAuto;
}
#ifdef UNICODE
//
// warning -- this function is implemented twice for ansi applications
// linking to the unicode library
//
void WINAPI DbgLogInfo(DWORD Type,DWORD Level,const CHAR *pFormat,...)
{
/* Check the current level for this type combination */
BOOL bAccept = DbgCheckModuleLevel(Type,Level);
if (bAccept == FALSE) {
return;
}
TCHAR szInfo[2000];
/* Format the variable length parameter list */
va_list va;
va_start(va, pFormat);
lstrcpy(szInfo,m_ModuleName);
wsprintf(szInfo + lstrlen(szInfo),
TEXT("(tid %x) %8d : "),
GetCurrentThreadId(), timeGetTime() - dwTimeOffset);
CHAR szInfoA[2000];
WideCharToMultiByte(CP_ACP, 0, szInfo, -1, szInfoA, NUMELMS(szInfoA), 0, 0);
wvsprintfA(szInfoA + lstrlenA(szInfoA), pFormat, va);
lstrcatA(szInfoA, "\r\n");
WCHAR wszOutString[2000];
MultiByteToWideChar(CP_ACP, 0, szInfoA, -1, wszOutString, NUMELMS(wszOutString));
DbgOutString(wszOutString);
va_end(va);
}
void WINAPI DbgAssert(const CHAR *pCondition,const CHAR *pFileName,INT iLine)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -