blob: 4a18d48366eb7e1cd1217cf33bb8ebd65f7ae8f6 [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;
41}
42
43void JdwpNetStateBase::ConsumeBytes(size_t count) {
44 CHECK_GT(count, 0U);
45 CHECK_LE(count, inputCount);
46
47 if (count == inputCount) {
48 inputCount = 0;
49 return;
50 }
51
52 memmove(inputBuffer, inputBuffer + count, inputCount - count);
53 inputCount -= count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070054}
55
56/*
57 * Write a packet. Grabs a mutex to assure atomicity.
58 */
Elliott Hughescb693062013-02-21 09:48:08 -080059ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply) {
Ian Rogers50b35e22012-10-04 10:09:15 -070060 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070061 return write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply));
62}
63
64/*
65 * Write a buffered packet. Grabs a mutex to assure atomicity.
66 */
Elliott Hughescb693062013-02-21 09:48:08 -080067ssize_t JdwpNetStateBase::WriteBufferedPacket(const iovec* iov, int iov_count) {
Ian Rogers50b35e22012-10-04 10:09:15 -070068 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughescccd84f2011-12-05 16:51:54 -080069 return writev(clientSock, iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070070}
71
Elliott Hughes376a7a02011-10-24 18:35:55 -070072bool JdwpState::IsConnected() {
Elliott Hughesa21039c2012-06-21 12:09:25 -070073 return (*transport_->isConnected)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070074}
75
Elliott Hughes376a7a02011-10-24 18:35:55 -070076bool JdwpState::SendRequest(ExpandBuf* pReq) {
Elliott Hughesa21039c2012-06-21 12:09:25 -070077 return (*transport_->sendRequest)(this, pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078}
79
Elliott Hughes376a7a02011-10-24 18:35:55 -070080/*
81 * Get the next "request" serial number. We use this when sending
82 * packets to the debugger.
83 */
84uint32_t JdwpState::NextRequestSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -070085 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070086 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070087}
88
Elliott Hughes376a7a02011-10-24 18:35:55 -070089/*
90 * Get the next "event" serial number. We use this in the response to
91 * message type EventRequest.Set.
92 */
93uint32_t JdwpState::NextEventSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -070094 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070095 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070096}
97
Elliott Hughes376a7a02011-10-24 18:35:55 -070098JdwpState::JdwpState(const JdwpOptions* options)
99 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800100 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700101 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 pthread_(0),
103 thread_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700104 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700105 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700106 run(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700107 transport_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108 netState(NULL),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800109 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700110 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700111 last_activity_time_ms_(0),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700112 serial_lock_("JDWP serial lock", kJdwpSerialLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700113 request_serial_(0x10000000),
114 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800115 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700116 event_list_(NULL),
117 event_list_size_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700118 event_thread_lock_("JDWP event thread lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700119 event_thread_cond_("JDWP event thread condition variable", event_thread_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700120 event_thread_id_(0),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800121 ddm_is_active_(false),
122 should_exit_(false),
123 exit_status_(0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124}
125
126/*
127 * Initialize JDWP.
128 *
129 * Does not return until JDWP thread is running, but may return before
130 * the thread is accepting network connections.
131 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700132JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700133 Thread* self = Thread::Current();
134 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes761928d2011-11-16 18:33:03 -0800135 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700136 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700137 case kJdwpTransportSocket:
138 // LOGD("prepping for JDWP over TCP");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700139 state->transport_ = SocketTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140 break;
141#ifdef HAVE_ANDROID_OS
142 case kJdwpTransportAndroidAdb:
143 // LOGD("prepping for JDWP over ADB");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700144 state->transport_ = AndroidAdbTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145 break;
146#endif
147 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700148 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700149 }
150
Elliott Hughesa21039c2012-06-21 12:09:25 -0700151 if (!(*state->transport_->startup)(state.get(), options)) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800152 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700153 }
154
155 /*
156 * Grab a mutex or two before starting the thread. This ensures they
157 * won't signal the cond var before we're waiting.
158 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700160 MutexLock thread_start_locker(self, state->thread_start_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 const bool should_suspend = options->suspend;
162 if (!should_suspend) {
163 /*
164 * We have bound to a port, or are trying to connect outbound to a
165 * debugger. Create the JDWP thread and let it continue the mission.
166 */
167 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169 /*
170 * Wait until the thread finishes basic initialization.
171 * TODO: cond vars should be waited upon in a loop
172 */
Ian Rogersc604d732012-10-14 16:09:54 -0700173 state->thread_start_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 } else {
175 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 /*
177 * We have bound to a port, or are trying to connect outbound to a
178 * debugger. Create the JDWP thread and let it continue the mission.
179 */
180 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182 /*
183 * Wait until the thread finishes basic initialization.
184 * TODO: cond vars should be waited upon in a loop
185 */
Ian Rogersc604d732012-10-14 16:09:54 -0700186 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 /*
189 * For suspend=y, wait for the debugger to connect to us or for us to
190 * connect to the debugger.
191 *
192 * The JDWP thread will signal us when it connects successfully or
193 * times out (for timeout=xxx), so we have to check to see what happened
194 * when we wake up.
195 */
196 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700197 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
jeffhao0dfbb7e2012-11-28 15:26:03 -0800198 MutexLock attach_locker(self, state->attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700199 state->attach_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 }
201 }
202 if (!state->IsActive()) {
203 LOG(ERROR) << "JDWP connection failed";
204 return NULL;
205 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207 LOG(INFO) << "JDWP connected";
208
209 /*
210 * Ordinarily we would pause briefly to allow the debugger to set
211 * breakpoints and so on, but for "suspend=y" the VM init code will
212 * pause the VM when it sends the VM_START message.
213 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700214 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 }
216
Elliott Hughes761928d2011-11-16 18:33:03 -0800217 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218}
219
220/*
221 * Reset all session-related state. There should not be an active connection
222 * to the client at this point. The rest of the VM still thinks there is
223 * a debugger attached.
224 *
225 * This includes freeing up the debugger event list.
226 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700227void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 /* could reset the serial numbers, but no need to */
229
Elliott Hughes761928d2011-11-16 18:33:03 -0800230 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700231 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700232 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700233 CHECK(event_list_ == NULL);
234 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235
236 /*
237 * Should not have one of these in progress. If the debugger went away
238 * mid-request, though, we could see this.
239 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700240 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800241 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700242 DCHECK(false);
243 }
244}
245
246/*
247 * Tell the JDWP thread to shut down. Frees "state".
248 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700249JdwpState::~JdwpState() {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700250 if (transport_ != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700251 if (IsConnected()) {
252 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253 }
254
255 /*
256 * Close down the network to inspire the thread to halt.
257 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800258 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700259 (*transport_->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260
Elliott Hughes376a7a02011-10-24 18:35:55 -0700261 if (debug_thread_started_) {
262 run = false;
263 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700264 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 LOG(WARNING) << "JDWP thread join failed";
266 }
267 }
268
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800269 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700270 (*transport_->free)(this);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700271 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700272 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700273 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700274
Elliott Hughes376a7a02011-10-24 18:35:55 -0700275 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700276}
277
278/*
279 * Are we talking to a debugger?
280 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700281bool JdwpState::IsActive() {
282 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700283}
284
Elliott Hughescb693062013-02-21 09:48:08 -0800285// Returns "false" if we encounter a connection-fatal error.
286bool JdwpState::HandlePacket() {
287 JdwpNetStateBase* netStateBase = reinterpret_cast<JdwpNetStateBase*>(netState);
288 JDWP::Request request(netStateBase->inputBuffer, netStateBase->inputCount);
289
290 ExpandBuf* pReply = expandBufAlloc();
291 ProcessRequest(request, pReply);
292 ssize_t cc = netStateBase->WritePacket(pReply);
293 if (cc != (ssize_t) expandBufGetLength(pReply)) {
294 PLOG(ERROR) << "Failed sending reply to debugger";
295 expandBufFree(pReply);
296 return false;
297 }
298 expandBufFree(pReply);
299 netStateBase->ConsumeBytes(request.GetLength());
300 return true;
301}
302
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303/*
304 * Entry point for JDWP thread. The thread was created through the VM
305 * mechanisms, so there is a java/lang/Thread associated with us.
306 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700307static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
309 CHECK(state != NULL);
310
Elliott Hughes376a7a02011-10-24 18:35:55 -0700311 state->Run();
312 return NULL;
313}
314
315void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700316 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800317 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
318 !runtime->IsCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800320 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700321
322 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700323 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324 * we're running.
325 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700326 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700327 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328
Ian Rogers81d425b2012-09-27 16:03:43 -0700329 {
330 MutexLock locker(thread_, thread_start_lock_);
331 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700332 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700333 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700334
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700336 CHECK_EQ(thread_->GetState(), kNative);
337 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338
339 /*
340 * Loop forever if we're in server mode, processing connections. In
341 * non-server mode, we bail out of the thread when the debugger drops
342 * us.
343 *
344 * We broadcast a notification when a debugger attaches, after we
345 * successfully process the handshake.
346 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700347 while (run) {
348 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349 /*
350 * Block forever, waiting for a connection. To support the
351 * "timeout=xxx" option we'll need to tweak this.
352 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700353 if (!(*transport_->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354 break;
355 }
356 } else {
357 /*
358 * If we're not acting as a server, we need to connect out to the
359 * debugger. To support the "timeout=xxx" option we need to
360 * have a timeout if the handshake reply isn't received in a
361 * reasonable amount of time.
362 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700363 if (!(*transport_->establish)(this, options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700364 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700365 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700366 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367 break;
368 }
369 }
370
371 /* prep debug code to handle the new connection */
372 Dbg::Connected();
373
374 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700375 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800376 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 {
378 // sanity check -- shouldn't happen?
Ian Rogers81d425b2012-09-27 16:03:43 -0700379 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 }
382
Elliott Hughesa21039c2012-06-21 12:09:25 -0700383 if (!(*transport_->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700384 /* blocking read */
385 break;
386 }
387
Elliott Hughes64f574f2013-02-20 14:57:12 -0800388 if (should_exit_) {
389 exit(exit_status_);
390 }
391
Elliott Hughesa21039c2012-06-21 12:09:25 -0700392 if (first && !(*transport_->awaitingHandshake)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700393 /* handshake worked, tell the interpreter that we're active */
394 first = false;
395
396 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397 {
398 ScopedObjectAccess soa(thread_);
399 debug_thread_id_ = Dbg::GetThreadSelfId();
400 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700401
402 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700403 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700404 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405 }
406 }
407
Elliott Hughesa21039c2012-06-21 12:09:25 -0700408 (*transport_->close)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700409
Elliott Hughesa21039c2012-06-21 12:09:25 -0700410 if (ddm_is_active_) {
411 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412
413 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417 }
418
419 /* release session state, e.g. remove breakpoint instructions */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700420 {
421 ScopedObjectAccess soa(thread_);
422 ResetState();
423 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424 /* tell the interpreter that the debugger is no longer around */
425 Dbg::Disconnected();
426
427 /* if we had threads suspended, resume them now */
428 Dbg::UndoDebuggerSuspensions();
429
430 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700431 if (!options_->server) {
432 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433 }
434 }
435
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700436 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700437 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
438 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700439
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800440 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700441 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442}
443
Elliott Hughesa21039c2012-06-21 12:09:25 -0700444void JdwpState::NotifyDdmsActive() {
445 if (!ddm_is_active_) {
446 ddm_is_active_ = true;
447 Dbg::DdmConnected();
448 }
449}
450
Elliott Hughes475fc232011-10-25 15:00:35 -0700451Thread* JdwpState::GetDebugThread() {
452 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453}
454
455/*
456 * Support routines for waitForDebugger().
457 *
458 * We can't have a trivial "waitForDebugger" function that returns the
459 * instant the debugger connects, because we run the risk of executing code
460 * before the debugger has had a chance to configure breakpoints or issue
461 * suspend calls. It would be nice to just sit in the suspended state, but
462 * most debuggers don't expect any threads to be suspended when they attach.
463 *
464 * There's no JDWP event we can post to tell the debugger, "we've stopped,
465 * and we like it that way". We could send a fake breakpoint, which should
466 * cause the debugger to immediately send a resume, but the debugger might
467 * send the resume immediately or might throw an exception of its own upon
468 * receiving a breakpoint event that it didn't ask for.
469 *
470 * What we really want is a "wait until the debugger is done configuring
471 * stuff" event. We can approximate this with a "wait until the debugger
472 * has been idle for a brief period".
473 */
474
475/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700476 * Return the time, in milliseconds, since the last debugger activity.
477 *
478 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
479 * processing a debugger request.
480 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700481int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700482 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483 LOG(DEBUG) << "no active debugger";
484 return -1;
485 }
486
Elliott Hughesa21039c2012-06-21 12:09:25 -0700487 int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488
489 /* initializing or in the middle of something? */
490 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800491 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492 return 0;
493 }
494
495 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700496 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800497 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700498
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800499 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 return now - last;
501}
502
Elliott Hughes64f574f2013-02-20 14:57:12 -0800503void JdwpState::ExitAfterReplying(int exit_status) {
504 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
505 should_exit_ = true;
506 exit_status_ = exit_status;
507}
508
Elliott Hughes03181a82011-11-17 17:22:21 -0800509std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800510 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800511 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Elliott Hughes74847412012-06-20 18:10:21 -0700512 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800513 return os;
514}
515
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800516bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700517 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
518 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800519}
520
521bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
522 return !(lhs == rhs);
523}
524
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525} // namespace JDWP
526
527} // namespace art