-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMemCheck.pas
1078 lines (894 loc) · 27.7 KB
/
MemCheck.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit MemCheck;
(*
プロジェクトソースの Uses 節の一番最初に MemCheck を追加する。一番最初に追加しないとプログラムが正常に動作しなくなります。
スタックトレース機能を使用するには以下の事をやる必要があります。
プロジェクトオプションのコンパイラタブの「スタックフレームの生成」を ON にする。
プロジェクトオプションのリンカタブの「TD32デバッグ情報を含める」を ON にする。
プロジェクトオプションのディレクトリ/条件タブの条件に MemCheckStackTrace を追加する。
これをやらないと従来のメモリリーク回数チェッカーになります。
プロジェクトソースで MemCheckInstallExceptionHandler 関数を呼び出して例外ハンドラを登録する。メモリリークだけをチェックしたいのならば不要。
またプロジェクトオプションのディレクトリ/条件タブで検索パスに「$(DELPHI)\Source\VCL」を追加することで VCL ソースコード内も追跡できるようになります。
*)
interface
{$DEFINE MemCheckStackTrace}
uses Windows;
type
TMemCheckGetExceptInfoFunc = procedure(Obj: TObject; var Message: string;
var ExceptionRecord: PExceptionRecord);
TMemCheckSetExceptMessageFunc = procedure(Obj: TObject; const NewMessage: string);
procedure MemCheckInstallExceptionHandler(GetExceptInfo: TMemCheckGetExceptInfoFunc;
SetExceptMessage: TMemCheckSetExceptMessageFunc);
implementation
function Max(A,B: Integer): Integer;
begin
if A > B then
Result := A
else
Result := B;
end;
function Min(A,B: Integer): Integer;
begin
if A < B then
Result := A
else
Result := B;
end;
const
HexTbl: array[0..15] of Char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
function IntToStr(val: Integer): string;
var
sign: Boolean;
begin
Result := '';
sign := val<0;
val := Abs(val);
while val<>0 do
begin
Result := HexTbl[val mod 10] + Result;
val := val div 10;
end;
if sign then
Result := '-' + Result;
end;
function IntToStr2(val, minLen: Integer): string;
var
sign: Boolean;
begin
Result := '';
sign := val<0;
val := Abs(val);
while val<>0 do
begin
Result := HexTbl[val mod 10] + Result;
val := val div 10;
end;
while Length(Result)<minLen do
Result := '0' + Result;
if sign then
Result := '-' + Result;
end;
function IntToHex(val: DWORD; minLen: Integer): string;
begin
Result := '';
while val<>0 do
begin
Result := HexTbl[val and $F] + Result;
val := val shr 4;
end;
while Length(Result)<minLen do
Result := '0' + Result;
end;
type
PPointer = ^Pointer;
PArrayByte = ^TArrayByte;
TArrayByte = array[0..10000] of Byte;
PArrayPointer = ^TArrayPointer;
TArrayPointer = array[0..10000] of Pointer;
PArrayWord = ^TArrayWord;
TArrayWord = array[0..10000] of Word;
TLineInfo = record
Line: Integer;
Address: Integer;
end;
PUnitInfo = ^TUnitInfo;
TUnitInfo = record
Name: string[64];
LineList: array of TLineInfo;
StartAddress: Integer;
EndAddress: Integer;
end;
PRoutineInfo = ^TRoutineInfo;
TRoutineInfo = record
Name: string[64];
Address: Integer;
Length: Integer;
UnitInfo: PUnitInfo;
end;
PMemBlockHeader = ^TMemBlockHeader;
TMemBlockHeader = record
Left: PMemBlockHeader;
Right: PMemBlockHeader;
Size: Integer;
StackFrame: array[0..31] of Pointer;
StackFrameCount: Integer;
end;
type
TDebugInfomation = class
private
FUnitInfoList: array of TUnitInfo;
FUnitInfoListCount: Integer;
FRoutineInfoList: array of TRoutineInfo;
FRoutineInfoListCount: Integer;
public
procedure Load(const FileName: string);
function SearchUnitFromAddress(Address: Integer): PUnitInfo;
function SearchLineFromAddress(UnitInfo: PUnitInfo; Address: Integer): Integer;
function SearchRoutineFromAddress(Address: Integer): PRoutineInfo;
end;
function TDebugInfomation.SearchUnitFromAddress(Address: Integer): PUnitInfo;
var
i: Integer;
begin
Result := nil;
for i:=0 to FUnitInfoListCount-1 do
if (FUnitInfoList[i].StartAddress<=Address) and (FUnitInfoList[i].EndAddress>Address) then
begin
Result := @FUnitInfoList[i];
Exit;
end;
end;
function TDebugInfomation.SearchLineFromAddress(UnitInfo: PUnitInfo; Address: Integer): Integer;
var
i: Integer;
begin
Result := -1;
for i:=Length(UnitInfo.LineList)-1 downto 0 do
if UnitInfo.LineList[i].Address<=Address then
begin
Result := UnitInfo.LineList[i].Line;
Exit;
end;
end;
function TDebugInfomation.SearchRoutineFromAddress(Address: Integer): PRoutineInfo;
var
i: Integer;
begin
Result := nil;
for i:=0 to FRoutineInfoListCount-1 do
if (Address>=FRoutineInfoList[i].Address) and (Address<FRoutineInfoList[i].Address+FRoutineInfoList[i].Length) then
begin
Result := @FRoutineInfoList[i];
Exit;
end;
end;
procedure TDebugInfomation.Load(const FileName: string);
var
SourceFile: file of Byte;
function Load_TD32: Boolean;
type
TTD32_DebugInfoHeader = packed record
Magic: array[0..3] of Char;
Size: Longint;
end;
TTD32_FileListHeader = packed record
HeaderSize: Word;
FileHeaderSize: Word;
FileCount: Word;
end;
TTD32_FileHeader = packed record
FileType: Word;
_1: Word;
Offset: Longint;
Size: Longint;
end;
TTD32_SymbolHeader = packed record
HeaderSize: Word;
SymbolType: Word;
_1: Longint;
_2: Longint;
_3: Longint;
CodeLength: Longint;
_4: Longint;
_5: Longint;
CodeAddress: Longint;
_6: Longint;
_7: Longint;
NameIndex: Longint;
end;
TTD32_UnitListHeader = packed record
UnitCount: Word;
_1: Word;
UnitList: array[0..0] of Longint;
end;
TTD32_UnitHeader = packed record
_1: Word;
NameIndex: Longint;
LineList: array[0..0] of Integer;
end;
TTD32_UnitLineHeader = packed record
_1: Word;
Count: Word;
OffsetList: array[0..0] of Integer;
end;
var
Header: TTD32_DebugInfoHeader;
FileListHeader: TTD32_FileListHeader;
DebugInfoBasePos, DebugInfoBasePos2: Integer;
FileHeaderBuf: array of Byte;
FileHeader: ^TTD32_FileHeader;
TextBuf: array of Char;
TextBufP: PChar;
TextList: array of PChar;
FileDataBuf: array of Byte;
FileDataBufPos: Integer;
i, j, n, Address: Integer;
SymbolHeader: ^TTD32_SymbolHeader;
UnitListHeader: ^TTD32_UnitListHeader;
UnitHeader: ^TTD32_UnitHeader;
UnitLineHeader: ^TTD32_UnitLineHeader;
CurrentUnit: PUnitInfo;
begin
Result := False;
{ ファイルの最後のヘッダをチェック }
Seek(SourceFile, FileSize(SourceFile)-SizeOf(Header));
BlockRead(SourceFile, Header, SizeOf(Header));
if (Header.Magic<>'FB09') and (Header.Magic<>'FB0A') or (Header.Size<=0) then Exit;
DebugInfoBasePos := FilePos(SourceFile) - Header.Size;
{ デバッグ情報の始まりのヘッダをチェック }
Seek(SourceFile, DebugInfoBasePos);
BlockRead(SourceFile, Header, SizeOf(Header));
if (Header.Magic<>'FB09') and (Header.Magic<>'FB0A') or (Header.Size<=0) then Exit;
DebugInfoBasePos2 := DebugInfoBasePos + Header.Size;
{ 各ファイルのヘッダをロード }
Seek(SourceFile, DebugInfoBasePos2);
BlockRead(SourceFile, FileListHeader, SizeOf(FileListHeader));
SetLength(FileHeaderBuf, FileListHeader.FileHeaderSize * FileListHeader.FileCount);
Seek(SourceFile, DebugInfoBasePos2 + FileListHeader.HeaderSize);
BlockRead(SourceFile, FileHeaderBuf[0], Length(FileHeaderBuf));
{ まずテキストをロード }
for i:=0 to FileListHeader.FileCount-1 do
begin
FileHeader := @FileHeaderBuf[i*FileListHeader.FileHeaderSize];
if FileHeader.FileType=$0130 then
begin
{ テキスト }
SetLength(TextBuf, FileHeader.Size);
Seek(SourceFile, DebugInfoBasePos+FileHeader.Offset);
BlockRead(SourceFile, TextBuf[0], Length(TextBuf));
{ テキストのリストを構築 }
SetLength(TextList, PLongint(@TextBuf[0])^);
TextBufP := @TextBuf[4];
for j:=0 to Length(TextList)-1 do
begin
TextList[j] := @TextBufP[1];
Inc(TextBufP, Ord(TextBufP[0])+2);
end;
end;
end;
{ 各種情報をロード }
for i:=0 to FileListHeader.FileCount-1 do
begin
FileHeader := @FileHeaderBuf[i*FileListHeader.FileHeaderSize];
if FileHeader.Size<=0 then Continue;
if FileHeader.FileType=$0125 then
begin
{ シンボル情報 }
SetLength(FileDataBuf, FileHeader.Size);
Seek(SourceFile, DebugInfoBasePos+FileHeader.Offset);
BlockRead(SourceFile, FileDataBuf[0], Length(FileDataBuf));
FileDataBufPos := 4;
while FileDataBufPos<=FileHeader.Size-SizeOf(TTD32_SymbolHeader) do
begin
SymbolHeader := @FileDataBuf[FileDataBufPos];
if ((SymbolHeader.SymbolType=$205) or (SymbolHeader.SymbolType=$204)) and (SymbolHeader.NameIndex>0) then
begin
if FRoutineInfoListCount mod 1024=0 then
SetLength(FRoutineInfoList, Length(FRoutineInfoList)+1024);
FRoutineInfoList[FRoutineInfoListCount].Name := TextList[SymbolHeader.NameIndex-1];
FRoutineInfoList[FRoutineInfoListCount].Address := SymbolHeader.CodeAddress;
FRoutineInfoList[FRoutineInfoListCount].Length := SymbolHeader.CodeLength;
FRoutineInfoList[FRoutineInfoListCount].UnitInfo := nil;
Inc(FRoutineInfoListCount);
end;
Inc(FileDataBufPos, SymbolHeader.HeaderSize+2);
end;
end else
if FileHeader.FileType=$0127 then
begin
{ ユニット情報 }
SetLength(FileDataBuf, FileHeader.Size);
Seek(SourceFile, DebugInfoBasePos+FileHeader.Offset);
BlockRead(SourceFile, FileDataBuf[0], Length(FileDataBuf));
UnitListHeader := @FileDataBuf[0];
for j:=0 to UnitListHeader.UnitCount-1 do
begin
UnitHeader := @FileDataBuf[UnitListHeader.UnitList[j]];
if UnitHeader.NameIndex>0 then
begin
if FUnitInfoListCount mod 16=0 then
SetLength(FUnitInfoList, Length(FUnitInfoList)+16);
CurrentUnit := @FUnitInfoList[FUnitInfoListCount];
Inc(FUnitInfoListCount);
CurrentUnit.Name := TextList[UnitHeader.NameIndex-1];
CurrentUnit.StartAddress := MaxInt;
CurrentUnit.EndAddress := 0;
{ ディレクトリ名を削除 }
for n:=Length(CurrentUnit.Name) downto 1 do
if CurrentUnit.Name[n]='\' then
begin
CurrentUnit.Name := Copy(CurrentUnit.Name, n+1, MaxInt);
Break;
end;
{ 行番号情報 }
UnitLineHeader := @FileDataBuf[UnitHeader.LineList[0]];
SetLength(CurrentUnit.LineList, UnitLineHeader.Count);
for n:=0 to UnitLineHeader.Count-1 do
begin
Address := UnitLineHeader.OffsetList[n];
CurrentUnit.StartAddress := Min(CurrentUnit.StartAddress, Address);
CurrentUnit.EndAddress := Max(CurrentUnit.EndAddress, Address);
CurrentUnit.LineList[n].Line := PArrayWord(@UnitLineHeader.OffsetList[UnitLineHeader.Count])[n];
CurrentUnit.LineList[n].Address := Address;
end;
end;
end;
end;
end;
Result := True;
end;
var
OldFileMode: Byte;
i: Integer;
begin
OldFileMode:= FileMode;
FileMode:= 0;
try
AssignFile(SourceFile, FileName);
try
Reset(SourceFile);
Load_TD32;
finally
CloseFile(SourceFile);
end;
finally
FileMode := OldFileMode;
end;
{ 各ルーチンのアドレスからユニットを検索 }
for i:=1 to FRoutineInfoListCount-1 do
FRoutineInfoList[i].UnitInfo := SearchUnitFromAddress(FRoutineInfoList[i].Address);
end;
var
GetMemCount: Integer;
OldMemMgr: TMemoryManager;
Lock: TRTLCriticalSection;
{$IFDEF MemCheckStackTrace}
const
DebugLogSepText = '---------------------------------------------------------------';
var
DebugInfo: TDebugInfomation;
ImageBaseAddress: Integer = $400000;
LastMemBlock: PMemBlockHeader = nil;
FatalErrorFlag: Boolean = False;
DebugLogFileName: string[255];
DebugLogFileNameFirstFlag: Boolean = True;
var
AppAtom: TAtom = 0;
{$I-}
procedure PutDebugLog(const Text: string);
const
CRLF: array[0..1] of Char = #13#10;
var
F: file of Byte;
FileNewFlag: Boolean;
procedure PutDebugLog2(const Text: string);
begin
BlockWrite(F, Text[1], Length(Text));
BlockWrite(F, CRLF, SizeOf(CRLF));
end;
begin
if DebugLogFileName='' then Exit;
FileNewFlag := False;
{if AppAtom=0 then
begin
if GlobalFindAtom(PChar(ParamStr(0)))=0 then
FileOpenMode := True;
AppAtom := GlobalAddAtomA(PChar(ParamStr(0)));
end;}
AssignFile(F, DebugLogFileName);
try
if FileNewFlag then
begin
Rewrite(F);
if IOResult<>0 then
Exit;
end else
begin
Reset(F);
if IOResult<>0 then
begin
Rewrite(F);
if IOResult<>0 then
Exit;
FileNewFlag := True;
end;
end;
Seek(F, FileSize(F));
if FileNewFlag then
begin
PutDebugLog2(ParamStr(0));
PutDebugLog2('');
PutDebugLog2(DebugLogSepText);
end;
PutDebugLog2(Text);
finally
CloseFile(F);
end;
end;
{$I+}
procedure EndPutDebugLog;
begin
if AppAtom<>0 then
GlobalDeleteAtom(AppAtom);
end;
procedure PutDebugLogHeader(const Text: string);
var
SystemTime: TSystemTime;
TimeText: string;
begin
GetLocalTime(SystemTime);
TimeText := IntToStr2(SystemTime.wYear, 4)+'/'+IntToStr2(SystemTime.wMonth, 2)+'/'+IntToStr2(SystemTime.wDay, 2)+' '+
IntToStr(SystemTime.wHour)+':'+IntToStr2(SystemTime.wMinute, 2)+':'+IntToStr2(SystemTime.wSecond, 2);
PutDebugLog(TimeText + ' #' + IntToHex(GetCurrentProcessId, 8) + ' - ' + Text);
end;
procedure PutDebugLogSep;
begin
PutDebugLog(DebugLogSepText);
end;
procedure InitImageBaseAddress;
var
NTHeader: PImageFileHeader;
NTOptHeader: PImageOptionalHeader;
begin
NTHeader:= PImageFileHeader(Cardinal(PImageDosHeader(HInstance)._lfanew) + HInstance + 4); {SizeOf(IMAGE_NT_SIGNATURE) = 4}
NTOptHeader:= PImageOptionalHeader(Cardinal(NTHeader) + IMAGE_SIZEOF_FILE_HEADER);
ImageBaseAddress := HInstance + NTOptHeader.BaseOfCode;
end;
procedure LoadDebugInfo;
begin
DebugInfo := TDebugInfomation.Create;
DebugInfo.Load(ParamStr(0));
end;
procedure UnLoadDebugInfo;
var
Block: PMemBlockHeader;
begin
DebugInfo.Free;
DebugInfo := nil;
while LastMemBlock<>nil do
begin
Block := LastMemBlock.Right;
OldMemMgr.FreeMem(LastMemBlock);
LastMemBlock := Block;
end;
end;
procedure TraceStackFrame(var Block: TMemBlockHeader; Offset: Integer; EBP: Pointer);
var
Address: Integer;
OrgEBP: Pointer;
begin
Block.StackFrameCount := 0;
OrgEBP := EBP;
while True do
begin
{ 4バイト境界に配置されている必要がある }
if DWORD(EBP) and 3<>0 then Exit;
{ 少なくとも8バイト正常に読める必要がある }
if IsBadReadPtr(EBP, 12) then Exit;
{ 関数アドレス取得 }
Address := PInteger(DWORD(EBP)+4)^-ImageBaseAddress-1;
if Address<0 then Exit;
{ 関数名取得 }
if Offset=0 then
begin
Block.StackFrame[Block.StackFrameCount] := PPointer(Integer(OrgEBP)+8)^;
Inc(Block.StackFrameCount);
if Block.StackFrameCount>=High(Block.StackFrame) then Exit;
end;
if Offset<=0 then
begin
Block.StackFrame[Block.StackFrameCount] := PPointer(DWORD(EBP)+4)^;
Inc(Block.StackFrameCount);
if Block.StackFrameCount>=High(Block.StackFrame) then Exit;
end;
Dec(Offset);
{ 次のスタックフレームへ移動 }
EBP := PPointer(EBP)^;
end;
end;
function GetRoutineInfoText(RoutineInfo: PRoutineInfo; Address: Integer): string;
begin
if RoutineInfo.UnitInfo<>nil then
Result := RoutineInfo.UnitInfo.Name + '('+IntToStr(DebugInfo.SearchLineFromAddress(RoutineInfo.UnitInfo, Address))+')'+' : '+RoutineInfo.Name+' 関数'
else
Result := '不明なファイル : ' + RoutineInfo.Name + ' 関数';
end;
function TraceMemBlockStackFrame(const Block: TMemBlockHeader): string;
var
IsAnsiString: Boolean;
function TraceMemBlockStackFrame_FuncList(const Block: TMemBlockHeader): string;
var
i: Integer;
Address: Integer;
RoutineInfo: PRoutineInfo;
begin
Result := '';
for i:=0 to Block.StackFrameCount-1 do
begin
Address := Integer(Block.StackFrame[i])-ImageBaseAddress-1;
if Address<0 then Break;
RoutineInfo := DebugInfo.SearchRoutineFromAddress(Address);
if RoutineInfo=nil then Continue;
if RoutineInfo.Name='@NewAnsiString' then
IsAnsiString := True;
Result := Result + ' ' + GetRoutineInfoText(RoutineInfo, Address) + #13#10;
end;
end;
function DumpBinaryData(Buffer: PByte; BufSize: Integer): string;
var
i: Integer;
begin
Result := '';
for i:=0 to BufSize-1 do
begin
Result := Result + HexTbl[Buffer^ shr 4];
Result := Result + HexTbl[Buffer^ and $F];
Inc(Buffer);
end;
end;
var
i: Integer;
s, TraceText: string;
P: PByte;
begin
Result := '';
IsAnsiString := False;
TraceText := TraceMemBlockStackFrame_FuncList(Block);
{ ブロックの種類解析 }
if Block.StackFrame[0]=Pointer(Integer(@TObject.NewInstance)+9) then
s := TObject(Integer(@Block)+SizeOf(TMemBlockHeader)).ClassName + ' クラス'
else if IsAnsiString then
s := '文字列(AnsiString)'
else
s := '不明';
Result := Result + '種類: ' + s + #13#10;
Result := Result + 'サイズ: ' + IntToStr(Block.Size) + ' バイト' + #13#10;
{ データをダンプする }
Result := Result + 'バイナリ:' + #13#10;
i := Min(Block.Size, 1024);
P := Pointer(Integer(@Block)+SizeOf(TMemBlockHeader));
while i>0 do
begin
Result := Result + ' '+DumpBinaryData(P, Min(i, 32)) + #13#10;
Inc(P, Min(i, 32));
Dec(i, 32);
end;
Result := Result + #13#10;
{ スタックフレーム }
Result := Result + TraceText;
end;
procedure AllocBlock(var Block: PMemBlockHeader; Size: Integer; EBP: Pointer);
begin
Block.Size := Size;
try
if IsMultiThread then EnterCriticalSection(Lock);
{ リンクリストに登録 }
Block.Left := nil;
Block.Right := LastMemBlock;
if LastMemBlock<>nil then LastMemBlock.Left := Block;
LastMemBlock := Block;
{ 確保回数を1増加 }
Inc(GetMemCount);
finally
if IsMultiThread then LeaveCriticalSection(Lock);
end;
{ スタックフレーム解析 }
Block.StackFrameCount := 0;
TraceStackFrame(Block^, 1, EBP);
Inc(PByte(Block), SizeOf(TMemBlockHeader));
end;
procedure DeallocBlock(var Block: PMemBlockHeader);
begin
Dec(PByte(Block), SizeOf(TMemBlockHeader));
try
if IsMultiThread then EnterCriticalSection(Lock);
{ 確保回数を1減らす }
Dec(GetMemCount);
{ リンクリストから削除 }
if Block.Left<>nil then Block.Left.Right := Block.Right;
if Block.Right<>nil then Block.Right.Left := Block.Left;
if LastMemBlock=Block then LastMemBlock := Block.Right;
finally
if IsMultiThread then LeaveCriticalSection(Lock);
end;
end;
function GetEBP: Pointer; assembler; register;
asm
mov eax,ebp
end;
function NewGetMem(Size: Integer): Pointer;
begin
if Size>0 then
begin
Result := OldMemMgr.GetMem(Size+SizeOf(TMemBlockHeader));
AllocBlock(PMemBlockHeader(Result), Size, GetEBP);
end else
Result := nil;
end;
function NewFreeMem(P: Pointer): Integer;
begin
if P<>nil then
begin
DeallocBlock(PMemBlockHeader(P));
Result := OldMemMgr.FreeMem(P);
end else
Result := 0;
end;
function NewReallocMem(P: Pointer; Size: Integer): Pointer;
begin
if P<>nil then
DeallocBlock(PMemBlockHeader(P));
Result := OldMemMgr.ReallocMem(P, Size+SizeOf(TMemBlockHeader));
if Result<>nil then
AllocBlock(PMemBlockHeader(Result), Size, GetEBP);
end;
var
OldExceptObjProc: Pointer;
GetExceptInfoFunc: TMemCheckGetExceptInfoFunc;
SetExceptMessageFunc: TMemCheckSetExceptMessageFunc;
function MyGetExceptionObject(P: PExceptionRecord): Pointer;
type
TExceptObjProc = function(P: PExceptionRecord): Pointer;
var
EBP: Pointer;
Address: Integer;
TraceList: array[0..31] of Integer;
TraceListCount: Integer;
ExceptionRecord: PExceptionRecord;
RoutineInfo: PRoutineInfo;
i: Integer;
Text: string;
begin
ExceptObjProc := OldExceptObjProc;
try
{ スタックフレームをトレースする }
EBP := GetEBP;
if IsBadReadPtr(EBP, 4) then
begin
Result := TExceptObjProc(OldExceptObjProc)(P);
Exit;
end;
EBP := PPointer(EBP)^;
if IsBadReadPtr(EBP, 4) then
begin
Result := TExceptObjProc(OldExceptObjProc)(P);
Exit;
end;
EBP := PPointer(EBP)^;
if IsBadReadPtr(EBP, 4) then
begin
Result := TExceptObjProc(OldExceptObjProc)(P);
Exit;
end;
EBP := PPointer(EBP)^;
TraceListCount := 0;
while True do
begin
{ 4バイト境界に配置されている必要がある }
if DWORD(EBP) and 3<>0 then Break;
{ 少なくとも8バイト正常に読める必要がある }
if IsBadReadPtr(EBP, 12) then Break;
{ 関数アドレス取得 }
Address := PInteger(DWORD(EBP)+4)^-ImageBaseAddress-1;
if Address<0 then Break;
{ 関数名取得 }
TraceList[TraceListCount] := Address;
Inc(TraceListCount);
{ 次のスタックフレームへ移動 }
EBP := PPointer(EBP)^;
end;
{ 元の ExceptObjProc を呼び出す }
Result := TExceptObjProc(OldExceptObjProc)(P);
{ 例外の情報を詳細化する }
Text := '';
ExceptionRecord := nil;
GetExceptInfoFunc(Result, Text, ExceptionRecord);
if Text<>'' then
begin
if (Copy(Text, Length(Text)-1, 2)<>'。') and (Copy(Text, Length(Text), 1)<>'.') then
Text := Text + '.';
end;
Text := Text + #13#10#13#10;
if ExceptionRecord<>nil then
begin
FatalErrorFlag := True;
Address := Integer(ExceptionRecord.ExceptionAddress)-ImageBaseAddress-1;
RoutineInfo := DebugInfo.SearchRoutineFromAddress(Address);
if RoutineInfo<>nil then
Text := Text + #13#10 + GetRoutineInfoText(RoutineInfo, Address) + #13#10;
end;
for i:=0 to TraceListCount-1 do
begin
RoutineInfo := DebugInfo.SearchRoutineFromAddress(TraceList[i]);
if RoutineInfo=nil then Break;
Text := Text + GetRoutineInfoText(RoutineInfo, TraceList[i]) + #13#10;
end;
for i:=Length(Text) to 1 do
if not (Text[i] in [#13, #10]) then
begin
Text := Copy(Text, 1, i);
Break;
end;
if Assigned(SetExceptMessageFunc) then
SetExceptMessageFunc(Result, Text);
{ ログに記録する }
try
if IsMultiThread then EnterCriticalSection(Lock);
PutDebugLogHeader(TObject(Result).ClassName + ' 例外が発生しました。');
PutDebugLog('');
PutDebugLog(Text);
PutDebugLogSep;
finally
if IsMultiThread then LeaveCriticalSection(Lock);
end;
finally
ExceptObjProc := @MyGetExceptionObject;
end;
end;
procedure MemCheckInstallExceptionHandler(GetExceptInfo: TMemCheckGetExceptInfoFunc;
SetExceptMessage: TMemCheckSetExceptMessageFunc);
begin
GetExceptInfoFunc := GetExceptInfo;
SetExceptMessageFunc := SetExceptMessage;
OldExceptObjProc := ExceptObjProc;
ExceptObjProc := @MyGetExceptionObject;
end;
procedure AppStart;
var
i: Integer;
Flag: Boolean;
begin
{ ログのファイル名取得 }
DebugLogFileName := ParamStr(0);
Flag := False;
for i:=Length(DebugLogFileName) downto 1 do
begin
if DebugLogFileName[i]='.' then
begin
DebugLogFileName := Copy(DebugLogFileName, 1, i) + 'log';
Flag := True;
Break;
end;
end;
if not Flag then
DebugLogFileName := DebugLogFileName + '.log';
{ ログ出力 }
PutDebugLogHeader('プログラムが起動しました。');
PutDebugLogSep;
end;
procedure AppExit;
var
s: string;
Block: PMemBlockHeader;
begin
s := '';
{ メモリリーク情報レポート }
if GetMemCount>0 then
begin
PutDebugLogHeader(IntToStr(GetMemCount) + ' 回メモリーを解放し忘れています。');
PutDebugLog('');
PutDebugLog('');
Block := LastMemBlock;
while Block<>nil do
begin
PutDebugLog(TraceMemBlockStackFrame(Block^));
Block := Block.Right;
end;
PutDebugLogSep;
if s<>'' then s := s + #13#13 + '---------------------------------------------------------' + #13#10;
s := s + IntToStr(GetMemCount) + ' 回メモリーを解放し忘れています。';
FatalErrorFlag := True;
end;
PutDebugLogHeader('プログラムを終了しました。');
PutDebugLogSep;
{ 警告表示 }
if FatalErrorFlag then
begin
if s<>'' then s := s + #13#13 + '---------------------------------------------------------' + #13#10;
s := s + '今回の使用で致命的なエラー(メモリリークなど)が発生したことがあります。' + #13#10#13#10;
if DebugLogFileName<>'' then
s := s + DebugLogFileName + ' を開発元へ送ると原因を究明できる場合があります。'
else
s := s + 'エラーのログを '+DebugLogFileName+' に出力しようとしましたが出来ませんでした。';
if DebugLogFileName<>'' then
MessageBox(0, PChar(s), '警告', MB_OK or MB_ICONEXCLAMATION)
else
MessageBox(0, PChar(s), '警告', MB_OK or MB_ICONEXCLAMATION);
end;
end;
{$ELSE}
function NewGetMem(Size: Integer): Pointer;
begin
Result := OldMemMgr.GetMem(Size);
if Result<>nil then
begin
try
if IsMultiThread then EnterCriticalSection(Lock);
Inc(GetMemCount);
finally
if IsMultiThread then LeaveCriticalSection(Lock);
end;
end;
end;
function NewFreeMem(P: Pointer): Integer;