blob: 5b30f0cd8ccf99a15991e480bee8342c86190111 [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"
25#include "debugger.h"
26#include "jdwp/jdwp_priv.h"
27#include "scoped_thread_state_change.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070028
29namespace art {
30
31namespace JDWP {
32
Elliott Hughes376a7a02011-10-24 18:35:55 -070033static void* StartJdwpThread(void* arg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070034
35/*
36 * JdwpNetStateBase class implementation
37 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070038JdwpNetStateBase::JdwpNetStateBase(JdwpState* state)
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070039 : state_(state), socket_lock_("JdwpNetStateBase lock", kJdwpSocketLock) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040 clientSock = -1;
Elliott Hughes5d10a872013-04-17 19:26:43 -070041 wake_pipe_[0] = -1;
42 wake_pipe_[1] = -1;
43 input_count_ = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070044 awaiting_handshake_ = false;
Elliott Hughescb693062013-02-21 09:48:08 -080045}
46
Elliott Hughes5d10a872013-04-17 19:26:43 -070047JdwpNetStateBase::~JdwpNetStateBase() {
48 if (wake_pipe_[0] != -1) {
49 close(wake_pipe_[0]);
50 wake_pipe_[0] = -1;
51 }
52 if (wake_pipe_[1] != -1) {
53 close(wake_pipe_[1]);
54 wake_pipe_[1] = -1;
55 }
56}
57
58bool JdwpNetStateBase::MakePipe() {
59 if (pipe(wake_pipe_) == -1) {
60 PLOG(ERROR) << "pipe failed";
61 return false;
62 }
63 return true;
64}
65
66void JdwpNetStateBase::WakePipe() {
67 // If we might be sitting in select, kick us loose.
68 if (wake_pipe_[1] != -1) {
69 VLOG(jdwp) << "+++ writing to wake pipe";
70 TEMP_FAILURE_RETRY(write(wake_pipe_[1], "", 1));
71 }
72}
73
Elliott Hughescb693062013-02-21 09:48:08 -080074void JdwpNetStateBase::ConsumeBytes(size_t count) {
75 CHECK_GT(count, 0U);
Elliott Hughes5d10a872013-04-17 19:26:43 -070076 CHECK_LE(count, input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -080077
Elliott Hughes5d10a872013-04-17 19:26:43 -070078 if (count == input_count_) {
79 input_count_ = 0;
Elliott Hughescb693062013-02-21 09:48:08 -080080 return;
81 }
82
Elliott Hughes5d10a872013-04-17 19:26:43 -070083 memmove(input_buffer_, input_buffer_ + count, input_count_ - count);
84 input_count_ -= count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070085}
86
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070087bool JdwpNetStateBase::HaveFullPacket() {
88 if (awaiting_handshake_) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070089 return (input_count_ >= kMagicHandshakeLen);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070090 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070091 if (input_count_ < 4) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070092 return false;
93 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070094 uint32_t length = Get4BE(input_buffer_);
95 return (input_count_ >= length);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070096}
97
98bool JdwpNetStateBase::IsAwaitingHandshake() {
99 return awaiting_handshake_;
100}
101
102void JdwpNetStateBase::SetAwaitingHandshake(bool new_state) {
103 awaiting_handshake_ = new_state;
104}
105
106bool JdwpNetStateBase::IsConnected() {
107 return clientSock >= 0;
108}
109
110// Close a connection from a debugger (which may have already dropped us).
111// Resets the state so we're ready to receive a new connection.
112// Only called from the JDWP thread.
113void JdwpNetStateBase::Close() {
114 if (clientSock < 0) {
115 return;
116 }
117
118 VLOG(jdwp) << "+++ closing JDWP connection on fd " << clientSock;
119
120 close(clientSock);
121 clientSock = -1;
122}
123
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124/*
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100125 * Write a packet of "length" bytes. Grabs a mutex to assure atomicity.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 */
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100127ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply, size_t length) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700128 MutexLock mu(Thread::Current(), socket_lock_);
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100129 DCHECK(IsConnected()) << "Connection with debugger is closed";
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100130 DCHECK_LE(length, expandBufGetLength(pReply));
131 return TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply), length));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132}
133
134/*
135 * Write a buffered packet. Grabs a mutex to assure atomicity.
136 */
Brian Carlstromf5293522013-07-19 00:24:00 -0700137ssize_t JdwpNetStateBase::WriteBufferedPacket(const std::vector<iovec>& iov) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700138 MutexLock mu(Thread::Current(), socket_lock_);
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800139 return WriteBufferedPacketLocked(iov);
140}
141
142ssize_t JdwpNetStateBase::WriteBufferedPacketLocked(const std::vector<iovec>& iov) {
143 socket_lock_.AssertHeld(Thread::Current());
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100144 DCHECK(IsConnected()) << "Connection with debugger is closed";
Brian Carlstromf5293522013-07-19 00:24:00 -0700145 return TEMP_FAILURE_RETRY(writev(clientSock, &iov[0], iov.size()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146}
147
Elliott Hughes376a7a02011-10-24 18:35:55 -0700148bool JdwpState::IsConnected() {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200149 return netState != nullptr && netState->IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150}
151
Brian Carlstromf5293522013-07-19 00:24:00 -0700152void JdwpState::SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov) {
Sebastien Hertz60ed7da2014-08-28 18:50:36 +0200153 if (!IsConnected()) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700154 // Can happen with some DDMS events.
155 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
156 return;
157 }
158
159 size_t expected = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700160 for (size_t i = 0; i < iov.size(); ++i) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700161 expected += iov[i].iov_len;
162 }
163
164 errno = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700165 ssize_t actual = netState->WriteBufferedPacket(iov);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700166 if (static_cast<size_t>(actual) != expected) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800167 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet %c%c%c%c to debugger (%zd of %zu)",
168 static_cast<char>(type >> 24),
169 static_cast<char>(type >> 16),
170 static_cast<char>(type >> 8),
171 static_cast<char>(type),
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700172 actual, expected);
173 }
174}
175
176void JdwpState::SendRequest(ExpandBuf* pReq) {
Sebastien Hertz60ed7da2014-08-28 18:50:36 +0200177 if (!IsConnected()) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700178 // Can happen with some DDMS events.
179 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
180 return;
181 }
182
183 errno = 0;
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100184 ssize_t actual = netState->WritePacket(pReq, expandBufGetLength(pReq));
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700185 if (static_cast<size_t>(actual) != expandBufGetLength(pReq)) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800186 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet to debugger (%zd of %zu)",
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700187 actual, expandBufGetLength(pReq));
188 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189}
190
Elliott Hughes376a7a02011-10-24 18:35:55 -0700191/*
192 * Get the next "request" serial number. We use this when sending
193 * packets to the debugger.
194 */
195uint32_t JdwpState::NextRequestSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700196 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197}
198
Elliott Hughes376a7a02011-10-24 18:35:55 -0700199/*
200 * Get the next "event" serial number. We use this in the response to
201 * message type EventRequest.Set.
202 */
203uint32_t JdwpState::NextEventSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700204 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205}
206
Elliott Hughes376a7a02011-10-24 18:35:55 -0700207JdwpState::JdwpState(const JdwpOptions* options)
208 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800209 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700210 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700211 pthread_(0),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200212 thread_(nullptr),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700214 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 run(false),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200216 netState(nullptr),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800217 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700218 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700219 last_activity_time_ms_(0),
Elliott Hughesf8349362012-06-18 15:00:06 -0700220 request_serial_(0x10000000),
221 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800222 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Sebastien Hertz7d955652014-10-22 10:57:10 +0200223 event_list_(nullptr),
Elliott Hughesf8349362012-06-18 15:00:06 -0700224 event_list_size_(0),
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100225 jdwp_token_lock_("JDWP token lock"),
226 jdwp_token_cond_("JDWP token condition variable", jdwp_token_lock_),
227 jdwp_token_owner_thread_id_(0),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800228 ddm_is_active_(false),
229 should_exit_(false),
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100230 exit_status_(0),
231 shutdown_lock_("JDWP shutdown lock", kJdwpShutdownLock),
232 shutdown_cond_("JDWP shutdown condition variable", shutdown_lock_),
233 processing_request_(false) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700234}
235
236/*
237 * Initialize JDWP.
238 *
239 * Does not return until JDWP thread is running, but may return before
240 * the thread is accepting network connections.
241 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700242JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700243 Thread* self = Thread::Current();
244 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700245 std::unique_ptr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700246 switch (options->transport) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200247 case kJdwpTransportSocket:
248 InitSocketTransport(state.get(), options);
249 break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700250#ifdef HAVE_ANDROID_OS
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200251 case kJdwpTransportAndroidAdb:
252 InitAdbTransport(state.get(), options);
253 break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700254#endif
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200255 default:
256 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700257 }
258
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200259 {
Jeff Haoadd037d2013-07-15 14:24:14 -0700260 /*
261 * Grab a mutex before starting the thread. This ensures they
262 * won't signal the cond var before we're waiting.
263 */
Ian Rogers50b35e22012-10-04 10:09:15 -0700264 MutexLock thread_start_locker(self, state->thread_start_lock_);
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200265
Jeff Haoadd037d2013-07-15 14:24:14 -0700266 /*
267 * We have bound to a port, or are trying to connect outbound to a
268 * debugger. Create the JDWP thread and let it continue the mission.
269 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200270 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, nullptr, StartJdwpThread, state.get()),
271 "JDWP thread");
Jeff Haoadd037d2013-07-15 14:24:14 -0700272
273 /*
274 * Wait until the thread finishes basic initialization.
Jeff Haoadd037d2013-07-15 14:24:14 -0700275 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200276 while (!state->debug_thread_started_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700277 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700278 }
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200279 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700280
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200281 if (options->suspend) {
Jeff Haoadd037d2013-07-15 14:24:14 -0700282 /*
283 * For suspend=y, wait for the debugger to connect to us or for us to
284 * connect to the debugger.
285 *
286 * The JDWP thread will signal us when it connects successfully or
287 * times out (for timeout=xxx), so we have to check to see what happened
288 * when we wake up.
289 */
290 {
291 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
292 MutexLock attach_locker(self, state->attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200293 while (state->debug_thread_id_ == 0) {
294 state->attach_cond_.Wait(self);
295 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700296 }
297 if (!state->IsActive()) {
298 LOG(ERROR) << "JDWP connection failed";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200299 return nullptr;
Jeff Haoadd037d2013-07-15 14:24:14 -0700300 }
301
302 LOG(INFO) << "JDWP connected";
303
304 /*
305 * Ordinarily we would pause briefly to allow the debugger to set
306 * breakpoints and so on, but for "suspend=y" the VM init code will
307 * pause the VM when it sends the VM_START message.
308 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700309 }
310
Elliott Hughes761928d2011-11-16 18:33:03 -0800311 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700312}
313
314/*
315 * Reset all session-related state. There should not be an active connection
316 * to the client at this point. The rest of the VM still thinks there is
317 * a debugger attached.
318 *
319 * This includes freeing up the debugger event list.
320 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700321void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700322 /* could reset the serial numbers, but no need to */
323
Elliott Hughes761928d2011-11-16 18:33:03 -0800324 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700325 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700326 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200327 CHECK(event_list_ == nullptr);
Elliott Hughesf8349362012-06-18 15:00:06 -0700328 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700329
330 /*
331 * Should not have one of these in progress. If the debugger went away
332 * mid-request, though, we could see this.
333 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100334 if (jdwp_token_owner_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800335 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 DCHECK(false);
337 }
338}
339
340/*
341 * Tell the JDWP thread to shut down. Frees "state".
342 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700343JdwpState::~JdwpState() {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200344 if (netState != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700345 /*
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100346 * Close down the network to inspire the thread to halt. If a request is being processed,
347 * we need to wait for it to finish first.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700348 */
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100349 {
350 Thread* self = Thread::Current();
351 MutexLock mu(self, shutdown_lock_);
352 while (processing_request_) {
353 VLOG(jdwp) << "JDWP command in progress: wait for it to finish ...";
354 shutdown_cond_.Wait(self);
355 }
356
357 VLOG(jdwp) << "JDWP shutting down net...";
358 netState->Shutdown();
359 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700360
Elliott Hughes376a7a02011-10-24 18:35:55 -0700361 if (debug_thread_started_) {
362 run = false;
363 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700364 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700365 LOG(WARNING) << "JDWP thread join failed";
366 }
367 }
368
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800369 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700370 delete netState;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200371 netState = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700372 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200373 CHECK(netState == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700374
Elliott Hughes376a7a02011-10-24 18:35:55 -0700375 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700376}
377
378/*
379 * Are we talking to a debugger?
380 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700381bool JdwpState::IsActive() {
382 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383}
384
Elliott Hughescb693062013-02-21 09:48:08 -0800385// Returns "false" if we encounter a connection-fatal error.
386bool JdwpState::HandlePacket() {
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100387 Thread* const self = Thread::Current();
388 {
389 MutexLock mu(self, shutdown_lock_);
390 processing_request_ = true;
391 }
392 JdwpNetStateBase* netStateBase = netState;
393 CHECK(netStateBase != nullptr) << "Connection has been closed";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700394 JDWP::Request request(netStateBase->input_buffer_, netStateBase->input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -0800395
396 ExpandBuf* pReply = expandBufAlloc();
Ian Rogersc0542af2014-09-03 16:16:56 -0700397 size_t replyLength = ProcessRequest(&request, pReply);
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100398 ssize_t cc = netStateBase->WritePacket(pReply, replyLength);
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100399
400 /*
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100401 * We processed this request and sent its reply so we can release the JDWP token.
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100402 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100403 ReleaseJdwpTokenForCommand();
Sebastien Hertz99660e12014-02-19 15:04:42 +0100404
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100405 if (cc != static_cast<ssize_t>(replyLength)) {
Elliott Hughescb693062013-02-21 09:48:08 -0800406 PLOG(ERROR) << "Failed sending reply to debugger";
407 expandBufFree(pReply);
408 return false;
409 }
410 expandBufFree(pReply);
411 netStateBase->ConsumeBytes(request.GetLength());
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100412 {
413 MutexLock mu(self, shutdown_lock_);
414 processing_request_ = false;
415 shutdown_cond_.Broadcast(self);
416 }
Elliott Hughescb693062013-02-21 09:48:08 -0800417 return true;
418}
419
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420/*
421 * Entry point for JDWP thread. The thread was created through the VM
422 * mechanisms, so there is a java/lang/Thread associated with us.
423 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700424static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700425 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200426 CHECK(state != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427
Elliott Hughes376a7a02011-10-24 18:35:55 -0700428 state->Run();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200429 return nullptr;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700430}
431
432void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800434 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
Mathieu Chartier184c9dc2015-03-05 13:20:54 -0800435 !runtime->IsAotCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800437 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438
439 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700440 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 * we're running.
442 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700443 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700444 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700445
Ian Rogers81d425b2012-09-27 16:03:43 -0700446 {
447 MutexLock locker(thread_, thread_start_lock_);
448 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700449 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700450 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700453 CHECK_EQ(thread_->GetState(), kNative);
Ian Rogers62d6c772013-02-27 08:32:07 -0800454 Locks::mutator_lock_->AssertNotHeld(thread_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700455 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700456
457 /*
458 * Loop forever if we're in server mode, processing connections. In
459 * non-server mode, we bail out of the thread when the debugger drops
460 * us.
461 *
462 * We broadcast a notification when a debugger attaches, after we
463 * successfully process the handshake.
464 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700465 while (run) {
466 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467 /*
468 * Block forever, waiting for a connection. To support the
469 * "timeout=xxx" option we'll need to tweak this.
470 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700471 if (!netState->Accept()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472 break;
473 }
474 } else {
475 /*
476 * If we're not acting as a server, we need to connect out to the
477 * debugger. To support the "timeout=xxx" option we need to
478 * have a timeout if the handshake reply isn't received in a
479 * reasonable amount of time.
480 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700481 if (!netState->Establish(options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700483 MutexLock mu(thread_, attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200484 debug_thread_id_ = static_cast<ObjectId>(-1);
Ian Rogersc604d732012-10-14 16:09:54 -0700485 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486 break;
487 }
488 }
489
490 /* prep debug code to handle the new connection */
491 Dbg::Connected();
492
493 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700494 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800495 while (!Dbg::IsDisposed()) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200496 // sanity check -- shouldn't happen?
497 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700498
Elliott Hughes5d10a872013-04-17 19:26:43 -0700499 if (!netState->ProcessIncoming()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 /* blocking read */
501 break;
502 }
503
Elliott Hughes64f574f2013-02-20 14:57:12 -0800504 if (should_exit_) {
505 exit(exit_status_);
506 }
507
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700508 if (first && !netState->IsAwaitingHandshake()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 /* handshake worked, tell the interpreter that we're active */
510 first = false;
511
512 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 {
514 ScopedObjectAccess soa(thread_);
515 debug_thread_id_ = Dbg::GetThreadSelfId();
516 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700517
518 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700519 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700520 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 }
522 }
523
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700524 netState->Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525
Elliott Hughesa21039c2012-06-21 12:09:25 -0700526 if (ddm_is_active_) {
527 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700528
529 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700532 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700533 }
534
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700535 {
536 ScopedObjectAccess soa(thread_);
Elliott Hughes0f827162013-02-26 12:12:58 -0800537
538 // Release session state, e.g. remove breakpoint instructions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700539 ResetState();
540 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800541 // Tell the rest of the runtime that the debugger is no longer around.
542 Dbg::Disconnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700543
544 /* if we had threads suspended, resume them now */
545 Dbg::UndoDebuggerSuspensions();
546
547 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700548 if (!options_->server) {
549 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700550 }
551 }
552
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700553 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700554 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
555 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700556
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800557 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700558 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700559}
560
Elliott Hughesa21039c2012-06-21 12:09:25 -0700561void JdwpState::NotifyDdmsActive() {
562 if (!ddm_is_active_) {
563 ddm_is_active_ = true;
564 Dbg::DdmConnected();
565 }
566}
567
Elliott Hughes475fc232011-10-25 15:00:35 -0700568Thread* JdwpState::GetDebugThread() {
569 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570}
571
572/*
573 * Support routines for waitForDebugger().
574 *
575 * We can't have a trivial "waitForDebugger" function that returns the
576 * instant the debugger connects, because we run the risk of executing code
577 * before the debugger has had a chance to configure breakpoints or issue
578 * suspend calls. It would be nice to just sit in the suspended state, but
579 * most debuggers don't expect any threads to be suspended when they attach.
580 *
581 * There's no JDWP event we can post to tell the debugger, "we've stopped,
582 * and we like it that way". We could send a fake breakpoint, which should
583 * cause the debugger to immediately send a resume, but the debugger might
584 * send the resume immediately or might throw an exception of its own upon
585 * receiving a breakpoint event that it didn't ask for.
586 *
587 * What we really want is a "wait until the debugger is done configuring
588 * stuff" event. We can approximate this with a "wait until the debugger
589 * has been idle for a brief period".
590 */
591
592/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700593 * Return the time, in milliseconds, since the last debugger activity.
594 *
595 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
596 * processing a debugger request.
597 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700598int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700599 if (!Dbg::IsDebuggerActive()) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700600 LOG(WARNING) << "no active debugger";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700601 return -1;
602 }
603
Ian Rogers37f3c962014-07-17 11:25:30 -0700604 int64_t last = last_activity_time_ms_.LoadSequentiallyConsistent();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605
606 /* initializing or in the middle of something? */
607 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800608 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609 return 0;
610 }
611
612 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700613 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800614 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800616 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617 return now - last;
618}
619
Elliott Hughes64f574f2013-02-20 14:57:12 -0800620void JdwpState::ExitAfterReplying(int exit_status) {
621 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
622 should_exit_ = true;
623 exit_status_ = exit_status;
624}
625
Elliott Hughes03181a82011-11-17 17:22:21 -0800626std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800627 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800628 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800629 << "@" << StringPrintf("%#" PRIx64, rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800630 return os;
631}
632
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800633bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700634 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
635 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800636}
637
638bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
639 return !(lhs == rhs);
640}
641
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800642bool operator==(const JdwpOptions& lhs, const JdwpOptions& rhs) {
643 if (&lhs == &rhs) {
644 return true;
645 }
646
647 return lhs.transport == rhs.transport &&
648 lhs.server == rhs.server &&
649 lhs.suspend == rhs.suspend &&
650 lhs.host == rhs.host &&
651 lhs.port == rhs.port;
652}
653
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700654} // namespace JDWP
655
656} // namespace art