Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 23 additions & 31 deletions onnxruntime/core/platform/windows/debug_alloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,41 @@ struct SymbolHelper {

SymbolHelper() = default;

static constexpr size_t kInitialBufferSize = sizeof(SYMBOL_INFO) + MAX_SYM_NAME;

bool LoookupSymAndInitialize(const ULONG_PTR address, char* buffer, size_t buffer_size, SYMBOL_INFO* symbol) {
if (SymFromAddr(process_handle_, address, 0, symbol) != TRUE) {
bool LookupSymAndInitialize(const void* address, SYMBOL_INFO* symbol, std::ostream& message) {
if (SymFromAddr(process_handle_, reinterpret_cast<ULONG_PTR>(address), 0, symbol) != TRUE) {
if (GetLastError() == ERROR_INVALID_HANDLE) {
// Try to initialize first
if (!InitializeWhenNeeded() || SymFromAddr(process_handle_, address, 0, symbol) != TRUE) {
_snprintf_s(buffer, buffer_size, _TRUNCATE, "0x%08IX (Unknown symbol)", address);
if (!InitializeWhenNeeded() ||
SymFromAddr(process_handle_, reinterpret_cast<ULONG_PTR>(address), 0, symbol) != TRUE) {
message << "0x" << address << " (Unknown symbol)";
return false;
}
} else {
_snprintf_s(buffer, buffer_size, _TRUNCATE, "0x%08IX (Unknown symbol)", address);
message << "0x" << address << " (Unknown symbol)";
return false;
}
}
return true;
}

void Lookup(std::string& string, const ULONG_PTR address) {
alignas(SYMBOL_INFO) char buffer[kInitialBufferSize] = {0};
SYMBOL_INFO* symbol = reinterpret_cast<SYMBOL_INFO*>(buffer);
void Lookup(const void* address, std::ostream& message) {
SYMBOL_INFO_PACKAGE symbol_info_package{};
SYMBOL_INFO* symbol = &symbol_info_package.si;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;
symbol->MaxNameLen = std::size(symbol_info_package.name);

if (!LoookupSymAndInitialize(address, buffer, kInitialBufferSize, symbol)) {
string.append(buffer);
if (!LookupSymAndInitialize(address, symbol, message)) {
return;
}

Line line;
DWORD displacement;
if (SymGetLineFromAddr(process_handle_, address, &displacement, &line) == false) {
_snprintf_s(buffer, _TRUNCATE, "(unknown file & line number): %s", symbol->Name);
string.append(buffer);
if (SymGetLineFromAddr(process_handle_, reinterpret_cast<ULONG_PTR>(address), &displacement, &line) == false) {
message << "(unknown file & line number): " << symbol->Name;
return;
}

_snprintf_s(buffer, _TRUNCATE, "%s(%d): %s", line.FileName, static_cast<int>(line.LineNumber), symbol->Name);
string.append(buffer);
message << line.FileName << "(" << line.LineNumber << "): " << symbol->Name;
}

struct Line : IMAGEHLP_LINE {
Expand Down Expand Up @@ -221,17 +217,17 @@ Memory_LeakCheck::~Memory_LeakCheck() {
const MemoryBlock& block = *static_cast<const MemoryBlock*>(entry.lpData);
const BYTE* pBlock = static_cast<const BYTE*>(entry.lpData) + sizeof(MemoryBlock);

std::string string;
char buffer[1024];
_snprintf_s(buffer, _TRUNCATE, "%Iu bytes at location 0x%08IX\n", entry.cbData - sizeof(MemoryBlock),
UINT_PTR(pBlock));
string.append(buffer);
std::ostringstream message;
message << (entry.cbData - sizeof(MemoryBlock)) << " bytes at location 0x" << static_cast<const void*>(pBlock)
<< "\n";
for (auto& p : block.m_pTraces) {
if (!p) break;
symbols.Lookup(string, reinterpret_cast<ULONG_PTR>(p));
string.push_back('\n');
symbols.Lookup(p, message);
message << "\n";
}

const std::string string = message.str();

// Google test has memory leaks that they haven't fixed. One such issue is tracked here: https://github.com/google/googletest/issues/692
//
// In gtest-port.cc in function: static ThreadIdToThreadLocals* GetThreadLocalsMapLocked()
Expand Down Expand Up @@ -271,12 +267,8 @@ Memory_LeakCheck::~Memory_LeakCheck() {
if (leaked_bytes) {
DebugPrint("-----Ending Heap Trace-----\n\n");

std::string string;
char buffer[1024];
_snprintf_s(buffer, _TRUNCATE, "%d bytes of memory leaked in %d allocations", static_cast<int>(leaked_bytes), static_cast<int>(leak_count));
string.append(buffer);

std::cout << "\n----- MEMORY LEAKS: " << string.c_str() << "\n";
std::cout << "\n----- MEMORY LEAKS: " << leaked_bytes << " bytes of memory leaked in "
<< leak_count << " allocations\n";
if (!IsDebuggerPresent()) {
exit(-1);
}
Expand Down
Loading