blob: beec7afc8430c37f64e545631f71f97a14ee411e [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
17/*
18 * JDWP initialization.
19 */
20
21#include "atomic.h"
22#include "debugger.h"
23#include "jdwp/jdwp_priv.h"
24#include "logging.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070026
27#include <stdlib.h>
28#include <unistd.h>
29#include <sys/time.h>
30#include <time.h>
31#include <errno.h>
32
33namespace art {
34
35namespace JDWP {
36
Elliott Hughes376a7a02011-10-24 18:35:55 -070037static void* StartJdwpThread(void* arg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070038
39/*
40 * JdwpNetStateBase class implementation
41 */
42JdwpNetStateBase::JdwpNetStateBase() : socket_lock_("JdwpNetStateBase lock") {
43 clientSock = -1;
44}
45
46/*
47 * Write a packet. Grabs a mutex to assure atomicity.
48 */
49ssize_t JdwpNetStateBase::writePacket(ExpandBuf* pReply) {
50 MutexLock mu(socket_lock_);
51 return write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply));
52}
53
54/*
55 * Write a buffered packet. Grabs a mutex to assure atomicity.
56 */
Elliott Hughescccd84f2011-12-05 16:51:54 -080057ssize_t JdwpNetStateBase::writeBufferedPacket(const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070058 MutexLock mu(socket_lock_);
Elliott Hughescccd84f2011-12-05 16:51:54 -080059 return writev(clientSock, iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070060}
61
Elliott Hughes376a7a02011-10-24 18:35:55 -070062bool JdwpState::IsConnected() {
Elliott Hughesa21039c2012-06-21 12:09:25 -070063 return (*transport_->isConnected)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070064}
65
Elliott Hughes376a7a02011-10-24 18:35:55 -070066bool JdwpState::SendRequest(ExpandBuf* pReq) {
Elliott Hughesa21039c2012-06-21 12:09:25 -070067 return (*transport_->sendRequest)(this, pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068}
69
Elliott Hughes376a7a02011-10-24 18:35:55 -070070/*
71 * Get the next "request" serial number. We use this when sending
72 * packets to the debugger.
73 */
74uint32_t JdwpState::NextRequestSerial() {
75 MutexLock mu(serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070076 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077}
78
Elliott Hughes376a7a02011-10-24 18:35:55 -070079/*
80 * Get the next "event" serial number. We use this in the response to
81 * message type EventRequest.Set.
82 */
83uint32_t JdwpState::NextEventSerial() {
84 MutexLock mu(serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070085 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070086}
87
Elliott Hughes376a7a02011-10-24 18:35:55 -070088JdwpState::JdwpState(const JdwpOptions* options)
89 : options_(options),
90 thread_start_lock_("JDWP thread start lock"),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091 thread_start_cond_("JDWP thread start condition variable"),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 pthread_(0),
93 thread_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070094 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -070095 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070096 run(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -070097 transport_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098 netState(NULL),
99 attach_lock_("JDWP attach lock"),
100 attach_cond_("JDWP attach condition variable"),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700101 last_activity_time_ms_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102 serial_lock_("JDWP serial lock"),
Elliott Hughesf8349362012-06-18 15:00:06 -0700103 request_serial_(0x10000000),
104 event_serial_(0x20000000),
105 event_list_lock_("JDWP event list lock"),
106 event_list_(NULL),
107 event_list_size_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108 event_thread_lock_("JDWP event thread lock"),
109 event_thread_cond_("JDWP event thread condition variable"),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700110 event_thread_id_(0),
111 ddm_is_active_(false) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700112}
113
114/*
115 * Initialize JDWP.
116 *
117 * Does not return until JDWP thread is running, but may return before
118 * the thread is accepting network connections.
119 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700120JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 GlobalSynchronization::mutator_lock_->AssertNotHeld();
Elliott Hughes761928d2011-11-16 18:33:03 -0800122 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700123 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124 case kJdwpTransportSocket:
125 // LOGD("prepping for JDWP over TCP");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700126 state->transport_ = SocketTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127 break;
128#ifdef HAVE_ANDROID_OS
129 case kJdwpTransportAndroidAdb:
130 // LOGD("prepping for JDWP over ADB");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700131 state->transport_ = AndroidAdbTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132 break;
133#endif
134 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700135 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136 }
137
Elliott Hughesa21039c2012-06-21 12:09:25 -0700138 if (!(*state->transport_->startup)(state.get(), options)) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800139 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140 }
141
142 /*
143 * Grab a mutex or two before starting the thread. This ensures they
144 * won't signal the cond var before we're waiting.
145 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 {
147 MutexLock thread_start_locker(state->thread_start_lock_);
148 const bool should_suspend = options->suspend;
149 if (!should_suspend) {
150 /*
151 * We have bound to a port, or are trying to connect outbound to a
152 * debugger. Create the JDWP thread and let it continue the mission.
153 */
154 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700155
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700156 /*
157 * Wait until the thread finishes basic initialization.
158 * TODO: cond vars should be waited upon in a loop
159 */
160 state->thread_start_cond_.Wait(state->thread_start_lock_);
161 } else {
162 {
163 MutexLock attach_locker(state->attach_lock_);
164 /*
165 * We have bound to a port, or are trying to connect outbound to a
166 * debugger. Create the JDWP thread and let it continue the mission.
167 */
168 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 /*
171 * Wait until the thread finishes basic initialization.
172 * TODO: cond vars should be waited upon in a loop
173 */
174 state->thread_start_cond_.Wait(state->thread_start_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700175
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 /*
177 * For suspend=y, wait for the debugger to connect to us or for us to
178 * connect to the debugger.
179 *
180 * The JDWP thread will signal us when it connects successfully or
181 * times out (for timeout=xxx), so we have to check to see what happened
182 * when we wake up.
183 */
184 {
185 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForDebuggerToAttach);
186 state->attach_cond_.Wait(state->attach_lock_);
187 }
188 }
189 if (!state->IsActive()) {
190 LOG(ERROR) << "JDWP connection failed";
191 return NULL;
192 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194 LOG(INFO) << "JDWP connected";
195
196 /*
197 * Ordinarily we would pause briefly to allow the debugger to set
198 * breakpoints and so on, but for "suspend=y" the VM init code will
199 * pause the VM when it sends the VM_START message.
200 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202 }
203
Elliott Hughes761928d2011-11-16 18:33:03 -0800204 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205}
206
207/*
208 * Reset all session-related state. There should not be an active connection
209 * to the client at this point. The rest of the VM still thinks there is
210 * a debugger attached.
211 *
212 * This includes freeing up the debugger event list.
213 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700214void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 /* could reset the serial numbers, but no need to */
216
Elliott Hughes761928d2011-11-16 18:33:03 -0800217 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700218 {
219 MutexLock mu(event_list_lock_);
220 CHECK(event_list_ == NULL);
221 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222
223 /*
224 * Should not have one of these in progress. If the debugger went away
225 * mid-request, though, we could see this.
226 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700227 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800228 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700229 DCHECK(false);
230 }
231}
232
233/*
234 * Tell the JDWP thread to shut down. Frees "state".
235 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700236JdwpState::~JdwpState() {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700237 if (transport_ != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700238 if (IsConnected()) {
239 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700240 }
241
242 /*
243 * Close down the network to inspire the thread to halt.
244 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800245 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700246 (*transport_->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247
Elliott Hughes376a7a02011-10-24 18:35:55 -0700248 if (debug_thread_started_) {
249 run = false;
250 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700251 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700252 LOG(WARNING) << "JDWP thread join failed";
253 }
254 }
255
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800256 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700257 (*transport_->free)(this);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700258 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700259 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700260 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261
Elliott Hughes376a7a02011-10-24 18:35:55 -0700262 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700263}
264
265/*
266 * Are we talking to a debugger?
267 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700268bool JdwpState::IsActive() {
269 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270}
271
272/*
273 * Entry point for JDWP thread. The thread was created through the VM
274 * mechanisms, so there is a java/lang/Thread associated with us.
275 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700276static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700277 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
278 CHECK(state != NULL);
279
Elliott Hughes376a7a02011-10-24 18:35:55 -0700280 state->Run();
281 return NULL;
282}
283
284void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700285 Runtime* runtime = Runtime::Current();
Ian Rogers365c1022012-06-22 15:05:28 -0700286 runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup());
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
Elliott Hughes376a7a02011-10-24 18:35:55 -0700297 thread_start_lock_.Lock();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 debug_thread_started_ = true;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700299 thread_start_cond_.Broadcast();
300 thread_start_lock_.Unlock();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700301
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
303 {
304 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
305 CHECK_EQ(thread_->GetState(), kNative);
306 thread_->SetState(kWaitingInMainDebuggerLoop);
307 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308
309 /*
310 * Loop forever if we're in server mode, processing connections. In
311 * non-server mode, we bail out of the thread when the debugger drops
312 * us.
313 *
314 * We broadcast a notification when a debugger attaches, after we
315 * successfully process the handshake.
316 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700317 while (run) {
318 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319 /*
320 * Block forever, waiting for a connection. To support the
321 * "timeout=xxx" option we'll need to tweak this.
322 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700323 if (!(*transport_->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324 break;
325 }
326 } else {
327 /*
328 * If we're not acting as a server, we need to connect out to the
329 * debugger. To support the "timeout=xxx" option we need to
330 * have a timeout if the handshake reply isn't received in a
331 * reasonable amount of time.
332 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700333 if (!(*transport_->establish)(this, options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700334 /* wake anybody who was waiting for us to succeed */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700335 MutexLock mu(attach_lock_);
336 attach_cond_.Broadcast();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700337 break;
338 }
339 }
340
341 /* prep debug code to handle the new connection */
342 Dbg::Connected();
343
344 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700345 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800346 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 {
348 // sanity check -- shouldn't happen?
349 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
350 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351 }
352
Elliott Hughesa21039c2012-06-21 12:09:25 -0700353 if (!(*transport_->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354 /* blocking read */
355 break;
356 }
357
Elliott Hughesa21039c2012-06-21 12:09:25 -0700358 if (first && !(*transport_->awaitingHandshake)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359 /* handshake worked, tell the interpreter that we're active */
360 first = false;
361
362 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 {
364 ScopedObjectAccess soa(thread_);
365 debug_thread_id_ = Dbg::GetThreadSelfId();
366 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367
368 /* wake anybody who's waiting for us */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700369 MutexLock mu(attach_lock_);
370 attach_cond_.Broadcast();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371 }
372 }
373
Elliott Hughesa21039c2012-06-21 12:09:25 -0700374 (*transport_->close)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700375
Elliott Hughesa21039c2012-06-21 12:09:25 -0700376 if (ddm_is_active_) {
377 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700378
379 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383 }
384
385 /* release session state, e.g. remove breakpoint instructions */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 {
387 ScopedObjectAccess soa(thread_);
388 ResetState();
389 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700390 /* tell the interpreter that the debugger is no longer around */
391 Dbg::Disconnected();
392
393 /* if we had threads suspended, resume them now */
394 Dbg::UndoDebuggerSuspensions();
395
396 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700397 if (!options_->server) {
398 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700399 }
400 }
401
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700402 /* back to native, for thread shutdown */
403 {
404 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
405 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
406 thread_->SetState(kNative);
407 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800409 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700410 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700411}
412
Elliott Hughesa21039c2012-06-21 12:09:25 -0700413void JdwpState::NotifyDdmsActive() {
414 if (!ddm_is_active_) {
415 ddm_is_active_ = true;
416 Dbg::DdmConnected();
417 }
418}
419
Elliott Hughes475fc232011-10-25 15:00:35 -0700420Thread* JdwpState::GetDebugThread() {
421 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700422}
423
424/*
425 * Support routines for waitForDebugger().
426 *
427 * We can't have a trivial "waitForDebugger" function that returns the
428 * instant the debugger connects, because we run the risk of executing code
429 * before the debugger has had a chance to configure breakpoints or issue
430 * suspend calls. It would be nice to just sit in the suspended state, but
431 * most debuggers don't expect any threads to be suspended when they attach.
432 *
433 * There's no JDWP event we can post to tell the debugger, "we've stopped,
434 * and we like it that way". We could send a fake breakpoint, which should
435 * cause the debugger to immediately send a resume, but the debugger might
436 * send the resume immediately or might throw an exception of its own upon
437 * receiving a breakpoint event that it didn't ask for.
438 *
439 * What we really want is a "wait until the debugger is done configuring
440 * stuff" event. We can approximate this with a "wait until the debugger
441 * has been idle for a brief period".
442 */
443
444/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700445 * Return the time, in milliseconds, since the last debugger activity.
446 *
447 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
448 * processing a debugger request.
449 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700450int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700451 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700452 LOG(DEBUG) << "no active debugger";
453 return -1;
454 }
455
Elliott Hughesa21039c2012-06-21 12:09:25 -0700456 int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457
458 /* initializing or in the middle of something? */
459 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800460 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700461 return 0;
462 }
463
464 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700465 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800466 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800468 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 return now - last;
470}
471
Elliott Hughes03181a82011-11-17 17:22:21 -0800472std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800473 os << "JdwpLocation["
Elliott Hughes74847412012-06-20 18:10:21 -0700474 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.class_id, rhs.method_id)
475 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800476 return os;
477}
478
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800479bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700480 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
481 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800482}
483
484bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
485 return !(lhs == rhs);
486}
487
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488} // namespace JDWP
489
490} // namespace art