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 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 39 | void HandleDebugEvent(const Debug::EventDetails& event_details); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 40 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 41 | // Start the remove debugger connecting to a V8 debugger agent on the specified |
| 42 | // port. |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 43 | void RunRemoteDebugger(Isolate* isolate, int port); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 44 | |
| 45 | // Forward declerations. |
| 46 | class RemoteDebuggerEvent; |
| 47 | class ReceiverThread; |
| 48 | |
| 49 | |
| 50 | // Remote debugging class. |
| 51 | class RemoteDebugger { |
| 52 | public: |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 53 | explicit RemoteDebugger(Isolate* isolate, int port) |
| 54 | : isolate_(isolate), |
| 55 | port_(port), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 56 | event_available_(i::OS::CreateSemaphore(0)), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 57 | head_(NULL), tail_(NULL) {} |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 58 | void Run(); |
| 59 | |
| 60 | // Handle events from the subordinate threads. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 61 | void MessageReceived(i::SmartArrayPointer<char> message); |
| 62 | void KeyboardCommand(i::SmartArrayPointer<char> command); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 63 | void ConnectionClosed(); |
| 64 | |
| 65 | private: |
| 66 | // Add new debugger event to the list. |
| 67 | void AddEvent(RemoteDebuggerEvent* event); |
| 68 | // Read next debugger event from the list. |
| 69 | RemoteDebuggerEvent* GetEvent(); |
| 70 | |
| 71 | // Handle a message from the debugged V8. |
| 72 | void HandleMessageReceived(char* message); |
| 73 | // Handle a keyboard command. |
| 74 | void HandleKeyboardCommand(char* command); |
| 75 | |
| 76 | // Get connection to agent in debugged V8. |
| 77 | i::Socket* conn() { return conn_; } |
| 78 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 79 | Isolate* isolate_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 80 | int port_; // Port used to connect to debugger V8. |
| 81 | i::Socket* conn_; // Connection to debugger agent in debugged V8. |
| 82 | |
| 83 | // Linked list of events from debugged V8 and from keyboard input. Access to |
| 84 | // the list is guarded by a mutex and a semaphore signals new items in the |
| 85 | // list. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 86 | i::Mutex event_access_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 87 | i::Semaphore* event_available_; |
| 88 | RemoteDebuggerEvent* head_; |
| 89 | RemoteDebuggerEvent* tail_; |
| 90 | |
| 91 | friend class ReceiverThread; |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | // Thread reading from debugged V8 instance. |
| 96 | class ReceiverThread: public i::Thread { |
| 97 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 98 | explicit ReceiverThread(RemoteDebugger* remote_debugger) |
| 99 | : Thread("d8:ReceiverThrd"), |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 100 | remote_debugger_(remote_debugger) {} |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 101 | ~ReceiverThread() {} |
| 102 | |
| 103 | void Run(); |
| 104 | |
| 105 | private: |
| 106 | RemoteDebugger* remote_debugger_; |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | // Thread reading keyboard input. |
| 111 | class KeyboardThread: public i::Thread { |
| 112 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 113 | explicit KeyboardThread(RemoteDebugger* remote_debugger) |
| 114 | : Thread("d8:KeyboardThrd"), |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 115 | remote_debugger_(remote_debugger) {} |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 116 | ~KeyboardThread() {} |
| 117 | |
| 118 | void Run(); |
| 119 | |
| 120 | private: |
| 121 | RemoteDebugger* remote_debugger_; |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | // Events processed by the main deubgger thread. |
| 126 | class RemoteDebuggerEvent { |
| 127 | public: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 128 | RemoteDebuggerEvent(int type, i::SmartArrayPointer<char> data) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 129 | : type_(type), data_(data), next_(NULL) { |
| 130 | ASSERT(type == kMessage || type == kKeyboard || type == kDisconnect); |
| 131 | } |
| 132 | |
| 133 | static const int kMessage = 1; |
| 134 | static const int kKeyboard = 2; |
| 135 | static const int kDisconnect = 3; |
| 136 | |
| 137 | int type() { return type_; } |
| 138 | char* data() { return *data_; } |
| 139 | |
| 140 | private: |
| 141 | void set_next(RemoteDebuggerEvent* event) { next_ = event; } |
| 142 | RemoteDebuggerEvent* next() { return next_; } |
| 143 | |
| 144 | int type_; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 145 | i::SmartArrayPointer<char> data_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 146 | RemoteDebuggerEvent* next_; |
| 147 | |
| 148 | friend class RemoteDebugger; |
| 149 | }; |
| 150 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 151 | |
| 152 | } // namespace v8 |
| 153 | |
| 154 | |
| 155 | #endif // V8_D8_DEBUG_H_ |