-
Notifications
You must be signed in to change notification settings - Fork 50
/
DbgMemoryProfiler.pas
293 lines (234 loc) · 8.02 KB
/
DbgMemoryProfiler.pas
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
unit DbgMemoryProfiler;
interface
uses System.Classes, WinApi.Windows, Collections.Queues, DbgHookTypes,
System.SysUtils, System.SyncObjs, DebugerTypes;
type
TProcessMemoryQueue = TQueue<PDbgMemInfoListBuf>;
TDbgMemoryProfiler = class
private
FProcessMemoryQueue: TProcessMemoryQueue;
FMemoryCheckMode: LongBool;
FMemoryCallStack: LongBool;
FMemoryCheckDoubleFree: LongBool;
procedure SetMemoryCallStack(const Value: LongBool);
procedure SetMemoryCheckDoubleFree(const Value: LongBool);
procedure SetMemoryCheckMode(const Value: LongBool);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function ProcessMemoryInfoQueue: LongBool;
procedure ProcessMemoryInfoBuf(const Buf: PDbgMemInfoListBuf);
function FindMemoryPointer(const Ptr: Pointer; var ThData: PThreadData; var MemInfo: TGetMemInfo): LongBool;
procedure LoadMemoryInfoPackEx(const MemInfoPack: Pointer; const Count: Cardinal);
procedure UpdateMemoryInfoObjectTypes;
procedure UpdateMemoryInfoObjectTypesOfThread(ThData: PThreadData);
property MemoryCheckMode: LongBool read FMemoryCheckMode write SetMemoryCheckMode;
property MemoryCallStack: LongBool read FMemoryCallStack write SetMemoryCallStack;
property MemoryCheckDoubleFree: LongBool read FMemoryCheckDoubleFree write SetMemoryCheckDoubleFree;
end;
implementation
uses Debuger, Collections.Base;
const
_MAX_MEM_INFO_BUF_COUNT = 512;
{ TDbgMemoryProfiler }
procedure TDbgMemoryProfiler.Clear;
begin
FProcessMemoryQueue.Clear;
end;
constructor TDbgMemoryProfiler.Create;
begin
inherited;
FProcessMemoryQueue := TProcessMemoryQueue.Create(True);
FProcessMemoryQueue.Capacity := _MAX_MEM_INFO_BUF_COUNT + 1;
end;
destructor TDbgMemoryProfiler.Destroy;
begin
Clear;
FreeAndNil(FProcessMemoryQueue);
inherited;
end;
function TDbgMemoryProfiler.FindMemoryPointer(const Ptr: Pointer; var ThData: PThreadData; var MemInfo: TGetMemInfo): LongBool;
var
Idx: Integer;
begin
Result := False;
// Èùåì â òåêóùåì ïîòîêå
if ThData <> Nil then
Result := ThData^.DbgGetMemInfo.TryGetValue(Ptr, MemInfo);
if not Result then
begin
// Èùåì â äðóãèõ ïîòîêàõ
Idx := 0;
repeat
ThData := gvDebuger.GetThreadDataByIdx(Idx);
if ThData <> Nil then
begin
Result := ThData^.DbgGetMemInfo.TryGetValue(Ptr, MemInfo);
Inc(Idx);
end;
until Result or (ThData = Nil);
end;
end;
procedure TDbgMemoryProfiler.LoadMemoryInfoPackEx(const MemInfoPack: Pointer; const Count: Cardinal);
var
Buf: PDbgMemInfoListBuf;
begin
if not MemoryCheckMode then
Exit;
while FProcessMemoryQueue.Count >= _MAX_MEM_INFO_BUF_COUNT do
SwitchToThread;
Buf := AllocMem(SizeOf(TDbgMemInfoListBuf));
Buf^.Count := Count;
Buf^.DbgMemInfoList := AllocMem(Count * SizeOf(TDbgMemInfo));
Buf^.DbgPointIdx := gvDebuger.ProcessData.CurDbgPointIdx;
if gvDebuger.ReadData(MemInfoPack, Buf^.DbgMemInfoList, Count * SizeOf(TDbgMemInfo)) then
FProcessMemoryQueue.Enqueue(Buf)
else
RaiseDebugCoreException();
end;
procedure TDbgMemoryProfiler.ProcessMemoryInfoBuf(const Buf: PDbgMemInfoListBuf);
var
Idx: Integer;
DbgMemInfo: PDbgMemInfo;
ThData: PThreadData;
FoundThData: PThreadData;
MemInfo: TGetMemInfo;
NewMemInfo: TGetMemInfo;
begin
ThData := Nil;
for Idx := 0 to Buf^.Count - 1 do
begin
DbgMemInfo := @Buf^.DbgMemInfoList^[Idx];
if (ThData = Nil) or (ThData^.ThreadID <> DbgMemInfo^.ThreadId) then
ThData := gvDebuger.GetThreadData(DbgMemInfo^.ThreadId, True);
if ThData = Nil then
RaiseDebugCoreException();
case DbgMemInfo^.MemInfoType of
miGetMem:
begin
//DoDbgLog(DbgMemInfo^.ThreadId, Format('%s: %p (%d)', ['GetMem', DbgMemInfo^.Ptr, DbgMemInfo^.Size]));
// Åñëè íàéäåí åù¸ íåîñâîáîæäåííûé îáúåêò, òî îí óæå áûë êåì-òî îñâîáîæäåí
// TODO: Åñëè åñòü òàêèå îáúåêòû, òî ýòî ìû ãäå-òî ïðîïóñòèëè FreeMem
(*
FoundThData := ThData;
if FindMemoryPointer(DbgMemInfo^.Ptr, FoundThData, MemInfo) then
begin
//DoDbgLog(FoundThData^.ThreadId, Format('<<< ERROR!!! FOUND BEFORE GETMEM (%d)', [MemInfo^.Size]));
Dec(FoundThData^.DbgGetMemInfoSize, MemInfo^.Size);
Dec(FProcessData.ProcessGetMemCount);
Dec(FProcessData.ProcessGetMemSize, MemInfo^.Size);
FoundThData^.DbgGetMemInfo.Remove(DbgMemInfo^.Ptr);
end;
*)
// Äîáàâëÿåì èíôó ïðî íîâûé îáúåêò
NewMemInfo := TGetMemInfo.Create;
NewMemInfo.PerfIdx := Buf^.DbgPointIdx;
NewMemInfo.ObjAddr := DbgMemInfo^.Ptr;
NewMemInfo.Size := DbgMemInfo^.Size;
NewMemInfo.ObjectType := ''; // Íà ýòîò ìîìåíò òèï åù¸ ìîæåò áûòü íåîïðåäåëåí
NewMemInfo.LoadStack(@DbgMemInfo^.Stack);
ThData^.DbgGetMemInfo.AddOrSetValue(DbgMemInfo^.Ptr, NewMemInfo);
TInterlocked.Add(ThData^.DbgGetMemInfoSize, NewMemInfo.Size);
TInterlocked.Add(gvDebuger.ProcessData.ProcessGetMemCount, 1);
TInterlocked.Add(gvDebuger.ProcessData.ProcessGetMemSize, NewMemInfo.Size);
end;
miFreeMem:
begin
//DoDbgLog(DbgMemInfo^.ThreadId, Format('%s: %p (%d)', ['FreeMem', DbgMemInfo^.Ptr, DbgMemInfo^.Size]));
FoundThData := ThData;
if FindMemoryPointer(DbgMemInfo^.Ptr, FoundThData, MemInfo) then
begin
TInterlocked.Add(FoundThData^.DbgGetMemInfoSize, -MemInfo.Size);
TInterlocked.Add(gvDebuger.ProcessData.ProcessGetMemCount, -1);
TInterlocked.Add(gvDebuger.ProcessData.ProcessGetMemSize, -MemInfo.Size);
FoundThData^.DbgGetMemInfo.Remove(DbgMemInfo^.Ptr);
end
else
begin
// Ñþäà ìîæåò çàéòè, åñëè îáúåêò ñîçäàëñÿ ðàíüøå óñòàíîâêè õóêà íà ìåíåäæåð ïàìÿòè
//RaiseDebugCoreException();
//DoDbgLog(DbgMemInfo^.ThreadId, '<<< ERROR!!! NOT FOUND FOR FREEMEM');
// TODO: Double free ???
end;
end;
end;
end;
end;
function TDbgMemoryProfiler.ProcessMemoryInfoQueue: LongBool;
var
Buf: PDbgMemInfoListBuf;
begin
Result := False;
if not MemoryCheckMode then
Exit;
try
if FProcessMemoryQueue.Count > 0 then
begin
Buf := FProcessMemoryQueue.Dequeue;
try
ProcessMemoryInfoBuf(Buf);
finally
FreeMemory(Buf^.DbgMemInfoList);
FreeMemory(Buf);
end;
Result := True;
end;
except
on E: Exception do ; // TODO:
end;
end;
procedure TDbgMemoryProfiler.SetMemoryCallStack(const Value: LongBool);
begin
FMemoryCallStack := Value;
end;
procedure TDbgMemoryProfiler.SetMemoryCheckDoubleFree(const Value: LongBool);
begin
FMemoryCheckDoubleFree := Value;
end;
procedure TDbgMemoryProfiler.SetMemoryCheckMode(const Value: LongBool);
begin
FMemoryCheckMode := Value;
end;
procedure TDbgMemoryProfiler.UpdateMemoryInfoObjectTypes;
var
Idx: Integer;
ThData: PThreadData;
begin
Idx := 0;
repeat
ThData := gvDebuger.GetThreadDataByIdx(Idx);
if ThData <> Nil then
begin
UpdateMemoryInfoObjectTypesOfThread(ThData);
Inc(Idx);
end;
until ThData = Nil;
// Ïîòåðÿøêè
(*
GetMemInfo := ProcessData.DbgGetMemInfo;
if GetMemInfo.Count > 0 then
begin
for GetMemInfoItem in GetMemInfo do
GetMemInfoItem.Value^.ObjectType := GetMemInfoItem.Value^.GetObjectType(GetMemInfoItem.Key);
end;
*)
end;
procedure TDbgMemoryProfiler.UpdateMemoryInfoObjectTypesOfThread(ThData: PThreadData);
var
GetMemInfo: TGetMemInfoList;
GetMemInfoItem: TGetMemInfoItem;
begin
GetMemInfo := ThData^.DbgGetMemInfo;
if GetMemInfo.Count > 0 then
begin
GetMemInfo.LockForRead;
try
for GetMemInfoItem in GetMemInfo do
GetMemInfoItem.Value.CheckObjectType;
finally
GetMemInfo.UnLockForRead;
end;
end;
end;
end.