blob: 064e566cd9a780b85b6962a9c0c006d38aedb707 [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) {
Ian Rogers50b35e22012-10-04 10:09:15 -070050 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070051 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) {
Ian Rogers50b35e22012-10-04 10:09:15 -070058 MutexLock mu(Thread::Current(), 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() {
Ian Rogers50b35e22012-10-04 10:09:15 -070075 MutexLock mu(Thread::Current(), 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() {
Ian Rogers50b35e22012-10-04 10:09:15 -070084 MutexLock mu(Thread::Current(), 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"),
Ian Rogersc604d732012-10-14 16:09:54 -070091 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
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"),
Ian Rogersc604d732012-10-14 16:09:54 -0700100 attach_cond_("JDWP attach condition variable", attach_lock_),
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"),
Ian Rogersc604d732012-10-14 16:09:54 -0700109 event_thread_cond_("JDWP event thread condition variable", event_thread_lock_),
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 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700148 MutexLock thread_start_locker(self, state->thread_start_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 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 Rogersc604d732012-10-14 16:09:54 -0700161 state->thread_start_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 } else {
163 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700164 MutexLock attach_locker(self, state->attach_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 /*
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 Rogersc604d732012-10-14 16:09:54 -0700175 state->thread_start_cond_.Wait(self);
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);
Ian Rogersc604d732012-10-14 16:09:54 -0700187 state->attach_cond_.Wait(self);
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 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700220 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700221 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 Rogers120f1c72012-09-28 17:17:10 -0700287 CHECK(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;
Ian Rogersc604d732012-10-14 16:09:54 -0700301 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700302 }
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 */
Ian Rogers50b35e22012-10-04 10:09:15 -0700305 CHECK_EQ(thread_->GetState(), kNative);
306 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700307
308 /*
309 * Loop forever if we're in server mode, processing connections. In
310 * non-server mode, we bail out of the thread when the debugger drops
311 * us.
312 *
313 * We broadcast a notification when a debugger attaches, after we
314 * successfully process the handshake.
315 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700316 while (run) {
317 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700318 /*
319 * Block forever, waiting for a connection. To support the
320 * "timeout=xxx" option we'll need to tweak this.
321 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700322 if (!(*transport_->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323 break;
324 }
325 } else {
326 /*
327 * If we're not acting as a server, we need to connect out to the
328 * debugger. To support the "timeout=xxx" option we need to
329 * have a timeout if the handshake reply isn't received in a
330 * reasonable amount of time.
331 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700332 if (!(*transport_->establish)(this, options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700334 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700335 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 break;
337 }
338 }
339
340 /* prep debug code to handle the new connection */
341 Dbg::Connected();
342
343 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700344 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800345 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700346 {
347 // sanity check -- shouldn't happen?
Ian Rogers81d425b2012-09-27 16:03:43 -0700348 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700350 }
351
Elliott Hughesa21039c2012-06-21 12:09:25 -0700352 if (!(*transport_->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700353 /* blocking read */
354 break;
355 }
356
Elliott Hughesa21039c2012-06-21 12:09:25 -0700357 if (first && !(*transport_->awaitingHandshake)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700358 /* handshake worked, tell the interpreter that we're active */
359 first = false;
360
361 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 {
363 ScopedObjectAccess soa(thread_);
364 debug_thread_id_ = Dbg::GetThreadSelfId();
365 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700366
367 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700368 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700369 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700370 }
371 }
372
Elliott Hughesa21039c2012-06-21 12:09:25 -0700373 (*transport_->close)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700374
Elliott Hughesa21039c2012-06-21 12:09:25 -0700375 if (ddm_is_active_) {
376 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700377
378 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700380 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700382 }
383
384 /* release session state, e.g. remove breakpoint instructions */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 {
386 ScopedObjectAccess soa(thread_);
387 ResetState();
388 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700389 /* tell the interpreter that the debugger is no longer around */
390 Dbg::Disconnected();
391
392 /* if we had threads suspended, resume them now */
393 Dbg::UndoDebuggerSuspensions();
394
395 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700396 if (!options_->server) {
397 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700398 }
399 }
400
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700402 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
403 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800405 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700406 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407}
408
Elliott Hughesa21039c2012-06-21 12:09:25 -0700409void JdwpState::NotifyDdmsActive() {
410 if (!ddm_is_active_) {
411 ddm_is_active_ = true;
412 Dbg::DdmConnected();
413 }
414}
415
Elliott Hughes475fc232011-10-25 15:00:35 -0700416Thread* JdwpState::GetDebugThread() {
417 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700418}
419
420/*
421 * Support routines for waitForDebugger().
422 *
423 * We can't have a trivial "waitForDebugger" function that returns the
424 * instant the debugger connects, because we run the risk of executing code
425 * before the debugger has had a chance to configure breakpoints or issue
426 * suspend calls. It would be nice to just sit in the suspended state, but
427 * most debuggers don't expect any threads to be suspended when they attach.
428 *
429 * There's no JDWP event we can post to tell the debugger, "we've stopped,
430 * and we like it that way". We could send a fake breakpoint, which should
431 * cause the debugger to immediately send a resume, but the debugger might
432 * send the resume immediately or might throw an exception of its own upon
433 * receiving a breakpoint event that it didn't ask for.
434 *
435 * What we really want is a "wait until the debugger is done configuring
436 * stuff" event. We can approximate this with a "wait until the debugger
437 * has been idle for a brief period".
438 */
439
440/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 * Return the time, in milliseconds, since the last debugger activity.
442 *
443 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
444 * processing a debugger request.
445 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700446int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700447 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700448 LOG(DEBUG) << "no active debugger";
449 return -1;
450 }
451
Elliott Hughesa21039c2012-06-21 12:09:25 -0700452 int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453
454 /* initializing or in the middle of something? */
455 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800456 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457 return 0;
458 }
459
460 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700461 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800462 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700463
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800464 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700465 return now - last;
466}
467
Elliott Hughes03181a82011-11-17 17:22:21 -0800468std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800469 os << "JdwpLocation["
Elliott Hughes74847412012-06-20 18:10:21 -0700470 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.class_id, rhs.method_id)
471 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800472 return os;
473}
474
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800475bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700476 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
477 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800478}
479
480bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
481 return !(lhs == rhs);
482}
483
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484} // namespace JDWP
485
486} // namespace art