blob: e23d80d119c25aa31a8b60d24d65b68ecc63ced7 [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 */
38JdwpNetStateBase::JdwpNetStateBase() : socket_lock_("JdwpNetStateBase lock") {
39 clientSock = -1;
Elliott Hughescb693062013-02-21 09:48:08 -080040 inputCount = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070041 awaiting_handshake_ = false;
Elliott Hughescb693062013-02-21 09:48:08 -080042}
43
44void JdwpNetStateBase::ConsumeBytes(size_t count) {
45 CHECK_GT(count, 0U);
46 CHECK_LE(count, inputCount);
47
48 if (count == inputCount) {
49 inputCount = 0;
50 return;
51 }
52
53 memmove(inputBuffer, inputBuffer + count, inputCount - count);
54 inputCount -= count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070055}
56
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070057bool JdwpNetStateBase::HaveFullPacket() {
58 if (awaiting_handshake_) {
59 return (inputCount >= kMagicHandshakeLen);
60 }
61 if (inputCount < 4) {
62 return false;
63 }
64 uint32_t length = Get4BE(inputBuffer);
65 return (inputCount >= length);
66}
67
68bool JdwpNetStateBase::IsAwaitingHandshake() {
69 return awaiting_handshake_;
70}
71
72void JdwpNetStateBase::SetAwaitingHandshake(bool new_state) {
73 awaiting_handshake_ = new_state;
74}
75
76bool JdwpNetStateBase::IsConnected() {
77 return clientSock >= 0;
78}
79
80// Close a connection from a debugger (which may have already dropped us).
81// Resets the state so we're ready to receive a new connection.
82// Only called from the JDWP thread.
83void JdwpNetStateBase::Close() {
84 if (clientSock < 0) {
85 return;
86 }
87
88 VLOG(jdwp) << "+++ closing JDWP connection on fd " << clientSock;
89
90 close(clientSock);
91 clientSock = -1;
92}
93
Elliott Hughes872d4ec2011-10-21 17:07:15 -070094/*
95 * Write a packet. Grabs a mutex to assure atomicity.
96 */
Elliott Hughescb693062013-02-21 09:48:08 -080097ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply) {
Ian Rogers50b35e22012-10-04 10:09:15 -070098 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughes068193c2013-04-15 16:05:28 -070099 return TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply)));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100}
101
102/*
103 * Write a buffered packet. Grabs a mutex to assure atomicity.
104 */
Elliott Hughescb693062013-02-21 09:48:08 -0800105ssize_t JdwpNetStateBase::WriteBufferedPacket(const iovec* iov, int iov_count) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700106 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughes068193c2013-04-15 16:05:28 -0700107 return TEMP_FAILURE_RETRY(writev(clientSock, iov, iov_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108}
109
Elliott Hughes376a7a02011-10-24 18:35:55 -0700110bool JdwpState::IsConnected() {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700111 return netState != NULL && netState->IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700112}
113
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700114void JdwpState::SendBufferedRequest(uint32_t type, const iovec* iov, int iov_count) {
115 if (netState->clientSock < 0) {
116 // Can happen with some DDMS events.
117 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
118 return;
119 }
120
121 size_t expected = 0;
122 for (int i = 0; i < iov_count; ++i) {
123 expected += iov[i].iov_len;
124 }
125
126 errno = 0;
127 ssize_t actual = netState->WriteBufferedPacket(iov, iov_count);
128 if (static_cast<size_t>(actual) != expected) {
129 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet %c%c%c%c to debugger (%d of %d)",
130 static_cast<uint8_t>(type >> 24),
131 static_cast<uint8_t>(type >> 16),
132 static_cast<uint8_t>(type >> 8),
133 static_cast<uint8_t>(type),
134 actual, expected);
135 }
136}
137
138void JdwpState::SendRequest(ExpandBuf* pReq) {
139 if (netState->clientSock < 0) {
140 // Can happen with some DDMS events.
141 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
142 return;
143 }
144
145 errno = 0;
146 ssize_t actual = netState->WritePacket(pReq);
147 if (static_cast<size_t>(actual) != expandBufGetLength(pReq)) {
148 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet to debugger (%d of %d)",
149 actual, expandBufGetLength(pReq));
150 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151}
152
Elliott Hughes376a7a02011-10-24 18:35:55 -0700153/*
154 * Get the next "request" serial number. We use this when sending
155 * packets to the debugger.
156 */
157uint32_t JdwpState::NextRequestSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700158 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700159 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700160}
161
Elliott Hughes376a7a02011-10-24 18:35:55 -0700162/*
163 * Get the next "event" serial number. We use this in the response to
164 * message type EventRequest.Set.
165 */
166uint32_t JdwpState::NextEventSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700167 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700168 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169}
170
Elliott Hughes376a7a02011-10-24 18:35:55 -0700171JdwpState::JdwpState(const JdwpOptions* options)
172 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800173 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700174 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 pthread_(0),
176 thread_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700177 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700178 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 run(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700180 transport_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181 netState(NULL),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800182 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700183 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700184 last_activity_time_ms_(0),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700185 serial_lock_("JDWP serial lock", kJdwpSerialLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700186 request_serial_(0x10000000),
187 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800188 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700189 event_list_(NULL),
190 event_list_size_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191 event_thread_lock_("JDWP event thread lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700192 event_thread_cond_("JDWP event thread condition variable", event_thread_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700193 event_thread_id_(0),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800194 ddm_is_active_(false),
195 should_exit_(false),
196 exit_status_(0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197}
198
199/*
200 * Initialize JDWP.
201 *
202 * Does not return until JDWP thread is running, but may return before
203 * the thread is accepting network connections.
204 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700205JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700206 Thread* self = Thread::Current();
207 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes761928d2011-11-16 18:33:03 -0800208 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700209 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210 case kJdwpTransportSocket:
211 // LOGD("prepping for JDWP over TCP");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700212 state->transport_ = SocketTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213 break;
214#ifdef HAVE_ANDROID_OS
215 case kJdwpTransportAndroidAdb:
216 // LOGD("prepping for JDWP over ADB");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700217 state->transport_ = AndroidAdbTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218 break;
219#endif
220 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700221 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222 }
223
Elliott Hughesa21039c2012-06-21 12:09:25 -0700224 if (!(*state->transport_->startup)(state.get(), options)) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800225 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 }
227
228 /*
229 * Grab a mutex or two before starting the thread. This ensures they
230 * won't signal the cond var before we're waiting.
231 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700233 MutexLock thread_start_locker(self, state->thread_start_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234 const bool should_suspend = options->suspend;
235 if (!should_suspend) {
236 /*
237 * We have bound to a port, or are trying to connect outbound to a
238 * debugger. Create the JDWP thread and let it continue the mission.
239 */
240 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 /*
243 * Wait until the thread finishes basic initialization.
244 * TODO: cond vars should be waited upon in a loop
245 */
Ian Rogersc604d732012-10-14 16:09:54 -0700246 state->thread_start_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 } else {
248 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 /*
250 * We have bound to a port, or are trying to connect outbound to a
251 * debugger. Create the JDWP thread and let it continue the mission.
252 */
253 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700254
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 /*
256 * Wait until the thread finishes basic initialization.
257 * TODO: cond vars should be waited upon in a loop
258 */
Ian Rogersc604d732012-10-14 16:09:54 -0700259 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 /*
262 * For suspend=y, wait for the debugger to connect to us or for us to
263 * connect to the debugger.
264 *
265 * The JDWP thread will signal us when it connects successfully or
266 * times out (for timeout=xxx), so we have to check to see what happened
267 * when we wake up.
268 */
269 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700270 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
jeffhao0dfbb7e2012-11-28 15:26:03 -0800271 MutexLock attach_locker(self, state->attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700272 state->attach_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 }
274 }
275 if (!state->IsActive()) {
276 LOG(ERROR) << "JDWP connection failed";
277 return NULL;
278 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700279
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 LOG(INFO) << "JDWP connected";
281
282 /*
283 * Ordinarily we would pause briefly to allow the debugger to set
284 * breakpoints and so on, but for "suspend=y" the VM init code will
285 * pause the VM when it sends the VM_START message.
286 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700287 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700288 }
289
Elliott Hughes761928d2011-11-16 18:33:03 -0800290 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700291}
292
293/*
294 * Reset all session-related state. There should not be an active connection
295 * to the client at this point. The rest of the VM still thinks there is
296 * a debugger attached.
297 *
298 * This includes freeing up the debugger event list.
299 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700300void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700301 /* could reset the serial numbers, but no need to */
302
Elliott Hughes761928d2011-11-16 18:33:03 -0800303 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700304 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700305 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700306 CHECK(event_list_ == NULL);
307 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308
309 /*
310 * Should not have one of these in progress. If the debugger went away
311 * mid-request, though, we could see this.
312 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700313 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800314 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700315 DCHECK(false);
316 }
317}
318
319/*
320 * Tell the JDWP thread to shut down. Frees "state".
321 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700322JdwpState::~JdwpState() {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700323 if (transport_ != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700324 if (IsConnected()) {
325 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700326 }
327
328 /*
329 * Close down the network to inspire the thread to halt.
330 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800331 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700332 (*transport_->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333
Elliott Hughes376a7a02011-10-24 18:35:55 -0700334 if (debug_thread_started_) {
335 run = false;
336 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700337 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 LOG(WARNING) << "JDWP thread join failed";
339 }
340 }
341
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800342 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700343 (*transport_->free)(this);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700344 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700345 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700346 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347
Elliott Hughes376a7a02011-10-24 18:35:55 -0700348 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349}
350
351/*
352 * Are we talking to a debugger?
353 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700354bool JdwpState::IsActive() {
355 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356}
357
Elliott Hughescb693062013-02-21 09:48:08 -0800358// Returns "false" if we encounter a connection-fatal error.
359bool JdwpState::HandlePacket() {
360 JdwpNetStateBase* netStateBase = reinterpret_cast<JdwpNetStateBase*>(netState);
361 JDWP::Request request(netStateBase->inputBuffer, netStateBase->inputCount);
362
363 ExpandBuf* pReply = expandBufAlloc();
364 ProcessRequest(request, pReply);
365 ssize_t cc = netStateBase->WritePacket(pReply);
366 if (cc != (ssize_t) expandBufGetLength(pReply)) {
367 PLOG(ERROR) << "Failed sending reply to debugger";
368 expandBufFree(pReply);
369 return false;
370 }
371 expandBufFree(pReply);
372 netStateBase->ConsumeBytes(request.GetLength());
373 return true;
374}
375
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700376/*
377 * Entry point for JDWP thread. The thread was created through the VM
378 * mechanisms, so there is a java/lang/Thread associated with us.
379 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700380static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
382 CHECK(state != NULL);
383
Elliott Hughes376a7a02011-10-24 18:35:55 -0700384 state->Run();
385 return NULL;
386}
387
388void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700389 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800390 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
391 !runtime->IsCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700392
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800393 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700394
395 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700396 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700397 * we're running.
398 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700399 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700400 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700401
Ian Rogers81d425b2012-09-27 16:03:43 -0700402 {
403 MutexLock locker(thread_, thread_start_lock_);
404 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700405 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700406 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700409 CHECK_EQ(thread_->GetState(), kNative);
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 Locks::mutator_lock_->AssertNotHeld(thread_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700411 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412
413 /*
414 * Loop forever if we're in server mode, processing connections. In
415 * non-server mode, we bail out of the thread when the debugger drops
416 * us.
417 *
418 * We broadcast a notification when a debugger attaches, after we
419 * successfully process the handshake.
420 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700421 while (run) {
422 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423 /*
424 * Block forever, waiting for a connection. To support the
425 * "timeout=xxx" option we'll need to tweak this.
426 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700427 if (!(*transport_->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428 break;
429 }
430 } else {
431 /*
432 * If we're not acting as a server, we need to connect out to the
433 * debugger. To support the "timeout=xxx" option we need to
434 * have a timeout if the handshake reply isn't received in a
435 * reasonable amount of time.
436 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700437 if (!(*transport_->establish)(this, options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700439 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700440 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 break;
442 }
443 }
444
445 /* prep debug code to handle the new connection */
446 Dbg::Connected();
447
448 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700449 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800450 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451 {
452 // sanity check -- shouldn't happen?
Ian Rogers81d425b2012-09-27 16:03:43 -0700453 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700454 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700455 }
456
Elliott Hughesa21039c2012-06-21 12:09:25 -0700457 if (!(*transport_->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700458 /* blocking read */
459 break;
460 }
461
Elliott Hughes64f574f2013-02-20 14:57:12 -0800462 if (should_exit_) {
463 exit(exit_status_);
464 }
465
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700466 if (first && !netState->IsAwaitingHandshake()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467 /* handshake worked, tell the interpreter that we're active */
468 first = false;
469
470 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700471 {
472 ScopedObjectAccess soa(thread_);
473 debug_thread_id_ = Dbg::GetThreadSelfId();
474 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475
476 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700477 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700478 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 }
480 }
481
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700482 netState->Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483
Elliott Hughesa21039c2012-06-21 12:09:25 -0700484 if (ddm_is_active_) {
485 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486
487 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700489 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700491 }
492
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 {
494 ScopedObjectAccess soa(thread_);
Elliott Hughes0f827162013-02-26 12:12:58 -0800495
496 // Release session state, e.g. remove breakpoint instructions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700497 ResetState();
498 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800499 // Tell the rest of the runtime that the debugger is no longer around.
500 Dbg::Disconnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700501
502 /* if we had threads suspended, resume them now */
503 Dbg::UndoDebuggerSuspensions();
504
505 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700506 if (!options_->server) {
507 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508 }
509 }
510
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700512 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
513 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800515 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700516 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700517}
518
Elliott Hughesa21039c2012-06-21 12:09:25 -0700519void JdwpState::NotifyDdmsActive() {
520 if (!ddm_is_active_) {
521 ddm_is_active_ = true;
522 Dbg::DdmConnected();
523 }
524}
525
Elliott Hughes475fc232011-10-25 15:00:35 -0700526Thread* JdwpState::GetDebugThread() {
527 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700528}
529
530/*
531 * Support routines for waitForDebugger().
532 *
533 * We can't have a trivial "waitForDebugger" function that returns the
534 * instant the debugger connects, because we run the risk of executing code
535 * before the debugger has had a chance to configure breakpoints or issue
536 * suspend calls. It would be nice to just sit in the suspended state, but
537 * most debuggers don't expect any threads to be suspended when they attach.
538 *
539 * There's no JDWP event we can post to tell the debugger, "we've stopped,
540 * and we like it that way". We could send a fake breakpoint, which should
541 * cause the debugger to immediately send a resume, but the debugger might
542 * send the resume immediately or might throw an exception of its own upon
543 * receiving a breakpoint event that it didn't ask for.
544 *
545 * What we really want is a "wait until the debugger is done configuring
546 * stuff" event. We can approximate this with a "wait until the debugger
547 * has been idle for a brief period".
548 */
549
550/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700551 * Return the time, in milliseconds, since the last debugger activity.
552 *
553 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
554 * processing a debugger request.
555 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700556int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700557 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700558 LOG(DEBUG) << "no active debugger";
559 return -1;
560 }
561
Elliott Hughesa21039c2012-06-21 12:09:25 -0700562 int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563
564 /* initializing or in the middle of something? */
565 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800566 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700567 return 0;
568 }
569
570 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700571 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800572 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800574 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575 return now - last;
576}
577
Elliott Hughes64f574f2013-02-20 14:57:12 -0800578void JdwpState::ExitAfterReplying(int exit_status) {
579 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
580 should_exit_ = true;
581 exit_status_ = exit_status;
582}
583
Elliott Hughes03181a82011-11-17 17:22:21 -0800584std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800585 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800586 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Elliott Hughes74847412012-06-20 18:10:21 -0700587 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800588 return os;
589}
590
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800591bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700592 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
593 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800594}
595
596bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
597 return !(lhs == rhs);
598}
599
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700600} // namespace JDWP
601
602} // namespace art