V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
v8-debugger.h
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_INSPECTOR_V8_DEBUGGER_H_
6 #define V8_INSPECTOR_V8_DEBUGGER_H_
7 
8 #include <list>
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <vector>
12 
13 #include "src/base/macros.h"
14 #include "src/inspector/inspected-context.h"
15 #include "src/inspector/protocol/Debugger.h"
16 #include "src/inspector/protocol/Forward.h"
17 #include "src/inspector/protocol/Runtime.h"
18 #include "src/inspector/v8-debugger-script.h"
19 #include "src/inspector/wasm-translation.h"
20 
21 #include "include/v8-inspector.h"
22 
23 namespace v8_inspector {
24 
25 class AsyncStackTrace;
26 class StackFrame;
27 class V8Debugger;
28 class V8DebuggerAgentImpl;
29 class V8InspectorImpl;
30 class V8StackTraceImpl;
31 struct V8StackTraceId;
32 
33 enum class WrapMode { kForceValue, kNoPreview, kWithPreview };
34 
35 using protocol::Response;
36 using ScheduleStepIntoAsyncCallback =
37  protocol::Debugger::Backend::ScheduleStepIntoAsyncCallback;
38 using TerminateExecutionCallback =
39  protocol::Runtime::Backend::TerminateExecutionCallback;
40 
43  public:
44  V8Debugger(v8::Isolate*, V8InspectorImpl*);
45  ~V8Debugger() override;
46 
47  bool enabled() const;
48  v8::Isolate* isolate() const { return m_isolate; }
49 
50  void setBreakpointsActive(bool);
51 
52  v8::debug::ExceptionBreakState getPauseOnExceptionsState();
53  void setPauseOnExceptionsState(v8::debug::ExceptionBreakState);
54  bool canBreakProgram();
55  void breakProgram(int targetContextGroupId);
56  void interruptAndBreak(int targetContextGroupId);
57  void continueProgram(int targetContextGroupId);
58  void breakProgramOnAssert(int targetContextGroupId);
59 
60  void setPauseOnNextCall(bool, int targetContextGroupId);
61  void stepIntoStatement(int targetContextGroupId, bool breakOnAsyncCall);
62  void stepOverStatement(int targetContextGroupId);
63  void stepOutOfFunction(int targetContextGroupId);
64  void scheduleStepIntoAsync(
65  std::unique_ptr<ScheduleStepIntoAsyncCallback> callback,
66  int targetContextGroupId);
67  void pauseOnAsyncCall(int targetContextGroupId, uintptr_t task,
68  const String16& debuggerId);
69 
70  void terminateExecution(std::unique_ptr<TerminateExecutionCallback> callback);
71 
72  Response continueToLocation(int targetContextGroupId,
73  V8DebuggerScript* script,
74  std::unique_ptr<protocol::Debugger::Location>,
75  const String16& targetCallFramess);
76 
77  // Each script inherits debug data from v8::Context where it has been
78  // compiled.
79  // Only scripts whose debug data matches |contextGroupId| will be reported.
80  // Passing 0 will result in reporting all scripts.
81  void getCompiledScripts(int contextGroupId,
82  std::vector<std::unique_ptr<V8DebuggerScript>>&);
83  void enable();
84  void disable();
85 
86  bool isPaused() const { return m_pausedContextGroupId; }
87  bool isPausedInContextGroup(int contextGroupId) const;
88 
89  int maxAsyncCallChainDepth() { return m_maxAsyncCallStackDepth; }
90  void setAsyncCallStackDepth(V8DebuggerAgentImpl*, int);
91 
92  std::shared_ptr<AsyncStackTrace> currentAsyncParent();
93  V8StackTraceId currentExternalParent();
94 
95  std::shared_ptr<StackFrame> symbolize(v8::Local<v8::StackFrame> v8Frame);
96 
97  std::unique_ptr<V8StackTraceImpl> createStackTrace(v8::Local<v8::StackTrace>);
98  std::unique_ptr<V8StackTraceImpl> captureStackTrace(bool fullStack);
99 
102 
103  v8::Local<v8::Array> queryObjects(v8::Local<v8::Context> context,
104  v8::Local<v8::Object> prototype);
105 
106  void asyncTaskScheduled(const StringView& taskName, void* task,
107  bool recurring);
108  void asyncTaskCanceled(void* task);
109  void asyncTaskStarted(void* task);
110  void asyncTaskFinished(void* task);
111  void allAsyncTasksCanceled();
112 
113  V8StackTraceId storeCurrentStackTrace(const StringView& description);
114  void externalAsyncTaskStarted(const V8StackTraceId& parent);
115  void externalAsyncTaskFinished(const V8StackTraceId& parent);
116 
117  uintptr_t storeStackTrace(std::shared_ptr<AsyncStackTrace> stack);
118 
119  void muteScriptParsedEvents();
120  void unmuteScriptParsedEvents();
121 
122  V8InspectorImpl* inspector() { return m_inspector; }
123 
124  WasmTranslation* wasmTranslation() { return &m_wasmTranslation; }
125 
126  void setMaxAsyncTaskStacksForTest(int limit);
127  void dumpAsyncTaskStacksStateForTest();
128 
129  v8_inspector::V8StackTraceId scheduledAsyncCall() {
130  return m_scheduledAsyncCall;
131  }
132 
133  std::pair<int64_t, int64_t> debuggerIdFor(int contextGroupId);
134  std::pair<int64_t, int64_t> debuggerIdFor(
135  const String16& serializedDebuggerId);
136  std::shared_ptr<AsyncStackTrace> stackTraceFor(int contextGroupId,
137  const V8StackTraceId& id);
138 
139  private:
140  bool addInternalObject(v8::Local<v8::Context> context,
141  v8::Local<v8::Object> object,
142  V8InternalValueType type);
143 
144  void clearContinueToLocation();
145  bool shouldContinueToCurrentLocation();
146 
147  static size_t nearHeapLimitCallback(void* data, size_t current_heap_limit,
148  size_t initial_heap_limit);
149  static void terminateExecutionCompletedCallback(v8::Isolate* isolate);
150 
151  void handleProgramBreak(
152  v8::Local<v8::Context> pausedContext, v8::Local<v8::Value> exception,
153  const std::vector<v8::debug::BreakpointId>& hitBreakpoints,
154  v8::debug::ExceptionType exception_type = v8::debug::kException,
155  bool isUncaught = false);
156 
157  enum ScopeTargetKind {
158  FUNCTION,
159  GENERATOR,
160  };
163  ScopeTargetKind);
164 
169  v8::MaybeLocal<v8::Array> collectionsEntries(v8::Local<v8::Context> context,
170  v8::Local<v8::Value> value);
171 
172  void asyncTaskScheduledForStack(const String16& taskName, void* task,
173  bool recurring);
174  void asyncTaskCanceledForStack(void* task);
175  void asyncTaskStartedForStack(void* task);
176  void asyncTaskFinishedForStack(void* task);
177 
178  void asyncTaskCandidateForStepping(void* task, bool isLocal);
179  void asyncTaskStartedForStepping(void* task);
180  void asyncTaskFinishedForStepping(void* task);
181  void asyncTaskCanceledForStepping(void* task);
182 
183  // v8::debug::DebugEventListener implementation.
184  void AsyncEventOccurred(v8::debug::DebugAsyncActionType type, int id,
185  bool isBlackboxed) override;
186  void ScriptCompiled(v8::Local<v8::debug::Script> script, bool is_live_edited,
187  bool has_compile_error) override;
188  void BreakProgramRequested(
189  v8::Local<v8::Context> paused_context,
190  const std::vector<v8::debug::BreakpointId>& break_points_hit) override;
191  void ExceptionThrown(v8::Local<v8::Context> paused_context,
192  v8::Local<v8::Value> exception,
193  v8::Local<v8::Value> promise, bool is_uncaught,
194  v8::debug::ExceptionType exception_type) override;
195  bool IsFunctionBlackboxed(v8::Local<v8::debug::Script> script,
196  const v8::debug::Location& start,
197  const v8::debug::Location& end) override;
198 
199  int currentContextGroupId();
200  bool asyncStepOutOfFunction(int targetContextGroupId, bool onlyAtReturn);
201 
202  v8::Isolate* m_isolate;
203  V8InspectorImpl* m_inspector;
204  int m_enableCount;
205  int m_breakpointsActiveCount = 0;
206  int m_ignoreScriptParsedEventsCounter;
207  size_t m_originalHeapLimit = 0;
208  bool m_scheduledOOMBreak = false;
209  bool m_scheduledAssertBreak = false;
210  int m_targetContextGroupId = 0;
211  int m_pausedContextGroupId = 0;
212  int m_continueToLocationBreakpointId;
213  String16 m_continueToLocationTargetCallFrames;
214  std::unique_ptr<V8StackTraceImpl> m_continueToLocationStack;
215 
216  using AsyncTaskToStackTrace =
217  std::unordered_map<void*, std::weak_ptr<AsyncStackTrace>>;
218  AsyncTaskToStackTrace m_asyncTaskStacks;
219  std::unordered_set<void*> m_recurringTasks;
220 
221  int m_maxAsyncCallStacks;
222  int m_maxAsyncCallStackDepth;
223 
224  std::vector<void*> m_currentTasks;
225  std::vector<std::shared_ptr<AsyncStackTrace>> m_currentAsyncParent;
226  std::vector<V8StackTraceId> m_currentExternalParent;
227 
228  void collectOldAsyncStacksIfNeeded();
229  int m_asyncStacksCount = 0;
230  // V8Debugger owns all the async stacks, while most of the other references
231  // are weak, which allows to collect some stacks when there are too many.
232  std::list<std::shared_ptr<AsyncStackTrace>> m_allAsyncStacks;
233  std::unordered_map<int, std::weak_ptr<StackFrame>> m_framesCache;
234 
235  std::unordered_map<V8DebuggerAgentImpl*, int> m_maxAsyncCallStackDepthMap;
236  void* m_taskWithScheduledBreak = nullptr;
237  String16 m_taskWithScheduledBreakDebuggerId;
238 
239  std::unique_ptr<ScheduleStepIntoAsyncCallback> m_stepIntoAsyncCallback;
240  bool m_breakRequested = false;
241 
242  v8::debug::ExceptionBreakState m_pauseOnExceptionsState;
243  bool m_pauseOnAsyncCall = false;
244  v8_inspector::V8StackTraceId m_scheduledAsyncCall;
245 
246  using StackTraceIdToStackTrace =
247  std::unordered_map<uintptr_t, std::weak_ptr<AsyncStackTrace>>;
248  StackTraceIdToStackTrace m_storedStackTraces;
249  uintptr_t m_lastStackTraceId = 0;
250 
251  std::unordered_map<int, std::pair<int64_t, int64_t>>
252  m_contextGroupIdToDebuggerId;
253  std::unordered_map<String16, std::pair<int64_t, int64_t>>
254  m_serializedDebuggerIdToDebuggerId;
255 
256  std::unique_ptr<TerminateExecutionCallback> m_terminateExecutionCallback;
257 
258  WasmTranslation m_wasmTranslation;
259 
260  DISALLOW_COPY_AND_ASSIGN(V8Debugger);
261 };
262 
263 } // namespace v8_inspector
264 
265 #endif // V8_INSPECTOR_V8_DEBUGGER_H_
Definition: v8.h:85