OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_D8_DEBUG_H_ | 5 #ifndef V8_D8_DEBUG_H_ |
6 #define V8_D8_DEBUG_H_ | 6 #define V8_D8_DEBUG_H_ |
7 | 7 |
8 | 8 |
9 #include "d8.h" | 9 #include "d8.h" |
10 #include "debug.h" | 10 #include "debug.h" |
11 #include "platform/socket.h" | |
12 | 11 |
13 | 12 |
14 namespace v8 { | 13 namespace v8 { |
15 | 14 |
16 | |
17 void HandleDebugEvent(const Debug::EventDetails& event_details); | 15 void HandleDebugEvent(const Debug::EventDetails& event_details); |
18 | 16 |
19 // Start the remove debugger connecting to a V8 debugger agent on the specified | |
20 // port. | |
21 void RunRemoteDebugger(Isolate* isolate, int port); | |
22 | |
23 // Forward declerations. | |
24 class RemoteDebuggerEvent; | |
25 class ReceiverThread; | |
26 | |
27 | |
28 // Remote debugging class. | |
29 class RemoteDebugger { | |
30 public: | |
31 explicit RemoteDebugger(Isolate* isolate, int port) | |
32 : isolate_(isolate), | |
33 port_(port), | |
34 event_available_(0), | |
35 head_(NULL), tail_(NULL) {} | |
36 void Run(); | |
37 | |
38 // Handle events from the subordinate threads. | |
39 void MessageReceived(i::SmartArrayPointer<char> message); | |
40 void KeyboardCommand(i::SmartArrayPointer<char> command); | |
41 void ConnectionClosed(); | |
42 | |
43 private: | |
44 // Add new debugger event to the list. | |
45 void AddEvent(RemoteDebuggerEvent* event); | |
46 // Read next debugger event from the list. | |
47 RemoteDebuggerEvent* GetEvent(); | |
48 | |
49 // Handle a message from the debugged V8. | |
50 void HandleMessageReceived(char* message); | |
51 // Handle a keyboard command. | |
52 void HandleKeyboardCommand(char* command); | |
53 | |
54 // Get connection to agent in debugged V8. | |
55 i::Socket* conn() { return conn_; } | |
56 | |
57 Isolate* isolate_; | |
58 int port_; // Port used to connect to debugger V8. | |
59 i::Socket* conn_; // Connection to debugger agent in debugged V8. | |
60 | |
61 // Linked list of events from debugged V8 and from keyboard input. Access to | |
62 // the list is guarded by a mutex and a semaphore signals new items in the | |
63 // list. | |
64 i::Mutex event_access_; | |
65 i::Semaphore event_available_; | |
66 RemoteDebuggerEvent* head_; | |
67 RemoteDebuggerEvent* tail_; | |
68 | |
69 friend class ReceiverThread; | |
70 }; | |
71 | |
72 | |
73 // Thread reading from debugged V8 instance. | |
74 class ReceiverThread: public i::Thread { | |
75 public: | |
76 explicit ReceiverThread(RemoteDebugger* remote_debugger) | |
77 : Thread("d8:ReceiverThrd"), | |
78 remote_debugger_(remote_debugger) {} | |
79 ~ReceiverThread() {} | |
80 | |
81 void Run(); | |
82 | |
83 private: | |
84 RemoteDebugger* remote_debugger_; | |
85 }; | |
86 | |
87 | |
88 // Thread reading keyboard input. | |
89 class KeyboardThread: public i::Thread { | |
90 public: | |
91 explicit KeyboardThread(RemoteDebugger* remote_debugger) | |
92 : Thread("d8:KeyboardThrd"), | |
93 remote_debugger_(remote_debugger) {} | |
94 ~KeyboardThread() {} | |
95 | |
96 void Run(); | |
97 | |
98 private: | |
99 RemoteDebugger* remote_debugger_; | |
100 }; | |
101 | |
102 | |
103 // Events processed by the main deubgger thread. | |
104 class RemoteDebuggerEvent { | |
105 public: | |
106 RemoteDebuggerEvent(int type, i::SmartArrayPointer<char> data) | |
107 : type_(type), data_(data), next_(NULL) { | |
108 ASSERT(type == kMessage || type == kKeyboard || type == kDisconnect); | |
109 } | |
110 | |
111 static const int kMessage = 1; | |
112 static const int kKeyboard = 2; | |
113 static const int kDisconnect = 3; | |
114 | |
115 int type() { return type_; } | |
116 char* data() { return data_.get(); } | |
117 | |
118 private: | |
119 void set_next(RemoteDebuggerEvent* event) { next_ = event; } | |
120 RemoteDebuggerEvent* next() { return next_; } | |
121 | |
122 int type_; | |
123 i::SmartArrayPointer<char> data_; | |
124 RemoteDebuggerEvent* next_; | |
125 | |
126 friend class RemoteDebugger; | |
127 }; | |
128 | |
129 | |
130 } // namespace v8 | 17 } // namespace v8 |
131 | 18 |
132 | 19 |
133 #endif // V8_D8_DEBUG_H_ | 20 #endif // V8_D8_DEBUG_H_ |
OLD | NEW |