ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 28 | #ifndef V8_D8_DEBUG_H_ |
| 29 | #define V8_D8_DEBUG_H_ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 30 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 31 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 56 | explicit RemoteDebugger(int port) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 57 | : port_(port), |
| 58 | event_access_(i::OS::CreateMutex()), |
| 59 | event_available_(i::OS::CreateSemaphore(0)), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 60 | head_(NULL), tail_(NULL) {} |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 61 | void Run(); |
| 62 | |
| 63 | // Handle events from the subordinate threads. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 64 | void MessageReceived(i::SmartArrayPointer<char> message); |
| 65 | void KeyboardCommand(i::SmartArrayPointer<char> command); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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. |
| 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 | |
| 93 | friend class ReceiverThread; |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | // Thread reading from debugged V8 instance. |
| 98 | class ReceiverThread: public i::Thread { |
| 99 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 100 | explicit ReceiverThread(RemoteDebugger* remote_debugger) |
| 101 | : Thread("d8:ReceiverThrd"), |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 102 | remote_debugger_(remote_debugger) {} |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 103 | ~ReceiverThread() {} |
| 104 | |
| 105 | void Run(); |
| 106 | |
| 107 | private: |
| 108 | RemoteDebugger* remote_debugger_; |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | // Thread reading keyboard input. |
| 113 | class KeyboardThread: public i::Thread { |
| 114 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 115 | explicit KeyboardThread(RemoteDebugger* remote_debugger) |
| 116 | : Thread("d8:KeyboardThrd"), |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 117 | remote_debugger_(remote_debugger) {} |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 118 | ~KeyboardThread() {} |
| 119 | |
| 120 | void Run(); |
| 121 | |
| 122 | private: |
| 123 | RemoteDebugger* remote_debugger_; |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | // Events processed by the main deubgger thread. |
| 128 | class RemoteDebuggerEvent { |
| 129 | public: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 130 | RemoteDebuggerEvent(int type, i::SmartArrayPointer<char> data) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 131 | : type_(type), data_(data), next_(NULL) { |
| 132 | ASSERT(type == kMessage || type == kKeyboard || type == kDisconnect); |
| 133 | } |
| 134 | |
| 135 | static const int kMessage = 1; |
| 136 | static const int kKeyboard = 2; |
| 137 | static const int kDisconnect = 3; |
| 138 | |
| 139 | int type() { return type_; } |
| 140 | char* data() { return *data_; } |
| 141 | |
| 142 | private: |
| 143 | void set_next(RemoteDebuggerEvent* event) { next_ = event; } |
| 144 | RemoteDebuggerEvent* next() { return next_; } |
| 145 | |
| 146 | int type_; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 147 | i::SmartArrayPointer<char> data_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 148 | RemoteDebuggerEvent* next_; |
| 149 | |
| 150 | friend class RemoteDebugger; |
| 151 | }; |
| 152 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 153 | |
| 154 | } // namespace v8 |
| 155 | |
| 156 | |
| 157 | #endif // V8_D8_DEBUG_H_ |