Skip to content

Commit

Permalink
Added call stack printing functionality for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaga committed Nov 26, 2015
1 parent 1c46217 commit 77da34e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
63 changes: 61 additions & 2 deletions Common/DebugUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

}}}
8 changes: 6 additions & 2 deletions Common/Include/DebugUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ ifeq ("$(BUILDTYPE)","debug")
endif
CXXFLAGS += -g
LDFLAGS += -rdynamic
CPPFLAGS += -D_DEBUG
CUFLAGS += -O0 -use_fast_math -lineinfo $(GENCODE_FLAGS)
endif
Expand Down

0 comments on commit 77da34e

Please sign in to comment.