Added call stack printing functionality for Linux

This commit is contained in:
Amit 2015-11-25 17:20:56 -08:00
Родитель 1c46217f58
Коммит 77da34e8d5
3 изменённых файлов: 68 добавлений и 4 удалений

Просмотреть файл

@ -59,8 +59,67 @@ void DebugUtil::PrintCallStack()
std::cerr << std::endl;
free(symbolInfo);
#endif // _WIN32
#else
std::cerr << std::endl << "[CALL STACK]" << std::endl;
unsigned int MAX_NUM_FRAMES= 1024;
void* backtraceAddresses[MAX_NUM_FRAMES];
unsigned int numFrames = backtrace(backtraceAddresses, MAX_NUM_FRAMES);
char** symbolList = backtrace_symbols(backtraceAddresses, numFrames);
for (unsigned int i = 0; i < numFrames; i++)
{
char* beginName = NULL;
char* beginOffset = NULL;
char* endOffset = NULL;
// Find parentheses and +address offset surrounding the mangled name
for (char* p = symbolList[i]; *p; ++p)
{
if (*p == '(')
beginName = p;
else if (*p == '+')
beginOffset = p;
else if ((*p == ')') && (beginOffset || beginName))
endOffset = p;
}
if (beginName && endOffset && (beginName < endOffset))
{
*beginName++ = '\0';
*endOffset++ = '\0';
if (beginOffset)
*beginOffset++ = '\0';
// Mangled name is now in [beginName, beginOffset) and caller offset in [beginOffset, endOffset).
int status = 0;
unsigned int MAX_FUNCNAME_SIZE= 4096;
size_t funcNameSize = MAX_FUNCNAME_SIZE;
char funcName[MAX_FUNCNAME_SIZE];
char* ret = abi::__cxa_demangle(beginName, funcName, &funcNameSize, &status);
char* fName = beginName;
if (status == 0)
fName = ret;
if (beginOffset)
{
fprintf(stderr, " %-30s ( %-40s + %-6s) %s\n", symbolList[i], fName, beginOffset, endOffset);
}
else
{
fprintf(stderr, " %-30s ( %-40s %-6s) %s\n", symbolList[i], fName, "", endOffset);
}
}
else
{
// Couldn't parse the line. Print the whole line.
fprintf(stderr, " %-30s\n", symbolList[i]);
}
}
free(symbolList);
#endif
}
}}}

Просмотреть файл

@ -11,9 +11,13 @@
#include <windows.h>
#include "DbgHelp.h"
#include <WinBase.h>
#include <iostream>
#pragma comment(lib, "Dbghelp.lib")
#endif // _WIN32
#else
#include <execinfo.h>
#include <cxxabi.h>
#endif
#include <iostream>
namespace Microsoft { namespace MSR { namespace CNTK {

Просмотреть файл

@ -150,6 +150,7 @@ ifeq ("$(BUILDTYPE)","debug")
endif
CXXFLAGS += -g
LDFLAGS += -rdynamic
CPPFLAGS += -D_DEBUG
CUFLAGS += -O0 -use_fast_math -lineinfo $(GENCODE_FLAGS)
endif