v8  8.1.307(node14.1.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)
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 const StringView& string() = 0;
69  // This method copies contents.
70  static std::unique_ptr<StringBuffer> create(const StringView&);
71 };
72 
74  public:
75  V8ContextInfo(v8::Local<v8::Context> context, int contextGroupId,
76  const 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 StringView topScriptId() const = 0;
109  virtual StringView topFunctionName() const = 0;
110 
111  virtual ~V8StackTrace() = default;
112  virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
113  buildInspectorObject() const = 0;
114  virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
115  buildInspectorObject(int maxAsyncDepth) const = 0;
116  virtual std::unique_ptr<StringBuffer> toString() const = 0;
117 
118  // Safe to pass between threads, drops async chain.
119  virtual std::unique_ptr<V8StackTrace> clone() = 0;
120 };
121 
123  public:
124  virtual ~V8InspectorSession() = default;
125 
126  // Cross-context inspectable values (DOM nodes in different worlds, etc.).
128  public:
129  virtual v8::Local<v8::Value> get(v8::Local<v8::Context>) = 0;
130  virtual ~Inspectable() = default;
131  };
132  virtual void addInspectedObject(std::unique_ptr<Inspectable>) = 0;
133 
134  // Dispatching protocol messages.
135  static bool canDispatchMethod(const StringView& method);
136  virtual void dispatchProtocolMessage(const StringView& message) = 0;
137  virtual std::vector<uint8_t> state() = 0;
138  virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
139  supportedDomains() = 0;
140 
141  // Debugger actions.
142  virtual void schedulePauseOnNextStatement(const StringView& breakReason,
143  const StringView& breakDetails) = 0;
144  virtual void cancelPauseOnNextStatement() = 0;
145  virtual void breakProgram(const StringView& breakReason,
146  const StringView& breakDetails) = 0;
147  virtual void setSkipAllPauses(bool) = 0;
148  virtual void resume() = 0;
149  virtual void stepOver() = 0;
150  virtual std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
151  searchInTextByLines(const StringView& text, const StringView& query,
152  bool caseSensitive, bool isRegex) = 0;
153 
154  // Remote objects.
155  virtual std::unique_ptr<protocol::Runtime::API::RemoteObject> wrapObject(
156  v8::Local<v8::Context>, v8::Local<v8::Value>, const StringView& groupName,
157  bool generatePreview) = 0;
158 
159  virtual bool unwrapObject(std::unique_ptr<StringBuffer>* error,
160  const StringView& objectId, v8::Local<v8::Value>*,
161  v8::Local<v8::Context>*,
162  std::unique_ptr<StringBuffer>* objectGroup) = 0;
163  virtual void releaseObjectGroup(const StringView&) = 0;
165  const StringView& occassion) = 0;
166 };
167 
169  public:
170  virtual ~V8InspectorClient() = default;
171 
172  virtual void runMessageLoopOnPause(int contextGroupId) {}
173  virtual void quitMessageLoopOnPause() {}
174  virtual void runIfWaitingForDebugger(int contextGroupId) {}
175 
176  virtual void muteMetrics(int contextGroupId) {}
177  virtual void unmuteMetrics(int contextGroupId) {}
178 
179  virtual void beginUserGesture() {}
180  virtual void endUserGesture() {}
181 
182  virtual std::unique_ptr<StringBuffer> valueSubtype(v8::Local<v8::Value>) {
183  return nullptr;
184  }
186  return false;
187  }
188  virtual bool isInspectableHeapObject(v8::Local<v8::Object>) { return true; }
189 
191  int contextGroupId) {
193  }
194  virtual void beginEnsureAllContextsInGroup(int contextGroupId) {}
195  virtual void endEnsureAllContextsInGroup(int contextGroupId) {}
196 
198  v8::Local<v8::Object>) {}
199  virtual void consoleAPIMessage(int contextGroupId,
200  v8::Isolate::MessageErrorLevel level,
201  const StringView& message,
202  const StringView& url, unsigned lineNumber,
203  unsigned columnNumber, V8StackTrace*) {}
205  v8::Local<v8::Context>) {
207  }
208 
209  virtual void consoleTime(const StringView& title) {}
210  virtual void consoleTimeEnd(const StringView& title) {}
211  virtual void consoleTimeStamp(const StringView& title) {}
212  virtual void consoleClear(int contextGroupId) {}
213  virtual double currentTimeMS() { return 0; }
214  typedef void (*TimerCallback)(void*);
215  virtual void startRepeatingTimer(double, TimerCallback, void* data) {}
216  virtual void cancelTimer(void* data) {}
217 
218  // TODO(dgozman): this was added to support service worker shadow page. We
219  // should not connect at all.
220  virtual bool canExecuteScripts(int contextGroupId) { return true; }
221 
222  virtual void maxAsyncCallStackDepthChanged(int depth) {}
223 
224  virtual std::unique_ptr<StringBuffer> resourceNameToUrl(
225  const StringView& resourceName) {
226  return nullptr;
227  }
228 };
229 
230 // These stack trace ids are intended to be passed between debuggers and be
231 // resolved later. This allows to track cross-debugger calls and step between
232 // them if a single client connects to multiple debuggers.
234  uintptr_t id;
235  std::pair<int64_t, int64_t> debugger_id;
236  bool should_pause = false;
237 
238  V8StackTraceId();
239  V8StackTraceId(const V8StackTraceId&) = default;
240  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id);
241  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id,
242  bool should_pause);
243  explicit V8StackTraceId(const StringView&);
244  V8StackTraceId& operator=(const V8StackTraceId&) = default;
245  V8StackTraceId& operator=(V8StackTraceId&&) noexcept = default;
246  ~V8StackTraceId() = default;
247 
248  bool IsInvalid() const;
249  std::unique_ptr<StringBuffer> ToString();
250 };
251 
253  public:
254  static std::unique_ptr<V8Inspector> create(v8::Isolate*, V8InspectorClient*);
255  virtual ~V8Inspector() = default;
256 
257  // Contexts instrumentation.
258  virtual void contextCreated(const V8ContextInfo&) = 0;
259  virtual void contextDestroyed(v8::Local<v8::Context>) = 0;
260  virtual void resetContextGroup(int contextGroupId) = 0;
261  virtual v8::MaybeLocal<v8::Context> contextById(int contextId) = 0;
262 
263  // Various instrumentation.
264  virtual void idleStarted() = 0;
265  virtual void idleFinished() = 0;
266 
267  // Async stack traces instrumentation.
268  virtual void asyncTaskScheduled(const StringView& taskName, void* task,
269  bool recurring) = 0;
270  virtual void asyncTaskCanceled(void* task) = 0;
271  virtual void asyncTaskStarted(void* task) = 0;
272  virtual void asyncTaskFinished(void* task) = 0;
273  virtual void allAsyncTasksCanceled() = 0;
274 
276  const StringView& description) = 0;
277  virtual void externalAsyncTaskStarted(const V8StackTraceId& parent) = 0;
278  virtual void externalAsyncTaskFinished(const V8StackTraceId& parent) = 0;
279 
280  // Exceptions instrumentation.
281  virtual unsigned exceptionThrown(
282  v8::Local<v8::Context>, const StringView& message,
283  v8::Local<v8::Value> exception, const StringView& detailedMessage,
284  const StringView& url, unsigned lineNumber, unsigned columnNumber,
285  std::unique_ptr<V8StackTrace>, int scriptId) = 0;
286  virtual void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
287  const StringView& message) = 0;
288 
289  // Connection.
291  public:
292  virtual ~Channel() = default;
293  virtual void sendResponse(int callId,
294  std::unique_ptr<StringBuffer> message) = 0;
295  virtual void sendNotification(std::unique_ptr<StringBuffer> message) = 0;
296  virtual void flushProtocolNotifications() = 0;
297  };
298  virtual std::unique_ptr<V8InspectorSession> connect(
299  int contextGroupId, Channel*, const StringView& state) = 0;
300 
301  // API methods.
302  virtual std::unique_ptr<V8StackTrace> createStackTrace(
303  v8::Local<v8::StackTrace>) = 0;
304  virtual std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) = 0;
305 
306  // Performance counters.
307  class V8_EXPORT Counters : public std::enable_shared_from_this<Counters> {
308  public:
309  explicit Counters(v8::Isolate* isolate);
310  ~Counters();
311  const std::unordered_map<std::string, int>& getCountersMap() const {
312  return m_countersMap;
313  }
314 
315  private:
316  static int* getCounterPtr(const char* name);
317 
318  v8::Isolate* m_isolate;
319  std::unordered_map<std::string, int> m_countersMap;
320  };
321 
322  virtual std::shared_ptr<Counters> enableCounters() = 0;
323 };
324 
325 } // namespace v8_inspector
326 
327 #endif // V8_V8_INSPECTOR_H_
virtual std::unique_ptr< V8StackTrace > captureStackTrace(bool fullStack)=0
virtual void idleFinished()=0
virtual void releaseObjectGroup(const StringView &)=0
virtual bool unwrapObject(std::unique_ptr< StringBuffer > *error, const StringView &objectId, v8::Local< v8::Value > *, v8::Local< v8::Context > *, std::unique_ptr< StringBuffer > *objectGroup)=0
virtual void setSkipAllPauses(bool)=0
virtual unsigned exceptionThrown(v8::Local< v8::Context >, const StringView &message, v8::Local< v8::Value > exception, const StringView &detailedMessage, const StringView &url, unsigned lineNumber, unsigned columnNumber, std::unique_ptr< V8StackTrace >, int scriptId)=0
static bool canDispatchMethod(const StringView &method)
friend class MaybeLocal
Definition: v8.h:9704
virtual ~V8StackTrace()=default
virtual const StringView & string()=0
#define V8_EXPORT
Definition: v8config.h:458
virtual ~V8InspectorSession()=default
void * operator new(size_t)=delete
virtual void externalAsyncTaskFinished(const V8StackTraceId &parent)=0
Definition: v8.h:2507
virtual std::shared_ptr< Counters > enableCounters()=0
virtual StringView topFunctionName() const =0
virtual v8::Local< v8::Context > ensureDefaultContextInGroup(int contextGroupId)
Definition: v8-inspector.h:190
virtual void cancelTimer(void *data)
Definition: v8-inspector.h:216
virtual void runMessageLoopOnPause(int contextGroupId)
Definition: v8-inspector.h:172
virtual std::unique_ptr< V8StackTrace > createStackTrace(v8::Local< v8::StackTrace >)=0
virtual void cancelPauseOnNextStatement()=0
virtual void triggerPreciseCoverageDeltaUpdate(const StringView &occassion)=0
virtual std::unique_ptr< protocol::Runtime::API::StackTrace > buildInspectorObject(int maxAsyncDepth) const =0
virtual void contextCreated(const V8ContextInfo &)=0
StringView(const uint16_t *characters, size_t length)
Definition: v8-inspector.h:45
V8StackTraceId(uintptr_t id, const std::pair< int64_t, int64_t > debugger_id)
virtual std::unique_ptr< V8InspectorSession > connect(int contextGroupId, Channel *, const StringView &state)=0
std::pair< int64_t, int64_t > debugger_id
Definition: v8-inspector.h:235
V8StackTraceId & operator=(const V8StackTraceId &)=default
MessageErrorLevel
Definition: v8.h:8417
virtual StringView topSourceURL() const =0
virtual void schedulePauseOnNextStatement(const StringView &breakReason, const StringView &breakDetails)=0
virtual void maxAsyncCallStackDepthChanged(int depth)
Definition: v8-inspector.h:222
virtual ~V8InspectorClient()=default
virtual bool canExecuteScripts(int contextGroupId)
Definition: v8-inspector.h:220
virtual void asyncTaskFinished(void *task)=0
V8StackTraceId(const StringView &)
const std::unordered_map< std::string, int > & getCountersMap() const
Definition: v8-inspector.h:311
virtual int topColumnNumber() const =0
virtual void runIfWaitingForDebugger(int contextGroupId)
Definition: v8-inspector.h:174
virtual std::unique_ptr< StringBuffer > resourceNameToUrl(const StringView &resourceName)
Definition: v8-inspector.h:224
virtual ~StringBuffer()=default
virtual std::vector< std::unique_ptr< protocol::Debugger::API::SearchMatch > > searchInTextByLines(const StringView &text, const StringView &query, bool caseSensitive, bool isRegex)=0
v8::Local< v8::Context > context
Definition: v8-inspector.h:82
virtual void installAdditionalCommandLineAPI(v8::Local< v8::Context >, v8::Local< v8::Object >)
Definition: v8-inspector.h:197
virtual void exceptionRevoked(v8::Local< v8::Context >, unsigned exceptionId, const StringView &message)=0
virtual void consoleTime(const StringView &title)
Definition: v8-inspector.h:209
virtual void idleStarted()=0
virtual std::unique_ptr< V8StackTrace > clone()=0
virtual void allAsyncTasksCanceled()=0
static std::unique_ptr< V8Inspector > create(v8::Isolate *, V8InspectorClient *)
Definition: libplatform.h:15
virtual void dispatchProtocolMessage(const StringView &message)=0
friend class Local
Definition: v8.h:9702
V8_INLINE Local()
Definition: v8.h:191
virtual bool isInspectableHeapObject(v8::Local< v8::Object >)
Definition: v8-inspector.h:188
V8ContextInfo(v8::Local< v8::Context > context, int contextGroupId, const StringView &humanReadableName)
Definition: v8-inspector.h:75
virtual void contextDestroyed(v8::Local< v8::Context >)=0
virtual ~V8Inspector()=default
std::unique_ptr< StringBuffer > ToString()
virtual int topLineNumber() const =0
virtual void resetContextGroup(int contextGroupId)=0
virtual std::unique_ptr< protocol::Runtime::API::RemoteObject > wrapObject(v8::Local< v8::Context >, v8::Local< v8::Value >, const StringView &groupName, bool generatePreview)=0
virtual void externalAsyncTaskStarted(const V8StackTraceId &parent)=0
virtual v8::Local< v8::Value > get(v8::Local< v8::Context >)=0
virtual void sendNotification(std::unique_ptr< StringBuffer > message)=0
virtual std::vector< uint8_t > state()=0
StringView(const uint8_t *characters, size_t length)
Definition: v8-inspector.h:42
virtual void asyncTaskScheduled(const StringView &taskName, void *task, bool recurring)=0
virtual StringView firstNonEmptySourceURL() const =0
V8ContextInfo & operator=(const V8ContextInfo &)=delete
virtual v8::MaybeLocal< v8::Context > contextById(int contextId)=0
virtual bool formatAccessorsAsProperties(v8::Local< v8::Value >)
Definition: v8-inspector.h:185
Counters(v8::Isolate *isolate)
virtual v8::MaybeLocal< v8::Value > memoryInfo(v8::Isolate *, v8::Local< v8::Context >)
Definition: v8-inspector.h:204
virtual V8StackTraceId storeCurrentStackTrace(const StringView &description)=0
V8_INLINE MaybeLocal()
Definition: v8.h:362
virtual void endEnsureAllContextsInGroup(int contextGroupId)
Definition: v8-inspector.h:195
virtual std::unique_ptr< StringBuffer > valueSubtype(v8::Local< v8::Value >)
Definition: v8-inspector.h:182
static int executionContextId(v8::Local< v8::Context > context)
virtual void unmuteMetrics(int contextGroupId)
Definition: v8-inspector.h:177
virtual std::vector< std::unique_ptr< protocol::Schema::API::Domain > > supportedDomains()=0
virtual std::unique_ptr< StringBuffer > toString() const =0
virtual void beginEnsureAllContextsInGroup(int contextGroupId)
Definition: v8-inspector.h:194
V8ContextInfo(const V8ContextInfo &)=delete
virtual void breakProgram(const StringView &breakReason, const StringView &breakDetails)=0
virtual void flushProtocolNotifications()=0
V8StackTraceId(uintptr_t id, const std::pair< int64_t, int64_t > debugger_id, bool should_pause)
virtual void sendResponse(int callId, std::unique_ptr< StringBuffer > message)=0
const uint8_t * characters8() const
Definition: v8-inspector.h:53
V8StackTraceId(const V8StackTraceId &)=default
virtual void consoleAPIMessage(int contextGroupId, v8::Isolate::MessageErrorLevel level, const StringView &message, const StringView &url, unsigned lineNumber, unsigned columnNumber, V8StackTrace *)
Definition: v8-inspector.h:199
const uint16_t * characters16() const
Definition: v8-inspector.h:54
virtual std::unique_ptr< protocol::Runtime::API::StackTrace > buildInspectorObject() const =0
virtual bool isEmpty() const =0
virtual void consoleTimeEnd(const StringView &title)
Definition: v8-inspector.h:210
virtual void asyncTaskCanceled(void *task)=0
size_t length() const
Definition: v8-inspector.h:49
static std::unique_ptr< StringBuffer > create(const StringView &)
void * operator new(size_t, void *)=delete
virtual void asyncTaskStarted(void *task)=0
virtual void quitMessageLoopOnPause()
Definition: v8-inspector.h:173
V8StackTraceId & operator=(V8StackTraceId &&) noexcept=default
virtual void muteMetrics(int contextGroupId)
Definition: v8-inspector.h:176
virtual void consoleTimeStamp(const StringView &title)
Definition: v8-inspector.h:211
virtual StringView topScriptId() const =0
virtual void startRepeatingTimer(double, TimerCallback, void *data)
Definition: v8-inspector.h:215
virtual void consoleClear(int contextGroupId)
Definition: v8-inspector.h:212
void * operator new(size_t, NotNullTagEnum, void *)=delete
virtual void addInspectedObject(std::unique_ptr< Inspectable >)=0