Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGDK.ToolsAPI.Debugger.pas
More file actions
Latest commit
297 lines (245 loc) · 8.7 KB
/
Copy pathGDK.ToolsAPI.Debugger.pas
File metadata and controls
297 lines (245 loc) · 8.7 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
unit GDK.ToolsAPI.Debugger;
interface
uses
ToolsAPI,
GDK.ToolsAPI.Debugger.Interfaces;
type
TToolsApiDebugger = class(TInterfacedObject, IToolsApiDebugger)
private
const EvaluateBufferSize = 8192;
const DeferredPollCount = 200;
type TEvaluateBuffer = array[0..EvaluateBufferSize - 1] of Char;
functionDebuggerServices: IOTADebuggerServices;
functionTryDebuggerServices(out Services: IOTADebuggerServices): Boolean;
functionCurrentProcess: IOTAProcess;
functionCurrentThread: IOTAThread;
functionFindSourceBreakpoint(const FileName: string; const LineNumber: Integer): IOTASourceBreakpoint;
functionMapState(const State: TOTAProcessState): TToolsApiProcessState;
procedureRunProcess(const Mode: TOTARunMode);
functionEvaluateOnce(const Thread: IOTAThread; const Expression: string; out Value: string): TOTAEvaluateResult;
public
procedureAddBreakpoint(const FileName: string; const LineNumber: Integer);
functionRemoveBreakpoint(const FileName: string; const LineNumber: Integer): Boolean;
functionRemoveAllBreakpoints: Integer;
functionListBreakpoints: TArray<TToolsApiBreakpointInfo>;
functionState: TToolsApiProcessState;
functionHasProcess: Boolean;
procedureContinue;
procedureStep(const Mode: TToolsApiStepMode);
procedurePause;
procedureTerminate;
functionEvaluate(const Expression: string): string;
functionCallStack: TArray<TToolsApiStackFrame>;
functionTryGetCurrentLocation(out FileName: string; out LineNumber: Integer): Boolean;
procedureProcessDebugEvents;
end;
implementation
uses
System.SysUtils;
functionTToolsApiDebugger.TryDebuggerServices(out Services: IOTADebuggerServices): Boolean;
begin
Result := Assigned(BorlandIDEServices) and
Supports(BorlandIDEServices, IOTADebuggerServices, Services);
end;
functionTToolsApiDebugger.DebuggerServices: IOTADebuggerServices;
begin
ifnot TryDebuggerServices(Result) then
raise EToolsApiNoDebuggerServices.Create('Debugger services unavailable');
end;
functionTToolsApiDebugger.CurrentProcess: IOTAProcess;
begin
Result := DebuggerServices.CurrentProcess;
end;
functionTToolsApiDebugger.CurrentThread: IOTAThread;
begin
const Process = CurrentProcess;
if Assigned(Process) then
Result := Process.CurrentThread
else
Result := nil;
end;
functionTToolsApiDebugger.FindSourceBreakpoint(const FileName: string; const LineNumber: Integer): IOTASourceBreakpoint;
begin
Result := nil;
const Services = DebuggerServices;
forvar Index := 0to Services.SourceBkptCount - 1do
begin
const Breakpoint = Services.SourceBkpts[Index];
const Matches = SameFileName(Breakpoint.FileName, FileName) and (Breakpoint.LineNumber = LineNumber);
if Matches then
Exit(Breakpoint);
end;
end;
procedureTToolsApiDebugger.AddBreakpoint(const FileName: string; const LineNumber: Integer);
begin
const Existing = FindSourceBreakpoint(FileName, LineNumber);
if Assigned(Existing) then
Exit;
DebuggerServices.NewSourceBreakpoint(FileName, LineNumber, nil);
end;
functionTToolsApiDebugger.RemoveBreakpoint(const FileName: string; const LineNumber: Integer): Boolean;
begin
const Existing = FindSourceBreakpoint(FileName, LineNumber);
Result := Assigned(Existing);
if Result then
DebuggerServices.RemoveBreakpoint(Existing);
end;
functionTToolsApiDebugger.RemoveAllBreakpoints: Integer;
begin
const Services = DebuggerServices;
// Removing shifts the breakpoint list, so iterate backwards.
Result := Services.SourceBkptCount;
forvar Index := Services.SourceBkptCount - 1downto0do
begin
Services.RemoveBreakpoint(Services.SourceBkpts[Index]);
end;
end;
functionTToolsApiDebugger.ListBreakpoints: TArray<TToolsApiBreakpointInfo>;
begin
const Services = DebuggerServices;
SetLength(Result, Services.SourceBkptCount);
forvar Index := 0to Services.SourceBkptCount - 1do
begin
const Breakpoint = Services.SourceBkpts[Index];
Result[Index].FileName := Breakpoint.FileName;
Result[Index].LineNumber := Breakpoint.LineNumber;
Result[Index].Enabled := Breakpoint.Enabled;
Result[Index].Expression := Breakpoint.Expression;
Result[Index].Valid := Breakpoint.ValidInCurrentProcess;
end;
end;
functionTToolsApiDebugger.MapState(const State: TOTAProcessState): TToolsApiProcessState;
begin
case State of
psNothing: Result := TToolsApiProcessState.Nothing;
psRunning: Result := TToolsApiProcessState.Running;
psStopping: Result := TToolsApiProcessState.Stopping;
psStopped: Result := TToolsApiProcessState.Stopped;
psFault: Result := TToolsApiProcessState.Fault;
psResFault: Result := TToolsApiProcessState.ResFault;
psTerminated: Result := TToolsApiProcessState.Terminated;
psException: Result := TToolsApiProcessState.Exception;
else
Result := TToolsApiProcessState.NoProcess;
end;
end;
functionTToolsApiDebugger.State: TToolsApiProcessState;
begin
var Services: IOTADebuggerServices;
ifnot TryDebuggerServices(Services) then
Exit(TToolsApiProcessState.NoProcess);
const Process = Services.CurrentProcess;
ifnot Assigned(Process) then
Exit(TToolsApiProcessState.NoProcess);
Result := MapState(Process.ProcessState);
end;
functionTToolsApiDebugger.HasProcess: Boolean;
begin
var Services: IOTADebuggerServices;
Result := TryDebuggerServices(Services) and Assigned(Services.CurrentProcess);
end;
procedureTToolsApiDebugger.RunProcess(const Mode: TOTARunMode);
begin
const Process = CurrentProcess;
ifnot Assigned(Process) then
Exit;
Process.Run(Mode);
end;
procedureTToolsApiDebugger.Continue;
begin
RunProcess(ormRun);
end;
procedureTToolsApiDebugger.Step(const Mode: TToolsApiStepMode);
begin
case Mode of
TToolsApiStepMode.StepInto: RunProcess(ormStmtStepInto);
TToolsApiStepMode.RunUntilReturn: RunProcess(ormRunUntilReturn);
else
RunProcess(ormStmtStepOver);
end;
end;
procedureTToolsApiDebugger.Pause;
begin
const Process = CurrentProcess;
if Assigned(Process) then
Process.Pause;
end;
procedureTToolsApiDebugger.Terminate;
begin
const Process = CurrentProcess;
if Assigned(Process) then
Process.Terminate;
end;
functionTToolsApiDebugger.Evaluate(const Expression: string): string;
begin
const Thread = CurrentThread;
ifnot Assigned(Thread) then
raise EToolsApiEvaluateFailed.Create('No stopped process to evaluate in');
varValue := '';
var EvalResult := EvaluateOnce(Thread, Expression, Value);
// Deferred means the evaluator has to call a function inside the debuggee;
// pump debug events until the result arrives or the attempt is abandoned.
var Polls := 0;
while (EvalResult = erDeferred) and (Polls < DeferredPollCount) do
begin
DebuggerServices.ProcessDebugEvents;
Inc(Polls);
EvalResult := EvaluateOnce(Thread, Expression, Value);
end;
case EvalResult of
erOK: Result := Value;
erBusy: raise EToolsApiEvaluateFailed.Create('Evaluator is busy, try again');
erDeferred: raise EToolsApiEvaluateFailed.Create('Evaluation did not complete in time');
else
// On erError the evaluator message is what came back in the buffer.
raise EToolsApiEvaluateFailed.Create(Value);
end;
end;
functionTToolsApiDebugger.EvaluateOnce(const Thread: IOTAThread; const Expression: string; out Value: string): TOTAEvaluateResult;
begin
var Buffer: TEvaluateBuffer;
FillChar(Buffer, SizeOf(Buffer), 0);
var CanModify := False;
var ResultAddr: LongWord := 0;
var ResultSize: LongWord := 0;
var ResultVal: LongWord := 0;
Result := Thread.Evaluate(Expression, @Buffer[0], EvaluateBufferSize,
CanModify, True, nil, ResultAddr, ResultSize, ResultVal);
Value := Buffer;
end;
functionTToolsApiDebugger.CallStack: TArray<TToolsApiStackFrame>;
begin
Result := nil;
const Thread = CurrentThread;
ifnot Assigned(Thread) then
Exit;
// GetCallCount must precede GetCallHeader/GetCallPos; frames are 1-based.
const Count = Thread.GetCallCount;
SetLength(Result, Count);
forvar Index := 1to Count do
begin
var FrameFile := '';
var FrameLine := 0;
Thread.GetCallPos(Index, FrameFile, FrameLine);
Result[Index - 1].Index := Index;
Result[Index - 1].Header := Thread.GetCallHeader(Index);
Result[Index - 1].FileName := FrameFile;
Result[Index - 1].LineNumber := FrameLine;
end;
end;
functionTToolsApiDebugger.TryGetCurrentLocation(out FileName: string; out LineNumber: Integer): Boolean;
begin
FileName := '';
LineNumber := 0;
const Thread = CurrentThread;
ifnot Assigned(Thread) then
Exit(False);
FileName := Thread.GetCurrentFile;
LineNumber := Integer(Thread.GetCurrentLine);
Result := (FileName <> '');
end;
procedureTToolsApiDebugger.ProcessDebugEvents;
begin
DebuggerServices.ProcessDebugEvents;
end;
end.