v8  7.9.317 (node 13.2.0)
V8 is Google's open source JavaScript engine
v8-inspector.h
Go to the documentation of this file.
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_V8_INSPECTOR_H_
6 #define V8_V8_INSPECTOR_H_
7 
8 #include <stdint.h>
9 #include <cctype>
10 
11 #include <memory>
12 
13 #include "v8.h" // NOLINT(build/include)
14 
15 namespace v8_inspector {
16 
17 namespace protocol {
18 namespace Debugger {
19 namespace API {
20 class SearchMatch;
21 }
22 }
23 namespace Runtime {
24 namespace API {
25 class RemoteObject;
26 class StackTrace;
27 class StackTraceId;
28 }
29 }
30 namespace Schema {
31 namespace API {
32 class Domain;
33 }
34 }
35 } // namespace protocol
36 
38  public:
39  StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}
40 
41  StringView(const uint8_t* characters, size_t length)
42  : m_is8Bit(true), m_length(length), m_characters8(characters) {}
43 
44  StringView(const uint16_t* characters, size_t length)
45  : m_is8Bit(false), m_length(length), m_characters16(characters) {}
46 
47  bool is8Bit() const { return m_is8Bit; }
48  size_t length() const { return m_length; }
49 
50  // TODO(dgozman): add DCHECK(m_is8Bit) to accessors once platform can be used
51  // here.
52  const uint8_t* characters8() const { return m_characters8; }
53  const uint16_t* characters16() const { return m_characters16; }
54 
55  private:
56  bool m_is8Bit;
57  size_t m_length;
58  union {
59  const uint8_t* m_characters8;
60  const uint16_t* m_characters16;
61  };
62 };
63 
65  public:
66  virtual ~StringBuffer() = default;
67  virtual const StringView& string() = 0;
68  // This method copies contents.
69  static std::unique_ptr<StringBuffer> create(const StringView&);
70 };
71 
73  public:
74  V8ContextInfo(v8::Local<v8::Context> context, int contextGroupId,
75  const StringView& humanReadableName)
76  : context(context),
77  contextGroupId(contextGroupId),
78  humanReadableName(humanReadableName),
79  hasMemoryOnConsole(false) {}
80 
82  // Each v8::Context is a part of a group. The group id must be non-zero.
88 
89  static int executionContextId(v8::Local<v8::Context> context);
90 
91  // Disallow copying and allocating this one.
93  void* operator new(size_t) = delete;
94  void* operator new(size_t, NotNullTagEnum, void*) = delete;
95  void* operator new(size_t, void*) = delete;
96  V8ContextInfo(const V8ContextInfo&) = delete;
97  V8ContextInfo& operator=(const V8ContextInfo&) = delete;
98 };
99 
101  public:
102  virtual StringView firstNonEmptySourceURL() const = 0;
103  virtual bool isEmpty() const = 0;
104  virtual StringView topSourceURL() const = 0;
105  virtual int topLineNumber() const = 0;
106  virtual int topColumnNumber() const = 0;
107  virtual StringView topScriptId() const = 0;
108  virtual StringView topFunctionName() const = 0;
109 
110  virtual ~V8StackTrace() = default;
111  virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
112  buildInspectorObject() const = 0;
113  virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
114  buildInspectorObject(int maxAsyncDepth) const = 0;
115  virtual std::unique_ptr<StringBuffer> toString() const = 0;
116 
117  // Safe to pass between threads, drops async chain.
118  virtual std::unique_ptr<V8StackTrace> clone() = 0;
119 };
120 
122  public:
123  virtual ~V8InspectorSession() = default;
124 
125  // Cross-context inspectable values (DOM nodes in different worlds, etc.).
127  public:
128  virtual v8::Local<v8::Value> get(v8::Local<v8::Context>) = 0;
129  virtual ~Inspectable() = default;
130  };
131  virtual void addInspectedObject(std::unique_ptr<Inspectable>) = 0;
132 
133  // Dispatching protocol messages.
134  static bool canDispatchMethod(const StringView& method);
135  virtual void dispatchProtocolMessage(const StringView& message) = 0;
136  virtual std::vector<uint8_t> state() = 0;
137  virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
139 
140  // Debugger actions.
141  virtual void schedulePauseOnNextStatement(const StringView& breakReason,
142  const StringView& breakDetails) = 0;
143  virtual void cancelPauseOnNextStatement() = 0;
144  virtual void breakProgram(const StringView& breakReason,
145  const StringView& breakDetails) = 0;
146  virtual void setSkipAllPauses(bool) = 0;
147  virtual void resume() = 0;
148  virtual void stepOver() = 0;
149  virtual std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
150  searchInTextByLines(const StringView& text, const StringView& query,
151  bool caseSensitive, bool isRegex) = 0;
152 
153  // Remote objects.
154  virtual std::unique_ptr<protocol::Runtime::API::RemoteObject> wrapObject(
155  v8::Local<v8::Context>, v8::Local<v8::Value>, const StringView& groupName,
156  bool generatePreview) = 0;
157 
158  virtual bool unwrapObject(std::unique_ptr<StringBuffer>* error,
159  const StringView& objectId, v8::Local<v8::Value>*,
160  v8::Local<v8::Context>*,
161  std::unique_ptr<StringBuffer>* objectGroup) = 0;
162  virtual void releaseObjectGroup(const StringView&) = 0;
163 };
164 
166  public:
167  virtual ~V8InspectorClient() = default;
168 
169  virtual void runMessageLoopOnPause(int contextGroupId) {}
170  virtual void quitMessageLoopOnPause() {}
171  virtual void runIfWaitingForDebugger(int contextGroupId) {}
172 
173  virtual void muteMetrics(int contextGroupId) {}
174  virtual void unmuteMetrics(int contextGroupId) {}
175 
176  virtual void beginUserGesture() {}
177  virtual void endUserGesture() {}
178 
179  virtual std::unique_ptr<StringBuffer> valueSubtype(v8::Local<v8::Value>) {
180  return nullptr;
181  }
183  return false;
184  }
185  virtual bool isInspectableHeapObject(v8::Local<v8::Object>) { return true; }
186 
188  int contextGroupId) {
190  }
191  virtual void beginEnsureAllContextsInGroup(int contextGroupId) {}
192  virtual void endEnsureAllContextsInGroup(int contextGroupId) {}
193 
195  v8::Local<v8::Object>) {}
196  virtual void consoleAPIMessage(int contextGroupId,
197  v8::Isolate::MessageErrorLevel level,
198  const StringView& message,
199  const StringView& url, unsigned lineNumber,
200  unsigned columnNumber, V8StackTrace*) {}
202  v8::Local<v8::Context>) {
204  }
205 
206  virtual void consoleTime(const StringView& title) {}
207  virtual void consoleTimeEnd(const StringView& title) {}
208  virtual void consoleTimeStamp(const StringView& title) {}
209  virtual void consoleClear(int contextGroupId) {}
210  virtual double currentTimeMS() { return 0; }
211  typedef void (*TimerCallback)(void*);
212  virtual void startRepeatingTimer(double, TimerCallback, void* data) {}
213  virtual void cancelTimer(void* data) {}
214 
215  // TODO(dgozman): this was added to support service worker shadow page. We
216  // should not connect at all.
217  virtual bool canExecuteScripts(int contextGroupId) { return true; }
218 
219  virtual void maxAsyncCallStackDepthChanged(int depth) {}
220 
221  virtual std::unique_ptr<StringBuffer> resourceNameToUrl(
222  const StringView& resourceName) {
223  return nullptr;
224  }
225 };
226 
227 // These stack trace ids are intended to be passed between debuggers and be
228 // resolved later. This allows to track cross-debugger calls and step between
229 // them if a single client connects to multiple debuggers.
231  uintptr_t id;
232  std::pair<int64_t, int64_t> debugger_id;
233  bool should_pause = false;
234 
236  V8StackTraceId(const V8StackTraceId&) = default;
237  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id);
238  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id,
239  bool should_pause);
240  explicit V8StackTraceId(const StringView&);
242  V8StackTraceId& operator=(V8StackTraceId&&) noexcept = default;
243  ~V8StackTraceId() = default;
244 
245  bool IsInvalid() const;
246  std::unique_ptr<StringBuffer> ToString();
247 };
248 
250  public:
251  static std::unique_ptr<V8Inspector> create(v8::Isolate*, V8InspectorClient*);
252  virtual ~V8Inspector() = default;
253 
254  // Contexts instrumentation.
255  virtual void contextCreated(const V8ContextInfo&) = 0;
256  virtual void contextDestroyed(v8::Local<v8::Context>) = 0;
257  virtual void resetContextGroup(int contextGroupId) = 0;
258  virtual v8::MaybeLocal<v8::Context> contextById(int contextId) = 0;
259 
260  // Various instrumentation.
261  virtual void idleStarted() = 0;
262  virtual void idleFinished() = 0;
263 
264  // Async stack traces instrumentation.
265  virtual void asyncTaskScheduled(const StringView& taskName, void* task,
266  bool recurring) = 0;
267  virtual void asyncTaskCanceled(void* task) = 0;
268  virtual void asyncTaskStarted(void* task) = 0;
269  virtual void asyncTaskFinished(void* task) = 0;
270  virtual void allAsyncTasksCanceled() = 0;
271 
273  const StringView& description) = 0;
274  virtual void externalAsyncTaskStarted(const V8StackTraceId& parent) = 0;
275  virtual void externalAsyncTaskFinished(const V8StackTraceId& parent) = 0;
276 
277  // Exceptions instrumentation.
278  virtual unsigned exceptionThrown(
279  v8::Local<v8::Context>, const StringView& message,
280  v8::Local<v8::Value> exception, const StringView& detailedMessage,
281  const StringView& url, unsigned lineNumber, unsigned columnNumber,
282  std::unique_ptr<V8StackTrace>, int scriptId) = 0;
283  virtual void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
284  const StringView& message) = 0;
285 
286  // Connection.
288  public:
289  virtual ~Channel() = default;
290  virtual void sendResponse(int callId,
291  std::unique_ptr<StringBuffer> message) = 0;
292  virtual void sendNotification(std::unique_ptr<StringBuffer> message) = 0;
293  virtual void flushProtocolNotifications() = 0;
294  };
295  virtual std::unique_ptr<V8InspectorSession> connect(
296  int contextGroupId, Channel*, const StringView& state) = 0;
297 
298  // API methods.
299  virtual std::unique_ptr<V8StackTrace> createStackTrace(
300  v8::Local<v8::StackTrace>) = 0;
301  virtual std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) = 0;
302 };
303 
304 } // namespace v8_inspector
305 
306 #endif // V8_V8_INSPECTOR_H_