Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
inspector_agent.h
Go to the documentation of this file.
1 #ifndef SRC_INSPECTOR_AGENT_H_
2 #define SRC_INSPECTOR_AGENT_H_
3 
4 #include <memory>
5 
6 #include <stddef.h>
7 
8 #if !HAVE_INSPECTOR
9 #error("This header can only be used when inspector is enabled")
10 #endif
11 
12 #include "node_debug_options.h"
13 
14 // Forward declaration to break recursive dependency chain with src/env.h.
15 namespace node {
16 class Environment;
17 class NodePlatform;
18 } // namespace node
19 
20 #include "v8.h"
21 
22 namespace v8_inspector {
23 class StringView;
24 } // namespace v8_inspector
25 
26 namespace node {
27 namespace inspector {
28 
30  public:
31  virtual ~InspectorSessionDelegate() = default;
32  virtual bool WaitForFrontendMessageWhilePaused() = 0;
33  virtual void SendMessageToFrontend(const v8_inspector::StringView& message)
34  = 0;
35 };
36 
37 class InspectorIo;
39 
40 class Agent {
41  public:
42  explicit Agent(node::Environment* env);
43  ~Agent();
44 
45  // Create client_, may create io_ if option enabled
46  bool Start(node::NodePlatform* platform, const char* path,
47  const DebugOptions& options);
48  // Stop and destroy io_
49  void Stop();
50 
51  bool IsStarted() { return !!client_; }
52 
53  // IO thread started, and client connected
54  bool IsConnected();
55 
56 
57  void WaitForDisconnect();
58  void FatalException(v8::Local<v8::Value> error,
59  v8::Local<v8::Message> message);
60 
61  // Async stack traces instrumentation.
62  void AsyncTaskScheduled(const v8_inspector::StringView& taskName, void* task,
63  bool recurring);
64  void AsyncTaskCanceled(void* task);
65  void AsyncTaskStarted(void* task);
66  void AsyncTaskFinished(void* task);
67  void AllAsyncTasksCanceled();
68 
69  void RegisterAsyncHook(v8::Isolate* isolate,
70  v8::Local<v8::Function> enable_function,
71  v8::Local<v8::Function> disable_function);
72 
73  // These methods are called by the WS protocol and JS binding to create
74  // inspector sessions. The inspector responds by using the delegate to send
75  // messages back.
76  void Connect(InspectorSessionDelegate* delegate);
77  void Disconnect();
78  void Dispatch(const v8_inspector::StringView& message);
79  InspectorSessionDelegate* delegate();
80 
81  void RunMessageLoop();
82  bool enabled() { return enabled_; }
83  void PauseOnNextJavascriptStatement(const std::string& reason);
84 
85  // Initialize 'inspector' module bindings
86  static void InitInspector(v8::Local<v8::Object> target,
87  v8::Local<v8::Value> unused,
88  v8::Local<v8::Context> context,
89  void* priv);
90 
92  return io_.get();
93  }
94 
95  // Can only be called from the the main thread.
96  bool StartIoThread(bool wait_for_connect);
97 
98  // Calls StartIoThread() from off the main thread.
99  void RequestIoThreadStart();
100 
101  DebugOptions& options() { return debug_options_; }
102  void ContextCreated(v8::Local<v8::Context> context);
103 
104  private:
105  node::Environment* parent_env_;
106  std::unique_ptr<NodeInspectorClient> client_;
107  std::unique_ptr<InspectorIo> io_;
108  v8::Platform* platform_;
109  bool enabled_;
110  std::string path_;
111  DebugOptions debug_options_;
112  int next_context_number_;
113 
114  v8::Persistent<v8::Function> enable_async_hook_function_;
115  v8::Persistent<v8::Function> disable_async_hook_function_;
116 };
117 
118 } // namespace inspector
119 } // namespace node
120 
121 #endif // SRC_INSPECTOR_AGENT_H_
void FatalException(Isolate *isolate, Local< Value > error, Local< Message > message)
Definition: node.cc:2623
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536
DebugOptions & options()