FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DelphiCodeCoverage/Source/DebugProcess.pas at master · JAM-Software/DelphiCodeCoverage · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
JAM-Software
/
DelphiCodeCoverage
Public
forked from
DelphiCodeCoverage/DelphiCodeCoverage
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DelphiCodeCoverage
/
Source
/
DebugProcess.pas
Copy path
More file actions
More file actions
Latest commit
History
History
History
350 lines (306 loc) · 9.53 KB
Breadcrumbs
DelphiCodeCoverage
/
Source
/
DebugProcess.pas
Copy path
File metadata and controls
350 lines (306 loc) · 9.53 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
(*
*********************************************************************
*)
(*
Delphi Code Coverage
*)
(*
*)
(*
A quick hack of a Code Coverage Tool for Delphi
*)
(*
by Christer Fahlgren and Nick Ring
*)
(*
*)
(*
This Source Code Form is subject to the terms of the Mozilla Public
*)
(*
License, v. 2.0. If a copy of the MPL was not distributed with this
*)
(*
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*)
unit
DebugProcess;
interface
uses
System.Classes,
System.Generics.Collections,
Winapi.Windows,
I_DebugThread,
I_DebugProcess,
I_LogManager,
I_DebugModule,
JCLDebug;
type
TDebugProcess =
class
(TInterfacedObject, IDebugProcess)
private
FProcessHandle: THandle;
FProcessModule: HMODULE;
FModuleList: TList<IDebugModule>;
FDebugThreadLst: IInterfaceList;
FLogManager: ILogManager;
FName: string;
FSize: Cardinal;
FMapScanner: TJCLMapScanner;
public
constructor
Create(
const
AProcessId: DWORD;
const
AProcessHandle: THandle;
const
AProcessModule: HMODULE;
const
AName: string;
const
ASize: Cardinal;
const
AMapScanner: TJCLMapScanner;
const
ALogManager: ILogManager);
destructor
Destroy; override;
procedure
AddThread
(
const
ADebugThread: IDebugThread);
procedure
RemoveThread
(
const
AThreadId: DWORD);
procedure
AddModule
(
const
AModule: IDebugModule);
procedure
RemoveModule
(
const
AModule: IDebugModule);
function
GetModule
(
const
AName: string): IDebugModule;
function
Name
: string; inline;
function
Base
: HMODULE; inline;
function
Handle
: THandle; inline;
function
Size
: Cardinal;
function
MapScanner
: TJCLMapScanner;
function
FindDebugModuleFromAddress
(Addr: Pointer): IDebugModule;
function
GetThreadById
(
const
AThreadId: DWORD): IDebugThread;
function
ReadProcessMemory
(
const
AAddress, AData: Pointer;
const
ASize: Cardinal;
const
AChangeProtect: Boolean = False): Integer;
function
WriteProcessMemory
(
const
AAddress, AData: Pointer;
const
ASize: Cardinal;
const
AChangeProtect: Boolean = False): Integer;
end
;
implementation
uses
System.SysUtils;
constructor
TDebugProcess.Create(
const
AProcessId: DWORD;
const
AProcessHandle: THandle;
const
AProcessModule: HMODULE;
const
AName: string;
const
ASize: Cardinal;
const
AMapScanner: TJCLMapScanner;
const
ALogManager: ILogManager);
begin
inherited
Create;
FProcessHandle := AProcessHandle;
FProcessModule := AProcessModule;
FDebugThreadLst := TInterfaceList.Create;
FModuleList := TList<IDebugModule>.Create;
FName := AName;
FLogManager := ALogManager;
FMapScanner := AMapScanner;
FSize := ASize;
end
;
destructor
TDebugProcess.Destroy;
begin
FDebugThreadLst :=
nil
;
FLogManager :=
nil
;
FModuleList.Free;
FModuleList :=
nil
;
inherited
;
end
;
procedure
TDebugProcess.AddThread
(
const
ADebugThread: IDebugThread);
begin
FDebugThreadLst.Add(ADebugThread);
end
;
procedure
TDebugProcess.RemoveThread
(
const
AThreadId: DWORD);
var
DebugThread: IDebugThread;
begin
DebugThread := GetThreadById(AThreadId);
if
(DebugThread <>
nil
)
then
FDebugThreadLst.Remove(DebugThread);
end
;
function
TDebugProcess.Name
: string;
begin
Result := FName;
end
;
procedure
TDebugProcess.AddModule
(
const
AModule: IDebugModule);
begin
FModuleList.Add(AModule);
end
;
procedure
TDebugProcess.RemoveModule
(
const
AModule: IDebugModule);
begin
FModuleList.Remove(AModule);
end
;
function
TDebugProcess.GetModule
(
const
AName: string): IDebugModule;
var
CurrentModule: IDebugModule;
begin
result :=
nil
;
for
CurrentModule
in
FModuleList
do
begin
if
CurrentModule.
Name
= AName
then
Exit(CurrentModule);
end
;
end
;
function
TDebugProcess.Handle
: THandle;
begin
Result := FProcessHandle;
end
;
function
TDebugProcess.Base
: HMODULE;
begin
Result := FProcessModule;
end
;
function
TDebugProcess.Size
: Cardinal;
begin
Result := FSize;
end
;
function
TDebugProcess.MapScanner
: TJCLMapScanner;
begin
Result := FMapScanner;
end
;
function
TDebugProcess.FindDebugModuleFromAddress
(Addr: Pointer): IDebugModule;
var
ModuleAddress: NativeUINT;
function
AddressBelongsToModule
(
const
AModule: IDebugModule): Boolean;
var
Base: HMODULE;
begin
Base := AModule.Base;
Result := ((ModuleAddress >= Base)
and
(ModuleAddress <= (Base + AModule.Size)));
end
;
var
CurrentModule: IDebugModule;
begin
Result :=
nil
;
ModuleAddress := NativeUINT(Addr);
if
AddressBelongsToModule(IDebugProcess(Self))
then
Result := IDebugProcess(self)
else
begin
for
CurrentModule
in
FModuleList
do
begin
if
AddressBelongsToModule(CurrentModule)
then
Exit(CurrentModule);
end
;
end
;
end
;
function
TDebugProcess.GetThreadById
(
const
AThreadId: DWORD): IDebugThread;
var
ThreadIndex: Integer;
CurrentThread: IInterface;
begin
Result :=
nil
;
for
ThreadIndex :=
0
to
FDebugThreadLst.Count -
1
do
begin
CurrentThread := FDebugThreadLst[ThreadIndex];
if
IDebugThread(CurrentThread).Id = AThreadId
then
Exit(IDebugThread(CurrentThread));
end
;
end
;
function
AddrToHex
(
const
AAddress: Pointer): string;
begin
Result := IntToHex(Integer(AAddress),
8
);
end
;
function
TDebugProcess.ReadProcessMemory
(
const
AAddress, AData: Pointer;
const
ASize: Cardinal;
const
AChangeProtect: Boolean = False): Integer;
var
OldProtect: UINT;
NumberOfBytesRead: SIZE_T;
Changed: Boolean;
begin
Changed := False;
if
not
Winapi.Windows.ReadProcessMemory(Handle, AAddress, AData, ASize, NumberOfBytesRead)
then
begin
//
try changing protection
if
AChangeProtect
and
not
VirtualProtectEx(Handle, AAddress, ASize, PAGE_EXECUTE_READ, @OldProtect)
then
begin
Changed := true;
if
not
Winapi.Windows.ReadProcessMemory(Handle, AAddress, AData, ASize, NumberOfBytesRead)
then
begin
FLogManager.Log(
'
ReadProcessMemory failed reading address -
'
+ AddrToHex(AAddress) +
'
Error:
'
+ I_LogManager.LastErrorInfo);
Result := -
1
;
exit;
end
;
end
else
begin
FLogManager.Log(
'
ReadProcessMemory failed to change protection -
'
+ AddrToHex(AAddress) +
'
Error:
'
+ I_LogManager.LastErrorInfo);
end
;
end
;
if
NumberOfBytesRead <> ASize
then
begin
FLogManager.Log(
'
ReadProcessMemory failed to read address -
'
+ AddrToHex(AAddress)
+
'
Wrong number of bytes -
'
+ IntToStr(NumberOfBytesRead)
+
'
Error:
'
+ I_LogManager.LastErrorInfo);
Result := -
1
;
exit;
end
;
if
Changed
then
begin
if
AChangeProtect
and
not
VirtualProtectEx(Handle, AAddress, ASize, OldProtect, @OldProtect)
then
begin
FLogManager.Log(
'
ReadProcessMemory Failed to restore access read address -
'
+ AddrToHex(AAddress)
+
'
Error:
'
+ I_LogManager.LastErrorInfo);
Result :=
0
;
exit;
end
;
end
;
Result := NumberOfBytesRead;
end
;
function
TDebugProcess.WriteProcessMemory
(
const
AAddress, AData: Pointer;
const
ASize: Cardinal;
const
AChangeProtect: Boolean = False): Integer;
var
OldProtect: UINT;
NumberOfBytesWritten: SIZE_T;
Changed: Boolean;
begin
Changed := False;
//
keep track if we changed page protection
if
not
Winapi.Windows.WriteProcessMemory(Handle, AAddress, AData, ASize, NumberOfBytesWritten)
then
begin
//
Failed to write, thus we try to change the protection
if
AChangeProtect
and
not
(VirtualProtectEx(Handle, AAddress, ASize, PAGE_EXECUTE_READWRITE, @OldProtect))
then
begin
FLogManager.Log(
'
WriteProcessMemory failed to change protection to PAGE_EXECUTE_READWRITE address -
'
+ AddrToHex(AAddress) +
'
Error:
'
+ I_LogManager.LastErrorInfo);
Result := -
1
;
exit;
end
else
begin
Changed := True;
//
Try again after changing protection
if
not
Winapi.Windows.WriteProcessMemory(Handle, AAddress, AData, ASize, NumberOfBytesWritten)
then
begin
FLogManager.Log(
'
WriteProcessMemory failed writing address -
'
+ AddrToHex(AAddress) +
'
Error:
'
+ I_LogManager.LastErrorInfo);
Result := -
1
;
exit;
end
;
end
;
end
;
if
(NumberOfBytesWritten <> ASize)
then
begin
FLogManager.Log(
'
WriteProcessMemory failed to write address -
'
+ AddrToHex(AAddress) +
'
Wrong number of bytes -
'
+ IntToStr(NumberOfBytesWritten) +
'
Error:
'
+ I_LogManager.LastErrorInfo);
Result := -
1
;
exit;
end
;
if
Changed
and
AChangeProtect
and
not
VirtualProtectEx(Handle, AAddress, ASize, OldProtect, @OldProtect)
then
begin
FLogManager.Log(
'
WriteProcessMemory: Failed to restore access read address -
'
+ AddrToHex(AAddress) +
'
Error:
'
+ I_LogManager.LastErrorInfo);
Result :=
0
;
exit;
end
;
if
not
(FlushInstructionCache(Handle, AAddress, NumberOfBytesWritten))
then
begin
FLogManager.Log(
'
WriteProcessMemory: FlushInstructionCache failed for address -
'
+ AddrToHex(AAddress) +
'
Error:
'
+ I_LogManager.LastErrorInfo);
end
;
Result := NumberOfBytesWritten;
end
;
end
.
Back
|
FazBrowse Home
|
New Git URL