blob: df7498851ddd8e86330f4cf02848f572f952c8f3 [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 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070038JdwpNetStateBase::JdwpNetStateBase(JdwpState* state)
39 : state_(state), socket_lock_("JdwpNetStateBase lock") {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040 clientSock = -1;
Elliott Hughes5d10a872013-04-17 19:26:43 -070041 wake_pipe_[0] = -1;
42 wake_pipe_[1] = -1;
43 input_count_ = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070044 awaiting_handshake_ = false;
Elliott Hughescb693062013-02-21 09:48:08 -080045}
46
Elliott Hughes5d10a872013-04-17 19:26:43 -070047JdwpNetStateBase::~JdwpNetStateBase() {
48 if (wake_pipe_[0] != -1) {
49 close(wake_pipe_[0]);
50 wake_pipe_[0] = -1;
51 }
52 if (wake_pipe_[1] != -1) {
53 close(wake_pipe_[1]);
54 wake_pipe_[1] = -1;
55 }
56}
57
58bool JdwpNetStateBase::MakePipe() {
59 if (pipe(wake_pipe_) == -1) {
60 PLOG(ERROR) << "pipe failed";
61 return false;
62 }
63 return true;
64}
65
66void JdwpNetStateBase::WakePipe() {
67 // If we might be sitting in select, kick us loose.
68 if (wake_pipe_[1] != -1) {
69 VLOG(jdwp) << "+++ writing to wake pipe";
70 TEMP_FAILURE_RETRY(write(wake_pipe_[1], "", 1));
71 }
72}
73
Elliott Hughescb693062013-02-21 09:48:08 -080074void JdwpNetStateBase::ConsumeBytes(size_t count) {
75 CHECK_GT(count, 0U);
Elliott Hughes5d10a872013-04-17 19:26:43 -070076 CHECK_LE(count, input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -080077
Elliott Hughes5d10a872013-04-17 19:26:43 -070078 if (count == input_count_) {
79 input_count_ = 0;
Elliott Hughescb693062013-02-21 09:48:08 -080080 return;
81 }
82
Elliott Hughes5d10a872013-04-17 19:26:43 -070083 memmove(input_buffer_, input_buffer_ + count, input_count_ - count);
84 input_count_ -= count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070085}
86
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070087bool JdwpNetStateBase::HaveFullPacket() {
88 if (awaiting_handshake_) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070089 return (input_count_ >= kMagicHandshakeLen);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070090 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070091 if (input_count_ < 4) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070092 return false;
93 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070094 uint32_t length = Get4BE(input_buffer_);
95 return (input_count_ >= length);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070096}
97
98bool JdwpNetStateBase::IsAwaitingHandshake() {
99 return awaiting_handshake_;
100}
101
102void JdwpNetStateBase::SetAwaitingHandshake(bool new_state) {
103 awaiting_handshake_ = new_state;
104}
105
106bool JdwpNetStateBase::IsConnected() {
107 return clientSock >= 0;
108}
109
110// Close a connection from a debugger (which may have already dropped us).
111// Resets the state so we're ready to receive a new connection.
112// Only called from the JDWP thread.
113void JdwpNetStateBase::Close() {
114 if (clientSock < 0) {
115 return;
116 }
117
118 VLOG(jdwp) << "+++ closing JDWP connection on fd " << clientSock;
119
120 close(clientSock);
121 clientSock = -1;
122}
123
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124/*
125 * Write a packet. Grabs a mutex to assure atomicity.
126 */
Elliott Hughescb693062013-02-21 09:48:08 -0800127ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700128 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughes068193c2013-04-15 16:05:28 -0700129 return TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply)));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130}
131
132/*
133 * Write a buffered packet. Grabs a mutex to assure atomicity.
134 */
Elliott Hughescb693062013-02-21 09:48:08 -0800135ssize_t JdwpNetStateBase::WriteBufferedPacket(const iovec* iov, int iov_count) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700136 MutexLock mu(Thread::Current(), socket_lock_);
Elliott Hughes068193c2013-04-15 16:05:28 -0700137 return TEMP_FAILURE_RETRY(writev(clientSock, iov, iov_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700138}
139
Elliott Hughes376a7a02011-10-24 18:35:55 -0700140bool JdwpState::IsConnected() {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700141 return netState != NULL && netState->IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700142}
143
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700144void JdwpState::SendBufferedRequest(uint32_t type, const iovec* iov, int iov_count) {
145 if (netState->clientSock < 0) {
146 // Can happen with some DDMS events.
147 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
148 return;
149 }
150
151 size_t expected = 0;
152 for (int i = 0; i < iov_count; ++i) {
153 expected += iov[i].iov_len;
154 }
155
156 errno = 0;
157 ssize_t actual = netState->WriteBufferedPacket(iov, iov_count);
158 if (static_cast<size_t>(actual) != expected) {
159 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet %c%c%c%c to debugger (%d of %d)",
160 static_cast<uint8_t>(type >> 24),
161 static_cast<uint8_t>(type >> 16),
162 static_cast<uint8_t>(type >> 8),
163 static_cast<uint8_t>(type),
164 actual, expected);
165 }
166}
167
168void JdwpState::SendRequest(ExpandBuf* pReq) {
169 if (netState->clientSock < 0) {
170 // Can happen with some DDMS events.
171 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
172 return;
173 }
174
175 errno = 0;
176 ssize_t actual = netState->WritePacket(pReq);
177 if (static_cast<size_t>(actual) != expandBufGetLength(pReq)) {
178 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet to debugger (%d of %d)",
179 actual, expandBufGetLength(pReq));
180 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181}
182
Elliott Hughes376a7a02011-10-24 18:35:55 -0700183/*
184 * Get the next "request" serial number. We use this when sending
185 * packets to the debugger.
186 */
187uint32_t JdwpState::NextRequestSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700188 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700189 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190}
191
Elliott Hughes376a7a02011-10-24 18:35:55 -0700192/*
193 * Get the next "event" serial number. We use this in the response to
194 * message type EventRequest.Set.
195 */
196uint32_t JdwpState::NextEventSerial() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700197 MutexLock mu(Thread::Current(), serial_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700198 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199}
200
Elliott Hughes376a7a02011-10-24 18:35:55 -0700201JdwpState::JdwpState(const JdwpOptions* options)
202 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800203 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700204 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 pthread_(0),
206 thread_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700207 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700208 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 run(false),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210 netState(NULL),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800211 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700212 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700213 last_activity_time_ms_(0),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700214 serial_lock_("JDWP serial lock", kJdwpSerialLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700215 request_serial_(0x10000000),
216 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800217 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700218 event_list_(NULL),
219 event_list_size_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700220 event_thread_lock_("JDWP event thread lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700221 event_thread_cond_("JDWP event thread condition variable", event_thread_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700222 event_thread_id_(0),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800223 ddm_is_active_(false),
224 should_exit_(false),
225 exit_status_(0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226}
227
228/*
229 * Initialize JDWP.
230 *
231 * Does not return until JDWP thread is running, but may return before
232 * the thread is accepting network connections.
233 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700234JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700235 Thread* self = Thread::Current();
236 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes761928d2011-11-16 18:33:03 -0800237 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700238 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700239 case kJdwpTransportSocket:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700240 InitSocketTransport(state.get(), options);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241 break;
242#ifdef HAVE_ANDROID_OS
243 case kJdwpTransportAndroidAdb:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700244 InitAdbTransport(state.get(), options);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245 break;
246#endif
247 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700248 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249 }
250
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251 /*
252 * Grab a mutex or two before starting the thread. This ensures they
253 * won't signal the cond var before we're waiting.
254 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700256 MutexLock thread_start_locker(self, state->thread_start_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 const bool should_suspend = options->suspend;
258 if (!should_suspend) {
259 /*
260 * We have bound to a port, or are trying to connect outbound to a
261 * debugger. Create the JDWP thread and let it continue the mission.
262 */
263 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 /*
266 * Wait until the thread finishes basic initialization.
267 * TODO: cond vars should be waited upon in a loop
268 */
Ian Rogersc604d732012-10-14 16:09:54 -0700269 state->thread_start_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 } else {
271 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 /*
273 * We have bound to a port, or are trying to connect outbound to a
274 * debugger. Create the JDWP thread and let it continue the mission.
275 */
276 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700277
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 /*
279 * Wait until the thread finishes basic initialization.
280 * TODO: cond vars should be waited upon in a loop
281 */
Ian Rogersc604d732012-10-14 16:09:54 -0700282 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700283
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 /*
285 * For suspend=y, wait for the debugger to connect to us or for us to
286 * connect to the debugger.
287 *
288 * The JDWP thread will signal us when it connects successfully or
289 * times out (for timeout=xxx), so we have to check to see what happened
290 * when we wake up.
291 */
292 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700293 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
jeffhao0dfbb7e2012-11-28 15:26:03 -0800294 MutexLock attach_locker(self, state->attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700295 state->attach_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 }
297 }
298 if (!state->IsActive()) {
299 LOG(ERROR) << "JDWP connection failed";
300 return NULL;
301 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700302
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 LOG(INFO) << "JDWP connected";
304
305 /*
306 * Ordinarily we would pause briefly to allow the debugger to set
307 * breakpoints and so on, but for "suspend=y" the VM init code will
308 * pause the VM when it sends the VM_START message.
309 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700311 }
312
Elliott Hughes761928d2011-11-16 18:33:03 -0800313 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314}
315
316/*
317 * Reset all session-related state. There should not be an active connection
318 * to the client at this point. The rest of the VM still thinks there is
319 * a debugger attached.
320 *
321 * This includes freeing up the debugger event list.
322 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700323void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324 /* could reset the serial numbers, but no need to */
325
Elliott Hughes761928d2011-11-16 18:33:03 -0800326 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700327 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700328 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700329 CHECK(event_list_ == NULL);
330 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331
332 /*
333 * Should not have one of these in progress. If the debugger went away
334 * mid-request, though, we could see this.
335 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700336 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800337 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 DCHECK(false);
339 }
340}
341
342/*
343 * Tell the JDWP thread to shut down. Frees "state".
344 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700345JdwpState::~JdwpState() {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700346 if (netState != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700347 if (IsConnected()) {
348 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349 }
350
351 /*
352 * Close down the network to inspire the thread to halt.
353 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800354 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700355 netState->Shutdown();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356
Elliott Hughes376a7a02011-10-24 18:35:55 -0700357 if (debug_thread_started_) {
358 run = false;
359 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700360 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361 LOG(WARNING) << "JDWP thread join failed";
362 }
363 }
364
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800365 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700366 delete netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700367 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700369 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700370
Elliott Hughes376a7a02011-10-24 18:35:55 -0700371 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700372}
373
374/*
375 * Are we talking to a debugger?
376 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700377bool JdwpState::IsActive() {
378 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379}
380
Elliott Hughescb693062013-02-21 09:48:08 -0800381// Returns "false" if we encounter a connection-fatal error.
382bool JdwpState::HandlePacket() {
383 JdwpNetStateBase* netStateBase = reinterpret_cast<JdwpNetStateBase*>(netState);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700384 JDWP::Request request(netStateBase->input_buffer_, netStateBase->input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -0800385
386 ExpandBuf* pReply = expandBufAlloc();
387 ProcessRequest(request, pReply);
388 ssize_t cc = netStateBase->WritePacket(pReply);
389 if (cc != (ssize_t) expandBufGetLength(pReply)) {
390 PLOG(ERROR) << "Failed sending reply to debugger";
391 expandBufFree(pReply);
392 return false;
393 }
394 expandBufFree(pReply);
395 netStateBase->ConsumeBytes(request.GetLength());
396 return true;
397}
398
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700399/*
400 * Entry point for JDWP thread. The thread was created through the VM
401 * mechanisms, so there is a java/lang/Thread associated with us.
402 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700403static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
405 CHECK(state != NULL);
406
Elliott Hughes376a7a02011-10-24 18:35:55 -0700407 state->Run();
408 return NULL;
409}
410
411void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800413 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
414 !runtime->IsCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800416 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417
418 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700419 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420 * we're running.
421 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700422 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700423 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424
Ian Rogers81d425b2012-09-27 16:03:43 -0700425 {
426 MutexLock locker(thread_, thread_start_lock_);
427 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700428 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700429 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700431 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700432 CHECK_EQ(thread_->GetState(), kNative);
Ian Rogers62d6c772013-02-27 08:32:07 -0800433 Locks::mutator_lock_->AssertNotHeld(thread_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700434 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435
436 /*
437 * Loop forever if we're in server mode, processing connections. In
438 * non-server mode, we bail out of the thread when the debugger drops
439 * us.
440 *
441 * We broadcast a notification when a debugger attaches, after we
442 * successfully process the handshake.
443 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700444 while (run) {
445 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700446 /*
447 * Block forever, waiting for a connection. To support the
448 * "timeout=xxx" option we'll need to tweak this.
449 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700450 if (!netState->Accept()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 break;
452 }
453 } else {
454 /*
455 * If we're not acting as a server, we need to connect out to the
456 * debugger. To support the "timeout=xxx" option we need to
457 * have a timeout if the handshake reply isn't received in a
458 * reasonable amount of time.
459 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700460 if (!netState->Establish(options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700461 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700462 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700463 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 break;
465 }
466 }
467
468 /* prep debug code to handle the new connection */
469 Dbg::Connected();
470
471 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700472 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800473 while (!Dbg::IsDisposed()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 {
475 // sanity check -- shouldn't happen?
Ian Rogers81d425b2012-09-27 16:03:43 -0700476 MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 }
479
Elliott Hughes5d10a872013-04-17 19:26:43 -0700480 if (!netState->ProcessIncoming()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700481 /* blocking read */
482 break;
483 }
484
Elliott Hughes64f574f2013-02-20 14:57:12 -0800485 if (should_exit_) {
486 exit(exit_status_);
487 }
488
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700489 if (first && !netState->IsAwaitingHandshake()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700490 /* handshake worked, tell the interpreter that we're active */
491 first = false;
492
493 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700494 {
495 ScopedObjectAccess soa(thread_);
496 debug_thread_id_ = Dbg::GetThreadSelfId();
497 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700498
499 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700500 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700501 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502 }
503 }
504
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700505 netState->Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506
Elliott Hughesa21039c2012-06-21 12:09:25 -0700507 if (ddm_is_active_) {
508 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509
510 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514 }
515
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700516 {
517 ScopedObjectAccess soa(thread_);
Elliott Hughes0f827162013-02-26 12:12:58 -0800518
519 // Release session state, e.g. remove breakpoint instructions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 ResetState();
521 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800522 // Tell the rest of the runtime that the debugger is no longer around.
523 Dbg::Disconnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524
525 /* if we had threads suspended, resume them now */
526 Dbg::UndoDebuggerSuspensions();
527
528 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700529 if (!options_->server) {
530 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531 }
532 }
533
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700534 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700535 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
536 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800538 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700539 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540}
541
Elliott Hughesa21039c2012-06-21 12:09:25 -0700542void JdwpState::NotifyDdmsActive() {
543 if (!ddm_is_active_) {
544 ddm_is_active_ = true;
545 Dbg::DdmConnected();
546 }
547}
548
Elliott Hughes475fc232011-10-25 15:00:35 -0700549Thread* JdwpState::GetDebugThread() {
550 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700551}
552
553/*
554 * Support routines for waitForDebugger().
555 *
556 * We can't have a trivial "waitForDebugger" function that returns the
557 * instant the debugger connects, because we run the risk of executing code
558 * before the debugger has had a chance to configure breakpoints or issue
559 * suspend calls. It would be nice to just sit in the suspended state, but
560 * most debuggers don't expect any threads to be suspended when they attach.
561 *
562 * There's no JDWP event we can post to tell the debugger, "we've stopped,
563 * and we like it that way". We could send a fake breakpoint, which should
564 * cause the debugger to immediately send a resume, but the debugger might
565 * send the resume immediately or might throw an exception of its own upon
566 * receiving a breakpoint event that it didn't ask for.
567 *
568 * What we really want is a "wait until the debugger is done configuring
569 * stuff" event. We can approximate this with a "wait until the debugger
570 * has been idle for a brief period".
571 */
572
573/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700574 * Return the time, in milliseconds, since the last debugger activity.
575 *
576 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
577 * processing a debugger request.
578 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700579int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700580 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700581 LOG(DEBUG) << "no active debugger";
582 return -1;
583 }
584
Elliott Hughesa21039c2012-06-21 12:09:25 -0700585 int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700586
587 /* initializing or in the middle of something? */
588 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800589 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700590 return 0;
591 }
592
593 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700594 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800595 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700596
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800597 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700598 return now - last;
599}
600
Elliott Hughes64f574f2013-02-20 14:57:12 -0800601void JdwpState::ExitAfterReplying(int exit_status) {
602 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
603 should_exit_ = true;
604 exit_status_ = exit_status;
605}
606
Elliott Hughes03181a82011-11-17 17:22:21 -0800607std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800608 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800609 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Elliott Hughes74847412012-06-20 18:10:21 -0700610 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800611 return os;
612}
613
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800614bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700615 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
616 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800617}
618
619bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
620 return !(lhs == rhs);
621}
622
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700623} // namespace JDWP
624
625} // namespace art