/
sourceTool.js
642 lines (519 loc) · 21.4 KB
/
sourceTool.js
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* See license.txt for terms of usage */
define([
"firebug/firebug",
"firebug/lib/trace",
"firebug/lib/object",
"firebug/lib/string",
"firebug/lib/url",
"firebug/lib/xpath",
"firebug/chrome/tool",
"firebug/console/errorStackTraceObserver",
"firebug/debugger/breakpoints/breakpointStore",
"firebug/debugger/breakpoints/breakpointTool",
"firebug/debugger/script/sourceFile",
"firebug/debugger/stack/stackFrame",
"firebug/debugger/debuggerLib",
"firebug/remoting/debuggerClient",
"arch/compilationunit",
],
function (Firebug, FBTrace, Obj, Str, Url, Xpath, Tool, ErrorStackTraceObserver, BreakpointStore,
BreakpointTool, SourceFile, StackFrame, DebuggerLib, DebuggerClient, CompilationUnit) {
"use strict";
// ********************************************************************************************* //
// Documentation
/**
* This module is responsible for handling events that indicate script creation and
* populate {@link TabContext} with proper object.
*
* The module should be also responsible for handling dynamically evaluated scripts,
* which is not fully supported by platform (JSD2, RDP).
*
* Related platform reports:
* Bug 911721 - Get type & originator for Debugger.Script object
* Bug 332176 - eval still uses call site line number as offset for eval'ed code in the year 2013
*
* Suggestions for the platform:
* 1) Missing script type (bug 911721)
* 2) Wrong URL for dynamic scripts
* 3) 'newScript' is not sent for dynamic scripts
*/
// ********************************************************************************************* //
// Constants
var TraceError = FBTrace.toError();
var Trace = FBTrace.to("DBG_SOURCETOOL");
var dynamicTypesMap = {
"eval": CompilationUnit.EVAL,
"Function": CompilationUnit.EVAL,
"eventHandler": CompilationUnit.BROWSER_GENERATED,
"scriptElement": CompilationUnit.EVAL,
"setTimeout": CompilationUnit.EVAL,
"setInterval": CompilationUnit.EVAL
};
// ********************************************************************************************* //
// Source Tool
function SourceTool(context)
{
this.context = context;
}
/**
* @object This tool object is responsible for logic related to sources. It requests sources
* from the server as well as transforms incoming packets into {@link SourceFile} instances that
* are stored inside the current {@link TabContext}. Any module can consequently use these sources.
* For example, the {@link ScriptPanel} is displaying it and the {@link ConsolePanel} displays source
* lines for logged errors.
*/
SourceTool.prototype = Obj.extend(new Tool(),
/** @lends SourceTool */
{
dispatchName: "SourceTool",
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Initialization
onAttach: function(reload)
{
Trace.sysout("sourceTool.attach; context ID: " + this.context.getId());
// Listen for 'newScript' events.
DebuggerClient.addListener(this);
// Get scripts from the server. Source as fetched on demand (e.g. when
// displayed in the Script panel).
this.updateScriptFiles();
// Hook local thread actor to get notification about dynamic scripts creation.
this.dynamicSourceCollector = new DynamicSourceCollector(this);
this.dynamicSourceCollector.attach();
// Listen for {@link BreakpointStore} events to create custom dynamic breakpoints.
// (i.e. breakpoints in dynamically created scripts).
BreakpointStore.addListener(this);
},
onDetach: function()
{
Trace.sysout("sourceTool.detach; context ID: " + this.context.getId());
// Clear all fetched source info. All script sources must be fetched
// from the back end after the thread actor is connected again.
this.context.clearSources();
DebuggerClient.removeListener(this);
this.dynamicSourceCollector.detach();
this.dynamicSourceCollector = null;
BreakpointStore.removeListener(this);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Implementation
updateScriptFiles: function()
{
Trace.sysout("sourceTool.updateScriptFiles; context id: " + this.context.getId());
var self = this;
this.context.activeThread.getSources(function(response)
{
// The tool is already destroyed so, bail out.
if (!self.attached)
return;
var sources = response.sources;
for (var i = 0; i < sources.length; i++)
self.addScript(sources[i]);
});
},
addScript: function(script)
{
// Ignore scripts generated from 'clientEvaluate' packets. These scripts are
// created e.g. as the user is evaluating expressions in the watch window.
if (DebuggerLib.isFrameLocationEval(script.url))
{
Trace.sysout("sourceTool.addScript; A script ignored " + script.type);
return;
}
if (!this.context.sourceFileMap)
{
TraceError.sysout("sourceTool.addScript; ERROR Source File Map is NULL", script);
return;
}
// xxxHonza: Ignore inner scripts for now
if (this.context.sourceFileMap[script.url])
{
Trace.sysout("sourceTool.addScript; A script ignored: " + script.url, script);
return;
}
// Create a source file and append it into the context. This is the only
// place where an instance of {@link SourceFile} is created.
var sourceFile = new SourceFile(this.context, script.actor, script.url,
script.isBlackBoxed);
this.context.addSourceFile(sourceFile);
// Notify listeners (e.g. the Script panel) to updated itself. It can happen
// that the Script panel has been empty until now and need to display a script.
this.dispatch("newSource", [sourceFile]);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// DebuggerClient Handlers
newSource: function(type, response)
{
Trace.sysout("sourceTool.newSource; context id: " + this.context.getId() +
", script url: " + response.source.url, response);
// Ignore scripts coming from different threads.
// This is because 'newSource' listener is registered in 'DebuggerClient' not
// in 'ThreadClient'.
if (this.context.activeThread.actor != response.from)
{
Trace.sysout("sourceTool.newSource; coming from different thread " +
response.source.url, response);
return;
}
this.addScript(response.source);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// BreakpointStore Event Listener
onAddBreakpoint: function(bp)
{
var sourceFile = this.context.getSourceFile(bp.href);
Trace.sysout("sourceTool.onAddBreakpoint; " + bp.href, sourceFile);
// The code creates dynamic breakpoints only in dynamic scripts.
if (!sourceFile || !sourceFile.dynamic)
return;
var sourceFile = this.context.getSourceFile(bp.href);
var scripts = sourceFile.scripts;
if (!scripts.length)
{
TraceError.sysout("sourceTool.onAddBreakpoint; ERROR no script object!");
return;
}
bp.params.dynamicHandler = new BreakpointHitHandler(this.context, bp);
// Set the breakpoint in all scripts associated with the same URL
// as the breakpoint.
for (var parentScript of scripts)
{
var childScripts = parentScript.getChildScripts();
var scripts = [parentScript];
scripts.push.apply(scripts, childScripts);
for (var script of scripts)
{
var offsets = script.getLineOffsets(bp.lineNo + parentScript.startLine);
if (offsets.length > 0)
{
// Clear first to avoid duplicities.
script.clearBreakpoint(bp.params.dynamicHandler);
script.setBreakpoint(offsets[0], bp.params.dynamicHandler);
}
}
}
},
onRemoveBreakpoint: function(bp)
{
var sourceFile = this.context.getSourceFile(bp.href);
Trace.sysout("sourceTool.onRemoveBreakpoint; " + bp.href, sourceFile);
if (!sourceFile || !sourceFile.dynamic)
return;
if (!bp.params.dynamicHandler)
{
TraceError.sysout("sourceTool.onRemoveBreakpoint; No hit handler!");
return;
}
var sourceFile = this.context.getSourceFile(bp.href);
var scripts = sourceFile.scripts;
if (!scripts.length)
{
TraceError.sysout("sourceTool.onRemoveBreakpoint; ERROR no script object!");
return;
}
for (var parentScript of scripts)
{
var childScripts = parentScript.getChildScripts();
var scripts = [parentScript];
scripts.push.apply(scripts, childScripts);
for (var script of scripts)
{
var offsets = script.getLineOffsets(bp.lineNo + parentScript.startLine);
if (offsets.length > 0)
script.clearBreakpoint(bp.params.dynamicHandler);
}
}
},
});
// ********************************************************************************************* //
// Dynamically Evaluated Scripts (mostly hacks, waiting for bug 911721)
function DynamicSourceCollector(sourceTool)
{
this.sourceTool = sourceTool;
this.context = sourceTool.context;
}
/**
* xxxHonza: workaround for missing RDP 'newSource' packets.
*
* This object uses backend Debugger instance |threadActor.dbg| to hook script creation
* (onNewScript callback). This way we can collect even all dynamically created scripts
* (which are currently not send over RDP) and populate the current {@link TabContext}
* with {@link SourceFile} instances that represent them.
*/
DynamicSourceCollector.prototype =
/** @lends DynamicSourceCollector */
{
attach: function()
{
var dbg = DebuggerLib.getThreadDebugger(this.context);
// Monkey patch the current debugger.
this.originalOnNewScript = dbg.onNewScript;
dbg.onNewScript = this.onNewScript.bind(this);
},
detach: function()
{
if (!this.originalOnNewScript)
return;
var dbg = DebuggerLib.getThreadDebugger(this.context);
if (dbg)
dbg.onNewScript = this.originalOnNewScript;
this.originalOnNewScript = null;
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
onNewScript: function(script)
{
var dbg = DebuggerLib.getThreadDebugger(this.context);
if (script.url == "debugger eval code")
return this.originalOnNewScript.apply(dbg, arguments);
var introType = script.source.introductionType;
var scriptType = dynamicTypesMap[introType];
if (scriptType)
{
this.addDynamicScript(script, scriptType);
}
else
{
Trace.sysout("sourceTool.onNewScript; (non dynamic) " + script.url + ", " +
introType, script);
Trace.sysout("sourceTool.onNewSource; (non dynamic) " + script.source.url + ", " +
introType, script.source);
}
// Don't forget to execute the original logic.
this.originalOnNewScript.apply(dbg, arguments);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
addDynamicScript: function(script, type)
{
// Dynamic scripts use unique URL that is composed from script's location
// such as line and column number.
var url = computeDynamicUrl(script);
// Tracing logs the script object itself and it can take a lot of memory
// in case of bigger dynamic web applications.
if (Trace.active)
{
var element = script.source.element;
if (element)
element = element.unsafeDereference();
var introType = script.source.introductionType;
Trace.sysout("sourceTool.addDynamicScript; " + script.url + ", " +
introType, script);
Trace.sysout("sourceTool.addDynamicSource; " + script.source.url + ", " +
introType, script.source);
}
// Get an existing instance of {@link SourceFile} by URL. We don't want to create
// a new instance for every dynamic script created from the same source. This is why
// dynamic script URLs should be uniquely generated according to the script location.
var sourceFile = this.context.getSourceFile(url);
if (!sourceFile)
{
// xxxHonza: there should be only one place where instance of SourceFile is created.
var sourceFile = new SourceFile(this.context, null, url, false);
// xxxHonza: duplicated from {@link SourceFile}
var source = script.source.text.replace(/\r\n/gm, "\n");
sourceFile.loaded = true;
sourceFile.inProgress = false;
sourceFile.lines = Str.splitLines(source);
sourceFile.contentType = "text/javascript";
sourceFile.startLine = script.startLine;
sourceFile.compilation_unit_type = type;
// xxxHonza: compilation_unit_type should be used
sourceFile.dynamic = true;
this.context.addSourceFile(sourceFile);
}
// Register new script object in the source file object, before "newSource" event.
// This way bp.params.dynamicHandler is set for dynamic breakpoints and filtered
// out during standard breakpoint initialization within:
// {@link BreakpointTool.newSource}.
this.registerScript(sourceFile, script);
// New source file created, so let the rest of the system to deal with it just
// like with any other (non dynamic) source file.
this.sourceTool.dispatch("newSource", [sourceFile]);
},
registerScript: function(sourceFile, script)
{
if (!sourceFile.scripts)
sourceFile.scripts = [];
var index = sourceFile.scripts.indexOf(script);
if (index != -1)
{
TraceError.sysout("sourceTool.addScript; ERROR Script already registered! " +
script.url);
return;
}
sourceFile.scripts.push(script);
// Initialize breakpoints for the new script.
var bps = BreakpointStore.getBreakpoints(sourceFile.href);
for (var bp of bps)
this.sourceTool.onAddBreakpoint(bp);
}
};
// ********************************************************************************************* //
// Breakpoint Hit Handler
// xxxHonza: what if we used the BreakpointActor object here on the client side?
// It could safe a lot of the code.
function BreakpointHitHandler(context, bp)
{
this.context = context;
this.bp = bp;
}
BreakpointHitHandler.prototype =
{
hit: function(frame)
{
if (this.bp && this.bp.condition)
{
// Copied from firebug/debugger/actors/breakpointActor
if (!DebuggerLib.evalBreakpointCondition(frame, this.bp))
return;
}
var threadActor = DebuggerLib.getThreadActor(this.context.browser);
var {url} = threadActor.synchronize(
threadActor.sources.getOriginalLocation({
url: this.bp.href,
line: this.bp.lineNo,
column: 0
})
);
if (threadActor.sources.isBlackBoxed(this.bp.href) || frame.onStep)
{
Trace.sysout("sourceTool.hit; can't pause");
return undefined;
}
Trace.sysout("sourceTool.hit; Dynamic breakpoint hit! " +
this.bp.href + ", " + this.bp.lineNo, frame);
// Send "pause" packet with a new "dynamic-breakpoint" type.
// The debugging will start as usual within {@link DebuggerTool#paused} method.
return threadActor._pauseAndRespond(frame, "dynamic-breakpoint");
}
}
// ********************************************************************************************* //
// StackFrame builder Decorator
var originalBuildStackFrame = StackFrame.buildStackFrame;
/**
* StackFrame build decorator fixes information related to dynamic scripts.
* 1) URL - dynamically evaluated scripts uses different URLs derived from the parent
* script URL.
*
* xxxHonza: This can be removed as soon as RDP sends proper URLs for dynamic scripts.
*/
function buildStackFrame(frame, context)
{
var stackFrame = originalBuildStackFrame(frame, context);
var threadActor = DebuggerLib.getThreadActor(context.browser);
if (threadActor.state != "paused")
TraceError.sysout("stackFrame.buildStackFrame; ERROR wrong thread actor state!");
//xxxHonza: rename: nativeFrame -> framePacket and jsdFrame -> nativeFrame
var frameActor = threadActor._framePool.get(frame.actor);
stackFrame.jsdFrame = frameActor.frame;
var sourceFile = getSourceFileByScript(context, frameActor.frame.script);
if (!sourceFile)
return stackFrame;
if (sourceFile)
{
// Use proper source file that corresponds to the current frame.
stackFrame.sourceFile = sourceFile;
// Use proper (dynamically generated) URL.
stackFrame.href = sourceFile.href;
}
Trace.sysout("sourceTool.buildStackFrame; New stack frame", stackFrame);
return stackFrame;
}
// Monkey patch the original function.
StackFrame.buildStackFrame = buildStackFrame;
// ********************************************************************************************* //
// ErrorStackTraceObserver
/**
* Monkey path the {@link ErrorStackTraceObserver} that is responsible for collecting
* error stack traces. We need to provide correct stacks (remap URLs) for errors too.
*/
var originalGetSourceFile = ErrorStackTraceObserver.getSourceFile;
ErrorStackTraceObserver.getSourceFile = function(context, script)
{
var introType = script.source.introductionType;
var scriptType = dynamicTypesMap[introType];
if (scriptType)
return getSourceFileByScript(context, script);
return originalGetSourceFile.apply(ErrorStackTraceObserver, arguments);
}
// ********************************************************************************************* //
// Script Helpers
// xxxHonza: optimize the source lookup (there can be a lot of scripts).
function getSourceFileByScript(context, script)
{
for (var url in context.sourceFileMap)
{
var source = context.sourceFileMap[url];
if (!source.scripts)
continue;
if (source.scripts.indexOf(script) != -1)
return source;
for (var parentScript of source.scripts)
{
var childScripts = parentScript.getChildScripts();
for (var childScript of childScripts)
{
if (childScript == script)
return source;
}
}
}
}
function computeDynamicUrl(script)
{
// If //# sourceURL is provided just use it. Use introduction URL as the
// base URL if sourceURL is relative.
// xxxHonza: displayURL for Functions is set asynchronously, why?.
var displayURL = script.source.displayURL;
if (displayURL)
{
if (Url.isAbsoluteUrl(displayURL))
return displayURL;
var introScript = script.source.introductionScript.url;
return Url.normalizeURL(introScript + "/" + displayURL);
}
// Compute unique URL from location information. We don't want to use any
// random numbers or counters since breakpoints derive URLs too and they
// should be persistent.
// xxxHonza: It might still happen that a lot of breakpoints could stay in
// the {@link BreakpointStore} using invalid location that changed during
// development. These dead breakpoints could slow down the BreakpointStore.
// Additional auto clean logic might be needed, something like:
// If an URL is not loaded within a week or two, remove all breakpoints
// associated with that URL.
var url = script.source.url;
var element = script.source.element;
if (element)
element = element.unsafeDereference();
var id = getElementId(script);
var type = script.source.introductionType;
switch (type)
{
case "eventHandler":
return url + id + " " + element.textContent;
case "scriptElement":
return url;
case "eval":
case "Function":
// xxxHonza: TODO These URLs are already unique, but will be removed (see Bug 977255)
return url;
}
return url;
}
function getElementId(script)
{
var element = script.source.element;
if (!element)
return "";
if (element)
element = element.unsafeDereference();
var attrName = script.source.elementAttributeName;
var id = element.getAttribute("id");
if (id)
return "/" + id + " " + attrName;
return Xpath.getElementTreeXPath(element) + " " + attrName;
}
// ********************************************************************************************* //
// Registration
Firebug.registerTool("source", SourceTool);
return SourceTool;
// ********************************************************************************************* //
});