GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/debug-agent.h Lines: 10 11 90.9 %
Date: 2016-12-18 Branches: 3 6 50.0 %

Line Branch Exec Source
1
// Copyright Fedor Indutny and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_DEBUG_AGENT_H_
23
#define SRC_DEBUG_AGENT_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "node_mutex.h"
28
#include "node_debug_options.h"
29
#include "util.h"
30
#include "util-inl.h"
31
#include "uv.h"
32
#include "v8.h"
33
#include "v8-debug.h"
34
35
#include <string.h>
36
#include <string>
37
38
// Forward declaration to break recursive dependency chain with src/env.h.
39
namespace node {
40
class Environment;
41
}  // namespace node
42
43
namespace node {
44
namespace debugger {
45
46
class AgentMessage {
47
 public:
48
10
  AgentMessage(uint16_t* val, int length) : length_(length) {
49
5
    if (val == nullptr) {
50
      data_ = val;
51
    } else {
52
5
      data_ = new uint16_t[length];
53
5
      memcpy(data_, val, length * sizeof(*data_));
54
    }
55
5
  }
56
57
15
  ~AgentMessage() {
58
5
    delete[] data_;
59
5
    data_ = nullptr;
60
5
  }
61
62
  inline const uint16_t* data() const { return data_; }
63
  inline int length() const { return length_; }
64
65
  ListNode<AgentMessage> member;
66
67
 private:
68
  uint16_t* data_;
69
  int length_;
70
};
71
72
class Agent {
73
 public:
74
  explicit Agent(node::Environment* env);
75
  ~Agent();
76
77
  typedef void (*DispatchHandler)(node::Environment* env);
78
79
  // Start the debugger agent thread
80
  bool Start(const DebugOptions& options);
81
  // Listen for debug events
82
  void Enable();
83
  // Stop the debugger agent
84
  void Stop();
85
86
  inline void set_dispatch_handler(DispatchHandler handler) {
87
6
    dispatch_handler_ = handler;
88
  }
89
90
  inline node::Environment* parent_env() const { return parent_env_; }
91
  inline node::Environment* child_env() const { return child_env_; }
92
93
 protected:
94
  void InitAdaptor(Environment* env);
95
96
  // Worker body
97
  void WorkerRun();
98
99
  static void ThreadCb(Agent* agent);
100
  static void ParentSignalCb(uv_async_t* signal);
101
  static void ChildSignalCb(uv_async_t* signal);
102
  static void MessageHandler(const v8::Debug::Message& message);
103
104
  // V8 API
105
  static Agent* Unwrap(const v8::FunctionCallbackInfo<v8::Value>& args);
106
  static void NotifyListen(const v8::FunctionCallbackInfo<v8::Value>& args);
107
  static void NotifyWait(const v8::FunctionCallbackInfo<v8::Value>& args);
108
  static void SendCommand(const v8::FunctionCallbackInfo<v8::Value>& args);
109
110
  void EnqueueMessage(AgentMessage* message);
111
112
  enum State {
113
    kNone,
114
    kRunning
115
  };
116
117
  State state_;
118
  DebugOptions options_;
119
120
  bool wait_;
121
122
  uv_sem_t start_sem_;
123
  node::Mutex message_mutex_;
124
  uv_async_t child_signal_;
125
126
  uv_thread_t thread_;
127
  node::Environment* parent_env_;
128
  node::Environment* child_env_;
129
  uv_loop_t child_loop_;
130
  v8::Persistent<v8::Object> api_;
131
132
  ListHead<AgentMessage, &AgentMessage::member> messages_;
133
134
  DispatchHandler dispatch_handler_;
135
};
136
137
}  // namespace debugger
138
}  // namespace node
139
140
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
141
142
#endif  // SRC_DEBUG_AGENT_H_