blob: cbac920baa761d9a3ba9cc39d8eecdd7197bb428 [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;
40}
41
42/*
43 * Write a packet. Grabs a mutex to assure atomicity.
44 */
45ssize_t JdwpNetStateBase::writePacket(ExpandBuf* pReply) {
Ian Rogers50b35e22012-10-04 10:09:15 -070046 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070047 return write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply));
48}
49
50/*
51 * Write a buffered packet. Grabs a mutex to assure atomicity.
52 */
Elliott Hughescccd84f2011-12-05 16:51:54 -080053ssize_t JdwpNetStateBase::writeBufferedPacket(const iovec* iov, int iov_count) {
Ian Rogers50b35e22012-10-04 10:09:15 -070054 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughescccd84f2011-12-05 16:51:54 -080055 return writev(clientSock, iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056}
57
Elliott Hughes376a7a02011-10-24 18:35:55 -070058bool JdwpState::IsConnected() {
Elliott Hughesa21039c2012-06-21 12:09:25 -070059 return (*transport_->isConnected)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070060}
61
Elliott Hughes376a7a02011-10-24 18:35:55 -070062bool JdwpState::SendRequest(ExpandBuf* pReq) {
Elliott Hughesa21039c2012-06-21 12:09:25 -070063 return (*transport_->sendRequest)(this, pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070064}
65
Elliott Hughes376a7a02011-10-24 18:35:55 -070066/*
67 * Get the next "request" serial number. We use this when sending
68 * packets to the debugger.
69 */
70uint32_t JdwpState::NextRequestSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -070071 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070072 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070073}
74
Elliott Hughes376a7a02011-10-24 18:35:55 -070075/*
76 * Get the next "event" serial number. We use this in the response to
77 * message type EventRequest.Set.
78 */
79uint32_t JdwpState::NextEventSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -070080 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070081 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070082}
83
Elliott Hughes376a7a02011-10-24 18:35:55 -070084JdwpState::JdwpState(const JdwpOptions* options)
85 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -080086 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -070087 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 pthread_(0),
89 thread_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070090 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -070091 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070092 run(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -070093 transport_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070094 netState(NULL),
jeffhao0dfbb7e2012-11-28 15:26:03 -080095 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -070096 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -070097 last_activity_time_ms_(0),
Ian Rogers15bf2d32012-08-28 17:33:04 -070098 serial_lock_("JDWP serial lock", kJdwpSerialLock),
Elliott Hughesf8349362012-06-18 15:00:06 -070099 request_serial_(0x10000000),
100 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800101 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700102 event_list_(NULL),
103 event_list_size_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700104 event_thread_lock_("JDWP event thread lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700105 event_thread_cond_("JDWP event thread condition variable", event_thread_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700106 event_thread_id_(0),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800107 ddm_is_active_(false),
108 should_exit_(false),
109 exit_status_(0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700110}
111
112/*
113 * Initialize JDWP.
114 *
115 * Does not return until JDWP thread is running, but may return before
116 * the thread is accepting network connections.
117 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700118JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700119 Thread* self = Thread::Current();
120 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes761928d2011-11-16 18:33:03 -0800121 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700122 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 case kJdwpTransportSocket:
124 // LOGD("prepping for JDWP over TCP");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700125 state->transport_ = SocketTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 break;
127#ifdef HAVE_ANDROID_OS
128 case kJdwpTransportAndroidAdb:
129 // LOGD("prepping for JDWP over ADB");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700130 state->transport_ = AndroidAdbTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131 break;
132#endif
133 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700134 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135 }
136
Elliott Hughesa21039c2012-06-21 12:09:25 -0700137 if (!(*state->transport_->startup)(state.get(), options)) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800138 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139 }
140
141 /*
142 * Grab a mutex or two before starting the thread. This ensures they
143 * won't signal the cond var before we're waiting.
144 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700146 MutexLock thread_start_locker(self, state->thread_start_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 const bool should_suspend = options->suspend;
148 if (!should_suspend) {
149 /*
150 * We have bound to a port, or are trying to connect outbound to a
151 * debugger. Create the JDWP thread and let it continue the mission.
152 */
153 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 /*
156 * Wait until the thread finishes basic initialization.
157 * TODO: cond vars should be waited upon in a loop
158 */
Ian Rogersc604d732012-10-14 16:09:54 -0700159 state->thread_start_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 } else {
161 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 /*
163 * We have bound to a port, or are trying to connect outbound to a
164 * debugger. Create the JDWP thread and let it continue the mission.
165 */
166 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 /*
169 * Wait until the thread finishes basic initialization.
170 * TODO: cond vars should be waited upon in a loop
171 */
Ian Rogersc604d732012-10-14 16:09:54 -0700172 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700173
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 /*
175 * For suspend=y, wait for the debugger to connect to us or for us to
176 * connect to the debugger.
177 *
178 * The JDWP thread will signal us when it connects successfully or
179 * times out (for timeout=xxx), so we have to check to see what happened
180 * when we wake up.
181 */
182 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700183 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
jeffhao0dfbb7e2012-11-28 15:26:03 -0800184 MutexLock attach_locker(self, state->attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700185 state->attach_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700186 }
187 }
188 if (!state->IsActive()) {
189 LOG(ERROR) << "JDWP connection failed";
190 return NULL;
191 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700192
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193 LOG(INFO) << "JDWP connected";
194
195 /*
196 * Ordinarily we would pause briefly to allow the debugger to set
197 * breakpoints and so on, but for "suspend=y" the VM init code will
198 * pause the VM when it sends the VM_START message.
199 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 }
202
Elliott Hughes761928d2011-11-16 18:33:03 -0800203 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700204}
205
206/*
207 * Reset all session-related state. There should not be an active connection
208 * to the client at this point. The rest of the VM still thinks there is
209 * a debugger attached.
210 *
211 * This includes freeing up the debugger event list.
212 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700213void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700214 /* could reset the serial numbers, but no need to */
215
Elliott Hughes761928d2011-11-16 18:33:03 -0800216 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700217 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700218 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700219 CHECK(event_list_ == NULL);
220 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221
222 /*
223 * Should not have one of these in progress. If the debugger went away
224 * mid-request, though, we could see this.
225 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700226 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800227 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 DCHECK(false);
229 }
230}
231
232/*
233 * Tell the JDWP thread to shut down. Frees "state".
234 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700235JdwpState::~JdwpState() {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700236 if (transport_ != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700237 if (IsConnected()) {
238 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700239 }
240
241 /*
242 * Close down the network to inspire the thread to halt.
243 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800244 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700245 (*transport_->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700246
Elliott Hughes376a7a02011-10-24 18:35:55 -0700247 if (debug_thread_started_) {
248 run = false;
249 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700250 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251 LOG(WARNING) << "JDWP thread join failed";
252 }
253 }
254
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800255 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700256 (*transport_->free)(this);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700257 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700259 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260
Elliott Hughes376a7a02011-10-24 18:35:55 -0700261 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262}
263
264/*
265 * Are we talking to a debugger?
266 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700267bool JdwpState::IsActive() {
268 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269}
270
271/*
272 * Entry point for JDWP thread. The thread was created through the VM
273 * mechanisms, so there is a java/lang/Thread associated with us.
274 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700275static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700276 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
277 CHECK(state != NULL);
278
Elliott Hughes376a7a02011-10-24 18:35:55 -0700279 state->Run();
280 return NULL;
281}
282
283void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800285 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
286 !runtime->IsCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700287
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800288 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700289
290 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700291 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700292 * we're running.
293 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700294 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700295 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700296
Ian Rogers81d425b2012-09-27 16:03:43 -0700297 {
298 MutexLock locker(thread_, thread_start_lock_);
299 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700300 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700301 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700302
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700304 CHECK_EQ(thread_->GetState(), kNative);
305 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306
307 /*
308 * Loop forever if we're in server mode, processing connections. In
309 * non-server mode, we bail out of the thread when the debugger drops
310 * us.
311 *
312 * We broadcast a notification when a debugger attaches, after we
313 * successfully process the handshake.
314 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700315 while (run) {
316 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700317 /*
318 * Block forever, waiting for a connection. To support the
319 * "timeout=xxx" option we'll need to tweak this.
320 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700321 if (!(*transport_->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700322 break;
323 }
324 } else {
325 /*
326 * If we're not acting as a server, we need to connect out to the
327 * debugger. To support the "timeout=xxx" option we need to
328 * have a timeout if the handshake reply isn't received in a
329 * reasonable amount of time.
330 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700331 if (!(*transport_->establish)(this, options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700332 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700333 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700334 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700335 break;
336 }
337 }
338
339 /* prep debug code to handle the new connection */
340 Dbg::Connected();
341
342 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700343 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800344 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 {
346 // sanity check -- shouldn't happen?
Ian Rogers81d425b2012-09-27 16:03:43 -0700347 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349 }
350
Elliott Hughesa21039c2012-06-21 12:09:25 -0700351 if (!(*transport_->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700352 /* blocking read */
353 break;
354 }
355
Elliott Hughes64f574f2013-02-20 14:57:12 -0800356 if (should_exit_) {
357 exit(exit_status_);
358 }
359
Elliott Hughesa21039c2012-06-21 12:09:25 -0700360 if (first && !(*transport_->awaitingHandshake)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361 /* handshake worked, tell the interpreter that we're active */
362 first = false;
363
364 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365 {
366 ScopedObjectAccess soa(thread_);
367 debug_thread_id_ = Dbg::GetThreadSelfId();
368 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700369
370 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700371 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700372 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 }
374 }
375
Elliott Hughesa21039c2012-06-21 12:09:25 -0700376 (*transport_->close)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700377
Elliott Hughesa21039c2012-06-21 12:09:25 -0700378 if (ddm_is_active_) {
379 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700380
381 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700385 }
386
387 /* release session state, e.g. remove breakpoint instructions */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 {
389 ScopedObjectAccess soa(thread_);
390 ResetState();
391 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700392 /* tell the interpreter that the debugger is no longer around */
393 Dbg::Disconnected();
394
395 /* if we had threads suspended, resume them now */
396 Dbg::UndoDebuggerSuspensions();
397
398 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700399 if (!options_->server) {
400 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700401 }
402 }
403
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700405 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
406 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800408 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700409 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700410}
411
Elliott Hughesa21039c2012-06-21 12:09:25 -0700412void JdwpState::NotifyDdmsActive() {
413 if (!ddm_is_active_) {
414 ddm_is_active_ = true;
415 Dbg::DdmConnected();
416 }
417}
418
Elliott Hughes475fc232011-10-25 15:00:35 -0700419Thread* JdwpState::GetDebugThread() {
420 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700421}
422
423/*
424 * Support routines for waitForDebugger().
425 *
426 * We can't have a trivial "waitForDebugger" function that returns the
427 * instant the debugger connects, because we run the risk of executing code
428 * before the debugger has had a chance to configure breakpoints or issue
429 * suspend calls. It would be nice to just sit in the suspended state, but
430 * most debuggers don't expect any threads to be suspended when they attach.
431 *
432 * There's no JDWP event we can post to tell the debugger, "we've stopped,
433 * and we like it that way". We could send a fake breakpoint, which should
434 * cause the debugger to immediately send a resume, but the debugger might
435 * send the resume immediately or might throw an exception of its own upon
436 * receiving a breakpoint event that it didn't ask for.
437 *
438 * What we really want is a "wait until the debugger is done configuring
439 * stuff" event. We can approximate this with a "wait until the debugger
440 * has been idle for a brief period".
441 */
442
443/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700444 * Return the time, in milliseconds, since the last debugger activity.
445 *
446 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
447 * processing a debugger request.
448 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700449int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700450 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 LOG(DEBUG) << "no active debugger";
452 return -1;
453 }
454
Elliott Hughesa21039c2012-06-21 12:09:25 -0700455 int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700456
457 /* initializing or in the middle of something? */
458 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800459 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700460 return 0;
461 }
462
463 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700464 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800465 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700466
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800467 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700468 return now - last;
469}
470
Elliott Hughes64f574f2013-02-20 14:57:12 -0800471void JdwpState::ExitAfterReplying(int exit_status) {
472 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
473 should_exit_ = true;
474 exit_status_ = exit_status;
475}
476
Elliott Hughes03181a82011-11-17 17:22:21 -0800477std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800478 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800479 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Elliott Hughes74847412012-06-20 18:10:21 -0700480 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800481 return os;
482}
483
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800484bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700485 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
486 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800487}
488
489bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
490 return !(lhs == rhs);
491}
492
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493} // namespace JDWP
494
495} // namespace art