Skip to content

Commit

Permalink
VLD will display last internal frame
Browse files Browse the repository at this point in the history
  • Loading branch information
KindDragon committed Apr 6, 2014
1 parent 747fe56 commit fd4ee38
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 153 deletions.
24 changes: 18 additions & 6 deletions callstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ void CallStack::dump(BOOL showInternalFrames, UINT start_frame) const

const size_t max_size = MAXREPORTLENGTH + 1;

// Use static here to increase performance, and avoid heap allocs. Hopefully this won't
// prove to be an issue in thread safety. If it does, it will have to be simply non-static.
static WCHAR stack_line[MAXREPORTLENGTH + 1] = L"";
bool isPrevFrameInternal = false;

// Iterate through each frame in the call stack.
for (UINT32 frame = start_frame; frame < m_size; frame++)
{
Expand All @@ -232,13 +237,15 @@ void CallStack::dump(BOOL showInternalFrames, UINT start_frame) const
DWORD displacement = 0;
DbgTrace(L"dbghelp32.dll %i: SymGetLineFromAddrW64\n", GetCurrentThreadId());
foundline = SymGetLineFromAddrW64(g_currentProcess, programCounter, &displacement, &sourceInfo);
bool isFrameInternal = false;
if (foundline && !showInternalFrames) {
wcscpy_s(lowerCaseName, sourceInfo.FileName);
_wcslwr_s(lowerCaseName, wcslen(lowerCaseName) + 1);
if (isInternalModule(lowerCaseName)) {
if (isInternalModule(lowerCaseName))
{
isFrameInternal = true;
// Don't show frames in files internal to the heap.
g_symbolLock.Leave();
continue;
}
}

Expand Down Expand Up @@ -276,9 +283,10 @@ void CallStack::dump(BOOL showInternalFrames, UINT start_frame) const
moduleName = callingModuleName;
}

// Use static here to increase performance, and avoid heap allocs. Hopefully this won't
// prove to be an issue in thread safety. If it does, it will have to be simply non-static.
static WCHAR stack_line[MAXREPORTLENGTH + 1] = L"";
if (!isFrameInternal && isPrevFrameInternal)
Print(stack_line);
isPrevFrameInternal = isFrameInternal;

int NumChars = -1;
// Display the current stack frame's information.
if (foundline) {
Expand All @@ -298,7 +306,8 @@ void CallStack::dump(BOOL showInternalFrames, UINT start_frame) const
programCounter, moduleName, functionName, (DWORD)displacement64);
}

Print(stack_line);
if (!isFrameInternal)
Print(stack_line);
}
}

Expand Down Expand Up @@ -511,11 +520,14 @@ bool CallStack::isInternalModule( const PWSTR filename ) const
wcsstr(filename, L"\\crt\\src\\dbgmalloc.c") ||
wcsstr(filename, L"\\crt\\src\\new.cpp") ||
wcsstr(filename, L"\\crt\\src\\newaop.cpp") ||
wcsstr(filename, L"\\crt\\src\\dbgnew.cpp") ||
wcsstr(filename, L"\\crt\\src\\dbgcalloc.c") ||
wcsstr(filename, L"\\crt\\src\\realloc.c") ||
wcsstr(filename, L"\\crt\\src\\dbgrealloc.c") ||
wcsstr(filename, L"\\crt\\src\\dbgdel.cp") ||
wcsstr(filename, L"\\crt\\src\\free.c") ||
wcsstr(filename, L"\\crt\\src\\strdup.c") ||
wcsstr(filename, L"\\crt\\src\\wcsdup.c") ||
wcsstr(filename, L"\\vc\\include\\xmemory0");
}

Expand Down
241 changes: 123 additions & 118 deletions tests/basics/Allocs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,148 +7,153 @@

void AllocF(LeakOption type, bool bFree)
{
int* leaked_memory = NULL;
int* leaked_memory_dbg = NULL;
if (type == eMalloc)
{
leaked_memory = (int*)malloc(78);
leaked_memory_dbg = (int*)_malloc_dbg(80, _NORMAL_BLOCK,__FILE__,__LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
}
}
else if (type == eNew)
{
leaked_memory = new int(4);
leaked_memory_dbg = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(7);
if (bFree)
{
delete leaked_memory;
delete leaked_memory_dbg;
}
}
else if (type == eNewArray)
{
leaked_memory = new int[3];
leaked_memory_dbg = new (_NORMAL_BLOCK,__FILE__,__LINE__) int[4];
int* leaked_memory = NULL;
int* leaked_memory_dbg = NULL;
if (type == eMalloc)
{
leaked_memory = (int*)malloc(78);
leaked_memory_dbg = (int*)_malloc_dbg(80, _NORMAL_BLOCK,__FILE__,__LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
}
}
else if (type == eNew)
{
leaked_memory = new int(4);
leaked_memory_dbg = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(7);
if (bFree)
{
delete leaked_memory;
delete leaked_memory_dbg;
}
}
else if (type == eNewArray)
{
leaked_memory = new int[3];
leaked_memory_dbg = new (_NORMAL_BLOCK,__FILE__,__LINE__) int[4];

// placement new operator
int temp[3];
void* place = temp;
float* placed_mem = new (place) float[3]; // doesn't work. Nothing gets patched by vld
if (bFree)
{
delete [] leaked_memory;
delete [] leaked_memory_dbg;
}
}
else if (type == eCalloc)
{
leaked_memory = (int*)calloc(47,sizeof(int));
leaked_memory_dbg = (int*)_calloc_dbg(39, sizeof(int), _NORMAL_BLOCK, __FILE__, __LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
}
}
else if (type == eRealloc)
{
int* temp = (int*)malloc(17);
leaked_memory = (int*)realloc(temp, 23);
leaked_memory = (int*)_recalloc(leaked_memory, 1, 31);
int* temp_dbg = (int*)malloc(9);
leaked_memory_dbg = (int*)_realloc_dbg(temp_dbg, 21, _NORMAL_BLOCK, __FILE__, __LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
}
}
else if (type == eCoTaskMem)
{
void* leaked = CoTaskMemAlloc(7);
void* realloced = CoTaskMemRealloc(leaked, 29);
if (bFree)
{
CoTaskMemFree(realloced);
}
}
else if (type == eAlignedMalloc)
{
void* leaked = _aligned_offset_malloc(64, 16, 1);
leaked_memory = (int*)_aligned_malloc(64, 16);
leaked_memory_dbg = (int*)_aligned_malloc_dbg(32, 16, __FILE__, __LINE__);
if (bFree)
{
_aligned_free(leaked);
_aligned_free(leaked_memory);
_aligned_free_dbg(leaked_memory_dbg);
}
}
else if (type == eAlignedRealloc)
{
void* leaked = _aligned_offset_malloc(64, 16, 1);
leaked_memory = (int*)_aligned_malloc(64, 16);
leaked_memory_dbg = (int*)_aligned_malloc_dbg(32, 16, __FILE__, __LINE__);
leaked = (int*)_aligned_offset_realloc(leaked, 48, 16, 2);
leaked_memory = (int*)_aligned_realloc(leaked_memory, 128, 16);
leaked_memory_dbg = (int*)_aligned_realloc_dbg(leaked_memory_dbg, 48, 16, __FILE__, __LINE__);
leaked = (int*)_aligned_offset_recalloc(leaked, 1, 52, 16, 2);
leaked_memory = (int*)_aligned_recalloc(leaked_memory, 1, 132, 16);
leaked_memory_dbg = (int*)_aligned_recalloc_dbg(leaked_memory_dbg, 1, 64, 16, __FILE__, __LINE__);
if (bFree)
{
_aligned_free(leaked);
_aligned_free(leaked_memory);
_aligned_free_dbg(leaked_memory_dbg);
}
}
else if (type == eStrdup)
{
leaked_memory = (int*)strdup("strdup() leaks!");
leaked_memory_dbg = (int*)_strdup_dbg("_strdup_dbg() leaks!", _NORMAL_BLOCK,__FILE__,__LINE__);
void* leaked_wmemory = (int*)wcsdup(L"wcsdup() leaks!");
void* leaked_wmemory_dbg = (int*)_wcsdup_dbg(L"_wcsdup_dbg() leaks!", _NORMAL_BLOCK,__FILE__,__LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
free(leaked_wmemory);
_free_dbg(leaked_wmemory_dbg,_NORMAL_BLOCK);
}
}
// placement new operator
int temp[3];
void* place = temp;
float* placed_mem = new (place) float[3]; // doesn't work. Nothing gets patched by vld
if (bFree)
{
delete [] leaked_memory;
delete [] leaked_memory_dbg;
}
}
else if (type == eCalloc)
{
leaked_memory = (int*)calloc(47,sizeof(int));
leaked_memory_dbg = (int*)_calloc_dbg(39, sizeof(int), _NORMAL_BLOCK, __FILE__, __LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
}
}
else if (type == eRealloc)
{
int* temp = (int*)malloc(17);
leaked_memory = (int*)realloc(temp, 23);
leaked_memory = (int*)_recalloc(leaked_memory, 1, 31);
int* temp_dbg = (int*)malloc(9);
leaked_memory_dbg = (int*)_realloc_dbg(temp_dbg, 21, _NORMAL_BLOCK, __FILE__, __LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
}
}
else if (type == eCoTaskMem)
{
void* leaked = CoTaskMemAlloc(7);
if (bFree)
{
CoTaskMemFree(leaked);
}
void* leaked2 = CoTaskMemAlloc(7);
void* realloced = CoTaskMemRealloc(leaked2, 29);
if (bFree)
{
CoTaskMemFree(realloced);
}
}
else if (type == eAlignedMalloc)
{
void* leaked = _aligned_offset_malloc(64, 16, 1);
leaked_memory = (int*)_aligned_malloc(64, 16);
leaked_memory_dbg = (int*)_aligned_malloc_dbg(32, 16, __FILE__, __LINE__);
if (bFree)
{
_aligned_free(leaked);
_aligned_free(leaked_memory);
_aligned_free_dbg(leaked_memory_dbg);
}
}
else if (type == eAlignedRealloc)
{
void* leaked = _aligned_offset_malloc(64, 16, 1);
leaked_memory = (int*)_aligned_malloc(64, 16);
leaked_memory_dbg = (int*)_aligned_malloc_dbg(32, 16, __FILE__, __LINE__);
leaked = (int*)_aligned_offset_realloc(leaked, 48, 16, 2);
leaked_memory = (int*)_aligned_realloc(leaked_memory, 128, 16);
leaked_memory_dbg = (int*)_aligned_realloc_dbg(leaked_memory_dbg, 48, 16, __FILE__, __LINE__);
leaked = (int*)_aligned_offset_recalloc(leaked, 1, 52, 16, 2);
leaked_memory = (int*)_aligned_recalloc(leaked_memory, 1, 132, 16);
leaked_memory_dbg = (int*)_aligned_recalloc_dbg(leaked_memory_dbg, 1, 64, 16, __FILE__, __LINE__);
if (bFree)
{
_aligned_free(leaked);
_aligned_free(leaked_memory);
_aligned_free_dbg(leaked_memory_dbg);
}
}
else if (type == eStrdup)
{
leaked_memory = (int*)strdup("strdup() leaks!");
leaked_memory_dbg = (int*)_strdup_dbg("_strdup_dbg() leaks!", _NORMAL_BLOCK,__FILE__,__LINE__);
void* leaked_wmemory = (int*)wcsdup(L"wcsdup() leaks!");
void* leaked_wmemory_dbg = (int*)_wcsdup_dbg(L"_wcsdup_dbg() leaks!", _NORMAL_BLOCK,__FILE__,__LINE__);
if (bFree)
{
free(leaked_memory);
_free_dbg(leaked_memory_dbg,_NORMAL_BLOCK);
free(leaked_wmemory);
_free_dbg(leaked_wmemory_dbg,_NORMAL_BLOCK);
}
}

}

void AllocE(LeakOption type, bool bFree)
{
AllocF(type, bFree);
AllocF(type, bFree);
}

void AllocD(LeakOption type, bool bFree)
{
AllocE(type, bFree);
AllocE(type, bFree);
}

void AllocC(LeakOption type, bool bFree)
{
AllocD(type, bFree);
AllocD(type, bFree);
}

void AllocB(LeakOption type, bool bFree)
{
AllocC(type, bFree);
AllocC(type, bFree);
}

void AllocA(LeakOption type, bool bFree)
{
AllocB(type, bFree);
AllocB(type, bFree);
}

void Alloc(LeakOption type, bool bFree)
{
AllocA(type, bFree);
AllocA(type, bFree);
}
6 changes: 3 additions & 3 deletions tests/basics/basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace tut
int prev = (int)VLDGetLeaksCount();
LeakMemory(eCoTaskMem,repeat,false);
int leaks = (int)VLDGetLeaksCount() - prev;
ensure("leaks", leaks == (repeat * 1));
ensure("leaks", leaks == (repeat * 2));
}

template<>
Expand Down Expand Up @@ -222,7 +222,7 @@ int _tmain(int argc, _TCHAR* argv[])
else if (_tcsicmp(_T("CoTaskMem"), argv[1]) == 0)
{
leak_type = eCoTaskMem;
multiplayer = 1;
multiplayer = 2;
}
else if (_tcsicmp(_T("AlignedMalloc"), argv[1]) == 0)
{
Expand All @@ -242,7 +242,7 @@ int _tmain(int argc, _TCHAR* argv[])
else if (_tcsicmp(_T("all"), argv[1]) == 0)
{
checkAll = true;
multiplayer = 21;
multiplayer = 22;
}
else
{
Expand Down
1 change: 0 additions & 1 deletion tests/basics/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>



Expand Down
Loading

0 comments on commit fd4ee38

Please sign in to comment.