Skip to content

Commit

Permalink
Improvements/fixes to Windows call stack printing code
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaga committed Nov 28, 2015
1 parent f8ac1ea commit cc1efb6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
23 changes: 19 additions & 4 deletions Common/DebugUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define _CRT_SECURE_NO_WARNINGS // "secure" CRT not available on all platforms --add this at the top of all CPP files that give "function or variable may be unsafe" warnings
#endif

#include "Basics.h"
#include "DebugUtil.h"

namespace Microsoft { namespace MSR { namespace CNTK {
Expand All @@ -31,7 +32,13 @@ void DebugUtil::PrintCallStack()
HANDLE process;

process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
if (!SymInitialize(process, NULL, TRUE))
{
DWORD error = GetLastError();
std::cerr << "Failed to print CALL STACK! SymInitialize error : " << msra::strfun::utf8(FormatWin32Error(error)) << std::endl;
return;
}

frames = (func)(0, MAX_CALLERS, callStack, NULL);
symbolInfo = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO)+256 * sizeof(char), 1);
symbolInfo->MaxNameLen = 255;
Expand All @@ -42,8 +49,6 @@ void DebugUtil::PrintCallStack()

for (unsigned int i = 1; i < frames; i++)
{
SymFromAddr(process, (DWORD64)(callStack[i]), 0, symbolInfo);

if (i == 1)
{
std::cerr << " >";
Expand All @@ -53,12 +58,22 @@ void DebugUtil::PrintCallStack()
std::cerr << " -";
}

std::cerr << symbolInfo->Name << std::endl;
if (SymFromAddr(process, (DWORD64)(callStack[i]), 0, symbolInfo))
{
std::cerr << symbolInfo->Name << std::endl;
}
else
{
DWORD error = GetLastError();
std::cerr << callStack[i] << " (SymFromAddr error : " << msra::strfun::utf8(FormatWin32Error(error)) << ")" << std::endl;
}
}

std::cerr << std::endl;

free(symbolInfo);

SymCleanup(process);
#else
std::cerr << std::endl << "[CALL STACK]" << std::endl;

Expand Down
18 changes: 18 additions & 0 deletions Common/Include/Basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,22 @@ namespace Microsoft { namespace MSR { namespace CNTK {

}}}

#ifdef _WIN32
// ----------------------------------------------------------------------------
// frequently missing Win32 functions
// ----------------------------------------------------------------------------

// strerror() for Win32 error codes
static inline std::wstring FormatWin32Error(DWORD error)
{
wchar_t buf[1024] = { 0 };
::FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, "", error, 0, buf, sizeof(buf) / sizeof(*buf) - 1, NULL);
std::wstring res(buf);
// eliminate newlines (and spaces) from the end
size_t last = res.find_last_not_of(L" \t\r\n");
if (last != std::string::npos) res.erase(last + 1, res.length());
return res;
}
#endif // _WIN32

#endif // _BASICS_H_
18 changes: 0 additions & 18 deletions DataReader/HTKMLFReader/basetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,4 @@ static inline bool comparator(const pair<int, F>& l, const pair<int, F>& r)
}
#endif

#ifdef _WIN32
// ----------------------------------------------------------------------------
// frequently missing Win32 functions
// ----------------------------------------------------------------------------

// strerror() for Win32 error codes
static inline std::wstring FormatWin32Error(DWORD error)
{
wchar_t buf[1024] = { 0 };
::FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, "", error, 0, buf, sizeof (buf) / sizeof (*buf) - 1, NULL);
std::wstring res(buf);
// eliminate newlines (and spaces) from the end
size_t last = res.find_last_not_of(L" \t\r\n");
if (last != std::string::npos) res.erase(last + 1, res.length());
return res;
}
#endif // _WIN32

#endif // _BASETYPES_

0 comments on commit cc1efb6

Please sign in to comment.