-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapProfiler.cpp
207 lines (183 loc) · 6.42 KB
/
HeapProfiler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "StdAfx.h"
#include "HeapProfiler.h"
#include <Windows.h>
#include <stdio.h>
#include "dbghelp.h"
#include <algorithm>
#include <iomanip>
#include "MemoryFence.h"
StackTrace::StackTrace() : hash(0)
{
memset(backtrace, 0, sizeof(void*)*backtraceSize);
}
void StackTrace::trace()
{
CaptureStackBackTrace(0, backtraceSize, backtrace, &hash);
}
void StackTrace::print(std::ostream &stream) const
{
HANDLE process = GetCurrentProcess();
const int MAXSYMBOLNAME = 128 - sizeof(IMAGEHLP_SYMBOL);
char symbol64_buf[sizeof(IMAGEHLP_SYMBOL) + MAXSYMBOLNAME] = {0};
IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL*>(symbol64_buf);
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
symbol->MaxNameLength = MAXSYMBOLNAME - 1;
// Print out stack trace. Skip the first frame (that's our hook function.)
for(size_t i = 1; i < backtraceSize; ++i)
{
if(backtrace[i])
{
// Output stack frame symbols if available.
if(SymGetSymFromAddr(process, (DWORD64)backtrace[i], 0, symbol)){
stream << " " << symbol->Name;
// Output filename + line info if available.
IMAGEHLP_LINE lineSymbol = {0};
lineSymbol.SizeOfStruct = sizeof(IMAGEHLP_LINE);
DWORD displacement;
if(SymGetLineFromAddr(process, (DWORD64)backtrace[i], &displacement, &lineSymbol))
stream << " " << lineSymbol.FileName << ":" << lineSymbol.LineNumber;
stream << " (" << std::setw(sizeof(void*)*2) << std::setfill('0') << backtrace[i] << ")\n";
}
else
stream << " <no symbol> " << " (" << std::setw(sizeof(void*)*2) << std::setfill('0') << backtrace[i] << ")\n";
}
else
break;
}
}
void StackTrace::printOnlineFile(FILE *f) const
{
HANDLE process = GetCurrentProcess();
const int MAXSYMBOLNAME = 128 - sizeof(IMAGEHLP_SYMBOL);
char symbol64_buf[sizeof(IMAGEHLP_SYMBOL) + MAXSYMBOLNAME] = { 0 };
IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL*>(symbol64_buf);
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
symbol->MaxNameLength = MAXSYMBOLNAME - 1;
// Print out stack trace. Skip the first frame (that's our hook function.)
for (size_t i = 1; i < backtraceSize; ++i)
{
if (backtrace[i])
{
// Output stack frame symbols if available.
if (SymGetSymFromAddr(process, (DWORD64)backtrace[i], 0, symbol))
{
fprintf(f, "%s:", symbol->Name);
// Output filename + line info if available.
IMAGEHLP_LINE lineSymbol = { 0 };
lineSymbol.SizeOfStruct = sizeof(IMAGEHLP_LINE);
DWORD displacement;
if (SymGetLineFromAddr(process, (DWORD64)backtrace[i], &displacement, &lineSymbol))
fprintf(f, "%s:%d:", lineSymbol.FileName, lineSymbol.LineNumber);
fprintf(f, "%p\n", backtrace[i]);
}
else
fprintf(f, "%p\n", backtrace[i]);
}
else
break;
}
fprintf(f, "\n");
}
#define ONLY_REPORT_ALLOCATIONS
#ifdef ONLY_REPORT_ALLOCATIONS
void HeapProfiler::malloc2(void *ptr, size_t size, const StackTrace &trace, int N)
{
std::lock_guard<std::mutex> lk(mutex);
if (stackTraces.find(trace.hash) == stackTraces.end())
{
stackTraces[trace.hash].trace = trace;
stackTraces[trace.hash].AllocatorIndex = N;
}
stackTraces[trace.hash].allocations[ptr] = size;
ptrs[ptr] = trace.hash;
}
int HeapProfiler::free2(void *ptr, const StackTrace &trace, int N)
{
std::lock_guard<std::mutex> lk(mutex);
auto it = ptrs.find(ptr);
if (it != ptrs.end())
{
StackHash stackHash = it->second;
auto SizeItr = stackTraces[stackHash].allocations.find(ptr);
size_t Size = SizeItr->second;
stackTraces[stackHash].allocations.erase(SizeItr);
ptrs.erase(it);
char FileName[500];
sprintf_s(FileName, sizeof(FileName), "d:/temp/Allocations_%p.txt", FreeHooksDllInst[N]);
FILE *f = fopen(FileName, "at");
if (f)
{
if (FreeHooksDllInst[N] != mallocHooksDllInst[stackTraces[stackHash].AllocatorIndex])
fprintf(f, "Not getting deallocated from same DLL?\n");
fprintf(f, "%s is deallocating a block of size %d at %p. Alloc Index %d. Dealloc index %d\n", FreeHooksDllInstNames[N], (int)Size, ptr, stackTraces[stackHash].AllocatorIndex, N);
stackTraces[stackHash].trace.printOnlineFile(f);
trace.printOnlineFile(f);
fprintf(f, "\n");
fclose(f);
}
return 1;
}
return 0;
}
#else
void HeapProfiler::malloc2(void *ptr, size_t size, const StackTrace &trace, int N)
{
std::lock_guard<std::mutex> lk(mutex);
if (stackTraces.find(trace.hash) == stackTraces.end())
{
stackTraces[trace.hash].trace = trace;
stackTraces[trace.hash].allocations[ptr] = size;
ptrs[ptr] = trace.hash;
}
}
int HeapProfiler::free2(void *ptr, const StackTrace &trace, int N)
{
std::lock_guard<std::mutex> lk(mutex);
auto it = ptrs.find(ptr);
if(it != ptrs.end())
{
#if CHECK_WRITE_AFTER_DEALLOC == 1
int size = stackTraces[trace.hash].allocations[ptr];
InitDeallocatedBlock(ptr, size);
DeletedStackTraces[trace.hash].trace = trace;
DeletedStackTraces[trace.hash].allocations[ptr] = size;
DeletedPtrs[ptr] = trace.hash;
ASSERT( CheckFence(ptr,size) == 0 );
#endif
StackHash stackHash = it->second;
stackTraces[stackHash].allocations.erase(ptr);
ptrs.erase(it);
return 1;
}
return 0;
}
#endif
void HeapProfiler::getAllocationSiteReport(std::vector<std::pair<StackTrace, size_t>> &allocs)
{
std::lock_guard<std::mutex> lk(mutex);
allocs.clear();
// For each allocation point.
for(auto &traceInfo : stackTraces)
{
// Sum up the size of all the allocations made.
size_t sumOfAlloced = 0;
for(auto &alloc : traceInfo.second.allocations)
sumOfAlloced += alloc.second;
// Add to alloation site report.
allocs.push_back(std::make_pair(traceInfo.second.trace, sumOfAlloced));
}
}
void HeapProfiler::CheckNoBadPointerUsage()
{
std::lock_guard<std::mutex> lk(mutex);
// For each allocation point.
for (auto &traceInfo : DeletedStackTraces)
{
for (auto &alloc : traceInfo.second.allocations)
if (CheckDeallocatedBlock(alloc.first,alloc.second))
{
printf("bad pointer usage at ");
ASSERT(false);
}
}
}