node/deps/v8/src/debug-agent.h

134 lines
4.7 KiB
C
Raw Normal View History

// Copyright 2006-2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2009-05-12 06:12:56 +08:00
#ifndef V8_DEBUG_AGENT_H_
#define V8_DEBUG_AGENT_H_
2009-05-12 06:12:56 +08:00
#ifdef ENABLE_DEBUGGER_SUPPORT
#include "../include/v8-debug.h"
#include "platform.h"
2009-06-09 00:34:06 +08:00
namespace v8 {
namespace internal {
// Forward decelrations.
class DebuggerAgentSession;
// Debugger agent which starts a socket listener on the debugger port and
// handles connection from a remote debugger.
class DebuggerAgent: public Thread {
public:
2011-07-09 07:40:11 +08:00
DebuggerAgent(const char* name, int port)
2011-01-18 03:32:56 +08:00
: Thread(name),
2011-07-09 07:40:11 +08:00
isolate_(Isolate::Current()),
2011-01-18 03:32:56 +08:00
name_(StrDup(name)), port_(port),
server_(OS::CreateSocket()), terminate_(false),
session_access_(OS::CreateMutex()), session_(NULL),
2009-10-07 17:53:03 +08:00
terminate_now_(OS::CreateSemaphore(0)),
listening_(OS::CreateSemaphore(0)) {
2011-07-09 07:40:11 +08:00
ASSERT(isolate_->debugger_agent_instance() == NULL);
isolate_->set_debugger_agent_instance(this);
2009-05-12 06:12:56 +08:00
}
~DebuggerAgent() {
2011-07-09 07:40:11 +08:00
isolate_->set_debugger_agent_instance(NULL);
2009-05-12 06:12:56 +08:00
delete server_;
}
void Shutdown();
2009-10-07 17:53:03 +08:00
void WaitUntilListening();
2011-07-09 07:40:11 +08:00
Isolate* isolate() { return isolate_; }
private:
void Run();
void CreateSession(Socket* socket);
2009-05-12 06:12:56 +08:00
void DebuggerMessage(const v8::Debug::Message& message);
void CloseSession();
void OnSessionClosed(DebuggerAgentSession* session);
2011-07-09 07:40:11 +08:00
Isolate* isolate_;
2011-09-16 00:41:08 +08:00
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.
Mutex* session_access_; // Mutex guarging access to session_.
DebuggerAgentSession* session_; // Current active session if any.
Semaphore* terminate_now_; // Semaphore to signal termination.
2009-10-07 17:53:03 +08:00
Semaphore* listening_;
friend class DebuggerAgentSession;
2009-05-12 06:12:56 +08:00
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)
2011-01-18 03:32:56 +08:00
: Thread("v8:DbgAgntSessn"),
agent_(agent), client_(client) {}
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:
2011-07-09 07:40:11 +08:00
static const char* const kContentLength;
static const int kContentLengthSize;
2011-09-16 00:41:08 +08:00
static SmartArrayPointer<char> ReceiveMessage(const Socket* conn);
static bool SendConnectMessage(const Socket* conn,
const char* embedding_host);
static bool SendMessage(const Socket* conn, const Vector<uint16_t> message);
static bool SendMessage(const Socket* conn,
const v8::Handle<v8::String> message);
static int ReceiveAll(const Socket* conn, char* data, int len);
};
} } // namespace v8::internal
2009-05-12 06:12:56 +08:00
#endif // ENABLE_DEBUGGER_SUPPORT
#endif // V8_DEBUG_AGENT_H_