blob: 55876229a320d2bded2ea9f65c55713f4993fe2e [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// 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.org32912102009-01-16 10:38:43 +000028#ifndef V8_D8_DEBUG_H_
29#define V8_D8_DEBUG_H_
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000030
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000031
ager@chromium.org32912102009-01-16 10:38:43 +000032#include "d8.h"
33#include "debug.h"
34
35
36namespace v8 {
37
38
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +000039void HandleDebugEvent(const Debug::EventDetails& event_details);
ager@chromium.org32912102009-01-16 10:38:43 +000040
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000041// Start the remove debugger connecting to a V8 debugger agent on the specified
42// port.
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000043void RunRemoteDebugger(Isolate* isolate, int port);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000044
45// Forward declerations.
46class RemoteDebuggerEvent;
47class ReceiverThread;
48
49
50// Remote debugging class.
51class RemoteDebugger {
52 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000053 explicit RemoteDebugger(Isolate* isolate, int port)
54 : isolate_(isolate),
55 port_(port),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +000056 event_available_(0),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000057 head_(NULL), tail_(NULL) {}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000058 void Run();
59
60 // Handle events from the subordinate threads.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +000061 void MessageReceived(i::SmartArrayPointer<char> message);
62 void KeyboardCommand(i::SmartArrayPointer<char> command);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000063 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.org2bda5432013-03-15 12:39:50 +000079 Isolate* isolate_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000080 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.orgdc94e192013-08-30 11:35:42 +000086 i::Mutex event_access_;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +000087 i::Semaphore event_available_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000088 RemoteDebuggerEvent* head_;
89 RemoteDebuggerEvent* tail_;
90
91 friend class ReceiverThread;
92};
93
94
95// Thread reading from debugged V8 instance.
96class ReceiverThread: public i::Thread {
97 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000098 explicit ReceiverThread(RemoteDebugger* remote_debugger)
99 : Thread("d8:ReceiverThrd"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000100 remote_debugger_(remote_debugger) {}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000101 ~ReceiverThread() {}
102
103 void Run();
104
105 private:
106 RemoteDebugger* remote_debugger_;
107};
108
109
110// Thread reading keyboard input.
111class KeyboardThread: public i::Thread {
112 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000113 explicit KeyboardThread(RemoteDebugger* remote_debugger)
114 : Thread("d8:KeyboardThrd"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000115 remote_debugger_(remote_debugger) {}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000116 ~KeyboardThread() {}
117
118 void Run();
119
120 private:
121 RemoteDebugger* remote_debugger_;
122};
123
124
125// Events processed by the main deubgger thread.
126class RemoteDebuggerEvent {
127 public:
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000128 RemoteDebuggerEvent(int type, i::SmartArrayPointer<char> data)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129 : 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.org83e16822011-09-13 08:21:47 +0000145 i::SmartArrayPointer<char> data_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000146 RemoteDebuggerEvent* next_;
147
148 friend class RemoteDebugger;
149};
150
ager@chromium.org32912102009-01-16 10:38:43 +0000151
152} // namespace v8
153
154
155#endif // V8_D8_DEBUG_H_