DWORD NCSDbgGetExceptionInfoMsg(EXCEPTION_POINTERS *pExceptionPtr, char *pMessage) { #if !defined(_WIN32_WCE)&&!defined(NCS_MINDEP_BUILD) char msg[1024]; void *pIP = pExceptionPtr->ExceptionRecord->ExceptionAddress; sprintf(pMessage, "\nAn error has occured in this application. If you receive this message,\n" "please report it at \"http://www.ermapper.com/support/supportform/\" to assist\n" "with fixing this error.\n\n"); switch(pExceptionPtr->ExceptionRecord->ExceptionCode) { case EXCEPTION_ACCESS_VIOLATION: sprintf(msg, "An Access Violation Exception occurred at code address 0x%I64x attempting to\n" "%s memory address 0x%I64x.\n\n", (UINT64)pIP, (pExceptionPtr->ExceptionRecord->ExceptionInformation[0] == 0) ? "READ from" : "WRITE to", (UINT64)pExceptionPtr->ExceptionRecord->ExceptionInformation[1]); break; default: sprintf(msg, "An Exception (number 0x%lx) occurred at address 0x%I64x\n\n", pExceptionPtr->ExceptionRecord->ExceptionCode, (UINT64)pIP); break; } strcat(pMessage, msg); #ifdef _M_AMD64 MemoryDump((void*)pExceptionPtr->ContextRecord->Rip, pMessage, 32); #else MemoryDump((void*)pExceptionPtr->ContextRecord->Eip, pMessage, 32); #endif strcat(pMessage, "\n"); StackTracer(pExceptionPtr->ContextRecord, pMessage, 8); #endif return(EXCEPTION_EXECUTE_HANDLER); }
int _tmain(int argc, _TCHAR* argv[]) { ESContext esContext; esInitContext ( &esContext ); esCreateWindow ( &esContext, "Hello Model", Globals::screenWidth, Globals::screenHeight, ES_WINDOW_RGB | ES_WINDOW_DEPTH); if ( Init ( &esContext ) != 0 ) return 0; esRegisterDrawFunc ( &esContext, Draw ); esRegisterUpdateFunc ( &esContext, Update ); esRegisterKeyFunc ( &esContext, Key); esMainLoop ( &esContext ); //releasing OpenGL resources CleanUp(); //identifying memory leaks MemoryDump(); printf("Press any key...\n"); _getch(); return 0; }
int Patch::OnMsgRecv(pGame__OnMsgRecv mr, void *self, const char *data, int sz) { Log::Get().Write(Log::Debug, StringFormat("[0x%04x, size: %u]", *(uint16*)data, sz - 2)); MemoryDump(data + 2, sz - 2); return mr(self, data, sz); }
static void StackTracer(CONTEXT *pCtx, char *pMessage, int MaxFrames) { #if !defined(_WIN32_WCE) CONTEXT Ctx; STACKFRAME Stk; #ifdef _M_AMD64 DWORD64 symDisplacement,dwInstance; #else DWORD symDisplacement,dwInstance; #endif HANDLE hProcess = GetCurrentProcess(); int nFrames; char Path[MAX_PATH]; static BYTE symbolBuffer[ sizeof(IMAGEHLP_SYMBOL) + 512 ]; Printf(pMessage,"Stack Trace:\n"); // initialise IMAGEHLP SymInitialize(hProcess,NULL,TRUE); // init the argument contexts. memset(&Stk,0,sizeof(STACKFRAME)); memset(&Ctx,0,sizeof(CONTEXT)); // funny - if I copy the struct wholesale I get crashes... #ifdef _M_AMD64 Ctx.Rsp = pCtx->Rsp; Ctx.Rbp = pCtx->Rbp; Ctx.Rip = pCtx->Rip; Stk.AddrStack.Offset = pCtx->Rsp; Stk.AddrStack.Mode = AddrModeFlat; Stk.AddrFrame.Offset = pCtx->Rbp; Stk.AddrFrame.Mode = AddrModeFlat; Stk.AddrPC.Offset = pCtx->Rip; Stk.AddrPC.Mode = AddrModeFlat; #else Ctx.Esp = pCtx->Esp; Ctx.Ebp = pCtx->Ebp; Ctx.Eip = pCtx->Eip; Stk.AddrStack.Offset = pCtx->Esp; Stk.AddrStack.Mode = AddrModeFlat; Stk.AddrFrame.Offset = pCtx->Ebp; Stk.AddrFrame.Mode = AddrModeFlat; Stk.AddrPC.Offset = pCtx->Eip; Stk.AddrPC.Mode = AddrModeFlat; #endif // run the loop. Just that. for(nFrames = 0; StackWalk(IMAGE_FILE_MACHINE_I386,hProcess, NULL,&Stk,&Ctx, (PREAD_PROCESS_MEMORY_ROUTINE)ReadProcessMemory, (PFUNCTION_TABLE_ACCESS_ROUTINE)SymFunctionTableAccess, SymGetModuleBase,NULL) && (nFrames < MaxFrames);nFrames++) { PIMAGEHLP_SYMBOL pSymbol; dwInstance = SymGetModuleBase(hProcess,Stk.AddrPC.Offset); // invalid address - no module covers it. if (!dwInstance) continue; // get name of dll. No Module - no valid frame. if (!GetModuleFileNameA((HINSTANCE)dwInstance,Path,sizeof(Path))) continue; // get the symbol name pSymbol = (PIMAGEHLP_SYMBOL)symbolBuffer; pSymbol->MaxNameLength = sizeof(symbolBuffer)-sizeof(PIMAGEHLP_SYMBOL); if (SymGetSymFromAddr(GetCurrentProcess(), Stk.AddrPC.Offset, &symDisplacement, pSymbol)) Printf(pMessage,"Function: %s\n",pSymbol->Name+1); else Printf(pMessage,"Function: <NOSYMBOL>\n"); // show the header line Printf(pMessage,"File: %s\nRVA: %8.8X HINSTANCE: %8.8X BP: %8.8X\n", Path,Stk.AddrPC.Offset - dwInstance, dwInstance,Stk.AddrFrame.Offset); MemoryDump((void*)Stk.AddrPC.Offset, pMessage, 32); nFrames ++; Printf(pMessage,"\n"); } SymCleanup(GetCurrentProcess()); #endif }