summaryrefslogtreecommitdiff
path: root/deps/v8/src/debug-agent.h
blob: 3e3f25a5ddd37f54e635aa185a3e3e926a2a6379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2006-2008 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_DEBUG_AGENT_H_
#define V8_DEBUG_AGENT_H_

#include "../include/v8-debug.h"
#include "platform.h"

namespace v8 {
namespace internal {

// Forward decelrations.
class DebuggerAgentSession;
class Socket;


// Debugger agent which starts a socket listener on the debugger port and
// handles connection from a remote debugger.
class DebuggerAgent: public Thread {
 public:
  DebuggerAgent(Isolate* isolate, const char* name, int port);
  ~DebuggerAgent();

  void Shutdown();
  void WaitUntilListening();

  Isolate* isolate() { return isolate_; }

 private:
  void Run();
  void CreateSession(Socket* socket);
  void DebuggerMessage(const v8::Debug::Message& message);
  void CloseSession();
  void OnSessionClosed(DebuggerAgentSession* session);

  Isolate* isolate_;
  SmartArrayPointer<const char> name_;  // Name of the embedding application.
  int port_;  // Port to use for the agent.
  Socket* server_;  // Server socket for listen/accept.
  bool terminate_;  // Termination flag.
  RecursiveMutex session_access_;  // Mutex guarding access to session_.
  DebuggerAgentSession* session_;  // Current active session if any.
  Semaphore terminate_now_;  // Semaphore to signal termination.
  Semaphore listening_;

  friend class DebuggerAgentSession;
  friend void DebuggerAgentMessageHandler(const v8::Debug::Message& message);

  DISALLOW_COPY_AND_ASSIGN(DebuggerAgent);
};


// Debugger agent session. The session receives requests from the remote
// debugger and sends debugger events/responses to the remote debugger.
class DebuggerAgentSession: public Thread {
 public:
  DebuggerAgentSession(DebuggerAgent* agent, Socket* client)
      : Thread("v8:DbgAgntSessn"),
        agent_(agent), client_(client) {}
  ~DebuggerAgentSession();

  void DebuggerMessage(Vector<uint16_t> message);
  void Shutdown();

 private:
  void Run();

  void DebuggerMessage(Vector<char> message);

  DebuggerAgent* agent_;
  Socket* client_;

  DISALLOW_COPY_AND_ASSIGN(DebuggerAgentSession);
};


// Utility methods factored out to be used by the D8 shell as well.
class DebuggerAgentUtil {
 public:
  static const char* const kContentLength;

  static SmartArrayPointer<char> ReceiveMessage(Socket* conn);
  static bool SendConnectMessage(Socket* conn, const char* embedding_host);
  static bool SendMessage(Socket* conn, const Vector<uint16_t> message);
  static bool SendMessage(Socket* conn, const v8::Handle<v8::String> message);
  static int ReceiveAll(Socket* conn, char* data, int len);
};

} }  // namespace v8::internal

#endif  // V8_DEBUG_AGENT_H_