blob: 08f13724edcdb6e6ba62095803b5b8f31c91503e [file] [log] [blame]
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_V8_DEBUG_AGENT_H_
29#define V8_V8_DEBUG_AGENT_H_
30
ager@chromium.org65dad4b2009-04-23 08:48:43 +000031#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000032#include "../include/v8-debug.h"
33#include "platform.h"
34
35namespace v8 { namespace internal {
36
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000037// Forward decelrations.
38class DebuggerAgentSession;
39
40
41// Debugger agent which starts a socket listener on the debugger port and
42// handles connection from a remote debugger.
43class DebuggerAgent: public Thread {
44 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000045 explicit DebuggerAgent(const char* name, int port)
46 : name_(StrDup(name)), port_(port),
47 server_(OS::CreateSocket()), terminate_(false),
48 session_access_(OS::CreateMutex()), session_(NULL),
ager@chromium.org65dad4b2009-04-23 08:48:43 +000049 terminate_now_(OS::CreateSemaphore(0)) {
50 ASSERT(instance_ == NULL);
51 instance_ = this;
52 }
53 ~DebuggerAgent() {
54 instance_ = NULL;
55 delete server_;
56 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000057
58 void Shutdown();
59
60 private:
61 void Run();
62 void CreateSession(Socket* socket);
63 void DebuggerMessage(const uint16_t* message, int length);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000064 void CloseSession();
65 void OnSessionClosed(DebuggerAgentSession* session);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000066
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000067 SmartPointer<const char> name_; // Name of the embedding application.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000068 int port_; // Port to use for the agent.
69 Socket* server_; // Server socket for listen/accept.
70 bool terminate_; // Termination flag.
71 Mutex* session_access_; // Mutex guarging access to session_.
72 DebuggerAgentSession* session_; // Current active session if any.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000073 Semaphore* terminate_now_; // Semaphore to signal termination.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000074
ager@chromium.org65dad4b2009-04-23 08:48:43 +000075 static DebuggerAgent* instance_;
76
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000077 friend class DebuggerAgentSession;
78 friend void DebuggerAgentMessageHandler(const uint16_t* message, int length,
ager@chromium.org65dad4b2009-04-23 08:48:43 +000079 v8::Debug::ClientData* client_data);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000080
81 DISALLOW_COPY_AND_ASSIGN(DebuggerAgent);
82};
83
84
85// Debugger agent session. The session receives requests from the remote
86// debugger and sends debugger events/responses to the remote debugger.
87class DebuggerAgentSession: public Thread {
88 public:
89 DebuggerAgentSession(DebuggerAgent* agent, Socket* client)
90 : agent_(agent), client_(client) {}
91
92 void DebuggerMessage(Vector<uint16_t> message);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000093 void Shutdown();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000094
95 private:
96 void Run();
97
98 void DebuggerMessage(Vector<char> message);
99
100 DebuggerAgent* agent_;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000101 Socket* client_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000102
103 DISALLOW_COPY_AND_ASSIGN(DebuggerAgentSession);
104};
105
106
107// Utility methods factored out to be used by the D8 shell as well.
108class DebuggerAgentUtil {
109 public:
110 static const char* kContentLength;
111 static int kContentLengthSize;
112
113 static SmartPointer<char> ReceiveMessage(const Socket* conn);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000114 static bool SendConnectMessage(const Socket* conn,
115 const char* embedding_host);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000116 static bool SendMessage(const Socket* conn, const Vector<uint16_t> message);
117 static bool SendMessage(const Socket* conn,
118 const v8::Handle<v8::String> message);
119 static int ReceiveAll(const Socket* conn, char* data, int len);
120};
121
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000122} } // namespace v8::internal
123
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000124#endif // ENABLE_DEBUGGER_SUPPORT
125
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000126#endif // V8_V8_DEBUG_AGENT_H_