Menu

[4a4584]: / Source / DebugProcess.pas  Maximize  Restore  History

Download this file

348 lines (304 with data), 9.5 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
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
(***********************************************************************)
(* 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
Classes,
Windows,
I_DebugThread,
I_DebugProcess,
I_LogManager,
I_DebugModule,
Generics.Collections,
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
SysUtils,
JwaWinBase;
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
CurrentModule: IDebugModule;
ModuleAddress: DWORD;
function AddressBelongsToModule(const AModule: IDebugModule): Boolean;
begin
Result := ((ModuleAddress >= AModule.Base)
and (ModuleAddress <= (AModule.Base + AModule.Size)));
end;
begin
Result := nil;
ModuleAddress := DWORD(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
oldprot: UINT;
numbytes: DWORD;
changed: Boolean;
begin
Changed := False;
if not JwaWinBase.ReadProcessMemory(Handle, AAddress, AData, ASize, @numbytes) then
begin
// try changing protection
if AChangeProtect
and not VirtualProtectEx(Handle, AAddress, ASize, PAGE_EXECUTE_READ, @oldprot) then
begin
changed := true;
if not JwaWinBase.ReadProcessMemory(Handle, AAddress, AData, ASize, @numbytes) 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 numbytes <> ASize then
begin
FLogManager.Log(
'ReadProcessMemory failed to read address - ' + AddrToHex(AAddress)
+ ' Wrong number of bytes - ' + IntToStr(numbytes)
+ ' Error:' + I_LogManager.LastErrorInfo);
Result := -1;
exit;
end;
if changed then
begin
if AChangeProtect
and not VirtualProtectEx(Handle, AAddress, ASize, oldprot, @oldprot) then
begin
FLogManager.Log(
'ReadProcessMemory Failed to restore access read address - ' + AddrToHex(AAddress)
+ ' Error:' + I_LogManager.LastErrorInfo);
Result := 0;
exit;
end;
end;
Result := numbytes;
end;
function TDebugProcess.WriteProcessMemory(
const AAddress, AData: Pointer;
const ASize: Cardinal;
const AChangeProtect: Boolean = False): Integer;
var
oldprot: UINT;
numbytes: DWORD;
changed: Boolean;
begin
changed := False; // keep track if we changed page protection
if not JwaWinBase.WriteProcessMemory(Handle, AAddress, AData, ASize, @numbytes) then
begin
// Failed to write, thus we try to change the protection
if AChangeProtect
and not(VirtualProtectEx(Handle, AAddress, ASize, PAGE_EXECUTE_READWRITE, @oldprot)) 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 JwaWinBase.WriteProcessMemory(Handle, AAddress, AData, ASize, @numbytes) then
begin
FLogManager.Log(
'WriteProcessMemory failed writing address - ' + AddrToHex(AAddress) +
' Error:' + I_LogManager.LastErrorInfo);
Result := -1;
exit;
end;
end;
end;
if (numbytes <> ASize) then
begin
FLogManager.Log(
'WriteProcessMemory failed to write address - ' + AddrToHex(AAddress) +
' Wrong number of bytes - ' + IntToStr(numbytes) +
' Error:' + I_LogManager.LastErrorInfo);
Result := -1;
exit;
end;
if changed
and AChangeProtect
and not VirtualProtectEx(Handle, AAddress, ASize, oldprot, @oldprot) 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, numbytes)) then
begin
FLogManager.Log(
'WriteProcessMemory: FlushInstructionCache failed for address - ' + AddrToHex(AAddress) +
' Error:' + I_LogManager.LastErrorInfo);
end;
Result := numbytes;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.