dumpclass.pp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. program DumpClass;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils;
  5. const
  6. VMT_COUNT = 100;
  7. type
  8. TMethodNameTableEntry = packed record
  9. Name: PShortstring;
  10. Addr: Pointer;
  11. end;
  12. TMethodNameTable = packed record
  13. Count: DWord;
  14. Entries: packed array[0..9999999] of TMethodNameTableEntry;
  15. end;
  16. PMethodNameTable = ^TMethodNameTable;
  17. TPointerArray = packed array[0..9999999] of Pointer;
  18. PPointerArray = ^TPointerArray;
  19. PFieldInfo = ^TFieldInfo;
  20. TFieldInfo = packed record
  21. FieldOffset: LongWord;
  22. ClassTypeIndex: Word;
  23. Name: ShortString;
  24. end;
  25. PFieldClassTable = ^TFieldClassTable;
  26. TFieldClassTable =
  27. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  28. packed
  29. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  30. record
  31. Count: Word;
  32. Entries: array[Word] of TPersistentClass;
  33. end;
  34. PFieldTable = ^TFieldTable;
  35. TFieldTable =
  36. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  37. packed
  38. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  39. record
  40. FieldCount: Word;
  41. ClassTable: PFieldClassTable;
  42. { Fields: array[Word] of TFieldInfo; Elements have variant size! }
  43. end;
  44. {$M+}
  45. TMyTest = class(TObject)
  46. published
  47. F1: TMyTest;
  48. F2: TMyTest;
  49. procedure P1; virtual;
  50. procedure P2; virtual;
  51. end;
  52. {$M-}
  53. TMyTest2 = class(TMyTest)
  54. F3: TMyTest;
  55. F4: TMyTest;
  56. procedure P2; override;
  57. procedure P3; virtual;
  58. end;
  59. TMyPersistent = class(TPersistent)
  60. procedure P1; virtual;
  61. procedure P2; virtual;
  62. end;
  63. procedure TMyTest.P1;
  64. begin
  65. end;
  66. procedure TMyTest.P2;
  67. begin
  68. end;
  69. procedure TMyTest2.P2;
  70. begin
  71. end;
  72. procedure TMyTest2.P3;
  73. begin
  74. end;
  75. procedure TMyPersistent.P1;
  76. begin
  77. end;
  78. procedure TMyPersistent.P2;
  79. begin
  80. end;
  81. procedure ClassDump(AClass: TClass);
  82. var
  83. Cvmt: PPointerArray;
  84. Cmnt: PMethodNameTable;
  85. Cft: PFieldTable;
  86. FieldOffset: LongWord;
  87. fi: PFieldInfo;
  88. Indent: String;
  89. n, idx: Integer;
  90. SearchAddr: Pointer;
  91. begin
  92. WriteLn('---------------------------------------------');
  93. WriteLn('Dump of ', AClass.ClassName);
  94. WriteLn('---------------------------------------------');
  95. Indent := '';
  96. while AClass <> nil do
  97. begin
  98. WriteLn(Indent, 'Processing ', AClass.Classname);
  99. Indent := Indent + ' ';
  100. //---
  101. Cmnt := PPointer(Pointer(AClass) + vmtMethodTable)^;
  102. if Cmnt <> nil
  103. then begin
  104. WriteLn(Indent, 'Method count: ', IntToStr(Cmnt^.Count));
  105. Cvmt := Pointer(AClass) + vmtMethodStart;
  106. for n := 0 to Cmnt^.Count - 1 do
  107. begin
  108. Write(Indent, 'Search: ', Cmnt^.Entries[n].Name^);
  109. SearchAddr := Cmnt^.Entries[n].Addr;
  110. for idx := 0 to VMT_COUNT - 1 do
  111. begin
  112. if Cvmt^[idx] = SearchAddr
  113. then begin
  114. WriteLn(Indent, ' Found at index: ', IntToStr(idx));
  115. Break;
  116. end;
  117. if idx = VMT_COUNT - 1
  118. then begin
  119. WriteLn('[WARNING] VMT entry "', Cmnt^.Entries[n].Name^, '" not found in "', AClass.ClassName, '"');
  120. Break;
  121. end;
  122. end;
  123. end;
  124. end;
  125. //---
  126. Cft := PPointer(Pointer(AClass) + vmtFieldTable)^;
  127. if Cft <> nil
  128. then begin
  129. WriteLn(Indent, 'Field count: ', Cft^.FieldCount);
  130. fi := @Cft^.ClassTable + SizeOf(Cft^.ClassTable);
  131. for n := 0 to Cft^.FieldCount - 1 do
  132. begin
  133. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  134. pointer(fi):=align(fi,sizeof(pointer));
  135. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  136. Move(fi^.FieldOffset, FieldOffset, SizeOf(FieldOffset));
  137. WriteLn(Indent, ' ', n, ': ', fi^.Name, ' @', FieldOffset);
  138. fi := @fi^.name + 1 + Ord(fi^.name[0]);
  139. end;
  140. WriteLn(Indent, 'Field class count: ', Cft^.ClassTable^.Count);
  141. for n := 0 to Cft^.ClassTable^.Count - 1 do
  142. begin
  143. WriteLn(Indent, ' ', n, ': ', Cft^.ClassTable^.Entries[n].ClassName);
  144. end;
  145. end;
  146. AClass := AClass.ClassParent;
  147. end;
  148. end;
  149. begin
  150. ClassDump(TMyTest);
  151. ClassDump(TMyTest2);
  152. ClassDump(TPersistent);
  153. ClassDump(TMyPersistent);
  154. end.