blob: a09c488d4a8d69d21753456e0181bcb15a28f9d4 [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),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700102 serial_lock_("JDWP serial lock", kJdwpSerialLock),
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 Rogers81d425b2012-09-27 16:03:43 -0700121 Thread* self = Thread::Current();
122 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes761928d2011-11-16 18:33:03 -0800123 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700124 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 case kJdwpTransportSocket:
126 // LOGD("prepping for JDWP over TCP");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700127 state->transport_ = SocketTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128 break;
129#ifdef HAVE_ANDROID_OS
130 case kJdwpTransportAndroidAdb:
131 // LOGD("prepping for JDWP over ADB");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700132 state->transport_ = AndroidAdbTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 break;
134#endif
135 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700136 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700137 }
138
Elliott Hughesa21039c2012-06-21 12:09:25 -0700139 if (!(*state->transport_->startup)(state.get(), options)) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800140 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700141 }
142
143 /*
144 * Grab a mutex or two before starting the thread. This ensures they
145 * won't signal the cond var before we're waiting.
146 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 {
148 MutexLock thread_start_locker(state->thread_start_lock_);
149 const bool should_suspend = options->suspend;
150 if (!should_suspend) {
151 /*
152 * We have bound to a port, or are trying to connect outbound to a
153 * debugger. Create the JDWP thread and let it continue the mission.
154 */
155 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 /*
158 * Wait until the thread finishes basic initialization.
159 * TODO: cond vars should be waited upon in a loop
160 */
Ian Rogers81d425b2012-09-27 16:03:43 -0700161 state->thread_start_cond_.Wait(self, state->thread_start_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 } else {
163 {
164 MutexLock attach_locker(state->attach_lock_);
165 /*
166 * We have bound to a port, or are trying to connect outbound to a
167 * debugger. Create the JDWP thread and let it continue the mission.
168 */
169 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171 /*
172 * Wait until the thread finishes basic initialization.
173 * TODO: cond vars should be waited upon in a loop
174 */
Ian Rogers81d425b2012-09-27 16:03:43 -0700175 state->thread_start_cond_.Wait(self, state->thread_start_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177 /*
178 * For suspend=y, wait for the debugger to connect to us or for us to
179 * connect to the debugger.
180 *
181 * The JDWP thread will signal us when it connects successfully or
182 * times out (for timeout=xxx), so we have to check to see what happened
183 * when we wake up.
184 */
185 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700186 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
187 state->attach_cond_.Wait(self, state->attach_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 }
189 }
190 if (!state->IsActive()) {
191 LOG(ERROR) << "JDWP connection failed";
192 return NULL;
193 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195 LOG(INFO) << "JDWP connected";
196
197 /*
198 * Ordinarily we would pause briefly to allow the debugger to set
199 * breakpoints and so on, but for "suspend=y" the VM init code will
200 * pause the VM when it sends the VM_START message.
201 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203 }
204
Elliott Hughes761928d2011-11-16 18:33:03 -0800205 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206}
207
208/*
209 * Reset all session-related state. There should not be an active connection
210 * to the client at this point. The rest of the VM still thinks there is
211 * a debugger attached.
212 *
213 * This includes freeing up the debugger event list.
214 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700215void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700216 /* could reset the serial numbers, but no need to */
217
Elliott Hughes761928d2011-11-16 18:33:03 -0800218 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700219 {
220 MutexLock mu(event_list_lock_);
221 CHECK(event_list_ == NULL);
222 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700223
224 /*
225 * Should not have one of these in progress. If the debugger went away
226 * mid-request, though, we could see this.
227 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700228 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800229 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700230 DCHECK(false);
231 }
232}
233
234/*
235 * Tell the JDWP thread to shut down. Frees "state".
236 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700237JdwpState::~JdwpState() {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700238 if (transport_ != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700239 if (IsConnected()) {
240 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241 }
242
243 /*
244 * Close down the network to inspire the thread to halt.
245 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800246 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700247 (*transport_->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248
Elliott Hughes376a7a02011-10-24 18:35:55 -0700249 if (debug_thread_started_) {
250 run = false;
251 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700252 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253 LOG(WARNING) << "JDWP thread join failed";
254 }
255 }
256
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800257 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700258 (*transport_->free)(this);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700259 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700261 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262
Elliott Hughes376a7a02011-10-24 18:35:55 -0700263 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700264}
265
266/*
267 * Are we talking to a debugger?
268 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700269bool JdwpState::IsActive() {
270 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700271}
272
273/*
274 * Entry point for JDWP thread. The thread was created through the VM
275 * mechanisms, so there is a java/lang/Thread associated with us.
276 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700277static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700278 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
279 CHECK(state != NULL);
280
Elliott Hughes376a7a02011-10-24 18:35:55 -0700281 state->Run();
282 return NULL;
283}
284
285void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700286 Runtime* runtime = Runtime::Current();
Ian Rogers365c1022012-06-22 15:05:28 -0700287 runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700288
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800289 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290
291 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700292 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293 * we're running.
294 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700295 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700296 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297
Ian Rogers81d425b2012-09-27 16:03:43 -0700298 {
299 MutexLock locker(thread_, thread_start_lock_);
300 debug_thread_started_ = true;
301 thread_start_cond_.Broadcast();
302 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
305 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700306 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 CHECK_EQ(thread_->GetState(), kNative);
308 thread_->SetState(kWaitingInMainDebuggerLoop);
309 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310
311 /*
312 * Loop forever if we're in server mode, processing connections. In
313 * non-server mode, we bail out of the thread when the debugger drops
314 * us.
315 *
316 * We broadcast a notification when a debugger attaches, after we
317 * successfully process the handshake.
318 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700319 while (run) {
320 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700321 /*
322 * Block forever, waiting for a connection. To support the
323 * "timeout=xxx" option we'll need to tweak this.
324 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700325 if (!(*transport_->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700326 break;
327 }
328 } else {
329 /*
330 * If we're not acting as a server, we need to connect out to the
331 * debugger. To support the "timeout=xxx" option we need to
332 * have a timeout if the handshake reply isn't received in a
333 * reasonable amount of time.
334 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700335 if (!(*transport_->establish)(this, options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 /* wake anybody who was waiting for us to succeed */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700337 MutexLock mu(attach_lock_);
338 attach_cond_.Broadcast();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700339 break;
340 }
341 }
342
343 /* prep debug code to handle the new connection */
344 Dbg::Connected();
345
346 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700347 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800348 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 {
350 // sanity check -- shouldn't happen?
Ian Rogers81d425b2012-09-27 16:03:43 -0700351 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700353 }
354
Elliott Hughesa21039c2012-06-21 12:09:25 -0700355 if (!(*transport_->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 /* blocking read */
357 break;
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_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700372 attach_cond_.Broadcast();
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 Hughes03181a82011-11-17 17:22:21 -0800471std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800472 os << "JdwpLocation["
Elliott Hughes74847412012-06-20 18:10:21 -0700473 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.class_id, rhs.method_id)
474 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800475 return os;
476}
477
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800478bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700479 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
480 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800481}
482
483bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
484 return !(lhs == rhs);
485}
486
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700487} // namespace JDWP
488
489} // namespace art