summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/frame.js
blob: 42afdeb0e22eab0fb6d2122256e780772740f4af (plain)
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
/* 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";

const Debugger = require("Debugger");
const { assert } = require("resource://devtools/shared/DevToolsUtils.js");
const { Pool } = require("resource://devtools/shared/protocol/Pool.js");
const {
  createValueGrip,
} = require("resource://devtools/server/actors/object/utils.js");
const {
  ActorClassWithSpec,
  Actor,
} = require("resource://devtools/shared/protocol.js");
const { frameSpec } = require("resource://devtools/shared/specs/frame.js");

function formatDisplayName(frame) {
  if (frame.type === "call") {
    const callee = frame.callee;
    return callee.name || callee.userDisplayName || callee.displayName;
  }

  return `(${frame.type})`;
}

function isDeadSavedFrame(savedFrame) {
  return Cu && Cu.isDeadWrapper(savedFrame);
}
function isValidSavedFrame(threadActor, savedFrame) {
  return (
    !isDeadSavedFrame(savedFrame) &&
    // If the frame's source is unknown to the debugger, then we ignore it
    // since the frame likely does not belong to a realm that is marked
    // as a debuggee.
    // This check will also fail if the frame would have been known but was
    // GCed before the debugger was opened on the page.
    // TODO: Use SavedFrame's security principal to limit non-debuggee frames
    // and pass all unknown frames to the debugger as a URL with no sourceID.
    getSavedFrameSource(threadActor, savedFrame)
  );
}
function getSavedFrameSource(threadActor, savedFrame) {
  return threadActor.sourcesManager.getSourceActorByInternalSourceId(
    savedFrame.sourceId
  );
}
function getSavedFrameParent(threadActor, savedFrame) {
  if (isDeadSavedFrame(savedFrame)) {
    return null;
  }

  while (true) {
    savedFrame = savedFrame.parent || savedFrame.asyncParent;

    // If the saved frame is a dead wrapper, we don't have any way to keep
    // stepping through parent frames.
    if (!savedFrame || isDeadSavedFrame(savedFrame)) {
      savedFrame = null;
      break;
    }

    if (isValidSavedFrame(threadActor, savedFrame)) {
      break;
    }
  }
  return savedFrame;
}

/**
 * An actor for a specified stack frame.
 */
const FrameActor = ActorClassWithSpec(frameSpec, {
  /**
   * Creates the Frame actor.
   *
   * @param frame Debugger.Frame|SavedFrame
   *        The debuggee frame.
   * @param threadActor ThreadActor
   *        The parent thread actor for this frame.
   */
  initialize(frame, threadActor, depth) {
    Actor.prototype.initialize.call(this, threadActor.conn);

    this.frame = frame;
    this.threadActor = threadActor;
    this.depth = depth;
  },

  /**
   * A pool that contains frame-lifetime objects, like the environment.
   */
  _frameLifetimePool: null,
  get frameLifetimePool() {
    if (!this._frameLifetimePool) {
      this._frameLifetimePool = new Pool(this.conn, "frame");
    }
    return this._frameLifetimePool;
  },

  /**
   * Finalization handler that is called when the actor is being evicted from
   * the pool.
   */
  destroy() {
    if (this._frameLifetimePool) {
      this._frameLifetimePool.destroy();
      this._frameLifetimePool = null;
    }
    Actor.prototype.destroy.call(this);
  },

  getEnvironment() {
    try {
      if (!this.frame.environment) {
        return {};
      }
    } catch (e) {
      // |this.frame| might not be live. FIXME Bug 1477030 we shouldn't be
      // using frames we derived from a point where we are not currently
      // paused at.
      return {};
    }

    const envActor = this.threadActor.createEnvironmentActor(
      this.frame.environment,
      this.frameLifetimePool
    );

    return envActor.form();
  },

  /**
   * Returns a frame form for use in a protocol message.
   */
  form() {
    // SavedFrame actors have their own frame handling.
    if (!(this.frame instanceof Debugger.Frame)) {
      // The Frame actor shouldn't be used after evaluation is resumed, so
      // there shouldn't be an easy way for the saved frame to be referenced
      // once it has died.
      assert(!isDeadSavedFrame(this.frame));

      const obj = {
        actor: this.actorID,
        // TODO: Bug 1610418 - Consider updating SavedFrame to have a type.
        type: "dead",
        asyncCause: this.frame.asyncCause,
        state: "dead",
        displayName: this.frame.functionDisplayName,
        arguments: [],
        where: {
          // The frame's source should always be known because
          // getSavedFrameParent will skip over frames with unknown sources.
          actor: getSavedFrameSource(this.threadActor, this.frame).actorID,
          line: this.frame.line,
          // SavedFrame objects have a 1-based column number, but this API and
          // Debugger API objects use a 0-based column value.
          column: this.frame.column - 1,
        },
        oldest: !getSavedFrameParent(this.threadActor, this.frame),
      };

      return obj;
    }

    const threadActor = this.threadActor;
    const form = {
      actor: this.actorID,
      type: this.frame.type,
      asyncCause: this.frame.onStack ? null : "await",
      state: this.frame.onStack ? "on-stack" : "suspended",
    };

    if (this.depth) {
      form.depth = this.depth;
    }

    if (this.frame.type != "wasmcall") {
      form.this = createValueGrip(
        this.frame.this,
        threadActor._pausePool,
        threadActor.objectGrip
      );
    }

    form.displayName = formatDisplayName(this.frame);
    form.arguments = this._args();

    if (this.frame.script) {
      const location = this.threadActor.sourcesManager.getFrameLocation(
        this.frame
      );
      form.where = {
        actor: location.sourceActor.actorID,
        line: location.line,
        column: location.column,
      };
    }

    if (!this.frame.older) {
      form.oldest = true;
    }

    return form;
  },

  _args() {
    if (!this.frame.onStack || !this.frame.arguments) {
      return [];
    }

    return this.frame.arguments.map(arg =>
      createValueGrip(
        arg,
        this.threadActor._pausePool,
        this.threadActor.objectGrip
      )
    );
  },
});

exports.FrameActor = FrameActor;
exports.formatDisplayName = formatDisplayName;
exports.getSavedFrameParent = getSavedFrameParent;
exports.isValidSavedFrame = isValidSavedFrame;