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