-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsDbgServer.pas
609 lines (508 loc) · 16.7 KB
/
jsDbgServer.pas
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
unit jsDbgServer;
interface
uses windows, classes, sysUtils, js15decl, SyncObjs, forms, dialogs, Generics.Collections, NamedPipesImpl;
Type
TJSDebuggerMode = (dmRun, dmStepInto, dmStepover, dmStepout, dmBreak);
TJSbreakPoints = TList<Integer>;
TJSDebugScript = class
fileName: string;
extent: integer;
start: integer;
end_: integer;
script: PJSObject;
breakPoints: TJSbreakPoints;
public
constructor Create;
destructor destroy; override;
end;
TJSBreakPoint = class
fileName: string;
line: integer;
end;
TJSDebugScripts = TObjectDictionary<PJSObject, TJSDebugScript>;
TJSDebugBreakpoints = TObjectList<TJSBreakPoint>;
TJSDebugServer = class
type
private
FPipeHandle: THandle;
FMode: TJSDebuggerMode;
FFrameStop: PJSStackFrame;
FlastStop: PJSObject;
FlastLine: Integer;
Fdepth: integer;
FCurDepth: integer;
FRuntime: PJSRuntime;
FContext: PJSContext;
FNamedPipe: TNamedPipeServer;
FScripts: TJSDebugScripts;
FBreakPoints: TJSDebugBreakpoints;
FTrapBreakpoint: Integer;
FCurrentScriptName: string;
protected
// JS callbacks
//called at every execution point
class function _JSTrapHandler(cx: PJSContext; Script: PJSObject; pc: pjsbytecode; rval: pjsval;
closure: pointer): JSTrapStatus; cdecl; static;
class function _JSBreakHandler(cx: PJSContext; Script: PJSObject; pc: pjsbytecode; rval: pjsval;
closure: pointer): JSTrapStatus; cdecl; static;
class procedure _JSNewScriptHook(cx: PJSContext; filename: PAnsiChar; // * URL of script
lineno: uintN; // * first line */
Script: PJSObject; fun: PJSFunction; callerdata: pointer); cdecl; static;
// called just before script destruction
class procedure _JSDestroyScriptHook(cx: PJSContext; Script: PJSObject; callerdata: pointer); cdecl; static;
class function _JSDebugErrorHook(cx: PJSContext; const _message: PAnsiChar; report: PJSErrorReport;
closure: pointer): JSBool; cdecl; static;
//gets called when a script starts to be executed
class function _JSInterpreterHook(cx: PJSContext; fp: PJSStackFrame; before: JSBool; ok: PJSBool;
closure: pointer): pointer; cdecl; static;
function Debug(cx: PJSContext; rval: pjsval; fpa: PJSStackFrame): JSTrapStatus;
function SetBreakPoint(script: PJSObject; lineno: uintN): integer; overload;
function ClearBreakPoint(script: PJSObject; lineno: uintN): boolean;
procedure ClearBreakPoints(script: PJSObject);
public
constructor Create(AContext: PJSContext);
destructor Destroy; override;
property Scripts: TJSDebugScripts read FScripts;
property BreakPoints: TJSDebugBreakpoints read FBreakPoints;
property TrapBreakpoint: Integer read FTrapBreakpoint write FTrapBreakpoint;
end;
implementation
uses jsintf;
function StrRestOf(const S: string; N: Integer): string;
begin
Result := Copy(S, N, (Length(S) - N + 1));
end;
function StrLeft(const S: string; Count: Integer): string;
begin
Result := Copy(S, 1, Count);
end;
function StrAfter(const SubStr, S: string): string;
var
P: Integer;
begin
P := Pos(SubStr, S); // StrFind is case-insensitive pos
if P <= 0 then
Result := '' // substr not found -> nothing after it
else
Result := StrRestOf(S, P + Length(SubStr));
end;
function StrBefore(const SubStr, S: string): string;
var
P: Integer;
begin
P := Pos(SubStr, S);
if P <= 0 then
Result := S
else
Result := StrLeft(S, P - 1);
end;
{ TJSDebugServer }
function TJSDebugServer.ClearBreakPoint(script: PJSObject; lineno: uintN): boolean;
var
pc: pjsbytecode;
t: JSTrapHandler;
p: pointer;
value: TJSDebugScript;
b: TJSBreakPoint;
begin
if lineNo = 0 then exit(false);
pc := JS_LineNumberToPC(FContext, Script, lineNo);
if pc = nil then
exit(false);
if FScripts.TryGetValue(script, value) then
begin
for b in FBreakPoints do
begin
if b.fileName = value.fileName then
begin
FBreakPoints.Remove(b);
break;
end;
end;
value.breakPoints.Remove(lineNo);
end;
JS_ClearTrap(FContext, Script, pc, t, p);
end;
procedure TJSDebugServer.ClearBreakPoints(script: PJSObject);
begin
JS_ClearScriptTraps(FContext, script);
end;
constructor TJSDebugServer.Create(AContext: PJSContext);
begin
// Result := TJSEngine(JS_GetRuntimePrivate(JS_GetRuntime(cx)));
FContext := AContext;
FRuntime := JS_GetRuntime(AContext);
JS_SetNewScriptHook(FRuntime, _JSNewScriptHook, self);
JS_SetDestroyScriptHook(FRuntime, _JSDestroyScriptHook, self);
JS_SetDebugErrorHook(FRuntime, _JSDebugErrorHook, self);
JS_SetExecuteHook(FRuntime, _JSInterpreterHook, self);
FMode := dmStepInto;
FFrameStop := nil;
Fdepth := 0;
FNamedPipe := TNamedPipeServer.Create('js_debug_pipe');
FNamedPipe.Open;
FScripts:= TJSDebugScripts.Create;
FBreakPoints:= TJSDebugBreakpoints.Create;
FTrapBreakpoint := -1;
end;
function TJSDebugServer.Debug(cx: PJSContext; rval: pjsval; fpa: PJSStackFrame): JSTrapStatus;
var
// Action: TJSDebuggerAction;
line, cmd: string;
script: PJSObject;
newLineNo, lineNo: integer;
pc: pjsbytecode;
scriptName, extra: string;
value: TJSDebugScript;
breakPoint: TJSBreakpoint;
m: TJSDebuggerMode;
eval: jsval;
fp, fi: PJSStackFrame;
function readLine: string;
begin
if FNamedPipe.Connected then
Result := FNamedPipe.Read(10);
while Result = '' do
begin
Application.ProcessMessages;
if FNamedPipe.Connected then
Result := FNamedPipe.Read(10)
else
sleep(10);
end;
end;
function getScript(fileName: string; lineNo: integer = 0): PJSObject;
var
value: TJSDebugScript;
top_script: PJSObject;
begin
top_script := nil;
fileName := stringReplace(fileName, '"', '', [rfReplaceAll]);
for value in FScripts.Values do
begin
// excluide top-level scripts
if sameText(value.fileName, fileName) then
begin
if lineNo >=0 then
begin
if (lineNo >= value.start) and (lineNo <= value.start + value.extent) then
begin
if value.start = 0 then top_script := value.script
else exit(value.script);
end;
end
else exit(value.script);
end;
end;
result := top_script;
end;
begin
// JS_GetFrameScript(cx, fp)
try
while True do
begin
{ TODO : Not sure about this code }
if Self.FTrapBreakpoint <> -1 then
begin
script := JS_GetFrameScript(cx, fpa);
scriptName := JS_GetScriptFilename(cx, script);
script := getScript(scriptName, Self.FTrapBreakpoint);
newLineNo := setBreakPoint(script, Self.FTrapBreakpoint);
line := 'run';
Self.FTrapBreakpoint := -1;
end
else line := readLine;
extra := strAfter(' ', line);
cmd := strBefore(' ', line);
fp := fpa;
if (fp = nil) then
fp := JS_FrameIterator(cx, fp);
if sameText(cmd, 'run') then
begin
JS_SetInterrupt(FRuntime, nil, nil);
FMode := dmRun;
exit(JSTRAP_CONTINUE);
end
else if sameText(cmd, 'stepover') then
begin
FFrameStop := fp;
FMode := dmStepover;
FDepth := FCurDepth;
exit(JSTRAP_CONTINUE);
end
else if sameText(cmd, 'stepinto') then
begin
JS_SetInterrupt(FRuntime, _JSTrapHandler, self);
FMode := dmStepInto;
exit(JSTRAP_CONTINUE);
end
else if sameText(cmd, 'setbreak') then
begin
lineNo:= strToIntDef(strAfter('@', extra), 0);
script := getScript(strBefore('@', extra), lineNo);
//script := JS_GetFrameScript(cx, fp);
if script <> nil then
begin
newLineNo := setBreakPoint(script, lineNo);
FNamedPipe.Write(format('OK,SETBREAK %s@%d@%d', [strBefore('@', extra), lineNo, newLineNo]));
end;
end
else if sameText(cmd, 'clearbreak') then
begin
lineNo:= strToIntDef(strAfter('@', extra), 0);
script := getScript(strBefore('@', extra), lineNo);
script := JS_GetFrameScript(cx, fp);
clearBreakPoint(script, strToIntDef(strAfter('@', extra), 0));
//FNamedPipe.Write('OK');
end
else if sameText(cmd, 'listbreak') then
begin
line := '';
extra := '';
for breakPoint in FBreakpoints do
begin
line := line + inttostr(breakpoint.line) + ',';
end;
FNamedPipe.Write('OK,LISTBREAK "'+extra+'"' + ' ' + line);
end
else if sameText(cmd, 'evaluate') then
begin
fi := nil;
fp := JS_FrameIterator(cx, fi);
(*if fp <> nil then
begin
JS_EvaluateInStackFrame(cx, fp,PAnsiChar(AnsiString(extra)), length(extra),0,0, @eval);
extra := JSStringToString(JS_ValueToString(cx, eval));
end;
*)
m := fmode;
FMode := dmRun;
if extra <> '' then
begin
if JS_EvaluateUCScript(cx, JS_GetGlobalObject(cx), PWideChar(extra), length(extra), '<debug eval>', 1, @eval) <> js_true then
FNamedPipe.Write(format('ERROR,EVALUATE "%s"', [extra] ))
else
FNamedPipe.Write(format('OK,EVALUATE "%s"="%s"', [extra,JSStringToString(cx, JS_ValueToString(cx, eval))] ));
{ if(fp <> nil) then
begin
if JS_EvaluateUCInStackFrame(cx, fp, PWideChar(extra), length(extra), '<debug eval>', 1, @eval) <> js_true then
FNamedPipe.Write(format('ERROR,EVALUATE "%s"', [extra] ))
else
FNamedPipe.Write(format('OK,EVALUATE "%s"="%s"', [extra,JSStringToString(JS_ValueToString(cx, eval))] ));
end
else begin
JS_EvaluateUCScript(cx, JS_GetGlobalObject(cx), PWideChar(extra), length(extra), '<debug eval>', 1, @eval);
FNamedPipe.Write(format('OK,EVALUATE "%s"="%s"', [extra,JSStringToString(JS_ValueToString(cx, eval))] ));
end;
} end;
fmode := m;
end
else if sameText(cmd, 'stop') then
begin
exit(JSTRAP_ERROR);
end;
end;
except
JS_SetInterrupt(FRuntime, nil, nil);
FMode := dmRun;
exit(JSTRAP_CONTINUE);
end;
end;
destructor TJSDebugServer.Destroy;
begin
FNamedPipe.Free;
FScripts.Free;
FBreakPoints.Free;
inherited;
end;
function TJSDebugServer.SetBreakPoint(script: PJSObject; lineno: uintN): integer;
var
pc: pjsbytecode;
value: TJSDebugScript;
b: TJSBreakPoint;
op: JSOp;
begin
Result := -1;
pc := JS_LineNumberToPC(FContext, Script, lineNo);
if pc = nil then
exit;
if JS_SetTrap(FContext, Script, pc, _JSBreakHandler, self) = JS_TRUE then
begin
Result := JS_PCToLineNumber(FContext, script, pc);
if FScripts.TryGetValue(script, value) then
begin
value.breakPoints.Add(Result);
b:= TJSBreakPoint.Create();
b.fileName := value.fileName;
b.line := Result;
FBreakPoints.Add(b);
end;
end;
end;
class function TJSDebugServer._JSBreakHandler(cx: PJSContext; Script: PJSObject; pc: pjsbytecode; rval: pjsval;
closure: pointer): JSTrapStatus;
var
scr: TJSDebugServer;
line: uintN;
begin
scr := closure;
line := JS_PCToLineNumber(cx, Script, pc);
scr.FMode := dmBreak;
// traphandler will be called at the beginning of the js_Execute loop
JS_SetInterrupt(scr.FRuntime, _JSTrapHandler, closure);
JS_SetExecuteHook(scr.FRuntime, _JSInterpreterHook, closure);
Result := JSTRAP_CONTINUE;
end;
class function TJSDebugServer._JSDebugErrorHook(cx: PJSContext; const _message: PAnsiChar; report: PJSErrorReport;
closure: pointer): JSBool;
var
scr: TJSDebugServer;
begin
scr := TJSDebugServer(closure);
if scr = NIL then
exit(JS_TRUE);
end;
class procedure TJSDebugServer._JSDestroyScriptHook(cx: PJSContext; Script: PJSObject; callerdata: pointer);
var
scr: TJSDebugServer;
begin
scr := TJSDebugServer(callerdata);
if scr = nil then
exit;
JS_ClearScriptTraps(cx, Script);
end;
class function TJSDebugServer._JSInterpreterHook(cx: PJSContext; fp: PJSStackFrame; before: JSBool; ok: PJSBool;
closure: pointer): pointer;
var
scr: TJSDebugServer;
s: PJSObject;
pc: pjsbytecode;
line: Integer;
scriptName: PAnsiChar;
begin
scr := TJSDebugServer(closure);
if scr.FMode = dmRun then
exit(nil);
if (scr.FFrameStop <> fp) and ((scr.FMode = dmStepout) or (scr.FMode = dmStepover)) then
exit(nil);
s := JS_GetFrameScript(cx, fp);
pc := JS_GetFramePC(cx, fp);
line := 0;
if (pc <> nil) then
line := JS_PCToLineNumber(cx, s, pc);
// opcode := JS_GetTrapOpcode(cx, s, pc);
scriptName := JS_GetScriptFilename(cx, s);
scr.FCurrentScriptName := scriptName;
scr.Fdepth := 0;
scr.FNamedPipe.Write(Format('STOP SCRIPT "%s"@%d@%d', [scriptName, line, nativeInt(cx)]));
scr.FlastStop := s;
scr.FlastLine := line;
if (scr.Debug(cx, nil, fp) = JSTRAP_ERROR) then
begin
// exit if possible
if (ok <> nil) then
ok^ := js_false;
exit(nil);
end;
if (before = JS_TRUE) then
begin
if (scr.FMode = dmStepover) or (scr.FMode = dmStepout) then
begin
JS_SetExecuteHook(scr.FRuntime, nil, nil);
JS_SetInterrupt(scr.FRuntime, nil, nil);
exit(closure); // call when done to restore debugger state.
end;
// running or stepping? no need to call again for this script.
exit(nil);
end
else
begin
scr.FMode := dmStepInto;
JS_SetExecuteHook(scr.FRuntime, _JSInterpreterHook, closure);
JS_SetInterrupt(scr.FRuntime, _JSTrapHandler, closure);
exit(nil);
end;
end;
class procedure TJSDebugServer._JSNewScriptHook(cx: PJSContext; filename: PAnsiChar; lineno: uintN; Script: PJSObject;
fun: PJSFunction; callerdata: pointer);
var
scr: TJSDebugServer;
extent: Cardinal;
s: TJSDebugScript;
begin
// showmessage('aa');
scr := TJSDebugServer(callerdata);
extent := JS_GetScriptLineExtent(cx, script);
if fileName <> '' then
begin
s:= TJSDebugScript.Create;
s.fileName := fileName;
s.extent := extent;
s.start := lineNo;
s.end_ := lineNo + extent;
s.script := script;
//s.start := JS_GetScriptBaseLineNumber(cx, script);
scr.FScripts.AddOrSetValue(script, s);
end;
end;
class function TJSDebugServer._JSTrapHandler(cx: PJSContext; Script: PJSObject; pc: pjsbytecode; rval: pjsval;
closure: pointer): JSTrapStatus;
var
str: PJSString;
//caller: PJSStackFrame;
scr: TJSDebugServer;
line: uintN;
scriptName: PAnsiChar;
fi, fp: PJSStackFrame;
begin
scr := closure;
if (script = nil) then exit(JSTRAP_CONTINUE);
if (scr.FMode = dmRun) then
exit(JSTRAP_CONTINUE);
scr.FCurDepth := 0;
fi := nil;
fp := JS_FrameIterator(cx, fi);
if (fp = nil) then
fp := JS_GetScriptedCaller(cx, nil);
while (fp <> nil) do
begin
if (JS_IsNativeFrame(cx, fp) = js_true) then
break;
inc(scr.FCurDepth);
fp := JS_GetScriptedCaller(cx, fp);
end;
line := JS_PCToLineNumber(cx, Script, pc);
if (((scr.FMode = dmStepInto) or (scr.FMode = dmStepover) or (scr.FMode = dmStepout)) and (Script = scr.FlastStop)
and (line = scr.FlastLine)) then
exit(JSTRAP_CONTINUE);
if (scr.FMode = dmStepOver) and (scr.FCurDepth > scr.Fdepth) then
begin
exit(JSTRAP_CONTINUE);
end;
scr.FlastStop := Script;
scr.FlastLine := line;
if (scr.FMode = dmBreak) or (scr.FMode = dmStepInto) then
begin
// scr.FlastLine := line;
end;
if scr.FNamedPipe.Connected then
begin
scriptName := JS_GetScriptFilename(cx, script);
if scr.FMode = dmBreak then
scr.FNamedPipe.Write(Format('STOP BREAK "%s"@%d@%d', [scriptName, line, nativeInt(cx)]))
else
scr.FNamedPipe.Write(Format('STOP STEP "%s"@%d@%d', [scriptName, line, nativeInt(cx)]));
end;
Result := scr.Debug(cx, rval, nil);
end;
{ TJSDebugScript }
constructor TJSDebugScript.Create;
begin
breakPoints:= TList<Integer>.Create;
end;
destructor TJSDebugScript.destroy;
begin
breakPoints.Free;
inherited;
end;
end.