v8-debugger.h 9.77 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_INSPECTOR_V8_DEBUGGER_H_
#define V8_INSPECTOR_V8_DEBUGGER_H_
7

8
#include <list>
9
#include <unordered_map>
10
#include <unordered_set>
11 12
#include <vector>

13
#include "src/base/macros.h"
14
#include "src/inspector/inspected-context.h"
15
#include "src/inspector/protocol/Debugger.h"
16 17
#include "src/inspector/protocol/Forward.h"
#include "src/inspector/protocol/Runtime.h"
18
#include "src/inspector/v8-debugger-script.h"
19
#include "src/inspector/wasm-translation.h"
20

21 22
#include "include/v8-inspector.h"

23 24
namespace v8_inspector {

25
class AsyncStackTrace;
26
class StackFrame;
27
class V8Debugger;
28 29 30
class V8DebuggerAgentImpl;
class V8InspectorImpl;
class V8StackTraceImpl;
31
struct V8StackTraceId;
32

33 34
enum class WrapMode { kForceValue, kNoPreview, kWithPreview };

35
using protocol::Response;
36 37
using TerminateExecutionCallback =
    protocol::Runtime::Backend::TerminateExecutionCallback;
38

39 40
class V8Debugger : public v8::debug::DebugDelegate,
                   public v8::debug::AsyncEventDelegate {
41 42
 public:
  V8Debugger(v8::Isolate*, V8InspectorImpl*);
43
  ~V8Debugger() override;
44 45

  bool enabled() const;
46
  v8::Isolate* isolate() const { return m_isolate; }
47

48
  void setBreakpointsActive(bool);
49

50 51
  v8::debug::ExceptionBreakState getPauseOnExceptionsState();
  void setPauseOnExceptionsState(v8::debug::ExceptionBreakState);
52
  bool canBreakProgram();
53
  void breakProgram(int targetContextGroupId);
54
  void interruptAndBreak(int targetContextGroupId);
55
  void continueProgram(int targetContextGroupId);
56
  void breakProgramOnAssert(int targetContextGroupId);
57

58
  void setPauseOnNextCall(bool, int targetContextGroupId);
59
  void stepIntoStatement(int targetContextGroupId, bool breakOnAsyncCall);
60 61
  void stepOverStatement(int targetContextGroupId);
  void stepOutOfFunction(int targetContextGroupId);
62 63
  void pauseOnAsyncCall(int targetContextGroupId, uintptr_t task,
                        const String16& debuggerId);
64

65 66
  void terminateExecution(std::unique_ptr<TerminateExecutionCallback> callback);

67
  Response continueToLocation(int targetContextGroupId,
68
                              V8DebuggerScript* script,
69 70
                              std::unique_ptr<protocol::Debugger::Location>,
                              const String16& targetCallFramess);
71

72 73 74 75 76 77 78 79 80
  // Each script inherits debug data from v8::Context where it has been
  // compiled.
  // Only scripts whose debug data matches |contextGroupId| will be reported.
  // Passing 0 will result in reporting all scripts.
  void getCompiledScripts(int contextGroupId,
                          std::vector<std::unique_ptr<V8DebuggerScript>>&);
  void enable();
  void disable();

81
  bool isPaused() const { return m_pausedContextGroupId; }
82
  bool isPausedInContextGroup(int contextGroupId) const;
83 84 85

  int maxAsyncCallChainDepth() { return m_maxAsyncCallStackDepth; }
  void setAsyncCallStackDepth(V8DebuggerAgentImpl*, int);
86 87

  std::shared_ptr<AsyncStackTrace> currentAsyncParent();
88
  V8StackTraceId currentExternalParent();
89

90 91
  std::shared_ptr<StackFrame> symbolize(v8::Local<v8::StackFrame> v8Frame);

92 93 94 95 96 97
  std::unique_ptr<V8StackTraceImpl> createStackTrace(v8::Local<v8::StackTrace>);
  std::unique_ptr<V8StackTraceImpl> captureStackTrace(bool fullStack);

  v8::MaybeLocal<v8::Array> internalProperties(v8::Local<v8::Context>,
                                               v8::Local<v8::Value>);

98
  v8::Local<v8::Array> queryObjects(v8::Local<v8::Context> context,
99
                                    v8::Local<v8::Object> prototype);
100

101 102 103 104 105 106 107
  void asyncTaskScheduled(const StringView& taskName, void* task,
                          bool recurring);
  void asyncTaskCanceled(void* task);
  void asyncTaskStarted(void* task);
  void asyncTaskFinished(void* task);
  void allAsyncTasksCanceled();

108 109 110 111
  V8StackTraceId storeCurrentStackTrace(const StringView& description);
  void externalAsyncTaskStarted(const V8StackTraceId& parent);
  void externalAsyncTaskFinished(const V8StackTraceId& parent);

112 113
  uintptr_t storeStackTrace(std::shared_ptr<AsyncStackTrace> stack);

114 115 116
  void muteScriptParsedEvents();
  void unmuteScriptParsedEvents();

117 118
  V8InspectorImpl* inspector() { return m_inspector; }

119 120
  WasmTranslation* wasmTranslation() { return &m_wasmTranslation; }

121
  void setMaxAsyncTaskStacksForTest(int limit);
122
  void dumpAsyncTaskStacksStateForTest();
123

124 125 126
  v8_inspector::V8StackTraceId scheduledAsyncCall() {
    return m_scheduledAsyncCall;
  }
127

128 129 130 131 132 133
  std::pair<int64_t, int64_t> debuggerIdFor(int contextGroupId);
  std::pair<int64_t, int64_t> debuggerIdFor(
      const String16& serializedDebuggerId);
  std::shared_ptr<AsyncStackTrace> stackTraceFor(int contextGroupId,
                                                 const V8StackTraceId& id);

134 135 136
  void reportTermination();

 private:
137 138 139 140
  bool addInternalObject(v8::Local<v8::Context> context,
                         v8::Local<v8::Object> object,
                         V8InternalValueType type);

141
  void clearContinueToLocation();
142
  bool shouldContinueToCurrentLocation();
143

144 145
  static size_t nearHeapLimitCallback(void* data, size_t current_heap_limit,
                                      size_t initial_heap_limit);
146
  static void terminateExecutionCompletedCallback(v8::Isolate* isolate);
147 148 149
  void handleProgramBreak(
      v8::Local<v8::Context> pausedContext, v8::Local<v8::Value> exception,
      const std::vector<v8::debug::BreakpointId>& hitBreakpoints,
150 151
      v8::debug::ExceptionType exception_type = v8::debug::kException,
      bool isUncaught = false);
152

153 154 155 156 157 158 159 160
  enum ScopeTargetKind {
    FUNCTION,
    GENERATOR,
  };
  v8::MaybeLocal<v8::Value> getTargetScopes(v8::Local<v8::Context>,
                                            v8::Local<v8::Value>,
                                            ScopeTargetKind);

161 162
  v8::MaybeLocal<v8::Value> functionScopes(v8::Local<v8::Context>,
                                           v8::Local<v8::Function>);
163 164
  v8::MaybeLocal<v8::Value> generatorScopes(v8::Local<v8::Context>,
                                            v8::Local<v8::Value>);
165 166
  v8::MaybeLocal<v8::Array> collectionsEntries(v8::Local<v8::Context> context,
                                               v8::Local<v8::Value> value);
167

168 169 170 171 172 173
  void asyncTaskScheduledForStack(const String16& taskName, void* task,
                                  bool recurring);
  void asyncTaskCanceledForStack(void* task);
  void asyncTaskStartedForStack(void* task);
  void asyncTaskFinishedForStack(void* task);

174
  void asyncTaskCandidateForStepping(void* task, bool isLocal);
175 176 177 178
  void asyncTaskStartedForStepping(void* task);
  void asyncTaskFinishedForStepping(void* task);
  void asyncTaskCanceledForStepping(void* task);

179
  // v8::debug::DebugEventListener implementation.
180 181
  void AsyncEventOccurred(v8::debug::DebugAsyncActionType type, int id,
                          bool isBlackboxed) override;
182
  void ScriptCompiled(v8::Local<v8::debug::Script> script, bool is_live_edited,
183
                      bool has_compile_error) override;
184
  void BreakProgramRequested(
185
      v8::Local<v8::Context> paused_context,
186
      const std::vector<v8::debug::BreakpointId>& break_points_hit) override;
187
  void ExceptionThrown(v8::Local<v8::Context> paused_context,
188
                       v8::Local<v8::Value> exception,
189 190
                       v8::Local<v8::Value> promise, bool is_uncaught,
                       v8::debug::ExceptionType exception_type) override;
191 192 193
  bool IsFunctionBlackboxed(v8::Local<v8::debug::Script> script,
                            const v8::debug::Location& start,
                            const v8::debug::Location& end) override;
194

195
  int currentContextGroupId();
196
  bool asyncStepOutOfFunction(int targetContextGroupId, bool onlyAtReturn);
197

198 199 200
  v8::Isolate* m_isolate;
  V8InspectorImpl* m_inspector;
  int m_enableCount;
201

202
  int m_breakpointsActiveCount = 0;
203
  int m_ignoreScriptParsedEventsCounter;
204
  size_t m_originalHeapLimit = 0;
205
  bool m_scheduledOOMBreak = false;
206
  bool m_scheduledAssertBreak = false;
207
  int m_targetContextGroupId = 0;
208
  int m_pausedContextGroupId = 0;
209
  int m_continueToLocationBreakpointId;
210 211
  String16 m_continueToLocationTargetCallFrames;
  std::unique_ptr<V8StackTraceImpl> m_continueToLocationStack;
212 213

  using AsyncTaskToStackTrace =
214
      std::unordered_map<void*, std::weak_ptr<AsyncStackTrace>>;
215
  AsyncTaskToStackTrace m_asyncTaskStacks;
216
  std::unordered_set<void*> m_recurringTasks;
217 218

  int m_maxAsyncCallStacks;
219
  int m_maxAsyncCallStackDepth;
220

221
  std::vector<void*> m_currentTasks;
222
  std::vector<std::shared_ptr<AsyncStackTrace>> m_currentAsyncParent;
223
  std::vector<V8StackTraceId> m_currentExternalParent;
224 225 226 227 228 229

  void collectOldAsyncStacksIfNeeded();
  int m_asyncStacksCount = 0;
  // V8Debugger owns all the async stacks, while most of the other references
  // are weak, which allows to collect some stacks when there are too many.
  std::list<std::shared_ptr<AsyncStackTrace>> m_allAsyncStacks;
230
  std::unordered_map<int, std::weak_ptr<StackFrame>> m_framesCache;
231

232
  std::unordered_map<V8DebuggerAgentImpl*, int> m_maxAsyncCallStackDepthMap;
233
  void* m_taskWithScheduledBreak = nullptr;
234
  String16 m_taskWithScheduledBreakDebuggerId;
235

236 237
  bool m_breakRequested = false;

238
  v8::debug::ExceptionBreakState m_pauseOnExceptionsState;
239
  bool m_pauseOnAsyncCall = false;
240
  v8_inspector::V8StackTraceId m_scheduledAsyncCall;
241

242
  using StackTraceIdToStackTrace =
243
      std::unordered_map<uintptr_t, std::weak_ptr<AsyncStackTrace>>;
244 245 246
  StackTraceIdToStackTrace m_storedStackTraces;
  uintptr_t m_lastStackTraceId = 0;

247
  std::unordered_map<int, std::pair<int64_t, int64_t>>
248
      m_contextGroupIdToDebuggerId;
249
  std::unordered_map<String16, std::pair<int64_t, int64_t>>
250 251
      m_serializedDebuggerIdToDebuggerId;

252 253
  std::unique_ptr<TerminateExecutionCallback> m_terminateExecutionCallback;

254 255
  WasmTranslation m_wasmTranslation;

256
  DISALLOW_COPY_AND_ASSIGN(V8Debugger);
257 258 259 260
};

}  // namespace v8_inspector

261
#endif  // V8_INSPECTOR_V8_DEBUGGER_H_