v8  9.4.146 (node 16.13.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 #include <unordered_map>
13 
14 #include "v8.h" // NOLINT(build/include_directory)
15 
16 namespace v8_inspector {
17 
18 namespace protocol {
19 namespace Debugger {
20 namespace API {
21 class SearchMatch;
22 }
23 }
24 namespace Runtime {
25 namespace API {
26 class RemoteObject;
27 class StackTrace;
28 class StackTraceId;
29 }
30 }
31 namespace Schema {
32 namespace API {
33 class Domain;
34 }
35 }
36 } // namespace protocol
37 
39  public:
40  StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}
41 
42  StringView(const uint8_t* characters, size_t length)
43  : m_is8Bit(true), m_length(length), m_characters8(characters) {}
44 
45  StringView(const uint16_t* characters, size_t length)
46  : m_is8Bit(false), m_length(length), m_characters16(characters) {}
47 
48  bool is8Bit() const { return m_is8Bit; }
49  size_t length() const { return m_length; }
50 
51  // TODO(dgozman): add DCHECK(m_is8Bit) to accessors once platform can be used
52  // here.
53  const uint8_t* characters8() const { return m_characters8; }
54  const uint16_t* characters16() const { return m_characters16; }
55 
56  private:
57  bool m_is8Bit;
58  size_t m_length;
59  union {
60  const uint8_t* m_characters8;
61  const uint16_t* m_characters16;
62  };
63 };
64 
66  public:
67  virtual ~StringBuffer() = default;
68  virtual StringView string() const = 0;
69  // This method copies contents.
70  static std::unique_ptr<StringBuffer> create(StringView);
71 };
72 
74  public:
75  V8ContextInfo(v8::Local<v8::Context> context, int contextGroupId,
76  StringView humanReadableName)
77  : context(context),
78  contextGroupId(contextGroupId),
79  humanReadableName(humanReadableName),
80  hasMemoryOnConsole(false) {}
81 
83  // Each v8::Context is a part of a group. The group id must be non-zero.
89 
90  static int executionContextId(v8::Local<v8::Context> context);
91 
92  // Disallow copying and allocating this one.
94  void* operator new(size_t) = delete;
95  void* operator new(size_t, NotNullTagEnum, void*) = delete;
96  void* operator new(size_t, void*) = delete;
97  V8ContextInfo(const V8ContextInfo&) = delete;
98  V8ContextInfo& operator=(const V8ContextInfo&) = delete;
99 };
100 
102  public:
103  virtual StringView firstNonEmptySourceURL() const = 0;
104  virtual bool isEmpty() const = 0;
105  virtual StringView topSourceURL() const = 0;
106  virtual int topLineNumber() const = 0;
107  virtual int topColumnNumber() const = 0;
108  virtual int topScriptId() const = 0;
109  V8_DEPRECATE_SOON("Use V8::StackTrace::topScriptId() instead.")
110  int topScriptIdAsInteger() const { return topScriptId(); }
111  virtual StringView topFunctionName() const = 0;
112 
113  virtual ~V8StackTrace() = default;
114  virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
115  buildInspectorObject() const = 0;
116  virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
117  buildInspectorObject(int maxAsyncDepth) const = 0;
118  virtual std::unique_ptr<StringBuffer> toString() const = 0;
119 
120  // Safe to pass between threads, drops async chain.
121  virtual std::unique_ptr<V8StackTrace> clone() = 0;
122 };
123 
125  public:
126  virtual ~V8InspectorSession() = default;
127 
128  // Cross-context inspectable values (DOM nodes in different worlds, etc.).
130  public:
131  virtual v8::Local<v8::Value> get(v8::Local<v8::Context>) = 0;
132  virtual ~Inspectable() = default;
133  };
135  public:
136  virtual ~CommandLineAPIScope() = default;
137  };
138  virtual void addInspectedObject(std::unique_ptr<Inspectable>) = 0;
139 
140  // Dispatching protocol messages.
141  static bool canDispatchMethod(StringView method);
142  virtual void dispatchProtocolMessage(StringView message) = 0;
143  virtual std::vector<uint8_t> state() = 0;
144  virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
146 
147  virtual std::unique_ptr<V8InspectorSession::CommandLineAPIScope>
148  initializeCommandLineAPIScope(int executionContextId) = 0;
149 
150  // Debugger actions.
151  virtual void schedulePauseOnNextStatement(StringView breakReason,
152  StringView breakDetails) = 0;
153  virtual void cancelPauseOnNextStatement() = 0;
154  virtual void breakProgram(StringView breakReason,
155  StringView breakDetails) = 0;
156  virtual void setSkipAllPauses(bool) = 0;
157  virtual void resume(bool setTerminateOnResume = false) = 0;
158  virtual void stepOver() = 0;
159  virtual std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
160  searchInTextByLines(StringView text, StringView query, bool caseSensitive,
161  bool isRegex) = 0;
162 
163  // Remote objects.
164  virtual std::unique_ptr<protocol::Runtime::API::RemoteObject> wrapObject(
165  v8::Local<v8::Context>, v8::Local<v8::Value>, StringView groupName,
166  bool generatePreview) = 0;
167 
168  virtual bool unwrapObject(std::unique_ptr<StringBuffer>* error,
169  StringView objectId, v8::Local<v8::Value>*,
170  v8::Local<v8::Context>*,
171  std::unique_ptr<StringBuffer>* objectGroup) = 0;
172  virtual void releaseObjectGroup(StringView) = 0;
173  virtual void triggerPreciseCoverageDeltaUpdate(StringView occasion) = 0;
174 };
175 
177  public:
178  virtual ~V8InspectorClient() = default;
179 
180  virtual void runMessageLoopOnPause(int contextGroupId) {}
181  virtual void quitMessageLoopOnPause() {}
182  virtual void runIfWaitingForDebugger(int contextGroupId) {}
183 
184  virtual void muteMetrics(int contextGroupId) {}
185  virtual void unmuteMetrics(int contextGroupId) {}
186 
187  virtual void beginUserGesture() {}
188  virtual void endUserGesture() {}
189 
190  virtual std::unique_ptr<StringBuffer> valueSubtype(v8::Local<v8::Value>) {
191  return nullptr;
192  }
193  virtual std::unique_ptr<StringBuffer> descriptionForValueSubtype(
194  v8::Local<v8::Context>, v8::Local<v8::Value>) {
195  return nullptr;
196  }
197  virtual bool isInspectableHeapObject(v8::Local<v8::Object>) { return true; }
198 
200  int contextGroupId) {
202  }
203  virtual void beginEnsureAllContextsInGroup(int contextGroupId) {}
204  virtual void endEnsureAllContextsInGroup(int contextGroupId) {}
205 
207  v8::Local<v8::Object>) {}
208  virtual void consoleAPIMessage(int contextGroupId,
209  v8::Isolate::MessageErrorLevel level,
210  const StringView& message,
211  const StringView& url, unsigned lineNumber,
212  unsigned columnNumber, V8StackTrace*) {}
214  v8::Local<v8::Context>) {
216  }
217 
218  virtual void consoleTime(const StringView& title) {}
219  virtual void consoleTimeEnd(const StringView& title) {}
220  virtual void consoleTimeStamp(const StringView& title) {}
221  virtual void consoleClear(int contextGroupId) {}
222  virtual double currentTimeMS() { return 0; }
223  typedef void (*TimerCallback)(void*);
224  virtual void startRepeatingTimer(double, TimerCallback, void* data) {}
225  virtual void cancelTimer(void* data) {}
226 
227  // TODO(dgozman): this was added to support service worker shadow page. We
228  // should not connect at all.
229  virtual bool canExecuteScripts(int contextGroupId) { return true; }
230 
231  virtual void maxAsyncCallStackDepthChanged(int depth) {}
232 
233  virtual std::unique_ptr<StringBuffer> resourceNameToUrl(
234  const StringView& resourceName) {
235  return nullptr;
236  }
237 
238  // The caller would defer to generating a random 64 bit integer if
239  // this method returns 0.
240  virtual int64_t generateUniqueId() { return 0; }
241 };
242 
243 // These stack trace ids are intended to be passed between debuggers and be
244 // resolved later. This allows to track cross-debugger calls and step between
245 // them if a single client connects to multiple debuggers.
247  uintptr_t id;
248  std::pair<int64_t, int64_t> debugger_id;
249  bool should_pause = false;
250 
252  V8StackTraceId(const V8StackTraceId&) = default;
253  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id);
254  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id,
255  bool should_pause);
258  V8StackTraceId& operator=(V8StackTraceId&&) noexcept = default;
259  ~V8StackTraceId() = default;
260 
261  bool IsInvalid() const;
262  std::unique_ptr<StringBuffer> ToString();
263 };
264 
266  public:
267  static std::unique_ptr<V8Inspector> create(v8::Isolate*, V8InspectorClient*);
268  virtual ~V8Inspector() = default;
269 
270  // Contexts instrumentation.
271  virtual void contextCreated(const V8ContextInfo&) = 0;
272  virtual void contextDestroyed(v8::Local<v8::Context>) = 0;
273  virtual void resetContextGroup(int contextGroupId) = 0;
274  virtual v8::MaybeLocal<v8::Context> contextById(int contextId) = 0;
275 
276  // Various instrumentation.
277  virtual void idleStarted() = 0;
278  virtual void idleFinished() = 0;
279 
280  // Async stack traces instrumentation.
281  virtual void asyncTaskScheduled(StringView taskName, void* task,
282  bool recurring) = 0;
283  virtual void asyncTaskCanceled(void* task) = 0;
284  virtual void asyncTaskStarted(void* task) = 0;
285  virtual void asyncTaskFinished(void* task) = 0;
286  virtual void allAsyncTasksCanceled() = 0;
287 
289  virtual void externalAsyncTaskStarted(const V8StackTraceId& parent) = 0;
290  virtual void externalAsyncTaskFinished(const V8StackTraceId& parent) = 0;
291 
292  // Exceptions instrumentation.
293  virtual unsigned exceptionThrown(v8::Local<v8::Context>, StringView message,
294  v8::Local<v8::Value> exception,
295  StringView detailedMessage, StringView url,
296  unsigned lineNumber, unsigned columnNumber,
297  std::unique_ptr<V8StackTrace>,
298  int scriptId) = 0;
299  virtual void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
300  StringView message) = 0;
302  v8::Local<v8::Value> exception,
303  v8::Local<v8::Name> key,
304  v8::Local<v8::Value> value) = 0;
305 
306  // Connection.
308  public:
309  virtual ~Channel() = default;
310  virtual void sendResponse(int callId,
311  std::unique_ptr<StringBuffer> message) = 0;
312  virtual void sendNotification(std::unique_ptr<StringBuffer> message) = 0;
313  virtual void flushProtocolNotifications() = 0;
314  };
315  virtual std::unique_ptr<V8InspectorSession> connect(int contextGroupId,
316  Channel*,
317  StringView state) = 0;
318 
319  // API methods.
320  virtual std::unique_ptr<V8StackTrace> createStackTrace(
321  v8::Local<v8::StackTrace>) = 0;
322  virtual std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) = 0;
323 
324  // Performance counters.
325  class V8_EXPORT Counters : public std::enable_shared_from_this<Counters> {
326  public:
327  explicit Counters(v8::Isolate* isolate);
329  const std::unordered_map<std::string, int>& getCountersMap() const {
330  return m_countersMap;
331  }
332 
333  private:
334  static int* getCounterPtr(const char* name);
335 
336  v8::Isolate* m_isolate;
337  std::unordered_map<std::string, int> m_countersMap;
338  };
339 
340  virtual std::shared_ptr<Counters> enableCounters() = 0;
341 };
342 
343 } // namespace v8_inspector
344 
345 #endif // V8_V8_INSPECTOR_H_