kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1 | // 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.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 31 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 32 | #include "../include/v8-debug.h" |
| 33 | #include "platform.h" |
| 34 | |
| 35 | namespace v8 { namespace internal { |
| 36 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 37 | // Forward decelrations. |
| 38 | class DebuggerAgentSession; |
| 39 | |
| 40 | |
| 41 | // Debugger agent which starts a socket listener on the debugger port and |
| 42 | // handles connection from a remote debugger. |
| 43 | class DebuggerAgent: public Thread { |
| 44 | public: |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 45 | 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.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 49 | terminate_now_(OS::CreateSemaphore(0)) { |
| 50 | ASSERT(instance_ == NULL); |
| 51 | instance_ = this; |
| 52 | } |
| 53 | ~DebuggerAgent() { |
| 54 | instance_ = NULL; |
| 55 | delete server_; |
| 56 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 57 | |
| 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.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 64 | void CloseSession(); |
| 65 | void OnSessionClosed(DebuggerAgentSession* session); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 66 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 67 | SmartPointer<const char> name_; // Name of the embedding application. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 68 | 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.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 73 | Semaphore* terminate_now_; // Semaphore to signal termination. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 74 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 75 | static DebuggerAgent* instance_; |
| 76 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 77 | friend class DebuggerAgentSession; |
| 78 | friend void DebuggerAgentMessageHandler(const uint16_t* message, int length, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 79 | v8::Debug::ClientData* client_data); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 80 | |
| 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. |
| 87 | class 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.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 93 | void Shutdown(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | void Run(); |
| 97 | |
| 98 | void DebuggerMessage(Vector<char> message); |
| 99 | |
| 100 | DebuggerAgent* agent_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 101 | Socket* client_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(DebuggerAgentSession); |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | // Utility methods factored out to be used by the D8 shell as well. |
| 108 | class DebuggerAgentUtil { |
| 109 | public: |
| 110 | static const char* kContentLength; |
| 111 | static int kContentLengthSize; |
| 112 | |
| 113 | static SmartPointer<char> ReceiveMessage(const Socket* conn); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 114 | static bool SendConnectMessage(const Socket* conn, |
| 115 | const char* embedding_host); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 116 | 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.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 122 | } } // namespace v8::internal |
| 123 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 124 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 125 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 126 | #endif // V8_V8_DEBUG_AGENT_H_ |