forked from grijjy/GrijjyFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grijjy.CloudLogging.InstanceTracker.pas
397 lines (340 loc) · 12.8 KB
/
Grijjy.CloudLogging.InstanceTracker.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
unit Grijjy.CloudLogging.InstanceTracker;
{ When using this unit with TRACK_MEMORY defined, instances of most classes will
be tracked for reporting to the Grijjy Log Viewer.
For most accurate results, it is recommended to put this unit at the top of
the uses-clause of the project (.dpr) file.
When TRACK_MEMORY is *not* defined, this unit does nothing and has no impact
on the application whatsoever.
Note that using this unit with TRACK_MEMORY defined may slow down the
application a bit and consume extra memory. }
interface
implementation
{$IFDEF TRACK_MEMORY}
uses
System.Rtti,
System.Classes,
System.SysUtils,
System.SyncObjs,
System.Messaging,
System.Generics.Collections,
Grijjy.Hooking,
Grijjy.Collections,
Grijjy.CloudLogging,
Grijjy.CloudLogging.Protocol;
type
{ These "class opener" types give us access to the protected FRefCount
fields of TObject and TInterfacedObject. }
TObjectOpener = class(TObject);
TInterfacedObjectOpener = class(TInterfacedObject);
type
TMessageListener = class
private
class function InstanceToString(const AInstance: TObject): String; static;
private
procedure HandleGetInstances(const Sender: TObject; const M: TMessage);
public
constructor Create;
destructor Destroy; override;
end;
var
{ This set keeps track of all allocated objects. Note that it is a set of
pointers instead of TObject's, since storing objects in the set would
create a strong reference and prevent destruction of all objects! }
GInstances: TgoSet<Pointer> = nil;
{ Lock to make GInstanceCounts thread-safe. }
GLock: TCriticalSection = nil;
{ Listens for TgoGetInstancesMessage to provide a list of live instances. }
GListener: TMessageListener = nil;
procedure TrackInstance(const AInstance: TObject);
begin
if Assigned(AInstance) and Assigned(GLock) then
begin
GLock.Acquire;
try
if Assigned(GInstances) then
GInstances.AddOrSet(AInstance);
finally
GLock.Release;
end;
end;
end;
procedure UntrackInstance(const AInstance: TObject);
begin
if Assigned(AInstance) and Assigned(GLock) then
begin
GLock.Acquire;
try
if Assigned(GInstances) then
GInstances.Remove(AInstance);
finally
GLock.Release;
end;
end;
end;
{ The following 3 routines implement the hooks for TObject.NewInstance,
TInterfacedObject.NewInstance and TObject.FreeInstance.
The implementation of these routines is identical to the original NewInstance
and FreeInstance methods, but in addition it tracks (or untracks) an
instance. }
function HookedObjectNewInstance(const Self: TClass): TObject {$IFDEF AUTOREFCOUNT} unsafe {$ENDIF};
var
Instance: Pointer;
begin
{ This is the hook for TObject.NewInstance. Since this method is a
(non-static) class method, it has an implicit Self parameter. But since it
is a class method, this Self parameter represents a class, not an object.
We start by mimicking the original source code for TObject.NewInstance: }
GetMem(Instance, Self.InstanceSize);
Result := Self.InitInstance(Instance);
{$IFDEF AUTOREFCOUNT}
{ On ARC platforms, each object has a FRefCount field that must be
initialized to 1. }
TObjectOpener(Result).FRefCount := 1;
{$ENDIF}
{ Now we can keep track of this instance. }
TrackInstance(Result);
end;
function HookedInterfacedObjectNewInstance(const Self: TClass): TObject {$IFDEF AUTOREFCOUNT} unsafe {$ENDIF};
var
Instance: Pointer;
begin
{ This is the hook for TInterfacedObject.NewInstance. This method is mostly
similar to TObject.NewInstance, with the exception that interfaced objects
also have a FRefCount field on non-ARC platforms. }
GetMem(Instance, Self.InstanceSize);
Result := Self.InitInstance(Instance);
TInterfacedObjectOpener(Result).FRefCount := 1;
{ Now we can keep track of this instance. }
TrackInstance(Result);
end;
procedure HookedObjectFreeInstance(const Self: TObject);
begin
{ This is the hook for TObject.FreeInstance. Since this is a (regular) method,
it has an implicit Self parameter containing the instance. We first stop
tracking this instance... }
UntrackInstance(Self);
{ ...and then execute the original code in TObject.FreeInstance: }
Self.CleanupInstance;
FreeMem(Pointer(Self));
end;
function InitializeCodeHooks: Boolean;
begin
{ This function tries HookCode to hook the implementations of the
TObject.NewInstance and TObject.FreeInstance methods. This will most likely
only succeed on Windows, macOS, iOS Simulator and Linux. }
Result := HookCode(@TObject.NewInstance, @HookedObjectNewInstance)
and HookCode(@TObject.FreeInstance, @HookedObjectFreeInstance);
end;
{ We are using the vmtNewInstance and vmtFreeInstance constants, which have been
deprecated for a long time, but are still available. Turn off warnings for
these. }
{$WARN SYMBOL_DEPRECATED OFF}
procedure InitializeVMTHooks;
var
Rtti: TRttiContext;
RttiType: TRttiType;
InstanceType: TRttiInstanceType;
VMTEntryNewInstance, VMTEntryFreeInstance: PPointer;
ObjectNewInstance, ObjectFreeInstance, InterfacedObjectNewInstance: Pointer;
begin
{ This version uses HookVMT instead of HookCode to hook the
TObject.NewInstance and TObject.FreeInstance methods.
Each Delphi class has its own Virtual Method Table. This means that we need
to hook the VMT's for all classes we care about. In this case, we use
TRttiContext.GetTypes to get a list of all Delphi classes (and other types)
linked into the application. We then change the VMT entries of each class
in that list.
The problem with this kind of hooking is that some classes may have
overridden the NewInstance and/or FreeInstance methods. Changing the VMT of
those classes would ignore any customizations those classes made to those
methods, and we don't want that. Fortunately, there are very few classes
that have overridden these methods.
So we only change the VMT's of those classes that have not overridden
NewInstance or FreeInstance. This single exception is the TInterfacedObject
class, which is so common that we want to support it. This class has
overridden the NewInstance method, so we need a separate hook for this
version.
First, we retrieve the code addresses of the original NewInstance and
FreeInstance methods. We use these to check if they are overridden by a
certain class. }
ObjectNewInstance := @TObject.NewInstance;
ObjectFreeInstance := @TObject.FreeInstance;
InterfacedObjectNewInstance := @TInterfacedObject.NewInstance;
{ Get a list of all Delphi types in the application with RTTI support. }
Rtti := TRttiContext.Create;
for RttiType in Rtti.GetTypes do
begin
{ Check if the type is a class type. }
if (RttiType.TypeKind = tkClass) then
begin
{ We can now safely typecase to TRttiInstanceType }
InstanceType := TRttiInstanceType(RttiType);
{ Retrieve the entry in the VMT of the FreeInstance method for this class. }
VMTEntryFreeInstance := PPointer(PByte(InstanceType.MetaclassType) + vmtFreeInstance);
{ Only track classes that didn't override TObject.FreeInstance. }
if (VMTEntryFreeInstance^ = ObjectFreeInstance) then
begin
{ Retrieve the entry in the VMT of the NewInstance method for this class. }
VMTEntryNewInstance := PPointer(PByte(InstanceType.MetaclassType) + vmtNewInstance);
{ Only track classes that didn't override TObject.NewInstance or
TInterfacedObject.NewInstance. }
if (VMTEntryNewInstance^ = ObjectNewInstance) then
begin
{ This class uses NewInstance and FreeInstance from TObject.
Hook those VMT entries. }
HookVMT(VMTEntryNewInstance, @HookedObjectNewInstance);
HookVMT(VMTEntryFreeInstance, @HookedObjectFreeInstance);
end
else if (VMTEntryNewInstance^ = InterfacedObjectNewInstance) then
begin
{ This class is (ultimately) derived from TInterfacedObject, so
we need to hook to a separate version of NewInstance. }
HookVMT(VMTEntryNewInstance, @HookedInterfacedObjectNewInstance);
HookVMT(VMTEntryFreeInstance, @HookedObjectFreeInstance);
end;
end;
end;
end;
end;
{$WARN SYMBOL_DEPRECATED ON}
procedure InitializeGlobals;
begin
{ These globals are used to keep track of instances. }
GLock := TCriticalSection.Create;
GInstances := TgoSet<Pointer>.Create;
GListener := TMessageListener.Create;
end;
procedure FinalizeGlobals;
begin
FreeAndNil(GLock);
FreeAndNil(GInstances);
FreeAndNil(GListener);
end;
{ TMessageListener }
constructor TMessageListener.Create;
begin
inherited Create;
TMessageManager.DefaultManager.SubscribeToMessage(TgoGetInstancesMessage,
HandleGetInstances)
end;
destructor TMessageListener.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TgoGetInstancesMessage,
HandleGetInstances);
inherited;
end;
procedure TMessageListener.HandleGetInstances(const Sender: TObject;
const M: TMessage);
type
TInstances = TList<TgoLogMemoryUsageProtocol.TInstance>;
var
Msg: TgoGetInstancesMessage absolute M;
Instances: TArray<Pointer>;
Instance: Pointer;
Counts: TDictionary<TClass, Integer>;
Pair: TPair<TClass, Integer>;
DetailClasses: TObjectDictionary<TClass, TInstances>;
DetailInstances: TInstances;
DetailInstance: TgoLogMemoryUsageProtocol.TInstance;
Obj: TObject;
ObjClass: TClass;
Component: TComponent absolute Obj;
Count: Integer;
begin
Assert(M is TgoGetInstancesMessage);
if (GLock = nil) then
Exit;
GLock.Acquire;
try
if (GInstances = nil) then
Exit;
Instances := GInstances.ToArray;
if (Instances = nil) then
Exit;
DetailClasses := nil;
Counts := TDictionary<TClass, Integer>.Create;
try
DetailClasses := TObjectDictionary<TClass, TInstances>.Create([doOwnsValues]);
for Count := 0 to Length(Msg.Classes) - 1 do
DetailClasses.AddOrSetValue(Msg.Classes[Count], nil);
for Instance in Instances do
begin
Obj := TObject(Instance);
ObjClass := Obj.ClassType;
if (Counts.TryGetValue(ObjClass, Count)) then
Counts[ObjClass] := Count + 1
else
Counts.Add(ObjClass, 1);
if (DetailClasses.TryGetValue(ObjClass, DetailInstances)) then
begin
{ Details are requested for this class. }
if (DetailInstances = nil) then
begin
DetailInstances := TInstances.Create;
DetailClasses[ObjClass] := DetailInstances;
end;
if (DetailInstances.Count < GrijjyLog.MaxInstancesPerClass) then
begin
{ Add string respresentation of this instance to details for the
class. }
if (Obj is TComponent) then
begin
DetailInstance.Caption := '';
if Assigned(Component.Owner) and (Component.Owner.Name <> '') then
DetailInstance.Caption := Component.Owner.Name + '.';
if (Component.Name = '') then
DetailInstance.Caption := DetailInstance.Caption + InstanceToString(Component)
else
DetailInstance.Caption := DetailInstance.Caption + Component.Name;
end
else
DetailInstance.Caption := InstanceToString(Obj);
DetailInstances.Add(DetailInstance);
end;
end;
end;
SetLength(Msg.Protocol.Entries, Counts.Count);
Count := 0;
for Pair in Counts do
begin
Assert(Count < Length(Msg.Protocol.Entries));
if Assigned(Pair.Key) then
begin
Msg.Protocol.Entries[Count].ClassName := Pair.Key.ClassName;
Msg.Protocol.Entries[Count].ClassHandle := THandle(Pair.Key);
end;
Msg.Protocol.Entries[Count].InstanceCount := Pair.Value;
if (DetailClasses.TryGetValue(Pair.Key, DetailInstances)) then
Msg.Protocol.Entries[Count].Instances := DetailInstances.ToArray;
Inc(Count);
end;
finally
DetailClasses.Free;
Counts.Free;
end;
finally
GLock.Release;
end;
end;
class function TMessageListener.InstanceToString(
const AInstance: TObject): String;
begin
Result := AInstance.ToString;
if (Result = AInstance.ClassName) then
{ The instance did not override the ToString method }
Result := Result + Format(' @ %p', [Pointer(AInstance)]);
end;
initialization
{ First we try code hooking to hook into the NewInstance and FreeInstance
methods. This is fastest and tracks all classes. }
if (not InitializeCodeHooks) then
{ If the first method fails, try VMT hooking instead. This hooks the
NewInstance and FreeInstance entries in the Virtual Method Tables of all
classes that have RTTI. }
InitializeVMTHooks;
{ Initialize some global variables. }
InitializeGlobals;
finalization
FinalizeGlobals;
{$ENDIF !TRACK_MEMORY}
end.