forked from AlexanderBagel/ProcessMemoryMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryMap.DebugMapData.pas
More file actions
276 lines (248 loc) · 8.03 KB
/
Copy pathMemoryMap.DebugMapData.pas
File metadata and controls
276 lines (248 loc) · 8.03 KB
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
////////////////////////////////////////////////////////////////////////////////
//
// ****************************************************************************
// * Project : MemoryMap
// * Unit Name : MemoryMap.DebugMapData.pas
// * Purpose : Êëàññ äëÿ ðàáîòû ñ îòëàäî÷íûì MAP ôàéëîì.
// * Author : Àëåêñàíäð (Rouse_) Áàãåëü
// * Copyright : © Fangorn Wizards Lab 1998 - 2017.
// * Version : 1.0.1
// * Home Page : http://rouse.drkb.ru
// * Home Blog : http://alexander-bagel.blogspot.ru
// ****************************************************************************
// * Stable Release : http://rouse.drkb.ru/winapi.php#pmm2
// * Latest Source : https://github.com/AlexanderBagel/ProcessMemoryMap
// ****************************************************************************
//
unit MemoryMap.DebugMapData;
interface
uses
Winapi.Windows,
System.Classes,
Generics.Collections,
Generics.Defaults,
System.SysUtils;
type
TDebugMapItem = record
Address, EndAddress: NativeUInt;
ModuleName,
FunctionName: string;
end;
TDebugMap = class
private type
TSectionData = record
Index: Integer;
StartAddr: DWORD;
Length: DWORD;
end;
private
FItems: TList<TDebugMapItem>;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Init(BaseAddress: ULONG_PTR; const ModulePath: string);
function GetDescriptionAtAddr(Address: ULONG_PTR): string;
function GetDescriptionAtAddrWithOffset(Address: ULONG_PTR): string;
procedure GetExportFuncList(const ModuleName: string; Value: TStringList);
end;
implementation
uses
MemoryMap.PEImage;
{ TDebugMap }
procedure TDebugMap.Clear;
begin
FItems.Clear;
end;
constructor TDebugMap.Create;
begin
FItems := TList<TDebugMapItem>.Create(
TComparer<TDebugMapItem>.Construct(
function (const A, B: TDebugMapItem): Integer
begin
Result := Integer(A.Address) - Integer(B.Address);
end)
);
end;
destructor TDebugMap.Destroy;
begin
FItems.Free;
inherited;
end;
function TDebugMap.GetDescriptionAtAddr(Address: ULONG_PTR): string;
var
I: Integer;
Item: TDebugMapItem;
begin
Result := '';
Item.Address := Address;
if FItems.BinarySearch(Item, I) then
if I >= 0 then
Result := FItems.List[I].ModuleName + '!' + FItems.List[I].FunctionName;
end;
function TDebugMap.GetDescriptionAtAddrWithOffset(Address: ULONG_PTR): string;
var
I: Integer;
begin
Result := '';
for I := 0 to FItems.Count - 1 do
if FItems.List[I].Address <= Address then
if FItems.List[I].EndAddress > Address then
begin
if FItems.List[I].Address = Address then
Result := FItems.List[I].ModuleName + '!' + FItems.List[I].FunctionName
else
Result := FItems.List[I].ModuleName + '!' + FItems.List[I].FunctionName +
' + 0x' + IntToHex(Address - FItems.List[I].Address, 1);
end;
end;
procedure TDebugMap.GetExportFuncList(const ModuleName: string;
Value: TStringList);
var
I: Integer;
begin
for I := 0 to FItems.Count - 1 do
if FItems[I].ModuleName = ModuleName then
Value.AddObject(FItems[I].FunctionName, Pointer(FItems[I].Address));
end;
procedure TDebugMap.Init(BaseAddress: ULONG_PTR; const ModulePath: string);
var
PEImage: TPEImage;
MapFile: TStringList;
SectionDataList: TList<TSectionData>;
I, A, Count, StartPosition, SpacePos, SectionIndex: Integer;
Line, SectionName, SectionClass: string;
Section: TSectionData;
FoundTable: Boolean;
SectionAddress: DWORD;
DebugMapItem: TDebugMapItem;
begin
StartPosition := FItems.Count;
try
MapFile := TStringList.Create;
try
MapFile.LoadFromFile(ChangeFileExt(ModulePath, '.map'));
DebugMapItem.ModuleName := ExtractFileName(ModulePath);
SectionDataList := TList<TSectionData>.Create;
try
PEImage := TPEImage.Create(0, False);
try
PEImage.GetInfoFromImage(ModulePath, nil, 0);
I := 0;
Count := MapFile.Count;
// èùåì íà÷àëî òàáëèöû ñåêöèé
while Copy(Trim(MapFile[I]), 1, 5) <> 'Start' do
begin
Inc(I);
if I = Count then Exit;
end;
// ïîëó÷àåì íîìåð ñåêöèè è åå èìÿ
Inc(I);
if I = Count then Exit;
Line := Trim(MapFile[I]);
while Line <> '' do
begin
Section.Index := StrToInt(Copy(Line, 1, 4));
Delete(Line, 1, 24);
Line := TrimLeft(Line);
SpacePos := Pos(' ', Line);
if SpacePos = 0 then Continue;
SectionName := Copy(Line, 1, SpacePos - 1);
Delete(Line, 1, SpacePos);
// â ñòàðûõ âåðñèÿõ äåëüôè â ÐÅ ôàéëå èìÿ ñåêöèè íàçûâàëîñü ïî èìåíè åå êëàññà
// ïîýòîìó áóäåì èñêàòü ïðàâèëüíóþ ñåêöèþ îïèðàÿñü íà ýòîò ìîìåíò
SectionClass := TrimLeft(Line);
// óáèðàåì äåêîðèðîâàíèå (MS VC++ Debug MAP)
SpacePos := Pos('$', SectionName);
if SpacePos > 0 then
SectionName := Copy(SectionName, 1, SpacePos - 1);
// åñëè ñåêöèÿ ñîäåðæèò êîä, çàíîñèì åå â ñïèñîê
for A := 0 to PEImage.Sections.Count - 1 do
if (PEImage.Sections[A].Caption = ShortString(SectionName)) or
(PEImage.Sections[A].Caption = ShortString(SectionClass)) then
begin
if PEImage.Sections[A].IsCode then
begin
Section.StartAddr := PEImage.Sections[A].Address + BaseAddress;
Section.Length := PEImage.Sections[A].Size;
SectionDataList.Add(Section);
end;
Break;
end;
Inc(I);
if I = Count then Exit;
Line := Trim(MapFile[I]);
end;
finally
PEImage.Free;
end;
// èùåì íà÷àëî òàáëèöû "Publics by Value"
FoundTable := False;
while not FoundTable do
begin
Inc(I);
if I = Count then Exit;
Line := Trim(MapFile[I]);
while Copy(Line, 1, 7) <> 'Address' do
begin
Inc(I);
if I = Count then Exit;
Line := Trim(MapFile[I]);
end;
Delete(Line, 1, 8);
FoundTable := Copy(TrimLeft(Line), 1, 16) = 'Publics by Value';
end;
// ïàðñèì òàáëèöó "Publics by Value"
Inc(I, 2);
if I >= Count then Exit;
Line := Trim(MapFile[I]);
while Line <> '' do
begin
SectionIndex := StrToInt(Copy(Line, 1, 4));
if SectionIndex <> 1 then
begin
Inc(I);
if I = Count then Exit;
Line := Trim(MapFile[I]);
Continue;
end;
SectionAddress := $FFFFFFFF;
for A := 0 to SectionDataList.Count - 1 do
if SectionDataList[A].Index = SectionIndex then
begin
SectionAddress := SectionDataList[A].StartAddr;
DebugMapItem.EndAddress := SectionAddress + SectionDataList[A].Length;
Break;
end;
if SectionAddress = $FFFFFFFF then Continue;
Delete(Line, 1, 5);
DebugMapItem.Address := SectionAddress + NativeUInt(StrToInt('$' + Copy(Line, 1, 8)));
Delete(Line, 1, 9);
Line := TrimLeft(Line);
SpacePos := Pos(' ', Line);
if SpacePos = 0 then
DebugMapItem.FunctionName := Line
else
DebugMapItem.FunctionName := Copy(Line, 1, SpacePos - 1);
FItems.Add(DebugMapItem);
Inc(I);
if I = Count then Exit;
Line := Trim(MapFile[I]);
end;
finally
SectionDataList.Free;
end;
finally
MapFile.Free;
end;
// âûñòàâëÿì ïðàâèëüíûå äëèíû ôóíêöèé
for I := FItems.Count - 1 downto StartPosition + 1 do
FItems.List[I - 1].EndAddress := FItems.List[I].Address;
FItems.Sort;
except
// åñëè íå ñìîãëè ðàñïàðñèòü - âûêèäûâàåì âñå èç ìàññèâà äàííûõ
for I := FItems.Count - 1 downto StartPosition do
FItems.Delete(I);
end;
end;
end.