dumpstak.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #include <imagehlp.h>
  12. #pragma comment(lib, "imagehlp.lib")
  13. #ifdef AFX_AUX_SEG
  14. #pragma code_seg(AFX_AUX_SEG)
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // Routine to produce stack dump
  18. static LPVOID __stdcall FunctionTableAccess(HANDLE hProcess, DWORD dwPCAddress);
  19. static DWORD __stdcall GetModuleBase(HANDLE hProcess, DWORD dwReturnAddress);
  20. #define MODULE_NAME_LEN 64
  21. #define SYMBOL_NAME_LEN 128
  22. struct SYMBOL_INFO
  23. {
  24. DWORD dwAddress;
  25. DWORD dwOffset;
  26. CHAR szModule[MODULE_NAME_LEN];
  27. CHAR szSymbol[SYMBOL_NAME_LEN];
  28. };
  29. static LPVOID __stdcall FunctionTableAccess(HANDLE hProcess, DWORD dwPCAddress)
  30. {
  31. return SymFunctionTableAccess(hProcess, dwPCAddress);
  32. }
  33. static DWORD __stdcall GetModuleBase(HANDLE hProcess, DWORD dwReturnAddress)
  34. {
  35. IMAGEHLP_MODULE moduleInfo;
  36. if (SymGetModuleInfo(hProcess, dwReturnAddress, &moduleInfo))
  37. return moduleInfo.BaseOfImage;
  38. else
  39. {
  40. MEMORY_BASIC_INFORMATION memoryBasicInfo;
  41. if (::VirtualQueryEx(hProcess, (LPVOID) dwReturnAddress,
  42. &memoryBasicInfo, sizeof(memoryBasicInfo)))
  43. {
  44. DWORD cch = 0;
  45. char szFile[MAX_PATH] = { 0 };
  46. cch = GetModuleFileNameA((HINSTANCE)memoryBasicInfo.AllocationBase,
  47. szFile, MAX_PATH);
  48. // Ignore the return code since we can't do anything with it.
  49. if (!SymLoadModule(hProcess,
  50. NULL, ((cch) ? szFile : NULL),
  51. NULL, (DWORD) memoryBasicInfo.AllocationBase, 0))
  52. {
  53. DWORD dwError = GetLastError();
  54. TRACE1("Error: %d\n", dwError);
  55. }
  56. return (DWORD) memoryBasicInfo.AllocationBase;
  57. }
  58. else
  59. TRACE1("Error is %d\n", GetLastError());
  60. }
  61. return 0;
  62. }
  63. static BOOL ResolveSymbol(HANDLE hProcess, DWORD dwAddress,
  64. SYMBOL_INFO &siSymbol)
  65. {
  66. BOOL fRetval = TRUE;
  67. siSymbol.dwAddress = dwAddress;
  68. union {
  69. CHAR rgchSymbol[sizeof(IMAGEHLP_SYMBOL) + 255];
  70. IMAGEHLP_SYMBOL sym;
  71. };
  72. CHAR szUndec[256];
  73. CHAR szWithOffset[256];
  74. LPSTR pszSymbol = NULL;
  75. IMAGEHLP_MODULE mi;
  76. memset(&siSymbol, 0, sizeof(SYMBOL_INFO));
  77. mi.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
  78. if (!SymGetModuleInfo(hProcess, dwAddress, &mi))
  79. lstrcpyA(siSymbol.szModule, "<no module>");
  80. else
  81. {
  82. LPSTR pszModule = strchr(mi.ImageName, '\\');
  83. if (pszModule == NULL)
  84. pszModule = mi.ImageName;
  85. else
  86. pszModule++;
  87. lstrcpynA(siSymbol.szModule, pszModule, _countof(siSymbol.szModule));
  88. lstrcatA(siSymbol.szModule, "! ");
  89. }
  90. __try
  91. {
  92. sym.SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
  93. sym.Address = dwAddress;
  94. sym.MaxNameLength = 255;
  95. if (SymGetSymFromAddr(hProcess, dwAddress, &(siSymbol.dwOffset), &sym))
  96. {
  97. pszSymbol = sym.Name;
  98. if (UnDecorateSymbolName(sym.Name, szUndec, _countof(szUndec),
  99. UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ACCESS_SPECIFIERS))
  100. {
  101. pszSymbol = szUndec;
  102. }
  103. else if (SymUnDName(&sym, szUndec, _countof(szUndec)))
  104. {
  105. pszSymbol = szUndec;
  106. }
  107. if (siSymbol.dwOffset != 0)
  108. {
  109. wsprintfA(szWithOffset, "%s + %d bytes", pszSymbol, siSymbol.dwOffset);
  110. pszSymbol = szWithOffset;
  111. }
  112. }
  113. else
  114. pszSymbol = "<no symbol>";
  115. }
  116. __except (EXCEPTION_EXECUTE_HANDLER)
  117. {
  118. pszSymbol = "<EX: no symbol>";
  119. siSymbol.dwOffset = dwAddress - mi.BaseOfImage;
  120. }
  121. lstrcpynA(siSymbol.szSymbol, pszSymbol, _countof(siSymbol.szSymbol));
  122. return fRetval;
  123. }
  124. class CTraceClipboardData
  125. {
  126. HGLOBAL m_hMemory;
  127. DWORD m_dwSize;
  128. DWORD m_dwUsed;
  129. DWORD m_dwTarget;
  130. public:
  131. void SendOut(LPCSTR pszData);
  132. CTraceClipboardData(DWORD dwTarget);
  133. ~CTraceClipboardData();
  134. };
  135. CTraceClipboardData::CTraceClipboardData(DWORD dwTarget)
  136. : m_dwTarget(dwTarget), m_dwSize(0), m_dwUsed(0), m_hMemory(NULL)
  137. {
  138. }
  139. CTraceClipboardData::~CTraceClipboardData()
  140. {
  141. if (m_hMemory != NULL)
  142. {
  143. // chuck it onto the clipboard
  144. // don't free it unless there's an error
  145. if (!OpenClipboard(NULL))
  146. GlobalFree(m_hMemory);
  147. else if (!EmptyClipboard() ||
  148. SetClipboardData(CF_TEXT, m_hMemory) == NULL)
  149. {
  150. GlobalFree(m_hMemory);
  151. }
  152. else
  153. CloseClipboard();
  154. }
  155. }
  156. void CTraceClipboardData::SendOut(LPCSTR pszData)
  157. {
  158. int nLength;
  159. if (pszData == NULL || (nLength = lstrlenA(pszData)) == 0)
  160. return;
  161. // send it to TRACE (can be redirected)
  162. if (m_dwTarget & AFX_STACK_DUMP_TARGET_TRACE)
  163. TRACE1("%hs", pszData);
  164. // send it to OutputDebugString() (can't redirect)
  165. if (m_dwTarget & AFX_STACK_DUMP_TARGET_ODS)
  166. OutputDebugStringA(pszData);
  167. // build a buffer for the clipboard
  168. if (m_dwTarget & AFX_STACK_DUMP_TARGET_CLIPBOARD)
  169. {
  170. if (m_hMemory == NULL)
  171. {
  172. m_hMemory = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, 1024);
  173. if (m_hMemory == NULL)
  174. {
  175. TRACE0("AfxDumpStack Error: No memory available for clipboard.\n");
  176. m_dwTarget &= ~AFX_STACK_DUMP_TARGET_CLIPBOARD;
  177. }
  178. else
  179. {
  180. m_dwUsed = nLength;
  181. m_dwSize = 1024;
  182. LPSTR pstr = (LPSTR) GlobalLock(m_hMemory);
  183. if (pstr != NULL)
  184. {
  185. lstrcpyA(pstr, pszData);
  186. GlobalUnlock(m_hMemory);
  187. }
  188. else
  189. {
  190. TRACE0("AfxDumpStack Error: Couldn't lock memory!\n");
  191. GlobalFree(m_hMemory);
  192. m_hMemory = NULL;
  193. m_dwTarget &= ~AFX_STACK_DUMP_TARGET_CLIPBOARD;
  194. }
  195. }
  196. }
  197. else
  198. {
  199. if ((m_dwUsed + nLength + 1) >= m_dwSize)
  200. {
  201. // grow by leaps and bounds
  202. m_dwSize *= 2;
  203. if (m_dwSize > (1024L*1024L))
  204. {
  205. TRACE0("AfxDumpStack Error: more than one megabyte on clipboard.\n");
  206. m_dwTarget &= ~AFX_STACK_DUMP_TARGET_CLIPBOARD;
  207. }
  208. HGLOBAL hMemory = GlobalReAlloc(m_hMemory, m_dwSize, GMEM_MOVEABLE);
  209. if (hMemory == NULL)
  210. {
  211. TRACE1("AfxDumpStack Error: Couldn't get %d bytes!\n", m_dwSize);
  212. m_dwTarget &= ~AFX_STACK_DUMP_TARGET_CLIPBOARD;
  213. }
  214. else
  215. m_hMemory = hMemory;
  216. }
  217. LPSTR pstr = (LPSTR) GlobalLock(m_hMemory);
  218. if (pstr != NULL)
  219. {
  220. lstrcpyA(pstr + m_dwUsed, pszData);
  221. m_dwUsed += nLength;
  222. GlobalUnlock(m_hMemory);
  223. }
  224. else
  225. {
  226. TRACE0("AfxDumpStack Error: Couldn't lock memory!\n");
  227. m_dwTarget &= ~AFX_STACK_DUMP_TARGET_CLIPBOARD;
  228. }
  229. }
  230. }
  231. return;
  232. }
  233. /////////////////////////////////////////////////////////////////////////////
  234. // AfxDumpStack API
  235. void AFXAPI AfxDumpStack(DWORD dwTarget /* = AFX_STACK_DUMP_TARGET_DEFAULT */)
  236. {
  237. CTraceClipboardData clipboardData(dwTarget);
  238. clipboardData.SendOut("=== begin AfxDumpStack output ===\r\n");
  239. CDWordArray adwAddress;
  240. HANDLE hProcess = ::GetCurrentProcess();
  241. if (SymInitialize(hProcess, NULL, FALSE))
  242. {
  243. // force undecorated names to get params
  244. DWORD dw = SymGetOptions();
  245. dw &= ~SYMOPT_UNDNAME;
  246. SymSetOptions(dw);
  247. HANDLE hThread = ::GetCurrentThread();
  248. CONTEXT threadContext;
  249. threadContext.ContextFlags = CONTEXT_FULL;
  250. if (::GetThreadContext(hThread, &threadContext))
  251. {
  252. STACKFRAME stackFrame;
  253. memset(&stackFrame, 0, sizeof(stackFrame));
  254. stackFrame.AddrPC.Mode = AddrModeFlat;
  255. DWORD dwMachType;
  256. #if defined(_M_IX86)
  257. dwMachType = IMAGE_FILE_MACHINE_I386;
  258. // program counter, stack pointer, and frame pointer
  259. stackFrame.AddrPC.Offset = threadContext.Eip;
  260. stackFrame.AddrStack.Offset = threadContext.Esp;
  261. stackFrame.AddrStack.Mode = AddrModeFlat;
  262. stackFrame.AddrFrame.Offset = threadContext.Ebp;
  263. stackFrame.AddrFrame.Mode = AddrModeFlat;
  264. #elif defined(_M_MRX000)
  265. // only program counter
  266. dwMachType = IMAGE_FILE_MACHINE_R4000;
  267. stackFrame.AddrPC. Offset = treadContext.Fir;
  268. #elif defined(_M_ALPHA)
  269. // only program counter
  270. dwMachType = IMAGE_FILE_MACHINE_ALPHA;
  271. stackFrame.AddrPC.Offset = (unsigned long) threadContext.Fir;
  272. #elif defined(_M_PPC)
  273. // only program counter
  274. dwMachType = IMAGE_FILE_MACHINE_POWERPC;
  275. stackFrame.AddrPC.Offset = threadContext.Iar;
  276. #elif
  277. #error("Unknown Target Machine");
  278. #endif
  279. adwAddress.SetSize(0, 16);
  280. int nFrame;
  281. for (nFrame = 0; nFrame < 1024; nFrame++)
  282. {
  283. if (!StackWalk(dwMachType, hProcess, hProcess,
  284. &stackFrame, &threadContext, NULL,
  285. FunctionTableAccess, GetModuleBase, NULL))
  286. {
  287. break;
  288. }
  289. adwAddress.SetAtGrow(nFrame, stackFrame.AddrPC.Offset);
  290. }
  291. }
  292. }
  293. else
  294. {
  295. DWORD dw = GetLastError();
  296. char sz[100];
  297. wsprintfA(sz,
  298. "AfxDumpStack Error: IMAGEHLP.DLL wasn't found. "
  299. "GetLastError() returned 0x%8.8X\r\n", dw);
  300. clipboardData.SendOut(sz);
  301. }
  302. // dump it out now
  303. int nAddress;
  304. int cAddresses = adwAddress.GetSize();
  305. for (nAddress = 0; nAddress < cAddresses; nAddress++)
  306. {
  307. SYMBOL_INFO info;
  308. DWORD dwAddress = adwAddress[nAddress];
  309. char sz[20];
  310. wsprintfA(sz, "%8.8X: ", dwAddress);
  311. clipboardData.SendOut(sz);
  312. if (ResolveSymbol(hProcess, dwAddress, info))
  313. {
  314. clipboardData.SendOut(info.szModule);
  315. clipboardData.SendOut(info.szSymbol);
  316. }
  317. else
  318. clipboardData.SendOut("symbol not found");
  319. clipboardData.SendOut("\r\n");
  320. }
  321. clipboardData.SendOut("=== end AfxDumpStack() output ===\r\n");
  322. }