1 // Copyright 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_D8_DEBUG_H_ 29 #define V8_D8_DEBUG_H_ 30 31 32 #include "d8.h" 33 #include "debug.h" 34 35 36 namespace v8 { 37 38 39 void HandleDebugEvent(DebugEvent event, 40 Handle<Object> exec_state, 41 Handle<Object> event_data, 42 Handle<Value> data); 43 44 // Start the remove debugger connecting to a V8 debugger agent on the specified 45 // port. 46 void RunRemoteDebugger(int port); 47 48 // Forward declerations. 49 class RemoteDebuggerEvent; 50 class ReceiverThread; 51 52 53 // Remote debugging class. 54 class RemoteDebugger { 55 public: RemoteDebugger(i::Isolate * isolate,int port)56 RemoteDebugger(i::Isolate* isolate, int port) 57 : port_(port), 58 event_access_(i::OS::CreateMutex()), 59 event_available_(i::OS::CreateSemaphore(0)), 60 head_(NULL), tail_(NULL), isolate_(isolate) {} 61 void Run(); 62 63 // Handle events from the subordinate threads. 64 void MessageReceived(i::SmartPointer<char> message); 65 void KeyboardCommand(i::SmartPointer<char> command); 66 void ConnectionClosed(); 67 68 private: 69 // Add new debugger event to the list. 70 void AddEvent(RemoteDebuggerEvent* event); 71 // Read next debugger event from the list. 72 RemoteDebuggerEvent* GetEvent(); 73 74 // Handle a message from the debugged V8. 75 void HandleMessageReceived(char* message); 76 // Handle a keyboard command. 77 void HandleKeyboardCommand(char* command); 78 79 // Get connection to agent in debugged V8. conn()80 i::Socket* conn() { return conn_; } 81 82 int port_; // Port used to connect to debugger V8. 83 i::Socket* conn_; // Connection to debugger agent in debugged V8. 84 85 // Linked list of events from debugged V8 and from keyboard input. Access to 86 // the list is guarded by a mutex and a semaphore signals new items in the 87 // list. 88 i::Mutex* event_access_; 89 i::Semaphore* event_available_; 90 RemoteDebuggerEvent* head_; 91 RemoteDebuggerEvent* tail_; 92 i::Isolate* isolate_; 93 94 friend class ReceiverThread; 95 }; 96 97 98 // Thread reading from debugged V8 instance. 99 class ReceiverThread: public i::Thread { 100 public: ReceiverThread(i::Isolate * isolate,RemoteDebugger * remote_debugger)101 ReceiverThread(i::Isolate* isolate, RemoteDebugger* remote_debugger) 102 : Thread(isolate, "d8:ReceiverThrd"), 103 remote_debugger_(remote_debugger) {} ~ReceiverThread()104 ~ReceiverThread() {} 105 106 void Run(); 107 108 private: 109 RemoteDebugger* remote_debugger_; 110 }; 111 112 113 // Thread reading keyboard input. 114 class KeyboardThread: public i::Thread { 115 public: KeyboardThread(i::Isolate * isolate,RemoteDebugger * remote_debugger)116 explicit KeyboardThread(i::Isolate* isolate, RemoteDebugger* remote_debugger) 117 : Thread(isolate, "d8:KeyboardThrd"), 118 remote_debugger_(remote_debugger) {} ~KeyboardThread()119 ~KeyboardThread() {} 120 121 void Run(); 122 123 private: 124 RemoteDebugger* remote_debugger_; 125 }; 126 127 128 // Events processed by the main deubgger thread. 129 class RemoteDebuggerEvent { 130 public: RemoteDebuggerEvent(int type,i::SmartPointer<char> data)131 RemoteDebuggerEvent(int type, i::SmartPointer<char> data) 132 : type_(type), data_(data), next_(NULL) { 133 ASSERT(type == kMessage || type == kKeyboard || type == kDisconnect); 134 } 135 136 static const int kMessage = 1; 137 static const int kKeyboard = 2; 138 static const int kDisconnect = 3; 139 type()140 int type() { return type_; } data()141 char* data() { return *data_; } 142 143 private: set_next(RemoteDebuggerEvent * event)144 void set_next(RemoteDebuggerEvent* event) { next_ = event; } next()145 RemoteDebuggerEvent* next() { return next_; } 146 147 int type_; 148 i::SmartPointer<char> data_; 149 RemoteDebuggerEvent* next_; 150 151 friend class RemoteDebugger; 152 }; 153 154 155 } // namespace v8 156 157 158 #endif // V8_D8_DEBUG_H_ 159