1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_DEBUG_AGENT_H_
6 #define V8_DEBUG_AGENT_H_
8 #include "../include/v8-debug.h"
14 // Forward decelrations.
15 class DebuggerAgentSession;
19 // Debugger agent which starts a socket listener on the debugger port and
20 // handles connection from a remote debugger.
21 class DebuggerAgent: public Thread {
23 DebuggerAgent(Isolate* isolate, const char* name, int port);
27 void WaitUntilListening();
29 Isolate* isolate() { return isolate_; }
33 void CreateSession(Socket* socket);
34 void DebuggerMessage(const v8::Debug::Message& message);
36 void OnSessionClosed(DebuggerAgentSession* session);
39 SmartArrayPointer<const char> name_; // Name of the embedding application.
40 int port_; // Port to use for the agent.
41 Socket* server_; // Server socket for listen/accept.
42 bool terminate_; // Termination flag.
43 RecursiveMutex session_access_; // Mutex guarding access to session_.
44 DebuggerAgentSession* session_; // Current active session if any.
45 Semaphore terminate_now_; // Semaphore to signal termination.
48 friend class DebuggerAgentSession;
49 friend void DebuggerAgentMessageHandler(const v8::Debug::Message& message);
51 DISALLOW_COPY_AND_ASSIGN(DebuggerAgent);
55 // Debugger agent session. The session receives requests from the remote
56 // debugger and sends debugger events/responses to the remote debugger.
57 class DebuggerAgentSession: public Thread {
59 DebuggerAgentSession(DebuggerAgent* agent, Socket* client)
60 : Thread("v8:DbgAgntSessn"),
61 agent_(agent), client_(client) {}
62 ~DebuggerAgentSession();
64 void DebuggerMessage(Vector<uint16_t> message);
70 void DebuggerMessage(Vector<char> message);
72 DebuggerAgent* agent_;
75 DISALLOW_COPY_AND_ASSIGN(DebuggerAgentSession);
79 // Utility methods factored out to be used by the D8 shell as well.
80 class DebuggerAgentUtil {
82 static const char* const kContentLength;
84 static SmartArrayPointer<char> ReceiveMessage(Socket* conn);
85 static bool SendConnectMessage(Socket* conn, const char* embedding_host);
86 static bool SendMessage(Socket* conn, const Vector<uint16_t> message);
87 static bool SendMessage(Socket* conn, const v8::Handle<v8::String> message);
88 static int ReceiveAll(Socket* conn, char* data, int len);
91 } } // namespace v8::internal
93 #endif // V8_DEBUG_AGENT_H_