c++ print stacktrace

c++ stack print stack trace debug utility
//z 2014-06-30 12:40:28 L.184'40772 BG57IV3@XCL T1809462551.K.F1586050276[T34,L813,R24,V564] is2120
#include <DbgHelp.h>
#pragma comment(lib, "dbghelp.lib")

void printStack(void)
{
	char cBuff[1024] = {0};
	unsigned int   i;
	void         * stack[128];
	unsigned short frames;
	SYMBOL_INFO  * symbol;
	HANDLE         process;

	process = GetCurrentProcess();

	SymInitialize(process, NULL, TRUE);

	frames = CaptureStackBackTrace(0, 128, stack, NULL);
	symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
	symbol->MaxNameLen = 255;
	symbol->SizeOfStruct = sizeof(SYMBOL_INFO);

	OutputDebugString(_T("##########################################################################\n"));
	for (i = 0; i < frames; i++)
	{
		SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);
		sprintf_s(cBuff,1024,("%i: %s - 0x%0X\n"),frames - i - 1, symbol->Name, symbol->Address);
		OutputDebugStringA(cBuff);
	}
	OutputDebugString(_T("--------------------------------------------------------------------\n"));

	free(symbol);
}
//z is2120 2014-06-30 12:40:28 L.184'40772 BG57IV3@XCL T1809462551.K.F1586050276[T34,L813,R24,V564]


posted @ 2014-06-30 12:39  BiG5  阅读(634)  评论(0编辑  收藏  举报