v8-inspector.h 13.5 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_V8_INSPECTOR_H_
#define V8_V8_INSPECTOR_H_

#include <stdint.h>
Dan Elphick's avatar
Dan Elphick committed
9

10
#include <cctype>
11
#include <memory>
Dan Elphick's avatar
Dan Elphick committed
12

13 14 15 16 17 18 19 20 21 22
#include "v8-isolate.h"       // NOLINT(build/include_directory)
#include "v8-local-handle.h"  // NOLINT(build/include_directory)

namespace v8 {
class Context;
class Name;
class Object;
class StackTrace;
class Value;
}  // namespace v8
23 24 25

namespace v8_inspector {

26 27 28 29
namespace internal {
class V8DebuggerId;
}  // namespace internal

30 31 32 33 34 35 36 37 38 39
namespace protocol {
namespace Debugger {
namespace API {
class SearchMatch;
}
}
namespace Runtime {
namespace API {
class RemoteObject;
class StackTrace;
40
class StackTraceId;
41 42 43 44 45 46 47 48 49
}
}
namespace Schema {
namespace API {
class Domain;
}
}
}  // namespace protocol

50
class V8_EXPORT StringView {
51 52 53
 public:
  StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}

54
  StringView(const uint8_t* characters, size_t length)
55 56
      : m_is8Bit(true), m_length(length), m_characters8(characters) {}

57
  StringView(const uint16_t* characters, size_t length)
58 59 60
      : m_is8Bit(false), m_length(length), m_characters16(characters) {}

  bool is8Bit() const { return m_is8Bit; }
61
  size_t length() const { return m_length; }
62 63 64 65 66 67 68 69

  // TODO(dgozman): add DCHECK(m_is8Bit) to accessors once platform can be used
  // here.
  const uint8_t* characters8() const { return m_characters8; }
  const uint16_t* characters16() const { return m_characters16; }

 private:
  bool m_is8Bit;
70
  size_t m_length;
71 72 73 74 75 76
  union {
    const uint8_t* m_characters8;
    const uint16_t* m_characters16;
  };
};

77
class V8_EXPORT StringBuffer {
78
 public:
79
  virtual ~StringBuffer() = default;
80
  virtual StringView string() const = 0;
81
  // This method copies contents.
82
  static std::unique_ptr<StringBuffer> create(StringView);
83 84
};

85
class V8_EXPORT V8ContextInfo {
86 87
 public:
  V8ContextInfo(v8::Local<v8::Context> context, int contextGroupId,
88
                StringView humanReadableName)
89 90 91 92 93 94 95 96 97 98 99 100 101
      : context(context),
        contextGroupId(contextGroupId),
        humanReadableName(humanReadableName),
        hasMemoryOnConsole(false) {}

  v8::Local<v8::Context> context;
  // Each v8::Context is a part of a group. The group id must be non-zero.
  int contextGroupId;
  StringView humanReadableName;
  StringView origin;
  StringView auxData;
  bool hasMemoryOnConsole;

102 103
  static int executionContextId(v8::Local<v8::Context> context);

104 105 106 107 108 109 110 111 112
  // Disallow copying and allocating this one.
  enum NotNullTagEnum { NotNullLiteral };
  void* operator new(size_t) = delete;
  void* operator new(size_t, NotNullTagEnum, void*) = delete;
  void* operator new(size_t, void*) = delete;
  V8ContextInfo(const V8ContextInfo&) = delete;
  V8ContextInfo& operator=(const V8ContextInfo&) = delete;
};

113 114 115 116 117 118 119 120
// This debugger id tries to be unique by generating two random
// numbers, which should most likely avoid collisions.
// Debugger id has a 1:1 mapping to context group. It is used to
// attribute stack traces to a particular debugging, when doing any
// cross-debugger operations (e.g. async step in).
// See also Runtime.UniqueDebuggerId in the protocol.
class V8_EXPORT V8DebuggerId {
 public:
121
  V8DebuggerId() = default;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  V8DebuggerId(const V8DebuggerId&) = default;
  V8DebuggerId& operator=(const V8DebuggerId&) = default;

  std::unique_ptr<StringBuffer> toString() const;
  bool isValid() const;
  std::pair<int64_t, int64_t> pair() const;

 private:
  friend class internal::V8DebuggerId;
  explicit V8DebuggerId(std::pair<int64_t, int64_t>);

  int64_t m_first = 0;
  int64_t m_second = 0;
};

137
class V8_EXPORT V8StackTrace {
138
 public:
139
  virtual StringView firstNonEmptySourceURL() const = 0;
140 141 142 143
  virtual bool isEmpty() const = 0;
  virtual StringView topSourceURL() const = 0;
  virtual int topLineNumber() const = 0;
  virtual int topColumnNumber() const = 0;
144
  virtual int topScriptId() const = 0;
145 146
  virtual StringView topFunctionName() const = 0;

147
  virtual ~V8StackTrace() = default;
148
  virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
149
  buildInspectorObject(int maxAsyncDepth) const = 0;
150 151 152 153 154 155
  virtual std::unique_ptr<StringBuffer> toString() const = 0;

  // Safe to pass between threads, drops async chain.
  virtual std::unique_ptr<V8StackTrace> clone() = 0;
};

156
class V8_EXPORT V8InspectorSession {
157
 public:
158
  virtual ~V8InspectorSession() = default;
159 160

  // Cross-context inspectable values (DOM nodes in different worlds, etc.).
161
  class V8_EXPORT Inspectable {
162 163
   public:
    virtual v8::Local<v8::Value> get(v8::Local<v8::Context>) = 0;
164
    virtual ~Inspectable() = default;
165
  };
166
  class V8_EXPORT CommandLineAPIScope {
167 168 169
   public:
    virtual ~CommandLineAPIScope() = default;
  };
170 171 172
  virtual void addInspectedObject(std::unique_ptr<Inspectable>) = 0;

  // Dispatching protocol messages.
173 174
  static bool canDispatchMethod(StringView method);
  virtual void dispatchProtocolMessage(StringView message) = 0;
175
  virtual std::vector<uint8_t> state() = 0;
176 177 178
  virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
  supportedDomains() = 0;

179 180
  virtual std::unique_ptr<V8InspectorSession::CommandLineAPIScope>
  initializeCommandLineAPIScope(int executionContextId) = 0;
181

182
  // Debugger actions.
183 184
  virtual void schedulePauseOnNextStatement(StringView breakReason,
                                            StringView breakDetails) = 0;
185
  virtual void cancelPauseOnNextStatement() = 0;
186 187
  virtual void breakProgram(StringView breakReason,
                            StringView breakDetails) = 0;
188
  virtual void setSkipAllPauses(bool) = 0;
189
  virtual void resume(bool setTerminateOnResume = false) = 0;
190 191
  virtual void stepOver() = 0;
  virtual std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
192 193
  searchInTextByLines(StringView text, StringView query, bool caseSensitive,
                      bool isRegex) = 0;
194 195

  // Remote objects.
196
  virtual std::unique_ptr<protocol::Runtime::API::RemoteObject> wrapObject(
197
      v8::Local<v8::Context>, v8::Local<v8::Value>, StringView groupName,
198 199
      bool generatePreview) = 0;

200
  virtual bool unwrapObject(std::unique_ptr<StringBuffer>* error,
201
                            StringView objectId, v8::Local<v8::Value>*,
202 203
                            v8::Local<v8::Context>*,
                            std::unique_ptr<StringBuffer>* objectGroup) = 0;
204
  virtual void releaseObjectGroup(StringView) = 0;
205
  virtual void triggerPreciseCoverageDeltaUpdate(StringView occasion) = 0;
206 207
};

208 209
class V8_EXPORT WebDriverValue {
 public:
210 211 212 213
  explicit WebDriverValue(std::unique_ptr<StringBuffer> type,
                          v8::MaybeLocal<v8::Value> value = {})
      : type(std::move(type)), value(value) {}
  std::unique_ptr<StringBuffer> type;
214 215 216
  v8::MaybeLocal<v8::Value> value;
};

217
class V8_EXPORT V8InspectorClient {
218
 public:
219
  virtual ~V8InspectorClient() = default;
220 221 222 223 224 225 226 227 228 229 230

  virtual void runMessageLoopOnPause(int contextGroupId) {}
  virtual void quitMessageLoopOnPause() {}
  virtual void runIfWaitingForDebugger(int contextGroupId) {}

  virtual void muteMetrics(int contextGroupId) {}
  virtual void unmuteMetrics(int contextGroupId) {}

  virtual void beginUserGesture() {}
  virtual void endUserGesture() {}

231 232 233 234
  virtual std::unique_ptr<WebDriverValue> serializeToWebDriverValue(
      v8::Local<v8::Value> v8_value, int max_depth) {
    return nullptr;
  }
235 236 237
  virtual std::unique_ptr<StringBuffer> valueSubtype(v8::Local<v8::Value>) {
    return nullptr;
  }
238 239 240 241
  virtual std::unique_ptr<StringBuffer> descriptionForValueSubtype(
      v8::Local<v8::Context>, v8::Local<v8::Value>) {
    return nullptr;
  }
242 243 244 245 246 247 248 249 250 251 252
  virtual bool isInspectableHeapObject(v8::Local<v8::Object>) { return true; }

  virtual v8::Local<v8::Context> ensureDefaultContextInGroup(
      int contextGroupId) {
    return v8::Local<v8::Context>();
  }
  virtual void beginEnsureAllContextsInGroup(int contextGroupId) {}
  virtual void endEnsureAllContextsInGroup(int contextGroupId) {}

  virtual void installAdditionalCommandLineAPI(v8::Local<v8::Context>,
                                               v8::Local<v8::Object>) {}
253 254 255 256 257
  virtual void consoleAPIMessage(int contextGroupId,
                                 v8::Isolate::MessageErrorLevel level,
                                 const StringView& message,
                                 const StringView& url, unsigned lineNumber,
                                 unsigned columnNumber, V8StackTrace*) {}
258 259 260 261 262 263 264 265
  virtual v8::MaybeLocal<v8::Value> memoryInfo(v8::Isolate*,
                                               v8::Local<v8::Context>) {
    return v8::MaybeLocal<v8::Value>();
  }

  virtual void consoleTime(const StringView& title) {}
  virtual void consoleTimeEnd(const StringView& title) {}
  virtual void consoleTimeStamp(const StringView& title) {}
266
  virtual void consoleClear(int contextGroupId) {}
267 268 269 270 271 272 273 274
  virtual double currentTimeMS() { return 0; }
  typedef void (*TimerCallback)(void*);
  virtual void startRepeatingTimer(double, TimerCallback, void* data) {}
  virtual void cancelTimer(void* data) {}

  // TODO(dgozman): this was added to support service worker shadow page. We
  // should not connect at all.
  virtual bool canExecuteScripts(int contextGroupId) { return true; }
275 276

  virtual void maxAsyncCallStackDepthChanged(int depth) {}
277 278 279 280 281

  virtual std::unique_ptr<StringBuffer> resourceNameToUrl(
      const StringView& resourceName) {
    return nullptr;
  }
282 283 284 285

  // The caller would defer to generating a random 64 bit integer if
  // this method returns 0.
  virtual int64_t generateUniqueId() { return 0; }
286 287 288

  virtual void dispatchError(v8::Local<v8::Context>, v8::Local<v8::Message>,
                             v8::Local<v8::Value>) {}
289 290
};

291 292 293
// These stack trace ids are intended to be passed between debuggers and be
// resolved later. This allows to track cross-debugger calls and step between
// them if a single client connects to multiple debuggers.
294
struct V8_EXPORT V8StackTraceId {
295 296
  uintptr_t id;
  std::pair<int64_t, int64_t> debugger_id;
297
  bool should_pause = false;
298 299

  V8StackTraceId();
300
  V8StackTraceId(const V8StackTraceId&) = default;
301
  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id);
302 303
  V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id,
                 bool should_pause);
304
  explicit V8StackTraceId(StringView);
305 306
  V8StackTraceId& operator=(const V8StackTraceId&) = default;
  V8StackTraceId& operator=(V8StackTraceId&&) noexcept = default;
307 308 309
  ~V8StackTraceId() = default;

  bool IsInvalid() const;
310
  std::unique_ptr<StringBuffer> ToString();
311 312
};

313
class V8_EXPORT V8Inspector {
314 315
 public:
  static std::unique_ptr<V8Inspector> create(v8::Isolate*, V8InspectorClient*);
316
  virtual ~V8Inspector() = default;
317 318 319 320 321

  // Contexts instrumentation.
  virtual void contextCreated(const V8ContextInfo&) = 0;
  virtual void contextDestroyed(v8::Local<v8::Context>) = 0;
  virtual void resetContextGroup(int contextGroupId) = 0;
322
  virtual v8::MaybeLocal<v8::Context> contextById(int contextId) = 0;
323
  virtual V8DebuggerId uniqueDebuggerId(int contextId) = 0;
324 325 326 327 328 329

  // Various instrumentation.
  virtual void idleStarted() = 0;
  virtual void idleFinished() = 0;

  // Async stack traces instrumentation.
330
  virtual void asyncTaskScheduled(StringView taskName, void* task,
331 332 333 334 335 336
                                  bool recurring) = 0;
  virtual void asyncTaskCanceled(void* task) = 0;
  virtual void asyncTaskStarted(void* task) = 0;
  virtual void asyncTaskFinished(void* task) = 0;
  virtual void allAsyncTasksCanceled() = 0;

337
  virtual V8StackTraceId storeCurrentStackTrace(StringView description) = 0;
338 339 340
  virtual void externalAsyncTaskStarted(const V8StackTraceId& parent) = 0;
  virtual void externalAsyncTaskFinished(const V8StackTraceId& parent) = 0;

341
  // Exceptions instrumentation.
342 343 344 345 346 347
  virtual unsigned exceptionThrown(v8::Local<v8::Context>, StringView message,
                                   v8::Local<v8::Value> exception,
                                   StringView detailedMessage, StringView url,
                                   unsigned lineNumber, unsigned columnNumber,
                                   std::unique_ptr<V8StackTrace>,
                                   int scriptId) = 0;
348
  virtual void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
349
                                StringView message) = 0;
350 351 352 353
  virtual bool associateExceptionData(v8::Local<v8::Context>,
                                      v8::Local<v8::Value> exception,
                                      v8::Local<v8::Name> key,
                                      v8::Local<v8::Value> value) = 0;
354 355

  // Connection.
356
  class V8_EXPORT Channel {
357
   public:
358
    virtual ~Channel() = default;
359
    virtual void sendResponse(int callId,
360 361
                              std::unique_ptr<StringBuffer> message) = 0;
    virtual void sendNotification(std::unique_ptr<StringBuffer> message) = 0;
362 363
    virtual void flushProtocolNotifications() = 0;
  };
364
  V8_DEPRECATED("Use version with client_is_trusted argument")
365
  virtual std::unique_ptr<V8InspectorSession> connect(int contextGroupId,
366 367 368 369 370 371 372 373 374 375
                                                      Channel* channel,
                                                      StringView state) {
    return connect(contextGroupId, channel, state, kFullyTrusted);
  }
  enum ClientTrustLevel { kUntrusted, kFullyTrusted };
  virtual std::unique_ptr<V8InspectorSession> connect(
      int contextGroupId, Channel*, StringView state,
      ClientTrustLevel client_trust_level) {
    return nullptr;
  }
376 377 378 379 380 381 382 383 384 385

  // API methods.
  virtual std::unique_ptr<V8StackTrace> createStackTrace(
      v8::Local<v8::StackTrace>) = 0;
  virtual std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) = 0;
};

}  // namespace v8_inspector

#endif  // V8_V8_INSPECTOR_H_