📄 dbgrptt.c
字号:
{
ReportHookNode *pnode=NULL;
ReportHookNodeW *pnodeW=NULL;
_mlock(_DEBUG_LOCK);
__try
{
for (pnode = _pReportHookList; pnode; pnode = pnode->next)
{
int hook_retval=0;
if ((*pnode->pfnHookFunc)(nRptType, szOutMessage, &hook_retval))
{
handled=TRUE;
retval=hook_retval;
break;
}
}
if(!handled)
{
for (pnodeW = _pReportHookListW; pnodeW; pnodeW = pnodeW->next)
{
int hook_retval=0;
if ((*pnodeW->pfnHookFunc)(nRptType, szOutMessage2, &hook_retval))
{
handled=TRUE;
retval=hook_retval;
break;
}
}
}
}
__finally
{
_munlock(_DEBUG_LOCK);
}
}
if(!handled)
{
if(_pfnReportHook)
{
int hook_retval=0;
if(((*_pfnReportHook)(nRptType, szOutMessage, &hook_retval)))
{
handled=TRUE;
retval=hook_retval;
}
}
if (!handled)
{
if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_FILE)
{
if (_CrtDbgFile[nRptType] != _CRTDBG_INVALID_HFILE)
{
DWORD written;
WriteFile(_CrtDbgFile[nRptType], szOutMessage, (unsigned long)strlen(szOutMessage), &written, NULL);
}
}
if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_DEBUG)
{
OutputDebugStringA(szOutMessage);
}
if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_WNDW)
{
szLineMessage[0] = 0;
if (nLine)
{
_ERRCHECK(_itoa_s(nLine, szLineMessage, DBGRPT_MAX_MSG, 10));
}
retval = __crtMessageWindowA(nRptType, szFile, (nLine ? szLineMessage : NULL), szModule, szUserMessage);
}
}
}
}
}
__finally
{
if (_CRT_ASSERT == nRptType)
{
InterlockedDecrement(&_crtAssertBusy);
}
}
return retval;
}
#pragma warning(pop)
/***
*int _VCrtDbgReportW() - _CrtDbgReportW calls into this function
*
*Purpose:
* See remarks for _CrtDbgReport.
*
*Entry:
* int nRptType - report type
* const wchar_t * szFile - file name
* int nLine - line number
* const wchar_t * szModule - module name
* const wchar_t * szFormat - format string
* va_list arglist - var args arglist
*
*Exit:
* See remarks for _CrtDbgReport
*
*Exceptions:
*
*******************************************************************************/
#pragma warning(push)
#pragma warning(disable:6262)
// prefast(6262): This func uses lots of stack because we want to tolerate very large reports, and we can't use malloc here.
_CRTIMP int __cdecl _VCrtDbgReportW
(
int nRptType,
const wchar_t * szFile,
int nLine,
const wchar_t * szModule,
const wchar_t * szFormat,
va_list arglist
)
{
int retval=0;
int handled=FALSE;
wchar_t szLineMessage[DBGRPT_MAX_MSG] = {0};
wchar_t szOutMessage[DBGRPT_MAX_MSG] = {0};
char szOutMessage2[DBGRPT_MAX_MSG] = {0};
wchar_t szUserMessage[DBGRPT_MAX_MSG] = {0};
if (nRptType < 0 || nRptType >= _CRT_ERRCNT)
return -1;
/*
* handle the (hopefully rare) case of
*
* 1) ASSERT while already dealing with an ASSERT
* or
* 2) two threads asserting at the same time
*/
__try
{
if (_CRT_ASSERT == nRptType && InterlockedIncrement(&_crtAssertBusy) > 0)
{
/* use only 'safe' functions -- must not assert in here! */
_ERRCHECK(_itow_s(nLine, szLineMessage, DBGRPT_MAX_MSG, 10));
OutputDebugStringW(L"Second Chance Assertion Failed: File ");
OutputDebugStringW(szFile ? szFile : L"<file unknown>");
OutputDebugStringW(L", Line ");
OutputDebugStringW(szLineMessage);
OutputDebugStringW(L"\n");
_CrtDbgBreak();
retval=-1;
}
else
{
// Leave space for ASSERTINTRO1 and "\r\n"
if (szFormat)
{
int szlen;
_ERRCHECK_SPRINTF(szlen = _vsnwprintf_s(szUserMessage, DBGRPT_MAX_MSG,
DBGRPT_MAX_MSG - 2 -
(max(sizeof(_CRT_WIDE(ASSERTINTRO1)),sizeof(_CRT_WIDE(ASSERTINTRO2)))/sizeof(wchar_t)),
szFormat,
arglist));
if (szlen < 0)
{
_ERRCHECK(wcscpy_s(szUserMessage, DBGRPT_MAX_MSG, _CRT_WIDE(DBGRPT_TOOLONGMSG)));
}
}
if (_CRT_ASSERT == nRptType)
_ERRCHECK(wcscpy_s(szLineMessage, DBGRPT_MAX_MSG, szFormat ? _CRT_WIDE(ASSERTINTRO1) : _CRT_WIDE(ASSERTINTRO2)));
_ERRCHECK(wcscat_s(szLineMessage, DBGRPT_MAX_MSG, szUserMessage));
if (_CRT_ASSERT == nRptType)
{
if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_FILE)
_ERRCHECK(wcscat_s(szLineMessage, DBGRPT_MAX_MSG, L"\r"));
{
_ERRCHECK(wcscat_s(szLineMessage, DBGRPT_MAX_MSG, L"\n"));
}
}
if (szFile)
{
int szlen = 0;
_ERRCHECK_SPRINTF(szlen = _snwprintf_s(szOutMessage, DBGRPT_MAX_MSG, DBGRPT_MAX_MSG, L"%s(%d) : %s",
szFile, nLine, szLineMessage));
if (szlen < 0)
_ERRCHECK(wcscpy_s(szOutMessage, DBGRPT_MAX_MSG, _CRT_WIDE(DBGRPT_TOOLONGMSG)));
}
else
{
_ERRCHECK(wcscpy_s(szOutMessage, DBGRPT_MAX_MSG, szLineMessage));
}
/* scope */
{
errno_t e = 0;
e = _ERRCHECK_EINVAL_ERANGE(wcstombs_s(NULL, szOutMessage2, DBGRPT_MAX_MSG, szOutMessage, _TRUNCATE));
if(e != 0)
_ERRCHECK(strcpy_s(szOutMessage2, DBGRPT_MAX_MSG, DBGRPT_INVALIDMSG));
}
/* User hook may handle report.
We have to check the ANSI Hook2 List & then the UNICODE Hook2 List.
Then we have check any ANSI individual Hook set through
SetReportHook */
if (_pReportHookList || _pReportHookListW)
{
ReportHookNode *pnode=NULL;
ReportHookNodeW *pnodeW=NULL;
_mlock(_DEBUG_LOCK);
__try
{
for (pnode = _pReportHookList; pnode; pnode = pnode->next)
{
int hook_retval=0;
if ((*pnode->pfnHookFunc)(nRptType, szOutMessage2, &hook_retval))
{
retval=hook_retval;
handled=TRUE;
break;
}
}
if(!handled)
{
for (pnodeW = _pReportHookListW; pnodeW; pnodeW = pnodeW->next)
{
int hook_retval=0;
if ((*pnodeW->pfnHookFunc)(nRptType, szOutMessage, &hook_retval))
{
retval=hook_retval;
handled=TRUE;
break;
}
}
}
}
__finally
{
_munlock(_DEBUG_LOCK);
}
}
if(!handled)
{
if(_pfnReportHook)
{
int hook_retval=0;
if(((*_pfnReportHook)(nRptType, szOutMessage2, &hook_retval)))
{
retval=hook_retval;
handled=TRUE;
}
}
if (!handled)
{
if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_FILE)
{
if (_CrtDbgFile[nRptType] != _CRTDBG_INVALID_HFILE)
{
/* Use WriteConsole for Consoles, WriteFile otherwise */
DWORD written;
switch(GetFileType(_CrtDbgFile[nRptType]))
{
case FILE_TYPE_CHAR:
if(WriteConsoleW(_CrtDbgFile[nRptType], szOutMessage, (unsigned long)wcslen(szOutMessage), &written, NULL))
break;
/* If WriteConsole fails & LastError is ERROR_INVALID_VALUE,
then the console is redirected */
if(GetLastError() != ERROR_INVALID_HANDLE)
break;
default:
{
char szaOutMessage[DBGRPT_MAX_MSG];
size_t ret = 0;
errno_t e = _ERRCHECK_EINVAL_ERANGE(wcstombs_s(&ret, szaOutMessage, DBGRPT_MAX_MSG, szOutMessage, _TRUNCATE));
if(e != 0 && e != STRUNCATE)
{
WriteFile(_CrtDbgFile[nRptType], szOutMessage, (unsigned long)wcslen(szOutMessage) * 2, &written, NULL);
}
else
{
/* ret counts for the null terminator as well */
if (ret > 0)
{
--ret;
}
WriteFile(_CrtDbgFile[nRptType], szaOutMessage, (unsigned long)ret, &written, NULL);
}
}
}
}
}
if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_DEBUG)
{
OutputDebugStringW(szOutMessage);
}
if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_WNDW)
{
szLineMessage[0] = 0;
if (nLine)
{
_ERRCHECK(_itow_s(nLine, szLineMessage, DBGRPT_MAX_MSG, 10));
}
retval = __crtMessageWindowW(nRptType, szFile, (nLine ? szLineMessage : NULL), szModule, szUserMessage);
}
}
}
}
}
__finally
{
if (_CRT_ASSERT == nRptType)
{
InterlockedDecrement(&_crtAssertBusy);
}
}
return retval;
}
#pragma warning(pop)
#endif /* _DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -