Menu

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

Download this file

242 lines (199 with data), 6.3 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
(***********************************************************************)
(* 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 BreakPoint;
interface
uses
Classes,
I_BreakPoint,
I_DebugThread,
I_DebugProcess,
I_DebugModule,
I_LogManager;
type
TBreakPoint = class(TInterfacedObject, IBreakPoint)
strict private
FOld_Opcode: Byte;
FActive: Boolean;
FAddress: Pointer;
FBreakCount: integer;
FProcess: IDebugProcess;
FModule: IDebugModule;
FDetailsCount: Integer;
FDetails: array of TBreakPointDetail;
FLogManager: ILogManager;
function DeActivate: Boolean;
public
constructor Create(const ADebugProcess: IDebugProcess;
const AAddress: Pointer;
const AModule: IDebugModule;
const ALogManager: ILogManager);
procedure Clear(const AThread: IDebugThread);
procedure AddDetails(const AModuleName: string;
const AUnitName: string;
const ALineNumber: Integer);
function DetailCount: Integer;
function DetailByIndex(const AIndex: Integer): TBreakPointDetail;
function IsActive: Boolean;
function BreakCount: integer;
procedure IncBreakCount;
function Activate: Boolean;
function Address: Pointer;
function Module: IDebugModule;
function GetCovered: Boolean;
property IsCovered: Boolean read GetCovered;
end;
implementation
uses
SysUtils,
Windows;
constructor TBreakPoint.Create(const ADebugProcess: IDebugProcess;
const AAddress: Pointer;
const AModule: IDebugModule;
const ALogManager: ILogManager);
begin
inherited Create;
FAddress := AAddress;
FProcess := ADebugProcess;
FActive := False;
FBreakCount := 0;
FModule := AModule;
FDetailsCount := 0;
SetLength(FDetails, 2);
FLogManager := ALogManager;
end;
function TBreakPoint.Activate: Boolean;
var
OpCode : Byte;
BytesRead: DWORD;
BytesWritten: DWORD;
DetailIndex: Integer;
begin
FLogManager.Log('TBreakPoint.Activate:');
Result := FActive;
if not Result then
begin
BytesRead := FProcess.ReadProcessMemory(FAddress, @FOld_Opcode, 1, true);
if BytesRead = 1 then
begin
OpCode := $CC;
BytesWritten := FProcess.WriteProcessMemory(FAddress, @OpCode, 1, true);
FlushInstructionCache(FProcess.Handle, nil, 0);
if BytesWritten = 1 then
begin
for DetailIndex := 0 to Pred(FDetailsCount) do
begin
FLogManager.Log(
'Activate ' + FDetails[DetailIndex].UnitName +
' line ' + IntToStr(FDetails[DetailIndex].Line) +
' BreakPoint at:' + IntToHex(Integer(FAddress), 8)
);
end;
FActive := True;
Result := True;
end;
end;
end;
end;
function TBreakPoint.DeActivate: Boolean;
var
BytesWritten: DWORD;
DetailIndex: Integer;
begin
Result := not FActive;
if not Result then
begin
BytesWritten := FProcess.writeProcessMemory(FAddress, @FOld_Opcode, 1,true);
FlushInstructionCache(FProcess.Handle,nil,0);
for DetailIndex := 0 to Pred(FDetailsCount) do
begin
FLogManager.Log(
'De-Activate ' + FDetails[DetailIndex].UnitName +
' line ' + IntToStr(FDetails[DetailIndex].Line) +
' BreakPoint at:' + IntToHex(Integer(FAddress), 8)
);
end;
Result := (BytesWritten = 1);
FActive := False;
end;
end;
function TBreakPoint.DetailByIndex(const AIndex: Integer): TBreakPointDetail;
begin
Result := FDetails[AIndex];
end;
function TBreakPoint.DetailCount: Integer;
begin
Result := FDetailsCount;
end;
procedure TBreakPoint.AddDetails(const AModuleName: string;
const AUnitName: string;
const ALineNumber: Integer);
begin
if (FDetailsCount = Length(FDetails)) then
begin
SetLength(FDetails, FDetailsCount + 5);
end;
FDetails[FDetailsCount].ModuleName := AModuleName;
FDetails[FDetailsCount].UnitName := AUnitName;
FDetails[FDetailsCount].Line := ALineNumber;
Inc(FDetailsCount);
end;
procedure TBreakPoint.Clear(const AThread: IDebugThread);
var
ContextRecord: CONTEXT;
Result: BOOL;
begin
FLogManager.Log('Clearing BreakPoint at ' + IntToHex(Integer(FAddress), 8));
ContextRecord.ContextFlags := CONTEXT_CONTROL;
Result := GetThreadContext(AThread.Handle, ContextRecord);
if Result then
begin
DeActivate;
{$IFDEF CPUX64}
Dec(ContextRecord.Rip);
{$ELSE}
Dec(ContextRecord.Eip);
{$ENDIF}
ContextRecord.ContextFlags := CONTEXT_CONTROL;
Result := SetThreadContext(AThread.Handle, ContextRecord);
if (not Result) then
begin
FLogManager.Log('Failed setting thread context:' + I_LogManager.LastErrorInfo);
end;
end
else
begin
FLogManager.Log('Failed to get thread context ' + I_LogManager.LastErrorInfo);
end;
end;
function TBreakPoint.IsActive: Boolean;
begin
Result := FActive;
end;
function TBreakPoint.Address: Pointer;
begin
Result := FAddress;
end;
function TBreakPoint.Module: IDebugModule;
begin
Result := FModule;
end;
function TBreakPoint.BreakCount: Integer;
begin
Result := FBreakCount;
end;
procedure TBreakPoint.IncBreakCount;
begin
Inc(FBreakCount);
end;
function TBreakPoint.GetCovered: Boolean;
begin
Result := FBreakCount > 0;
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.