blob: 7707ba49321640f622e40e94539981e5cf11ab3c [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "atomic.h"
26#include "base/logging.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/time_utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "debugger.h"
29#include "jdwp/jdwp_priv.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070031
32namespace art {
33
34namespace JDWP {
35
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036using android::base::StringPrintf;
37
Elliott Hughes376a7a02011-10-24 18:35:55 -070038static void* StartJdwpThread(void* arg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070039
40/*
41 * JdwpNetStateBase class implementation
42 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070043JdwpNetStateBase::JdwpNetStateBase(JdwpState* state)
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070044 : state_(state), socket_lock_("JdwpNetStateBase lock", kJdwpSocketLock) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070045 clientSock = -1;
Elliott Hughes5d10a872013-04-17 19:26:43 -070046 wake_pipe_[0] = -1;
47 wake_pipe_[1] = -1;
48 input_count_ = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070049 awaiting_handshake_ = false;
Elliott Hughescb693062013-02-21 09:48:08 -080050}
51
Elliott Hughes5d10a872013-04-17 19:26:43 -070052JdwpNetStateBase::~JdwpNetStateBase() {
53 if (wake_pipe_[0] != -1) {
54 close(wake_pipe_[0]);
55 wake_pipe_[0] = -1;
56 }
57 if (wake_pipe_[1] != -1) {
58 close(wake_pipe_[1]);
59 wake_pipe_[1] = -1;
60 }
61}
62
63bool JdwpNetStateBase::MakePipe() {
64 if (pipe(wake_pipe_) == -1) {
65 PLOG(ERROR) << "pipe failed";
66 return false;
67 }
68 return true;
69}
70
71void JdwpNetStateBase::WakePipe() {
72 // If we might be sitting in select, kick us loose.
73 if (wake_pipe_[1] != -1) {
74 VLOG(jdwp) << "+++ writing to wake pipe";
75 TEMP_FAILURE_RETRY(write(wake_pipe_[1], "", 1));
76 }
77}
78
Elliott Hughescb693062013-02-21 09:48:08 -080079void JdwpNetStateBase::ConsumeBytes(size_t count) {
80 CHECK_GT(count, 0U);
Elliott Hughes5d10a872013-04-17 19:26:43 -070081 CHECK_LE(count, input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -080082
Elliott Hughes5d10a872013-04-17 19:26:43 -070083 if (count == input_count_) {
84 input_count_ = 0;
Elliott Hughescb693062013-02-21 09:48:08 -080085 return;
86 }
87
Elliott Hughes5d10a872013-04-17 19:26:43 -070088 memmove(input_buffer_, input_buffer_ + count, input_count_ - count);
89 input_count_ -= count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070090}
91
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070092bool JdwpNetStateBase::HaveFullPacket() {
93 if (awaiting_handshake_) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070094 return (input_count_ >= kMagicHandshakeLen);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070095 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070096 if (input_count_ < 4) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070097 return false;
98 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070099 uint32_t length = Get4BE(input_buffer_);
100 return (input_count_ >= length);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700101}
102
103bool JdwpNetStateBase::IsAwaitingHandshake() {
104 return awaiting_handshake_;
105}
106
107void JdwpNetStateBase::SetAwaitingHandshake(bool new_state) {
108 awaiting_handshake_ = new_state;
109}
110
111bool JdwpNetStateBase::IsConnected() {
112 return clientSock >= 0;
113}
114
115// Close a connection from a debugger (which may have already dropped us).
116// Resets the state so we're ready to receive a new connection.
117// Only called from the JDWP thread.
118void JdwpNetStateBase::Close() {
119 if (clientSock < 0) {
120 return;
121 }
122
123 VLOG(jdwp) << "+++ closing JDWP connection on fd " << clientSock;
124
125 close(clientSock);
126 clientSock = -1;
127}
128
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700129/*
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100130 * Write a packet of "length" bytes. Grabs a mutex to assure atomicity.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131 */
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100132ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply, size_t length) {
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100133 DCHECK_LE(length, expandBufGetLength(pReply));
Sebastien Hertz738c4c92015-09-02 14:30:13 +0200134 if (!IsConnected()) {
135 LOG(WARNING) << "Connection with debugger is closed";
136 return -1;
137 }
138 MutexLock mu(Thread::Current(), socket_lock_);
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100139 return TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply), length));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140}
141
142/*
143 * Write a buffered packet. Grabs a mutex to assure atomicity.
144 */
Brian Carlstromf5293522013-07-19 00:24:00 -0700145ssize_t JdwpNetStateBase::WriteBufferedPacket(const std::vector<iovec>& iov) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700146 MutexLock mu(Thread::Current(), socket_lock_);
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800147 return WriteBufferedPacketLocked(iov);
148}
149
150ssize_t JdwpNetStateBase::WriteBufferedPacketLocked(const std::vector<iovec>& iov) {
151 socket_lock_.AssertHeld(Thread::Current());
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100152 DCHECK(IsConnected()) << "Connection with debugger is closed";
Brian Carlstromf5293522013-07-19 00:24:00 -0700153 return TEMP_FAILURE_RETRY(writev(clientSock, &iov[0], iov.size()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154}
155
Elliott Hughes376a7a02011-10-24 18:35:55 -0700156bool JdwpState::IsConnected() {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200157 return netState != nullptr && netState->IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700158}
159
Brian Carlstromf5293522013-07-19 00:24:00 -0700160void JdwpState::SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov) {
Sebastien Hertz60ed7da2014-08-28 18:50:36 +0200161 if (!IsConnected()) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700162 // Can happen with some DDMS events.
163 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
164 return;
165 }
166
167 size_t expected = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700168 for (size_t i = 0; i < iov.size(); ++i) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700169 expected += iov[i].iov_len;
170 }
171
172 errno = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700173 ssize_t actual = netState->WriteBufferedPacket(iov);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700174 if (static_cast<size_t>(actual) != expected) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800175 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet %c%c%c%c to debugger (%zd of %zu)",
176 static_cast<char>(type >> 24),
177 static_cast<char>(type >> 16),
178 static_cast<char>(type >> 8),
179 static_cast<char>(type),
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700180 actual, expected);
181 }
182}
183
184void JdwpState::SendRequest(ExpandBuf* pReq) {
Sebastien Hertz60ed7da2014-08-28 18:50:36 +0200185 if (!IsConnected()) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700186 // Can happen with some DDMS events.
187 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
188 return;
189 }
190
191 errno = 0;
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100192 ssize_t actual = netState->WritePacket(pReq, expandBufGetLength(pReq));
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700193 if (static_cast<size_t>(actual) != expandBufGetLength(pReq)) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800194 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet to debugger (%zd of %zu)",
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700195 actual, expandBufGetLength(pReq));
196 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197}
198
Elliott Hughes376a7a02011-10-24 18:35:55 -0700199/*
200 * Get the next "request" serial number. We use this when sending
201 * packets to the debugger.
202 */
203uint32_t JdwpState::NextRequestSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700204 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205}
206
Elliott Hughes376a7a02011-10-24 18:35:55 -0700207/*
208 * Get the next "event" serial number. We use this in the response to
209 * message type EventRequest.Set.
210 */
211uint32_t JdwpState::NextEventSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700212 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213}
214
Elliott Hughes376a7a02011-10-24 18:35:55 -0700215JdwpState::JdwpState(const JdwpOptions* options)
216 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800217 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700218 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 pthread_(0),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200220 thread_(nullptr),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700222 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700223 run(false),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200224 netState(nullptr),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800225 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700226 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700227 last_activity_time_ms_(0),
Elliott Hughesf8349362012-06-18 15:00:06 -0700228 request_serial_(0x10000000),
229 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800230 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200231 event_list_(nullptr),
Elliott Hughesf8349362012-06-18 15:00:06 -0700232 event_list_size_(0),
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100233 jdwp_token_lock_("JDWP token lock"),
234 jdwp_token_cond_("JDWP token condition variable", jdwp_token_lock_),
235 jdwp_token_owner_thread_id_(0),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800236 ddm_is_active_(false),
237 should_exit_(false),
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100238 exit_status_(0),
239 shutdown_lock_("JDWP shutdown lock", kJdwpShutdownLock),
240 shutdown_cond_("JDWP shutdown condition variable", shutdown_lock_),
241 processing_request_(false) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700242}
243
244/*
245 * Initialize JDWP.
246 *
247 * Does not return until JDWP thread is running, but may return before
248 * the thread is accepting network connections.
249 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700250JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700251 Thread* self = Thread::Current();
252 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700253 std::unique_ptr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700254 switch (options->transport) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200255 case kJdwpTransportSocket:
256 InitSocketTransport(state.get(), options);
257 break;
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100258#ifdef ART_TARGET_ANDROID
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200259 case kJdwpTransportAndroidAdb:
260 InitAdbTransport(state.get(), options);
261 break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262#endif
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200263 default:
264 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 }
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200266 {
Jeff Haoadd037d2013-07-15 14:24:14 -0700267 /*
268 * Grab a mutex before starting the thread. This ensures they
269 * won't signal the cond var before we're waiting.
270 */
Mathieu Chartier90443472015-07-16 20:32:27 -0700271 state->thread_start_lock_.AssertNotHeld(self);
Ian Rogers50b35e22012-10-04 10:09:15 -0700272 MutexLock thread_start_locker(self, state->thread_start_lock_);
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200273
Jeff Haoadd037d2013-07-15 14:24:14 -0700274 /*
275 * We have bound to a port, or are trying to connect outbound to a
276 * debugger. Create the JDWP thread and let it continue the mission.
277 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200278 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, nullptr, StartJdwpThread, state.get()),
279 "JDWP thread");
Jeff Haoadd037d2013-07-15 14:24:14 -0700280
281 /*
282 * Wait until the thread finishes basic initialization.
Jeff Haoadd037d2013-07-15 14:24:14 -0700283 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200284 while (!state->debug_thread_started_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700285 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700286 }
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200287 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700288
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200289 if (options->suspend) {
Jeff Haoadd037d2013-07-15 14:24:14 -0700290 /*
291 * For suspend=y, wait for the debugger to connect to us or for us to
292 * connect to the debugger.
293 *
294 * The JDWP thread will signal us when it connects successfully or
295 * times out (for timeout=xxx), so we have to check to see what happened
296 * when we wake up.
297 */
298 {
299 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
300 MutexLock attach_locker(self, state->attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200301 while (state->debug_thread_id_ == 0) {
302 state->attach_cond_.Wait(self);
303 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700304 }
305 if (!state->IsActive()) {
306 LOG(ERROR) << "JDWP connection failed";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200307 return nullptr;
Jeff Haoadd037d2013-07-15 14:24:14 -0700308 }
309
310 LOG(INFO) << "JDWP connected";
311
312 /*
313 * Ordinarily we would pause briefly to allow the debugger to set
314 * breakpoints and so on, but for "suspend=y" the VM init code will
315 * pause the VM when it sends the VM_START message.
316 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700317 }
318
Elliott Hughes761928d2011-11-16 18:33:03 -0800319 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320}
321
322/*
323 * Reset all session-related state. There should not be an active connection
324 * to the client at this point. The rest of the VM still thinks there is
325 * a debugger attached.
326 *
327 * This includes freeing up the debugger event list.
328 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700329void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330 /* could reset the serial numbers, but no need to */
331
Elliott Hughes761928d2011-11-16 18:33:03 -0800332 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700333 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700334 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200335 CHECK(event_list_ == nullptr);
Elliott Hughesf8349362012-06-18 15:00:06 -0700336 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700337
338 /*
339 * Should not have one of these in progress. If the debugger went away
340 * mid-request, though, we could see this.
341 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100342 if (jdwp_token_owner_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800343 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700344 DCHECK(false);
345 }
346}
347
348/*
349 * Tell the JDWP thread to shut down. Frees "state".
350 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700351JdwpState::~JdwpState() {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200352 if (netState != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700353 /*
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100354 * Close down the network to inspire the thread to halt. If a request is being processed,
355 * we need to wait for it to finish first.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 */
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100357 {
358 Thread* self = Thread::Current();
359 MutexLock mu(self, shutdown_lock_);
360 while (processing_request_) {
361 VLOG(jdwp) << "JDWP command in progress: wait for it to finish ...";
362 shutdown_cond_.Wait(self);
363 }
364
365 VLOG(jdwp) << "JDWP shutting down net...";
366 netState->Shutdown();
367 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368
Elliott Hughes376a7a02011-10-24 18:35:55 -0700369 if (debug_thread_started_) {
370 run = false;
371 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700372 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 LOG(WARNING) << "JDWP thread join failed";
374 }
375 }
376
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800377 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700378 delete netState;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200379 netState = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700380 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200381 CHECK(netState == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700382
Elliott Hughes376a7a02011-10-24 18:35:55 -0700383 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700384}
385
386/*
387 * Are we talking to a debugger?
388 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700389bool JdwpState::IsActive() {
390 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700391}
392
Elliott Hughescb693062013-02-21 09:48:08 -0800393// Returns "false" if we encounter a connection-fatal error.
394bool JdwpState::HandlePacket() {
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100395 Thread* const self = Thread::Current();
396 {
397 MutexLock mu(self, shutdown_lock_);
398 processing_request_ = true;
399 }
400 JdwpNetStateBase* netStateBase = netState;
401 CHECK(netStateBase != nullptr) << "Connection has been closed";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700402 JDWP::Request request(netStateBase->input_buffer_, netStateBase->input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -0800403
404 ExpandBuf* pReply = expandBufAlloc();
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200405 bool skip_reply = false;
406 size_t replyLength = ProcessRequest(&request, pReply, &skip_reply);
407 ssize_t cc = 0;
408 if (!skip_reply) {
409 cc = netStateBase->WritePacket(pReply, replyLength);
410 } else {
411 DCHECK_EQ(replyLength, 0U);
412 }
413 expandBufFree(pReply);
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100414
415 /*
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100416 * We processed this request and sent its reply so we can release the JDWP token.
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100417 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100418 ReleaseJdwpTokenForCommand();
Sebastien Hertz99660e12014-02-19 15:04:42 +0100419
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100420 if (cc != static_cast<ssize_t>(replyLength)) {
Elliott Hughescb693062013-02-21 09:48:08 -0800421 PLOG(ERROR) << "Failed sending reply to debugger";
Elliott Hughescb693062013-02-21 09:48:08 -0800422 return false;
423 }
Elliott Hughescb693062013-02-21 09:48:08 -0800424 netStateBase->ConsumeBytes(request.GetLength());
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100425 {
426 MutexLock mu(self, shutdown_lock_);
427 processing_request_ = false;
428 shutdown_cond_.Broadcast(self);
429 }
Elliott Hughescb693062013-02-21 09:48:08 -0800430 return true;
431}
432
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433/*
434 * Entry point for JDWP thread. The thread was created through the VM
435 * mechanisms, so there is a java/lang/Thread associated with us.
436 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700437static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200439 CHECK(state != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440
Elliott Hughes376a7a02011-10-24 18:35:55 -0700441 state->Run();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200442 return nullptr;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700443}
444
445void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700446 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800447 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
Mathieu Chartier184c9dc2015-03-05 13:20:54 -0800448 !runtime->IsAotCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700449
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800450 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451
452 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700453 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 * we're running.
455 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700456 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700457 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700458
Ian Rogers81d425b2012-09-27 16:03:43 -0700459 {
460 MutexLock locker(thread_, thread_start_lock_);
461 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700462 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700463 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700465 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700466 CHECK_EQ(thread_->GetState(), kNative);
Ian Rogers62d6c772013-02-27 08:32:07 -0800467 Locks::mutator_lock_->AssertNotHeld(thread_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700468 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469
470 /*
471 * Loop forever if we're in server mode, processing connections. In
472 * non-server mode, we bail out of the thread when the debugger drops
473 * us.
474 *
475 * We broadcast a notification when a debugger attaches, after we
476 * successfully process the handshake.
477 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700478 while (run) {
479 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700480 /*
481 * Block forever, waiting for a connection. To support the
482 * "timeout=xxx" option we'll need to tweak this.
483 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700484 if (!netState->Accept()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485 break;
486 }
487 } else {
488 /*
489 * If we're not acting as a server, we need to connect out to the
490 * debugger. To support the "timeout=xxx" option we need to
491 * have a timeout if the handshake reply isn't received in a
492 * reasonable amount of time.
493 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700494 if (!netState->Establish(options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700496 MutexLock mu(thread_, attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200497 debug_thread_id_ = static_cast<ObjectId>(-1);
Ian Rogersc604d732012-10-14 16:09:54 -0700498 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700499 break;
500 }
501 }
502
503 /* prep debug code to handle the new connection */
504 Dbg::Connected();
505
506 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700507 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800508 while (!Dbg::IsDisposed()) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200509 // sanity check -- shouldn't happen?
510 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700511
Elliott Hughes5d10a872013-04-17 19:26:43 -0700512 if (!netState->ProcessIncoming()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700513 /* blocking read */
514 break;
515 }
516
Elliott Hughes64f574f2013-02-20 14:57:12 -0800517 if (should_exit_) {
518 exit(exit_status_);
519 }
520
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700521 if (first && !netState->IsAwaitingHandshake()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700522 /* handshake worked, tell the interpreter that we're active */
523 first = false;
524
525 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700526 {
527 ScopedObjectAccess soa(thread_);
528 debug_thread_id_ = Dbg::GetThreadSelfId();
529 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700530
531 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700532 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700533 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700534 }
535 }
536
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700537 netState->Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538
Elliott Hughesa21039c2012-06-21 12:09:25 -0700539 if (ddm_is_active_) {
540 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700541
542 /* broadcast the disconnect; must be in RUNNING state */
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700543 ScopedObjectAccess soa(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544 Dbg::DdmDisconnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545 }
546
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700547 {
548 ScopedObjectAccess soa(thread_);
Elliott Hughes0f827162013-02-26 12:12:58 -0800549
550 // Release session state, e.g. remove breakpoint instructions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700551 ResetState();
552 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800553 // Tell the rest of the runtime that the debugger is no longer around.
554 Dbg::Disconnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700555
556 /* if we had threads suspended, resume them now */
557 Dbg::UndoDebuggerSuspensions();
558
559 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700560 if (!options_->server) {
561 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700562 }
563 }
564
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700566 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
567 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700568
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800569 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700570 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571}
572
Elliott Hughesa21039c2012-06-21 12:09:25 -0700573void JdwpState::NotifyDdmsActive() {
574 if (!ddm_is_active_) {
575 ddm_is_active_ = true;
576 Dbg::DdmConnected();
577 }
578}
579
Elliott Hughes475fc232011-10-25 15:00:35 -0700580Thread* JdwpState::GetDebugThread() {
581 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582}
583
584/*
585 * Support routines for waitForDebugger().
586 *
587 * We can't have a trivial "waitForDebugger" function that returns the
588 * instant the debugger connects, because we run the risk of executing code
589 * before the debugger has had a chance to configure breakpoints or issue
590 * suspend calls. It would be nice to just sit in the suspended state, but
591 * most debuggers don't expect any threads to be suspended when they attach.
592 *
593 * There's no JDWP event we can post to tell the debugger, "we've stopped,
594 * and we like it that way". We could send a fake breakpoint, which should
595 * cause the debugger to immediately send a resume, but the debugger might
596 * send the resume immediately or might throw an exception of its own upon
597 * receiving a breakpoint event that it didn't ask for.
598 *
599 * What we really want is a "wait until the debugger is done configuring
600 * stuff" event. We can approximate this with a "wait until the debugger
601 * has been idle for a brief period".
602 */
603
604/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605 * Return the time, in milliseconds, since the last debugger activity.
606 *
607 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
608 * processing a debugger request.
609 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700610int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700611 if (!Dbg::IsDebuggerActive()) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700612 LOG(WARNING) << "no active debugger";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613 return -1;
614 }
615
Ian Rogers37f3c962014-07-17 11:25:30 -0700616 int64_t last = last_activity_time_ms_.LoadSequentiallyConsistent();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
618 /* initializing or in the middle of something? */
619 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800620 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621 return 0;
622 }
623
624 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700625 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800626 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700627
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800628 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700629 return now - last;
630}
631
Elliott Hughes64f574f2013-02-20 14:57:12 -0800632void JdwpState::ExitAfterReplying(int exit_status) {
633 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
634 should_exit_ = true;
635 exit_status_ = exit_status;
636}
637
Elliott Hughes03181a82011-11-17 17:22:21 -0800638std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800639 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800640 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800641 << "@" << StringPrintf("%#" PRIx64, rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800642 return os;
643}
644
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800645bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700646 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
647 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800648}
649
650bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
651 return !(lhs == rhs);
652}
653
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800654bool operator==(const JdwpOptions& lhs, const JdwpOptions& rhs) {
655 if (&lhs == &rhs) {
656 return true;
657 }
658
659 return lhs.transport == rhs.transport &&
660 lhs.server == rhs.server &&
661 lhs.suspend == rhs.suspend &&
662 lhs.host == rhs.host &&
663 lhs.port == rhs.port;
664}
665
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700666} // namespace JDWP
667
668} // namespace art