forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapFrames.js
More file actions
210 lines (180 loc) · 5.49 KB
/
mapFrames.js
File metadata and controls
210 lines (180 loc) · 5.49 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
/* 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/>. */
// @flow
import {
getFrames,
getSymbols,
getSource,
getSourceFromId,
getSelectedFrame
} from "../../selectors";
import assert from "../../utils/assert";
import { findClosestFunction } from "../../utils/ast";
import { setSymbols } from "../sources/symbols";
import type { Frame, OriginalFrame, ThreadContext } from "../../types";
import type { State } from "../../reducers/types";
import type { ThunkArgs } from "../types";
import SourceMaps, { isGeneratedId } from "devtools-source-map";
function isFrameBlackboxed(state, frame) {
const source = getSource(state, frame.location.sourceId);
return source && source.isBlackBoxed;
}
function getSelectedFrameId(state, thread, frames) {
let selectedFrame = getSelectedFrame(state, thread);
if (selectedFrame && !isFrameBlackboxed(state, selectedFrame)) {
return selectedFrame.id;
}
selectedFrame = frames.find(frame => !isFrameBlackboxed(state, frame));
return selectedFrame && selectedFrame.id;
}
export function updateFrameLocation(
frame: Frame,
sourceMaps: typeof SourceMaps
) {
if (frame.isOriginal) {
return Promise.resolve(frame);
}
return sourceMaps.getOriginalLocation(frame.location).then(loc => ({
...frame,
location: loc,
generatedLocation: frame.generatedLocation || frame.location
}));
}
function updateFrameLocations(
frames: Frame[],
sourceMaps: typeof SourceMaps
): Promise<Frame[]> {
if (!frames || frames.length == 0) {
return Promise.resolve(frames);
}
return Promise.all(
frames.map(frame => updateFrameLocation(frame, sourceMaps))
);
}
export function mapDisplayNames(
frames: Frame[],
getState: () => State
): Frame[] {
return frames.map(frame => {
if (frame.isOriginal) {
return frame;
}
const source = getSource(getState(), frame.location.sourceId);
if (!source) {
return frame;
}
const symbols = getSymbols(getState(), source);
if (!symbols || !symbols.functions) {
return frame;
}
const originalFunction = findClosestFunction(symbols, frame.location);
if (!originalFunction) {
return frame;
}
const originalDisplayName = originalFunction.name;
return { ...frame, originalDisplayName };
});
}
function isWasmOriginalSourceFrame(frame, getState: () => State): boolean {
if (isGeneratedId(frame.location.sourceId)) {
return false;
}
const generatedSource = getSource(
getState(),
frame.generatedLocation.sourceId
);
return Boolean(generatedSource && generatedSource.isWasm);
}
async function expandFrames(
frames: Frame[],
sourceMaps: typeof SourceMaps,
getState: () => State
): Promise<Frame[]> {
const result = [];
for (let i = 0; i < frames.length; ++i) {
const frame = frames[i];
if (frame.isOriginal || !isWasmOriginalSourceFrame(frame, getState)) {
result.push(frame);
continue;
}
const originalFrames: ?Array<
OriginalFrame
> = await sourceMaps.getOriginalStackFrames(frame.generatedLocation);
if (!originalFrames) {
result.push(frame);
continue;
}
assert(originalFrames.length > 0, "Expected at least one original frame");
// First entry has not specific location -- use one from original frame.
originalFrames[0] = {
...originalFrames[0],
location: frame.location
};
originalFrames.forEach((originalFrame, j) => {
if (!originalFrame.location || !originalFrame.thread) {
return;
}
// Keep outer most frame with true actor ID, and generate uniquie
// one for the nested frames.
const id = j == 0 ? frame.id : `${frame.id}-originalFrame${j}`;
result.push({
id,
thread: originalFrame.thread,
displayName: originalFrame.displayName,
location: originalFrame.location,
source: null,
scope: frame.scope,
this: frame.this,
isOriginal: true,
// More fields that will be added by the mapDisplayNames and
// updateFrameLocation.
generatedLocation: frame.generatedLocation,
originalDisplayName: originalFrame.displayName
});
});
}
return result;
}
async function updateFrameSymbols(cx, frames, { dispatch, getState }) {
await Promise.all(
frames.map(frame => {
const source = getSourceFromId(getState(), frame.location.sourceId);
return dispatch(setSymbols({ cx, source }));
})
);
}
/**
* Map call stack frame locations and display names to originals.
* e.g.
* 1. When the debuggee pauses
* 2. When a source is pretty printed
* 3. When symbols are loaded
* @memberof actions/pause
* @static
*/
export function mapFrames(cx: ThreadContext) {
return async function(thunkArgs: ThunkArgs) {
const { dispatch, getState, sourceMaps } = thunkArgs;
const frames = getFrames(getState(), cx.thread);
if (!frames) {
return;
}
let mappedFrames = await updateFrameLocations(frames, sourceMaps);
await updateFrameSymbols(cx, mappedFrames, thunkArgs);
mappedFrames = await expandFrames(mappedFrames, sourceMaps, getState);
mappedFrames = mapDisplayNames(mappedFrames, getState);
const selectedFrameId = getSelectedFrameId(
getState(),
cx.thread,
mappedFrames
);
dispatch({
type: "MAP_FRAMES",
cx,
thread: cx.thread,
frames: mappedFrames,
selectedFrameId
});
};
}