当前位置:   article > 正文

C++ 程序崩溃调用堆栈打印_windows 崩溃 记录调用栈信息

windows 崩溃 记录调用栈信息

程序现场发布,最头痛程序崩溃 但是日志又找不出什么蛛丝马迹  win32下可以增加如下 可以在release下打印调用堆栈信息  


//.h

  1. #ifndef _CALL_STACK_TOOL_2017_10_30_
  2. #define _CALL_STACK_TOOL_2017_10_30_
  3. #include <Windows.h>
  4. #include <DbgHelp.h>
  5. #include <iostream>
  6. #include <vector>
  7. // 添加对dbghelp.lib的编译依赖
  8. //
  9. #pragma comment(lib, "dbghelp.lib")
  10. using namespace std;
  11. const int MAX_ADDRESS_LENGTH = 32;
  12. const int MAX_NAME_LENGTH = 1024;
  13. // 崩溃信息
  14. //
  15. struct CrashInfo
  16. {
  17. CHAR ErrorCode[MAX_ADDRESS_LENGTH];
  18. CHAR Address[MAX_ADDRESS_LENGTH];
  19. CHAR Flags[MAX_ADDRESS_LENGTH];
  20. };
  21. // CallStack信息
  22. //
  23. struct CallStackInfo
  24. {
  25. CHAR ModuleName[MAX_NAME_LENGTH];
  26. CHAR MethodName[MAX_NAME_LENGTH];
  27. CHAR FileName[MAX_NAME_LENGTH];
  28. CHAR LineNumber[MAX_NAME_LENGTH];
  29. };
  30. // 安全拷贝字符串函数
  31. void SafeStrCpy(char* szDest, size_t nMaxDestSize, const char* szSrc);
  32. // 得到程序崩溃信息
  33. CrashInfo GetCrashInfo(const EXCEPTION_RECORD *pRecord);
  34. // 得到CallStack信息
  35. vector<CallStackInfo> GetCallStack(const CONTEXT *pContext);
  36. LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException);
  37. #endif
//.cpp
  1. #include "stdafx.h"
  2. #include "CallStackTool.h"
  3. #include "VpPublic.h"
  4. #include <iostream>
  5. #include <windows.h>
  6. #include <psapi.h>
  7. //#pragma comment(lib,"psapi.lib")
  8. void SafeStrCpy(char* szDest, size_t nMaxDestSize, const char* szSrc)
  9. {
  10. if (nMaxDestSize <= 0) return;
  11. if (strlen(szSrc) < nMaxDestSize)
  12. {
  13. strcpy_s(szDest, nMaxDestSize, szSrc);
  14. }
  15. else
  16. {
  17. strncpy_s(szDest, nMaxDestSize, szSrc, nMaxDestSize);
  18. szDest[nMaxDestSize - 1] = '\0';
  19. }
  20. }
  21. // 得到程序崩溃信息
  22. //
  23. CrashInfo GetCrashInfo(const EXCEPTION_RECORD *pRecord)
  24. {
  25. CrashInfo crashinfo;
  26. SafeStrCpy(crashinfo.Address, MAX_ADDRESS_LENGTH, "N/A");
  27. SafeStrCpy(crashinfo.ErrorCode, MAX_ADDRESS_LENGTH, "N/A");
  28. SafeStrCpy(crashinfo.Flags, MAX_ADDRESS_LENGTH, "N/A");
  29. sprintf_s(crashinfo.Address, "%08X", pRecord->ExceptionAddress);
  30. sprintf_s(crashinfo.ErrorCode, "%08X", pRecord->ExceptionCode);
  31. sprintf_s(crashinfo.Flags, "%08X", pRecord->ExceptionFlags);
  32. return crashinfo;
  33. }
  34. // 得到CallStack信息
  35. //
  36. vector<CallStackInfo> GetCallStack(const CONTEXT *pContext)
  37. {
  38. HANDLE hProcess = GetCurrentProcess();
  39. SymInitialize(hProcess, NULL, TRUE);
  40. vector<CallStackInfo> arrCallStackInfo;
  41. CONTEXT c = *pContext;
  42. STACKFRAME64 sf;
  43. memset(&sf, 0, sizeof(STACKFRAME64));
  44. DWORD dwImageType = IMAGE_FILE_MACHINE_I386;
  45. // 不同的CPU类型,具体信息可查询MSDN
  46. //
  47. #ifdef _M_IX86
  48. sf.AddrPC.Offset = c.Eip;
  49. sf.AddrPC.Mode = AddrModeFlat;
  50. sf.AddrStack.Offset = c.Esp;
  51. sf.AddrStack.Mode = AddrModeFlat;
  52. sf.AddrFrame.Offset = c.Ebp;
  53. sf.AddrFrame.Mode = AddrModeFlat;
  54. #elif _M_X64
  55. dwImageType = IMAGE_FILE_MACHINE_AMD64;
  56. sf.AddrPC.Offset = c.Rip;
  57. sf.AddrPC.Mode = AddrModeFlat;
  58. sf.AddrFrame.Offset = c.Rsp;
  59. sf.AddrFrame.Mode = AddrModeFlat;
  60. sf.AddrStack.Offset = c.Rsp;
  61. sf.AddrStack.Mode = AddrModeFlat;
  62. #elif _M_IA64
  63. dwImageType = IMAGE_FILE_MACHINE_IA64;
  64. sf.AddrPC.Offset = c.StIIP;
  65. sf.AddrPC.Mode = AddrModeFlat;
  66. sf.AddrFrame.Offset = c.IntSp;
  67. sf.AddrFrame.Mode = AddrModeFlat;
  68. sf.AddrBStore.Offset = c.RsBSP;
  69. sf.AddrBStore.Mode = AddrModeFlat;
  70. sf.AddrStack.Offset = c.IntSp;
  71. sf.AddrStack.Mode = AddrModeFlat;
  72. #else
  73. #error "Platform not supported!"
  74. #endif
  75. HANDLE hThread = GetCurrentThread();
  76. while (true)
  77. {
  78. // 该函数是实现这个功能的最重要的一个函数
  79. // 函数的用法以及参数和返回值的具体解释可以查询MSDN
  80. //
  81. if (!StackWalk64(dwImageType, hProcess, hThread, &sf, &c, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
  82. {
  83. break;
  84. }
  85. if (sf.AddrFrame.Offset == 0)
  86. {
  87. break;
  88. }
  89. CallStackInfo callstackinfo;
  90. SafeStrCpy(callstackinfo.MethodName, MAX_NAME_LENGTH, "N/A");
  91. SafeStrCpy(callstackinfo.FileName, MAX_NAME_LENGTH, "N/A");
  92. SafeStrCpy(callstackinfo.ModuleName, MAX_NAME_LENGTH, "N/A");
  93. SafeStrCpy(callstackinfo.LineNumber, MAX_NAME_LENGTH, "N/A");
  94. BYTE symbolBuffer[sizeof(IMAGEHLP_SYMBOL64) + MAX_NAME_LENGTH];
  95. IMAGEHLP_SYMBOL64 *pSymbol = (IMAGEHLP_SYMBOL64*)symbolBuffer;
  96. memset(pSymbol, 0, sizeof(IMAGEHLP_SYMBOL64) + MAX_NAME_LENGTH);
  97. pSymbol->SizeOfStruct = sizeof(symbolBuffer);
  98. pSymbol->MaxNameLength = MAX_NAME_LENGTH;
  99. DWORD symDisplacement = 0;
  100. // 得到函数名
  101. //
  102. if (SymGetSymFromAddr64(hProcess, sf.AddrPC.Offset, NULL, pSymbol))
  103. {
  104. SafeStrCpy(callstackinfo.MethodName, MAX_NAME_LENGTH, pSymbol->Name);
  105. }
  106. IMAGEHLP_LINE64 lineInfo;
  107. memset(&lineInfo, 0, sizeof(IMAGEHLP_LINE64));
  108. lineInfo.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
  109. DWORD dwLineDisplacement;
  110. // 得到文件名和所在的代码行
  111. //
  112. if (SymGetLineFromAddr64(hProcess, sf.AddrPC.Offset, &dwLineDisplacement, &lineInfo))
  113. {
  114. SafeStrCpy(callstackinfo.FileName, MAX_NAME_LENGTH, lineInfo.FileName);
  115. sprintf_s(callstackinfo.LineNumber, "%d", lineInfo.LineNumber);
  116. }
  117. IMAGEHLP_MODULE64 moduleInfo;
  118. memset(&moduleInfo, 0, sizeof(IMAGEHLP_MODULE64));
  119. moduleInfo.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
  120. // 得到模块名
  121. //
  122. if (SymGetModuleInfo64(hProcess, sf.AddrPC.Offset, &moduleInfo))
  123. {
  124. SafeStrCpy(callstackinfo.ModuleName, MAX_NAME_LENGTH, moduleInfo.ModuleName);
  125. }
  126. arrCallStackInfo.push_back(callstackinfo);
  127. }
  128. SymCleanup(hProcess);
  129. return arrCallStackInfo;
  130. }
  131. LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException)
  132. {
  133. // 确保有足够的栈空间
  134. //
  135. #ifdef _M_IX86
  136. if (pException->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW)
  137. {
  138. static char TempStack[1024 * 128];
  139. __asm mov eax, offset TempStack[1024 * 128];
  140. __asm mov esp, eax;
  141. }
  142. #endif
  143. CrashInfo crashinfo = GetCrashInfo(pException->ExceptionRecord);
  144. OS_LOG_INFO("********************************************************************************");
  145. OS_LOG_INFO("ErrorCode: " << crashinfo.ErrorCode);
  146. OS_LOG_INFO("Address: " << crashinfo.Address);
  147. OS_LOG_INFO("Flags: " << crashinfo.Flags);
  148. vector<CallStackInfo> arrCallStackInfo = GetCallStack(pException->ContextRecord);
  149. // 输出CallStack
  150. //
  151. OS_LOG_INFO("CallStack: ");
  152. for (vector<CallStackInfo>::iterator i = arrCallStackInfo.begin(); i != arrCallStackInfo.end(); ++i)
  153. {
  154. CallStackInfo callstackinfo = (*i);
  155. OS_LOG_INFO(callstackinfo.MethodName << "() : [" << callstackinfo.ModuleName << "] (File: " << callstackinfo.FileName << " @Line " << callstackinfo.LineNumber << ")" );
  156. }
  157. OS_LOG_INFO("********************************************************************************");
  158. HANDLE handle = GetCurrentProcess();
  159. PROCESS_MEMORY_COUNTERS pmc;
  160. GetProcessMemoryInfo(handle,&pmc,sizeof(pmc));
  161. OS_LOG_INFO("当前使用内存:" << pmc.WorkingSetSize/1024/1024 << "M,"
  162. "峰值使用内存:" << pmc.PeakWorkingSetSize/1014/1024 << "M,"
  163. "虚拟内存:" << pmc.PagefileUsage/1024/1024 << "M,"
  164. "峰值虚拟内存:" << pmc.PeakPagefileUsage/1014/1024 << "M"
  165. );
  166. return EXCEPTION_EXECUTE_HANDLER;
  167. }




PS:
每次发布记得随身带pdb 文件





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/245760
推荐阅读
相关标签
  

闽ICP备14008679号