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

Line 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 "util.h"
29
#include "util-inl.h"
30
#include "uv.h"
31
#include "v8.h"
32
#include "v8-debug.h"
33
34
#include <string.h>
35
#include <string>
36
37
// Forward declaration to break recursive dependency chain with src/env.h.
38
namespace node {
39
class Environment;
40
}  // namespace node
41
42
namespace node {
43
namespace debugger {
44
45
class AgentMessage {
46
 public:
47
10
  AgentMessage(uint16_t* val, int length) : length_(length) {
48
5
    if (val == nullptr) {
49
      data_ = val;
50
    } else {
51
5
      data_ = new uint16_t[length];
52
5
      memcpy(data_, val, length * sizeof(*data_));
53
    }
54
5
  }
55
56
15
  ~AgentMessage() {
57
5
    delete[] data_;
58
5
    data_ = nullptr;
59
5
  }
60
61
  inline const uint16_t* data() const { return data_; }
62
  inline int length() const { return length_; }
63
64
  ListNode<AgentMessage> member;
65
66
 private:
67
  uint16_t* data_;
68
  int length_;
69
};
70
71
class Agent {
72
 public:
73
  explicit Agent(node::Environment* env);
74
  ~Agent();
75
76
  typedef void (*DispatchHandler)(node::Environment* env);
77
78
  // Start the debugger agent thread
79
  bool Start(const std::string& host, int port, bool wait);
80
  // Listen for debug events
81
  void Enable();
82
  // Stop the debugger agent
83
  void Stop();
84
85
  inline void set_dispatch_handler(DispatchHandler handler) {
86
6
    dispatch_handler_ = handler;
87
  }
88
89
  inline node::Environment* parent_env() const { return parent_env_; }
90
  inline node::Environment* child_env() const { return child_env_; }
91
92
 protected:
93
  void InitAdaptor(Environment* env);
94
95
  // Worker body
96
  void WorkerRun();
97
98
  static void ThreadCb(Agent* agent);
99
  static void ParentSignalCb(uv_async_t* signal);
100
  static void ChildSignalCb(uv_async_t* signal);
101
  static void MessageHandler(const v8::Debug::Message& message);
102
103
  // V8 API
104
  static Agent* Unwrap(const v8::FunctionCallbackInfo<v8::Value>& args);
105
  static void NotifyListen(const v8::FunctionCallbackInfo<v8::Value>& args);
106
  static void NotifyWait(const v8::FunctionCallbackInfo<v8::Value>& args);
107
  static void SendCommand(const v8::FunctionCallbackInfo<v8::Value>& args);
108
109
  void EnqueueMessage(AgentMessage* message);
110
111
  enum State {
112
    kNone,
113
    kRunning
114
  };
115
116
  State state_;
117
118
  std::string host_;
119
  int port_;
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_