blob: 6bc5e27f853f7c348fe910f68af6d28469f8b4c6 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include <errno.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070018#include <stdlib.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070019#include <sys/time.h>
20#include <time.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include <unistd.h>
22
23#include "atomic.h"
24#include "base/logging.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "debugger.h"
27#include "jdwp/jdwp_priv.h"
28#include "scoped_thread_state_change.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070029
30namespace art {
31
32namespace JDWP {
33
Elliott Hughes376a7a02011-10-24 18:35:55 -070034static void* StartJdwpThread(void* arg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070035
36/*
37 * JdwpNetStateBase class implementation
38 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070039JdwpNetStateBase::JdwpNetStateBase(JdwpState* state)
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070040 : state_(state), socket_lock_("JdwpNetStateBase lock", kJdwpSocketLock) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070041 clientSock = -1;
Elliott Hughes5d10a872013-04-17 19:26:43 -070042 wake_pipe_[0] = -1;
43 wake_pipe_[1] = -1;
44 input_count_ = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070045 awaiting_handshake_ = false;
Elliott Hughescb693062013-02-21 09:48:08 -080046}
47
Elliott Hughes5d10a872013-04-17 19:26:43 -070048JdwpNetStateBase::~JdwpNetStateBase() {
49 if (wake_pipe_[0] != -1) {
50 close(wake_pipe_[0]);
51 wake_pipe_[0] = -1;
52 }
53 if (wake_pipe_[1] != -1) {
54 close(wake_pipe_[1]);
55 wake_pipe_[1] = -1;
56 }
57}
58
59bool JdwpNetStateBase::MakePipe() {
60 if (pipe(wake_pipe_) == -1) {
61 PLOG(ERROR) << "pipe failed";
62 return false;
63 }
64 return true;
65}
66
67void JdwpNetStateBase::WakePipe() {
68 // If we might be sitting in select, kick us loose.
69 if (wake_pipe_[1] != -1) {
70 VLOG(jdwp) << "+++ writing to wake pipe";
71 TEMP_FAILURE_RETRY(write(wake_pipe_[1], "", 1));
72 }
73}
74
Elliott Hughescb693062013-02-21 09:48:08 -080075void JdwpNetStateBase::ConsumeBytes(size_t count) {
76 CHECK_GT(count, 0U);
Elliott Hughes5d10a872013-04-17 19:26:43 -070077 CHECK_LE(count, input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -080078
Elliott Hughes5d10a872013-04-17 19:26:43 -070079 if (count == input_count_) {
80 input_count_ = 0;
Elliott Hughescb693062013-02-21 09:48:08 -080081 return;
82 }
83
Elliott Hughes5d10a872013-04-17 19:26:43 -070084 memmove(input_buffer_, input_buffer_ + count, input_count_ - count);
85 input_count_ -= count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070086}
87
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070088bool JdwpNetStateBase::HaveFullPacket() {
89 if (awaiting_handshake_) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070090 return (input_count_ >= kMagicHandshakeLen);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070091 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070092 if (input_count_ < 4) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070093 return false;
94 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070095 uint32_t length = Get4BE(input_buffer_);
96 return (input_count_ >= length);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070097}
98
99bool JdwpNetStateBase::IsAwaitingHandshake() {
100 return awaiting_handshake_;
101}
102
103void JdwpNetStateBase::SetAwaitingHandshake(bool new_state) {
104 awaiting_handshake_ = new_state;
105}
106
107bool JdwpNetStateBase::IsConnected() {
108 return clientSock >= 0;
109}
110
111// Close a connection from a debugger (which may have already dropped us).
112// Resets the state so we're ready to receive a new connection.
113// Only called from the JDWP thread.
114void JdwpNetStateBase::Close() {
115 if (clientSock < 0) {
116 return;
117 }
118
119 VLOG(jdwp) << "+++ closing JDWP connection on fd " << clientSock;
120
121 close(clientSock);
122 clientSock = -1;
123}
124
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125/*
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100126 * Write a packet of "length" bytes. Grabs a mutex to assure atomicity.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127 */
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100128ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply, size_t length) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700129 MutexLock mu(Thread::Current(), socket_lock_);
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100130 DCHECK(IsConnected()) << "Connection with debugger is closed";
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100131 DCHECK_LE(length, expandBufGetLength(pReply));
132 return TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply), length));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133}
134
135/*
136 * Write a buffered packet. Grabs a mutex to assure atomicity.
137 */
Brian Carlstromf5293522013-07-19 00:24:00 -0700138ssize_t JdwpNetStateBase::WriteBufferedPacket(const std::vector<iovec>& iov) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700139 MutexLock mu(Thread::Current(), socket_lock_);
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800140 return WriteBufferedPacketLocked(iov);
141}
142
143ssize_t JdwpNetStateBase::WriteBufferedPacketLocked(const std::vector<iovec>& iov) {
144 socket_lock_.AssertHeld(Thread::Current());
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100145 DCHECK(IsConnected()) << "Connection with debugger is closed";
Brian Carlstromf5293522013-07-19 00:24:00 -0700146 return TEMP_FAILURE_RETRY(writev(clientSock, &iov[0], iov.size()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147}
148
Elliott Hughes376a7a02011-10-24 18:35:55 -0700149bool JdwpState::IsConnected() {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200150 return netState != nullptr && netState->IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151}
152
Brian Carlstromf5293522013-07-19 00:24:00 -0700153void JdwpState::SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov) {
Sebastien Hertz60ed7da2014-08-28 18:50:36 +0200154 if (!IsConnected()) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700155 // Can happen with some DDMS events.
156 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
157 return;
158 }
159
160 size_t expected = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700161 for (size_t i = 0; i < iov.size(); ++i) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700162 expected += iov[i].iov_len;
163 }
164
165 errno = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700166 ssize_t actual = netState->WriteBufferedPacket(iov);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700167 if (static_cast<size_t>(actual) != expected) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800168 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet %c%c%c%c to debugger (%zd of %zu)",
169 static_cast<char>(type >> 24),
170 static_cast<char>(type >> 16),
171 static_cast<char>(type >> 8),
172 static_cast<char>(type),
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700173 actual, expected);
174 }
175}
176
177void JdwpState::SendRequest(ExpandBuf* pReq) {
Sebastien Hertz60ed7da2014-08-28 18:50:36 +0200178 if (!IsConnected()) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700179 // Can happen with some DDMS events.
180 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
181 return;
182 }
183
184 errno = 0;
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100185 ssize_t actual = netState->WritePacket(pReq, expandBufGetLength(pReq));
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700186 if (static_cast<size_t>(actual) != expandBufGetLength(pReq)) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800187 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet to debugger (%zd of %zu)",
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700188 actual, expandBufGetLength(pReq));
189 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190}
191
Elliott Hughes376a7a02011-10-24 18:35:55 -0700192/*
193 * Get the next "request" serial number. We use this when sending
194 * packets to the debugger.
195 */
196uint32_t JdwpState::NextRequestSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700197 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198}
199
Elliott Hughes376a7a02011-10-24 18:35:55 -0700200/*
201 * Get the next "event" serial number. We use this in the response to
202 * message type EventRequest.Set.
203 */
204uint32_t JdwpState::NextEventSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700205 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206}
207
Elliott Hughes376a7a02011-10-24 18:35:55 -0700208JdwpState::JdwpState(const JdwpOptions* options)
209 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800210 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700211 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 pthread_(0),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200213 thread_(nullptr),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700214 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700215 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700216 run(false),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200217 netState(nullptr),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800218 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700219 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700220 last_activity_time_ms_(0),
Elliott Hughesf8349362012-06-18 15:00:06 -0700221 request_serial_(0x10000000),
222 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800223 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200224 event_list_(nullptr),
Elliott Hughesf8349362012-06-18 15:00:06 -0700225 event_list_size_(0),
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100226 jdwp_token_lock_("JDWP token lock"),
227 jdwp_token_cond_("JDWP token condition variable", jdwp_token_lock_),
228 jdwp_token_owner_thread_id_(0),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800229 ddm_is_active_(false),
230 should_exit_(false),
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100231 exit_status_(0),
232 shutdown_lock_("JDWP shutdown lock", kJdwpShutdownLock),
233 shutdown_cond_("JDWP shutdown condition variable", shutdown_lock_),
234 processing_request_(false) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235}
236
237/*
238 * Initialize JDWP.
239 *
240 * Does not return until JDWP thread is running, but may return before
241 * the thread is accepting network connections.
242 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700243JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700244 Thread* self = Thread::Current();
245 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700246 std::unique_ptr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700247 switch (options->transport) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200248 case kJdwpTransportSocket:
249 InitSocketTransport(state.get(), options);
250 break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251#ifdef HAVE_ANDROID_OS
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200252 case kJdwpTransportAndroidAdb:
253 InitAdbTransport(state.get(), options);
254 break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700255#endif
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200256 default:
257 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258 }
259
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200260 {
Jeff Haoadd037d2013-07-15 14:24:14 -0700261 /*
262 * Grab a mutex before starting the thread. This ensures they
263 * won't signal the cond var before we're waiting.
264 */
Ian Rogers50b35e22012-10-04 10:09:15 -0700265 MutexLock thread_start_locker(self, state->thread_start_lock_);
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200266
Jeff Haoadd037d2013-07-15 14:24:14 -0700267 /*
268 * We have bound to a port, or are trying to connect outbound to a
269 * debugger. Create the JDWP thread and let it continue the mission.
270 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200271 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, nullptr, StartJdwpThread, state.get()),
272 "JDWP thread");
Jeff Haoadd037d2013-07-15 14:24:14 -0700273
274 /*
275 * Wait until the thread finishes basic initialization.
Jeff Haoadd037d2013-07-15 14:24:14 -0700276 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200277 while (!state->debug_thread_started_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700278 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700279 }
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200280 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700281
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200282 if (options->suspend) {
Jeff Haoadd037d2013-07-15 14:24:14 -0700283 /*
284 * For suspend=y, wait for the debugger to connect to us or for us to
285 * connect to the debugger.
286 *
287 * The JDWP thread will signal us when it connects successfully or
288 * times out (for timeout=xxx), so we have to check to see what happened
289 * when we wake up.
290 */
291 {
292 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
293 MutexLock attach_locker(self, state->attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200294 while (state->debug_thread_id_ == 0) {
295 state->attach_cond_.Wait(self);
296 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700297 }
298 if (!state->IsActive()) {
299 LOG(ERROR) << "JDWP connection failed";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200300 return nullptr;
Jeff Haoadd037d2013-07-15 14:24:14 -0700301 }
302
303 LOG(INFO) << "JDWP connected";
304
305 /*
306 * Ordinarily we would pause briefly to allow the debugger to set
307 * breakpoints and so on, but for "suspend=y" the VM init code will
308 * pause the VM when it sends the VM_START message.
309 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310 }
311
Elliott Hughes761928d2011-11-16 18:33:03 -0800312 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313}
314
315/*
316 * Reset all session-related state. There should not be an active connection
317 * to the client at this point. The rest of the VM still thinks there is
318 * a debugger attached.
319 *
320 * This includes freeing up the debugger event list.
321 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700322void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323 /* could reset the serial numbers, but no need to */
324
Elliott Hughes761928d2011-11-16 18:33:03 -0800325 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700326 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700327 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200328 CHECK(event_list_ == nullptr);
Elliott Hughesf8349362012-06-18 15:00:06 -0700329 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330
331 /*
332 * Should not have one of these in progress. If the debugger went away
333 * mid-request, though, we could see this.
334 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100335 if (jdwp_token_owner_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800336 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700337 DCHECK(false);
338 }
339}
340
341/*
342 * Tell the JDWP thread to shut down. Frees "state".
343 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700344JdwpState::~JdwpState() {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200345 if (netState != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700346 /*
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100347 * Close down the network to inspire the thread to halt. If a request is being processed,
348 * we need to wait for it to finish first.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349 */
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100350 {
351 Thread* self = Thread::Current();
352 MutexLock mu(self, shutdown_lock_);
353 while (processing_request_) {
354 VLOG(jdwp) << "JDWP command in progress: wait for it to finish ...";
355 shutdown_cond_.Wait(self);
356 }
357
358 VLOG(jdwp) << "JDWP shutting down net...";
359 netState->Shutdown();
360 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361
Elliott Hughes376a7a02011-10-24 18:35:55 -0700362 if (debug_thread_started_) {
363 run = false;
364 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700365 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700366 LOG(WARNING) << "JDWP thread join failed";
367 }
368 }
369
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800370 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700371 delete netState;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200372 netState = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200374 CHECK(netState == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700375
Elliott Hughes376a7a02011-10-24 18:35:55 -0700376 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700377}
378
379/*
380 * Are we talking to a debugger?
381 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700382bool JdwpState::IsActive() {
383 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700384}
385
Elliott Hughescb693062013-02-21 09:48:08 -0800386// Returns "false" if we encounter a connection-fatal error.
387bool JdwpState::HandlePacket() {
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100388 Thread* const self = Thread::Current();
389 {
390 MutexLock mu(self, shutdown_lock_);
391 processing_request_ = true;
392 }
393 JdwpNetStateBase* netStateBase = netState;
394 CHECK(netStateBase != nullptr) << "Connection has been closed";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700395 JDWP::Request request(netStateBase->input_buffer_, netStateBase->input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -0800396
397 ExpandBuf* pReply = expandBufAlloc();
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200398 bool skip_reply = false;
399 size_t replyLength = ProcessRequest(&request, pReply, &skip_reply);
400 ssize_t cc = 0;
401 if (!skip_reply) {
402 cc = netStateBase->WritePacket(pReply, replyLength);
403 } else {
404 DCHECK_EQ(replyLength, 0U);
405 }
406 expandBufFree(pReply);
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100407
408 /*
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100409 * We processed this request and sent its reply so we can release the JDWP token.
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100410 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100411 ReleaseJdwpTokenForCommand();
Sebastien Hertz99660e12014-02-19 15:04:42 +0100412
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100413 if (cc != static_cast<ssize_t>(replyLength)) {
Elliott Hughescb693062013-02-21 09:48:08 -0800414 PLOG(ERROR) << "Failed sending reply to debugger";
Elliott Hughescb693062013-02-21 09:48:08 -0800415 return false;
416 }
Elliott Hughescb693062013-02-21 09:48:08 -0800417 netStateBase->ConsumeBytes(request.GetLength());
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100418 {
419 MutexLock mu(self, shutdown_lock_);
420 processing_request_ = false;
421 shutdown_cond_.Broadcast(self);
422 }
Elliott Hughescb693062013-02-21 09:48:08 -0800423 return true;
424}
425
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426/*
427 * Entry point for JDWP thread. The thread was created through the VM
428 * mechanisms, so there is a java/lang/Thread associated with us.
429 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700430static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700431 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200432 CHECK(state != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433
Elliott Hughes376a7a02011-10-24 18:35:55 -0700434 state->Run();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200435 return nullptr;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700436}
437
438void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700439 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800440 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
Mathieu Chartier184c9dc2015-03-05 13:20:54 -0800441 !runtime->IsAotCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800443 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700444
445 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700446 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447 * we're running.
448 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700449 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700450 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451
Ian Rogers81d425b2012-09-27 16:03:43 -0700452 {
453 MutexLock locker(thread_, thread_start_lock_);
454 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700455 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700456 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700458 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700459 CHECK_EQ(thread_->GetState(), kNative);
Ian Rogers62d6c772013-02-27 08:32:07 -0800460 Locks::mutator_lock_->AssertNotHeld(thread_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700461 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462
463 /*
464 * Loop forever if we're in server mode, processing connections. In
465 * non-server mode, we bail out of the thread when the debugger drops
466 * us.
467 *
468 * We broadcast a notification when a debugger attaches, after we
469 * successfully process the handshake.
470 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700471 while (run) {
472 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700473 /*
474 * Block forever, waiting for a connection. To support the
475 * "timeout=xxx" option we'll need to tweak this.
476 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700477 if (!netState->Accept()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 break;
479 }
480 } else {
481 /*
482 * If we're not acting as a server, we need to connect out to the
483 * debugger. To support the "timeout=xxx" option we need to
484 * have a timeout if the handshake reply isn't received in a
485 * reasonable amount of time.
486 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700487 if (!netState->Establish(options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700489 MutexLock mu(thread_, attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200490 debug_thread_id_ = static_cast<ObjectId>(-1);
Ian Rogersc604d732012-10-14 16:09:54 -0700491 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492 break;
493 }
494 }
495
496 /* prep debug code to handle the new connection */
497 Dbg::Connected();
498
499 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700500 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800501 while (!Dbg::IsDisposed()) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200502 // sanity check -- shouldn't happen?
503 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504
Elliott Hughes5d10a872013-04-17 19:26:43 -0700505 if (!netState->ProcessIncoming()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 /* blocking read */
507 break;
508 }
509
Elliott Hughes64f574f2013-02-20 14:57:12 -0800510 if (should_exit_) {
511 exit(exit_status_);
512 }
513
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700514 if (first && !netState->IsAwaitingHandshake()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700515 /* handshake worked, tell the interpreter that we're active */
516 first = false;
517
518 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700519 {
520 ScopedObjectAccess soa(thread_);
521 debug_thread_id_ = Dbg::GetThreadSelfId();
522 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523
524 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700525 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700526 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 }
528 }
529
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700530 netState->Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531
Elliott Hughesa21039c2012-06-21 12:09:25 -0700532 if (ddm_is_active_) {
533 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700534
535 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700536 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700538 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539 }
540
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541 {
542 ScopedObjectAccess soa(thread_);
Elliott Hughes0f827162013-02-26 12:12:58 -0800543
544 // Release session state, e.g. remove breakpoint instructions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700545 ResetState();
546 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800547 // Tell the rest of the runtime that the debugger is no longer around.
548 Dbg::Disconnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700549
550 /* if we had threads suspended, resume them now */
551 Dbg::UndoDebuggerSuspensions();
552
553 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700554 if (!options_->server) {
555 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700556 }
557 }
558
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700559 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700560 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
561 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700562
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800563 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700564 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700565}
566
Elliott Hughesa21039c2012-06-21 12:09:25 -0700567void JdwpState::NotifyDdmsActive() {
568 if (!ddm_is_active_) {
569 ddm_is_active_ = true;
570 Dbg::DdmConnected();
571 }
572}
573
Elliott Hughes475fc232011-10-25 15:00:35 -0700574Thread* JdwpState::GetDebugThread() {
575 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700576}
577
578/*
579 * Support routines for waitForDebugger().
580 *
581 * We can't have a trivial "waitForDebugger" function that returns the
582 * instant the debugger connects, because we run the risk of executing code
583 * before the debugger has had a chance to configure breakpoints or issue
584 * suspend calls. It would be nice to just sit in the suspended state, but
585 * most debuggers don't expect any threads to be suspended when they attach.
586 *
587 * There's no JDWP event we can post to tell the debugger, "we've stopped,
588 * and we like it that way". We could send a fake breakpoint, which should
589 * cause the debugger to immediately send a resume, but the debugger might
590 * send the resume immediately or might throw an exception of its own upon
591 * receiving a breakpoint event that it didn't ask for.
592 *
593 * What we really want is a "wait until the debugger is done configuring
594 * stuff" event. We can approximate this with a "wait until the debugger
595 * has been idle for a brief period".
596 */
597
598/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700599 * Return the time, in milliseconds, since the last debugger activity.
600 *
601 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
602 * processing a debugger request.
603 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700604int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700605 if (!Dbg::IsDebuggerActive()) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700606 LOG(WARNING) << "no active debugger";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607 return -1;
608 }
609
Ian Rogers37f3c962014-07-17 11:25:30 -0700610 int64_t last = last_activity_time_ms_.LoadSequentiallyConsistent();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611
612 /* initializing or in the middle of something? */
613 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800614 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615 return 0;
616 }
617
618 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700619 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800620 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800622 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700623 return now - last;
624}
625
Elliott Hughes64f574f2013-02-20 14:57:12 -0800626void JdwpState::ExitAfterReplying(int exit_status) {
627 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
628 should_exit_ = true;
629 exit_status_ = exit_status;
630}
631
Elliott Hughes03181a82011-11-17 17:22:21 -0800632std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800633 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800634 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800635 << "@" << StringPrintf("%#" PRIx64, rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800636 return os;
637}
638
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800639bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700640 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
641 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800642}
643
644bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
645 return !(lhs == rhs);
646}
647
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800648bool operator==(const JdwpOptions& lhs, const JdwpOptions& rhs) {
649 if (&lhs == &rhs) {
650 return true;
651 }
652
653 return lhs.transport == rhs.transport &&
654 lhs.server == rhs.server &&
655 lhs.suspend == rhs.suspend &&
656 lhs.host == rhs.host &&
657 lhs.port == rhs.port;
658}
659
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700660} // namespace JDWP
661
662} // namespace art