summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/dumpclass.pp
blob: b627dd11664fb71d96d562fc7de002a28ad7306e (plain)
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
program DumpClass;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils;

const
  VMT_COUNT = 100;


type
  TMethodNameTableEntry = packed record
      Name: PShortstring;
      Addr: Pointer;
    end;

  TMethodNameTable = packed record
    Count: DWord;
    Entries: packed array[0..9999999] of TMethodNameTableEntry;
  end;
  PMethodNameTable =  ^TMethodNameTable;

  TPointerArray = packed array[0..9999999] of Pointer;
  PPointerArray = ^TPointerArray;

  PFieldInfo = ^TFieldInfo;
  TFieldInfo = packed record
    FieldOffset: LongWord;
    ClassTypeIndex: Word;
    Name: ShortString;
  end;

  PFieldClassTable = ^TFieldClassTable;
  TFieldClassTable =
{$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  packed
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  record
    Count: Word;
    Entries: array[Word] of TPersistentClass;
  end;

  PFieldTable = ^TFieldTable;
  TFieldTable =
{$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  packed
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  record
    FieldCount: Word;
    ClassTable: PFieldClassTable;
    { Fields: array[Word] of TFieldInfo;  Elements have variant size! }
  end;

{$M+}
  TMyTest = class(TObject)
  published
    F1: TMyTest;
    F2: TMyTest;
    procedure P1; virtual;
    procedure P2; virtual;
  end;
{$M-}

  TMyTest2 = class(TMyTest)
    F3: TMyTest;
    F4: TMyTest;
    procedure P2; override;
    procedure P3; virtual;
  end;

  TMyPersistent = class(TPersistent)
    procedure P1; virtual;
    procedure P2; virtual;
  end;

procedure TMyTest.P1;
begin
end;

procedure TMyTest.P2;
begin
end;

procedure TMyTest2.P2;
begin
end;

procedure TMyTest2.P3;
begin
end;

procedure TMyPersistent.P1;
begin
end;

procedure TMyPersistent.P2;
begin
end;

procedure ClassDump(AClass: TClass);
var
  Cvmt: PPointerArray;
  Cmnt: PMethodNameTable;
  Cft:  PFieldTable;
  FieldOffset: LongWord;
  fi:  PFieldInfo;
  Indent: String;
  n, idx: Integer;
  SearchAddr: Pointer;
begin
  WriteLn('---------------------------------------------');
  WriteLn('Dump of ', AClass.ClassName);
  WriteLn('---------------------------------------------');
  Indent := '';
  while AClass <> nil do
  begin
    WriteLn(Indent, 'Processing ', AClass.Classname);
    Indent := Indent + ' ';

    //---
    Cmnt := PPointer(Pointer(AClass) + vmtMethodTable)^;
    if Cmnt <> nil
    then begin
      WriteLn(Indent, 'Method count: ', IntToStr(Cmnt^.Count));

      Cvmt := Pointer(AClass) + vmtMethodStart;

      for n := 0 to Cmnt^.Count - 1 do
      begin
        Write(Indent, 'Search: ', Cmnt^.Entries[n].Name^);

        SearchAddr := Cmnt^.Entries[n].Addr;
        for idx := 0 to VMT_COUNT - 1 do
        begin
          if Cvmt^[idx] = SearchAddr
          then begin
            WriteLn(Indent, ' Found at index: ', IntToStr(idx));
            Break;
          end;
          if idx = VMT_COUNT - 1
          then begin
            WriteLn('[WARNING] VMT entry "', Cmnt^.Entries[n].Name^, '" not found in "', AClass.ClassName, '"');
            Break;
          end;
        end;
      end;
    end;


    //---
    Cft := PPointer(Pointer(AClass) + vmtFieldTable)^;
    if Cft <> nil
    then begin
      WriteLn(Indent, 'Field count: ', Cft^.FieldCount);
      fi := @Cft^.ClassTable + SizeOf(Cft^.ClassTable);
      for n := 0 to Cft^.FieldCount - 1 do
      begin
{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
        pointer(fi):=align(fi,sizeof(pointer));
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}

        Move(fi^.FieldOffset, FieldOffset, SizeOf(FieldOffset));
        WriteLn(Indent, ' ', n, ': ', fi^.Name, ' @', FieldOffset);
        fi := @fi^.name + 1 + Ord(fi^.name[0]);
      end;
      WriteLn(Indent, 'Field class count: ', Cft^.ClassTable^.Count);
      for n := 0 to Cft^.ClassTable^.Count - 1 do
      begin
        WriteLn(Indent, ' ', n, ': ', Cft^.ClassTable^.Entries[n].ClassName);
      end;
    end;

    AClass := AClass.ClassParent;
  end;
end;

begin
  ClassDump(TMyTest);
  ClassDump(TMyTest2);
  ClassDump(TPersistent);
  ClassDump(TMyPersistent);
end.