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
|
/* 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/. */
"use strict";
// Bug 1827382, as this module can be used from the worker thread,
// the following JSM may be loaded by the worker loader until
// we have proper support for ESM from workers.
const {
startTracing,
stopTracing,
addTracingListener,
removeTracingListener,
} = require("resource://devtools/server/tracer/tracer.jsm");
const { Actor } = require("resource://devtools/shared/protocol.js");
const { tracerSpec } = require("resource://devtools/shared/specs/tracer.js");
const { throttle } = require("resource://devtools/shared/throttle.js");
const {
TYPES,
getResourceWatcher,
} = require("resource://devtools/server/actors/resources/index.js");
const LOG_METHODS = {
STDOUT: "stdout",
CONSOLE: "console",
};
const CONSOLE_ARGS_STYLES = [
"color: var(--theme-toolbarbutton-checked-hover-background)",
"padding-inline: 4px; margin-inline: 2px; background-color: var(--theme-toolbarbutton-checked-hover-background); color: var(--theme-toolbarbutton-checked-hover-color);",
"",
"color: var(--theme-highlight-blue); margin-inline: 2px;",
];
const CONSOLE_THROTTLING_DELAY = 250;
class TracerActor extends Actor {
constructor(conn, targetActor) {
super(conn, tracerSpec);
this.targetActor = targetActor;
this.sourcesManager = this.targetActor.sourcesManager;
// Flag used by CONSOLE_MESSAGE resources
this.isChromeContext = /conn\d+\.parentProcessTarget\d+/.test(
this.targetActor.actorID
);
this.throttledConsoleMessages = [];
this.throttleLogMessages = throttle(
this.flushConsoleMessages.bind(this),
CONSOLE_THROTTLING_DELAY
);
}
destroy() {
this.stopTracing();
}
getLogMethod() {
return this.logMethod;
}
startTracing(logMethod = LOG_METHODS.STDOUT) {
this.logMethod = logMethod;
this.tracingListener = {
onTracingFrame: this.onTracingFrame.bind(this),
onTracingInfiniteLoop: this.onTracingInfiniteLoop.bind(this),
};
addTracingListener(this.tracingListener);
startTracing({
global: this.targetActor.window || this.targetActor.workerGlobal,
});
}
stopTracing() {
if (!this.tracingListener) {
return;
}
stopTracing();
removeTracingListener(this.tracingListener);
this.logMethod = null;
this.tracingListener = null;
}
onTracingInfiniteLoop() {
if (this.logMethod == LOG_METHODS.STDOUT) {
return true;
}
const consoleMessageWatcher = getResourceWatcher(
this.targetActor,
TYPES.CONSOLE_MESSAGE
);
if (!consoleMessageWatcher) {
return true;
}
const message =
"Looks like an infinite recursion? We stopped the JavaScript tracer, but code may still be running!";
consoleMessageWatcher.emitMessages([
{
arguments: [message],
styles: [],
level: "logTrace",
chromeContext: this.isChromeContext,
timeStamp: ChromeUtils.dateNow(),
},
]);
return false;
}
/**
* Called by JavaScriptTracer class when a new JavaScript frame is executed.
*
* @param {Debugger.Frame} frame
* A descriptor object for the JavaScript frame.
* @param {Number} depth
* Represents the depth of the frame in the call stack.
* @param {String} formatedDisplayName
* A human readable name for the current frame.
* @param {String} prefix
* A string to be displayed as a prefix of any logged frame.
* @return {Boolean}
* Return true, if the JavaScriptTracer should log the frame to stdout.
*/
onTracingFrame({ frame, depth, formatedDisplayName, prefix }) {
const { script } = frame;
const { lineNumber, columnNumber } = script.getOffsetMetadata(frame.offset);
const url = script.source.url;
// Ignore blackboxed sources
if (this.sourcesManager.isBlackBoxed(url, lineNumber, columnNumber)) {
return false;
}
if (this.logMethod == LOG_METHODS.STDOUT) {
// By returning true, we let JavaScriptTracer class log the message to stdout.
return true;
}
const args = [
prefix + "—".repeat(depth + 1),
frame.implementation,
"⟶",
formatedDisplayName,
];
// Create a message object that fits Console Message Watcher expectations
this.throttledConsoleMessages.push({
filename: url,
lineNumber,
columnNumber,
arguments: args,
styles: CONSOLE_ARGS_STYLES,
level: "logTrace",
chromeContext: this.isChromeContext,
sourceId: script.source.id,
timeStamp: ChromeUtils.dateNow(),
});
this.throttleLogMessages();
return false;
}
/**
* This method is throttled and will notify all pending traces to be logged in the console
* via the console message watcher.
*/
flushConsoleMessages() {
const consoleMessageWatcher = getResourceWatcher(
this.targetActor,
TYPES.CONSOLE_MESSAGE
);
// Ignore the request if the frontend isn't listening to console messages for that target.
if (!consoleMessageWatcher) {
return;
}
const messages = this.throttledConsoleMessages;
this.throttledConsoleMessages = [];
consoleMessageWatcher.emitMessages(messages);
}
}
exports.TracerActor = TracerActor;
|