BOOL RaiseExceptionOnAssert(RaiseOnAssertOptions option = rTestAndRaise) { STATIC_CONTRACT_NOTHROW; STATIC_CONTRACT_GC_NOTRIGGER; STATIC_CONTRACT_DEBUG_ONLY; STATIC_CONTRACT_FORBID_FAULT; STATIC_CONTRACT_SUPPORTS_DAC; // ok for debug-only code to take locks CONTRACT_VIOLATION(TakesLockViolation); DWORD fRet = 0; #if !defined(DACCESS_COMPILE) static ConfigDWORD fRaiseExceptionOnAssert; // // we don't want this config key to affect mscordacwks as well! // EX_TRY { fRet = fRaiseExceptionOnAssert.val(CLRConfig::INTERNAL_RaiseExceptionOnAssert); } EX_CATCH { } EX_END_CATCH(SwallowAllExceptions); if (option == rTestAndRaise && fRet != 0) { DoRaiseExceptionOnAssert(fRet); } #endif // !DACCESS_COMPILE return fRet != 0; }
BOOL DebugBreakOnAssert() { STATIC_CONTRACT_NOTHROW; STATIC_CONTRACT_GC_NOTRIGGER; STATIC_CONTRACT_DEBUG_ONLY; STATIC_CONTRACT_FORBID_FAULT; STATIC_CONTRACT_SUPPORTS_DAC; // ok for debug-only code to take locks CONTRACT_VIOLATION(TakesLockViolation); BOOL fRet = FALSE; #ifndef DACCESS_COMPILE static ConfigDWORD fDebugBreak; // // we don't want this config key to affect mscordacwks as well! // EX_TRY { fRet = fDebugBreak.val(CLRConfig::INTERNAL_DebugBreakOnAssert); } EX_CATCH { } EX_END_CATCH(SwallowAllExceptions); #endif // DACCESS_COMPILE return fRet; }
DWORD WINAPI APIThreadStress::StartThread(void *arg) { APIThreadStress *pThis = (APIThreadStress *) arg; ClrFlsSetValue(TlsIdx_StressThread, pThis); EX_TRY { // Perform initial synchronization WaitForSingleObjectEx(pThis->m_syncEvent, INFINITE, FALSE); InterlockedIncrement(&pThis->m_runCount); LOG((LF_SYNC, LL_INFO100, "Stressing operation on thread %d\n", GetCurrentThreadId())); ((APIThreadStress *)arg)->Invoke(); LOG((LF_SYNC, LL_INFO100, "End stress operation on thread %d\n", GetCurrentThreadId())); if (InterlockedDecrement(&pThis->m_runCount) == 0) ::SetEvent(pThis->m_syncEvent); } EX_CATCH { LOG((LF_SYNC, LL_ERROR, "Exception during stress operation on thread %d\n", GetCurrentThreadId())); } EX_END_CATCH(SwallowAllExceptions); return 0; }
HRESULT CSimpleFusionBindSink::Init(void **ppInterface) { HRESULT hr = S_OK; _ASSERTE(ppInterface); _ppInterface = ppInterface; if(_hEvent == NULL) { // Initialize the event to require manual reset // and to initially signaled. _hEvent = NEW(Event()); if (!_hEvent) { hr = E_OUTOFMEMORY; goto Exit; } EX_TRY { _hEvent->CreateManualEvent(TRUE); } EX_CATCH { hr = GET_EXCEPTION()->GetHR(); } EX_END_CATCH(SwallowAllExceptions); }
//***************************************************************************** // Pull the PEKind and Machine out of PE headers -- if we have PE headers. //***************************************************************************** HRESULT CLiteWeightStgdbRW::GetPEKind( // S_OK or error. MAPPINGTYPE mtMapping, // The type of mapping the image has DWORD *pdwPEKind, // [OUT] The kind of PE (0 - not a PE) DWORD *pdwMachine) // [OUT] Machine as defined in NT header { HRESULT hr = NOERROR; DWORD dwKind=0; // Working copy of pe kind. DWORD dwMachine=0; // Working copy of machine. #ifndef DACCESS_COMPILE // Do we already have cached information? if (m_dwPEKind != (DWORD)(-1)) { dwKind = m_dwPEKind; dwMachine = m_dwMachine; } else if (m_pImage) { NewHolder<PEDecoder> pe; EX_TRY { // We need to use different PEDecoder constructors based on the type of data we give it. // We use the one with a 'bool' as the second argument when dealing with a mapped file, // and we use the one that takes a COUNT_T as the second argument when dealing with a // flat file. if (mtMapping == MTYPE_IMAGE) pe = new (nothrow) PEDecoder(m_pImage, false); else pe = new (nothrow) PEDecoder(m_pImage, (COUNT_T)(m_dwImageSize)); } EX_CATCH { hr = COR_E_BADIMAGEFORMAT; } EX_END_CATCH(SwallowAllExceptions) IfFailRet(hr); IfNullRet(pe); if (pe->HasContents() && pe->HasNTHeaders()) { pe->GetPEKindAndMachine(&dwKind,&dwMachine); // Cache entries. m_dwPEKind = dwKind; m_dwMachine = dwMachine; } else // if (pe.HasContents()... hr = COR_E_BADIMAGEFORMAT; } // if (m_pImage) else
BOOL LaunchJITDebugger() { STATIC_CONTRACT_NOTHROW; STATIC_CONTRACT_GC_NOTRIGGER; STATIC_CONTRACT_DEBUG_ONLY; BOOL fSuccess = FALSE; #ifndef FEATURE_PAL EX_TRY { SString debugger; GetDebuggerSettingInfo(debugger, NULL); SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; // We can leave this event as it is since it is inherited by a child process. // We will block one scheduler, but the process is asking a user if they want to attach debugger. HandleHolder eventHandle = WszCreateEvent(&sa, TRUE, FALSE, NULL); if (eventHandle == NULL) ThrowOutOfMemory(); SString cmdLine; cmdLine.Printf(debugger, GetCurrentProcessId(), eventHandle.GetValue()); STARTUPINFO StartupInfo; memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(StartupInfo); StartupInfo.lpDesktop = W("Winsta0\\Default"); PROCESS_INFORMATION ProcessInformation; if (WszCreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInformation)) { WaitForSingleObject(eventHandle.GetValue(), INFINITE); } fSuccess = TRUE; } EX_CATCH { } EX_END_CATCH(SwallowAllExceptions); #endif // !FEATURE_PAL return fSuccess; }
HRESULT STDMETHODCALLTYPE ClrDataStackWalk::GetContext( /* [in] */ ULONG32 contextFlags, /* [in] */ ULONG32 contextBufSize, /* [out] */ ULONG32 *contextSize, /* [size_is][out] */ BYTE contextBuf[ ]) { HRESULT status; if (contextSize) { *contextSize = ContextSizeForFlags(contextFlags); } if (!CheckContextSizeForFlags(contextBufSize, contextFlags)) { return E_INVALIDARG; } DAC_ENTER_SUB(m_dac); EX_TRY { if (!m_frameIter.IsValid()) { status = S_FALSE; } else { *(PT_CONTEXT)contextBuf = m_context; UpdateContextFromRegDisp(&m_regDisp, (PT_CONTEXT)contextBuf); status = S_OK; } } EX_CATCH { if (!DacExceptionFilter(GET_EXCEPTION(), m_dac, &status)) { EX_RETHROW; } } EX_END_CATCH(SwallowAllExceptions) DAC_LEAVE(); return status; }
HRESULT STDMETHODCALLTYPE ClrDataStackWalk::SetContext2( /* [in] */ ULONG32 flags, /* [in] */ ULONG32 contextSize, /* [size_is][in] */ BYTE context[ ]) { HRESULT status; if ((flags & ~(CLRDATA_STACK_SET_CURRENT_CONTEXT | CLRDATA_STACK_SET_UNWIND_CONTEXT)) != 0 || !CheckContextSizeForBuffer(contextSize, context)) { return E_INVALIDARG; } DAC_ENTER_SUB(m_dac); EX_TRY { // Copy the context to local state so // that its lifetime extends beyond this call. m_context = *(PT_CONTEXT)context; m_thread->FillRegDisplay(&m_regDisp, &m_context); m_frameIter.ResetRegDisp(&m_regDisp, (flags & CLRDATA_STACK_SET_CURRENT_CONTEXT) != 0); m_stackPrev = (TADDR)GetRegdisplaySP(&m_regDisp); FilterFrames(); status = S_OK; } EX_CATCH { if (!DacExceptionFilter(GET_EXCEPTION(), m_dac, &status)) { EX_RETHROW; } } EX_END_CATCH(SwallowAllExceptions) DAC_LEAVE(); return status; }
//***************************************************************************** // This function will handle ignore codes and tell the user what is happening. //***************************************************************************** bool _DbgBreakCheck( LPCSTR szFile, int iLine, LPCSTR szExpr, BOOL fConstrained) { STATIC_CONTRACT_THROWS; STATIC_CONTRACT_GC_NOTRIGGER; STATIC_CONTRACT_FORBID_FAULT; STATIC_CONTRACT_DEBUG_ONLY; RaiseExceptionOnAssert(rTestAndRaise); if (DebugBreakOnAssert()) { DebugBreak(); } DBGIGNORE* pDBGIFNORE = GetDBGIGNORE(); _DBGIGNOREDATA *psData; int i; // Check for ignore all. for (i = 0, psData = pDBGIFNORE->Ptr(); i < pDBGIFNORE->Count(); i++, psData++) { if (psData->iLine == iLine && SString::_stricmp(psData->rcFile, szFile) == 0 && psData->bIgnore == true) { return false; } } CONTRACT_VIOLATION(FaultNotFatal | GCViolation | TakesLockViolation); SString debugOutput; SString dialogOutput; SString modulePath; SString dialogTitle; SString dialogIgnoreMessage; BOOL formattedMessages = FALSE; // If we are low on memory we cannot even format a message. If this happens we want to // contain the exception here but display as much information as we can about the exception. if (!fConstrained) { EX_TRY { ClrGetModuleFileName(0, modulePath); debugOutput.Printf( W("\nAssert failure(PID %d [0x%08x], Thread: %d [0x%04x]): %hs\n") W(" File: %hs Line: %d\n") W(" Image: "), GetCurrentProcessId(), GetCurrentProcessId(), GetCurrentThreadId(), GetCurrentThreadId(), szExpr, szFile, iLine); debugOutput.Append(modulePath); debugOutput.Append(W("\n\n")); // Change format for message box. The extra spaces in the title // are there to get around format truncation. dialogOutput.Printf( W("%hs\n\n%hs, Line: %d\n\nAbort - Kill program\nRetry - Debug\nIgnore - Keep running\n") W("\n\nImage:\n"), szExpr, szFile, iLine); dialogOutput.Append(modulePath); dialogOutput.Append(W("\n")); dialogTitle.Printf(W("Assert Failure (PID %d, Thread %d/0x%04x)"), GetCurrentProcessId(), GetCurrentThreadId(), GetCurrentThreadId()); dialogIgnoreMessage.Printf(W("Ignore the assert for the rest of this run?\nYes - Assert will never fire again.\nNo - Assert will continue to fire.\n\n%hs\nLine: %d\n"), szFile, iLine); formattedMessages = TRUE; } EX_CATCH { } EX_END_CATCH(SwallowAllExceptions); }