blob: f4250e5675b0cdd94c037dd9024e6273bbde3cd0 [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),
107 ddm_is_active_(false) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108}
109
110/*
111 * Initialize JDWP.
112 *
113 * Does not return until JDWP thread is running, but may return before
114 * the thread is accepting network connections.
115 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700116JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700117 Thread* self = Thread::Current();
118 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes761928d2011-11-16 18:33:03 -0800119 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700120 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 case kJdwpTransportSocket:
122 // LOGD("prepping for JDWP over TCP");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700123 state->transport_ = SocketTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124 break;
125#ifdef HAVE_ANDROID_OS
126 case kJdwpTransportAndroidAdb:
127 // LOGD("prepping for JDWP over ADB");
Elliott Hughesa21039c2012-06-21 12:09:25 -0700128 state->transport_ = AndroidAdbTransport();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700129 break;
130#endif
131 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700132 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 }
134
Elliott Hughesa21039c2012-06-21 12:09:25 -0700135 if (!(*state->transport_->startup)(state.get(), options)) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800136 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700137 }
138
139 /*
140 * Grab a mutex or two before starting the thread. This ensures they
141 * won't signal the cond var before we're waiting.
142 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700144 MutexLock thread_start_locker(self, state->thread_start_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 const bool should_suspend = options->suspend;
146 if (!should_suspend) {
147 /*
148 * We have bound to a port, or are trying to connect outbound to a
149 * debugger. Create the JDWP thread and let it continue the mission.
150 */
151 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 /*
154 * Wait until the thread finishes basic initialization.
155 * TODO: cond vars should be waited upon in a loop
156 */
Ian Rogersc604d732012-10-14 16:09:54 -0700157 state->thread_start_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 } else {
159 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 /*
161 * We have bound to a port, or are trying to connect outbound to a
162 * debugger. Create the JDWP thread and let it continue the mission.
163 */
164 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 /*
167 * Wait until the thread finishes basic initialization.
168 * TODO: cond vars should be waited upon in a loop
169 */
Ian Rogersc604d732012-10-14 16:09:54 -0700170 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700171
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 /*
173 * For suspend=y, wait for the debugger to connect to us or for us to
174 * connect to the debugger.
175 *
176 * The JDWP thread will signal us when it connects successfully or
177 * times out (for timeout=xxx), so we have to check to see what happened
178 * when we wake up.
179 */
180 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700181 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
jeffhao0dfbb7e2012-11-28 15:26:03 -0800182 MutexLock attach_locker(self, state->attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700183 state->attach_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 }
185 }
186 if (!state->IsActive()) {
187 LOG(ERROR) << "JDWP connection failed";
188 return NULL;
189 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 LOG(INFO) << "JDWP connected";
192
193 /*
194 * Ordinarily we would pause briefly to allow the debugger to set
195 * breakpoints and so on, but for "suspend=y" the VM init code will
196 * pause the VM when it sends the VM_START message.
197 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199 }
200
Elliott Hughes761928d2011-11-16 18:33:03 -0800201 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202}
203
204/*
205 * Reset all session-related state. There should not be an active connection
206 * to the client at this point. The rest of the VM still thinks there is
207 * a debugger attached.
208 *
209 * This includes freeing up the debugger event list.
210 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700211void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212 /* could reset the serial numbers, but no need to */
213
Elliott Hughes761928d2011-11-16 18:33:03 -0800214 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700215 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700216 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700217 CHECK(event_list_ == NULL);
218 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700219
220 /*
221 * Should not have one of these in progress. If the debugger went away
222 * mid-request, though, we could see this.
223 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700224 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800225 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 DCHECK(false);
227 }
228}
229
230/*
231 * Tell the JDWP thread to shut down. Frees "state".
232 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700233JdwpState::~JdwpState() {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700234 if (transport_ != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700235 if (IsConnected()) {
236 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700237 }
238
239 /*
240 * Close down the network to inspire the thread to halt.
241 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800242 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700243 (*transport_->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244
Elliott Hughes376a7a02011-10-24 18:35:55 -0700245 if (debug_thread_started_) {
246 run = false;
247 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700248 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249 LOG(WARNING) << "JDWP thread join failed";
250 }
251 }
252
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800253 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughesa21039c2012-06-21 12:09:25 -0700254 (*transport_->free)(this);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700255 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700256 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700257 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258
Elliott Hughes376a7a02011-10-24 18:35:55 -0700259 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260}
261
262/*
263 * Are we talking to a debugger?
264 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700265bool JdwpState::IsActive() {
266 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700267}
268
269/*
270 * Entry point for JDWP thread. The thread was created through the VM
271 * mechanisms, so there is a java/lang/Thread associated with us.
272 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700273static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700274 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
275 CHECK(state != NULL);
276
Elliott Hughes376a7a02011-10-24 18:35:55 -0700277 state->Run();
278 return NULL;
279}
280
281void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800283 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
284 !runtime->IsCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700285
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800286 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700287
288 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700289 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290 * we're running.
291 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700292 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700293 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700294
Ian Rogers81d425b2012-09-27 16:03:43 -0700295 {
296 MutexLock locker(thread_, thread_start_lock_);
297 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700298 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700299 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700300
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700302 CHECK_EQ(thread_->GetState(), kNative);
303 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700304
305 /*
306 * Loop forever if we're in server mode, processing connections. In
307 * non-server mode, we bail out of the thread when the debugger drops
308 * us.
309 *
310 * We broadcast a notification when a debugger attaches, after we
311 * successfully process the handshake.
312 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700313 while (run) {
314 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700315 /*
316 * Block forever, waiting for a connection. To support the
317 * "timeout=xxx" option we'll need to tweak this.
318 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700319 if (!(*transport_->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320 break;
321 }
322 } else {
323 /*
324 * If we're not acting as a server, we need to connect out to the
325 * debugger. To support the "timeout=xxx" option we need to
326 * have a timeout if the handshake reply isn't received in a
327 * reasonable amount of time.
328 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700329 if (!(*transport_->establish)(this, options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700331 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700332 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 break;
334 }
335 }
336
337 /* prep debug code to handle the new connection */
338 Dbg::Connected();
339
340 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700341 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800342 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343 {
344 // sanity check -- shouldn't happen?
Ian Rogers81d425b2012-09-27 16:03:43 -0700345 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700346 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347 }
348
Elliott Hughesa21039c2012-06-21 12:09:25 -0700349 if (!(*transport_->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700350 /* blocking read */
351 break;
352 }
353
Elliott Hughesa21039c2012-06-21 12:09:25 -0700354 if (first && !(*transport_->awaitingHandshake)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355 /* handshake worked, tell the interpreter that we're active */
356 first = false;
357
358 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 {
360 ScopedObjectAccess soa(thread_);
361 debug_thread_id_ = Dbg::GetThreadSelfId();
362 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700363
364 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -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 }
368 }
369
Elliott Hughesa21039c2012-06-21 12:09:25 -0700370 (*transport_->close)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371
Elliott Hughesa21039c2012-06-21 12:09:25 -0700372 if (ddm_is_active_) {
373 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700374
375 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700377 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379 }
380
381 /* release session state, e.g. remove breakpoint instructions */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 {
383 ScopedObjectAccess soa(thread_);
384 ResetState();
385 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700386 /* tell the interpreter that the debugger is no longer around */
387 Dbg::Disconnected();
388
389 /* if we had threads suspended, resume them now */
390 Dbg::UndoDebuggerSuspensions();
391
392 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700393 if (!options_->server) {
394 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700395 }
396 }
397
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700398 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700399 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
400 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700401
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800402 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700403 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404}
405
Elliott Hughesa21039c2012-06-21 12:09:25 -0700406void JdwpState::NotifyDdmsActive() {
407 if (!ddm_is_active_) {
408 ddm_is_active_ = true;
409 Dbg::DdmConnected();
410 }
411}
412
Elliott Hughes475fc232011-10-25 15:00:35 -0700413Thread* JdwpState::GetDebugThread() {
414 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415}
416
417/*
418 * Support routines for waitForDebugger().
419 *
420 * We can't have a trivial "waitForDebugger" function that returns the
421 * instant the debugger connects, because we run the risk of executing code
422 * before the debugger has had a chance to configure breakpoints or issue
423 * suspend calls. It would be nice to just sit in the suspended state, but
424 * most debuggers don't expect any threads to be suspended when they attach.
425 *
426 * There's no JDWP event we can post to tell the debugger, "we've stopped,
427 * and we like it that way". We could send a fake breakpoint, which should
428 * cause the debugger to immediately send a resume, but the debugger might
429 * send the resume immediately or might throw an exception of its own upon
430 * receiving a breakpoint event that it didn't ask for.
431 *
432 * What we really want is a "wait until the debugger is done configuring
433 * stuff" event. We can approximate this with a "wait until the debugger
434 * has been idle for a brief period".
435 */
436
437/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 * Return the time, in milliseconds, since the last debugger activity.
439 *
440 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
441 * processing a debugger request.
442 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700443int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700444 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700445 LOG(DEBUG) << "no active debugger";
446 return -1;
447 }
448
Elliott Hughesa21039c2012-06-21 12:09:25 -0700449 int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700450
451 /* initializing or in the middle of something? */
452 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800453 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 return 0;
455 }
456
457 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700458 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800459 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700460
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800461 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462 return now - last;
463}
464
Elliott Hughes03181a82011-11-17 17:22:21 -0800465std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800466 os << "JdwpLocation["
Elliott Hughes74847412012-06-20 18:10:21 -0700467 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.class_id, rhs.method_id)
468 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800469 return os;
470}
471
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800472bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700473 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
474 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800475}
476
477bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
478 return !(lhs == rhs);
479}
480
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700481} // namespace JDWP
482
483} // namespace art