blob: 72aff5c856d9588e82a5367df999fc9b3fe3d4be [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"
25
26#include <stdlib.h>
27#include <unistd.h>
28#include <sys/time.h>
29#include <time.h>
30#include <errno.h>
31
32namespace art {
33
34namespace JDWP {
35
Elliott Hughes376a7a02011-10-24 18:35:55 -070036static void* StartJdwpThread(void* arg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070037
38/*
39 * JdwpNetStateBase class implementation
40 */
41JdwpNetStateBase::JdwpNetStateBase() : socket_lock_("JdwpNetStateBase lock") {
42 clientSock = -1;
43}
44
45/*
46 * Write a packet. Grabs a mutex to assure atomicity.
47 */
48ssize_t JdwpNetStateBase::writePacket(ExpandBuf* pReply) {
49 MutexLock mu(socket_lock_);
50 return write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply));
51}
52
53/*
54 * Write a buffered packet. Grabs a mutex to assure atomicity.
55 */
Elliott Hughescccd84f2011-12-05 16:51:54 -080056ssize_t JdwpNetStateBase::writeBufferedPacket(const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057 MutexLock mu(socket_lock_);
Elliott Hughescccd84f2011-12-05 16:51:54 -080058 return writev(clientSock, iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059}
60
Elliott Hughes376a7a02011-10-24 18:35:55 -070061bool JdwpState::IsConnected() {
62 return (*transport->isConnected)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070063}
64
Elliott Hughes376a7a02011-10-24 18:35:55 -070065bool JdwpState::SendRequest(ExpandBuf* pReq) {
66 return (*transport->sendRequest)(this, pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070067}
68
Elliott Hughes376a7a02011-10-24 18:35:55 -070069/*
70 * Get the next "request" serial number. We use this when sending
71 * packets to the debugger.
72 */
73uint32_t JdwpState::NextRequestSerial() {
74 MutexLock mu(serial_lock_);
75 return requestSerial++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070076}
77
Elliott Hughes376a7a02011-10-24 18:35:55 -070078/*
79 * Get the next "event" serial number. We use this in the response to
80 * message type EventRequest.Set.
81 */
82uint32_t JdwpState::NextEventSerial() {
83 MutexLock mu(serial_lock_);
84 return eventSerial++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070085}
86
Elliott Hughes376a7a02011-10-24 18:35:55 -070087JdwpState::JdwpState(const JdwpOptions* options)
88 : options_(options),
89 thread_start_lock_("JDWP thread start lock"),
Elliott Hughes872d4ec2011-10-21 17:07:15 -070090 thread_start_cond_("JDWP thread start condition variable"),
91 debug_thread_started_(false),
92 debugThreadId(0),
93 run(false),
94 transport(NULL),
95 netState(NULL),
96 attach_lock_("JDWP attach lock"),
97 attach_cond_("JDWP attach condition variable"),
98 lastActivityWhen(0),
99 requestSerial(0x10000000),
100 eventSerial(0x20000000),
101 serial_lock_("JDWP serial lock"),
102 numEvents(0),
103 eventList(NULL),
104 event_lock_("JDWP event lock"),
105 event_thread_lock_("JDWP event thread lock"),
106 event_thread_cond_("JDWP event thread condition variable"),
107 eventThreadId(0),
108 ddmActive(false) {
109}
110
111/*
112 * Initialize JDWP.
113 *
114 * Does not return until JDWP thread is running, but may return before
115 * the thread is accepting network connections.
116 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700117JdwpState* JdwpState::Create(const JdwpOptions* options) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700118 /* comment this out when debugging JDWP itself */
119 //android_setMinPriority(LOG_TAG, ANDROID_LOG_DEBUG);
120
Elliott Hughes761928d2011-11-16 18:33:03 -0800121 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700122 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 case kJdwpTransportSocket:
124 // LOGD("prepping for JDWP over TCP");
125 state->transport = SocketTransport();
126 break;
127#ifdef HAVE_ANDROID_OS
128 case kJdwpTransportAndroidAdb:
129 // LOGD("prepping for JDWP over ADB");
130 state->transport = AndroidAdbTransport();
131 break;
132#endif
133 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700134 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135 }
136
Elliott Hughes761928d2011-11-16 18:33:03 -0800137 if (!(*state->transport->startup)(state.get(), options)) {
138 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139 }
140
141 /*
142 * Grab a mutex or two before starting the thread. This ensures they
143 * won't signal the cond var before we're waiting.
144 */
145 state->thread_start_lock_.Lock();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700146 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147 state->attach_lock_.Lock();
148 }
149
150 /*
151 * We have bound to a port, or are trying to connect outbound to a
152 * debugger. Create the JDWP thread and let it continue the mission.
153 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800154 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700155
156 /*
157 * Wait until the thread finishes basic initialization.
158 * TODO: cond vars should be waited upon in a loop
159 */
160 state->thread_start_cond_.Wait(state->thread_start_lock_);
161 state->thread_start_lock_.Unlock();
162
163 /*
164 * For suspend=y, wait for the debugger to connect to us or for us to
165 * connect to the debugger.
166 *
167 * The JDWP thread will signal us when it connects successfully or
168 * times out (for timeout=xxx), so we have to check to see what happened
169 * when we wake up.
170 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700171 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 {
173 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
174
175 state->attach_cond_.Wait(state->attach_lock_);
176 state->attach_lock_.Unlock();
177 }
178
Elliott Hughes376a7a02011-10-24 18:35:55 -0700179 if (!state->IsActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 LOG(ERROR) << "JDWP connection failed";
Elliott Hughes761928d2011-11-16 18:33:03 -0800181 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182 }
183
184 LOG(INFO) << "JDWP connected";
185
186 /*
187 * Ordinarily we would pause briefly to allow the debugger to set
188 * breakpoints and so on, but for "suspend=y" the VM init code will
189 * pause the VM when it sends the VM_START message.
190 */
191 }
192
Elliott Hughes761928d2011-11-16 18:33:03 -0800193 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194}
195
196/*
197 * Reset all session-related state. There should not be an active connection
198 * to the client at this point. The rest of the VM still thinks there is
199 * a debugger attached.
200 *
201 * This includes freeing up the debugger event list.
202 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700203void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700204 /* could reset the serial numbers, but no need to */
205
Elliott Hughes761928d2011-11-16 18:33:03 -0800206 UnregisterAll();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700207 CHECK(eventList == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208
209 /*
210 * Should not have one of these in progress. If the debugger went away
211 * mid-request, though, we could see this.
212 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700213 if (eventThreadId != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800214 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 DCHECK(false);
216 }
217}
218
219/*
220 * Tell the JDWP thread to shut down. Frees "state".
221 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700222JdwpState::~JdwpState() {
223 if (transport != NULL) {
224 if (IsConnected()) {
225 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 }
227
228 /*
229 * Close down the network to inspire the thread to halt.
230 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800231 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughes376a7a02011-10-24 18:35:55 -0700232 (*transport->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700233
Elliott Hughes376a7a02011-10-24 18:35:55 -0700234 if (debug_thread_started_) {
235 run = false;
236 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700237 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238 LOG(WARNING) << "JDWP thread join failed";
239 }
240 }
241
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800242 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughes376a7a02011-10-24 18:35:55 -0700243 (*transport->free)(this);
244 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700246 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247
Elliott Hughes376a7a02011-10-24 18:35:55 -0700248 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249}
250
251/*
252 * Are we talking to a debugger?
253 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700254bool JdwpState::IsActive() {
255 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700256}
257
258/*
259 * Entry point for JDWP thread. The thread was created through the VM
260 * mechanisms, so there is a java/lang/Thread associated with us.
261 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700262static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700263 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
264 CHECK(state != NULL);
265
Elliott Hughes376a7a02011-10-24 18:35:55 -0700266 state->Run();
267 return NULL;
268}
269
270void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700271 Runtime* runtime = Runtime::Current();
272 runtime->AttachCurrentThread("JDWP", true);
273
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800274 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700275
276 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700277 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700278 * we're running.
279 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700280 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700281 run = true;
282 android_atomic_release_store(true, &debug_thread_started_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700283
Elliott Hughes376a7a02011-10-24 18:35:55 -0700284 thread_start_lock_.Lock();
285 thread_start_cond_.Broadcast();
286 thread_start_lock_.Unlock();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700287
288 /* set the thread state to VMWAIT so GCs don't wait for us */
289 Dbg::ThreadWaiting();
290
291 /*
292 * Loop forever if we're in server mode, processing connections. In
293 * non-server mode, we bail out of the thread when the debugger drops
294 * us.
295 *
296 * We broadcast a notification when a debugger attaches, after we
297 * successfully process the handshake.
298 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700299 while (run) {
300 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700301 /*
302 * Block forever, waiting for a connection. To support the
303 * "timeout=xxx" option we'll need to tweak this.
304 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700305 if (!(*transport->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306 break;
307 }
308 } else {
309 /*
310 * If we're not acting as a server, we need to connect out to the
311 * debugger. To support the "timeout=xxx" option we need to
312 * have a timeout if the handshake reply isn't received in a
313 * reasonable amount of time.
314 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700315 if (!(*transport->establish)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700316 /* wake anybody who was waiting for us to succeed */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700317 MutexLock mu(attach_lock_);
318 attach_cond_.Broadcast();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319 break;
320 }
321 }
322
323 /* prep debug code to handle the new connection */
324 Dbg::Connected();
325
326 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700327 bool first = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328 while (true) {
329 // sanity check -- shouldn't happen?
330 if (Thread::Current()->GetState() != Thread::kVmWait) {
331 LOG(ERROR) << "JDWP thread no longer in VMWAIT (now " << Thread::Current()->GetState() << "); resetting";
332 Dbg::ThreadWaiting();
333 }
334
Elliott Hughes376a7a02011-10-24 18:35:55 -0700335 if (!(*transport->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 /* blocking read */
337 break;
338 }
339
Elliott Hughes376a7a02011-10-24 18:35:55 -0700340 if (first && !(*transport->awaitingHandshake)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700341 /* handshake worked, tell the interpreter that we're active */
342 first = false;
343
344 /* set thread ID; requires object registry to be active */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700345 debugThreadId = Dbg::GetThreadSelfId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700346
347 /* wake anybody who's waiting for us */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700348 MutexLock mu(attach_lock_);
349 attach_cond_.Broadcast();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700350 }
351 }
352
Elliott Hughes376a7a02011-10-24 18:35:55 -0700353 (*transport->close)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354
Elliott Hughes376a7a02011-10-24 18:35:55 -0700355 if (ddmActive) {
356 ddmActive = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700357
358 /* broadcast the disconnect; must be in RUNNING state */
359 Dbg::ThreadRunning();
360 Dbg::DdmDisconnected();
361 Dbg::ThreadWaiting();
362 }
363
364 /* release session state, e.g. remove breakpoint instructions */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700365 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700366
367 /* tell the interpreter that the debugger is no longer around */
368 Dbg::Disconnected();
369
370 /* if we had threads suspended, resume them now */
371 Dbg::UndoDebuggerSuspensions();
372
373 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700374 if (!options_->server) {
375 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700376 }
377 }
378
379 /* back to running, for thread shutdown */
380 Dbg::ThreadRunning();
381
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800382 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700383 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700384}
385
Elliott Hughes475fc232011-10-25 15:00:35 -0700386Thread* JdwpState::GetDebugThread() {
387 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700388}
389
390/*
391 * Support routines for waitForDebugger().
392 *
393 * We can't have a trivial "waitForDebugger" function that returns the
394 * instant the debugger connects, because we run the risk of executing code
395 * before the debugger has had a chance to configure breakpoints or issue
396 * suspend calls. It would be nice to just sit in the suspended state, but
397 * most debuggers don't expect any threads to be suspended when they attach.
398 *
399 * There's no JDWP event we can post to tell the debugger, "we've stopped,
400 * and we like it that way". We could send a fake breakpoint, which should
401 * cause the debugger to immediately send a resume, but the debugger might
402 * send the resume immediately or might throw an exception of its own upon
403 * receiving a breakpoint event that it didn't ask for.
404 *
405 * What we really want is a "wait until the debugger is done configuring
406 * stuff" event. We can approximate this with a "wait until the debugger
407 * has been idle for a brief period".
408 */
409
410/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700411 * Return the time, in milliseconds, since the last debugger activity.
412 *
413 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
414 * processing a debugger request.
415 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700416int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417 if (!Dbg::IsDebuggerConnected()) {
418 LOG(DEBUG) << "no active debugger";
419 return -1;
420 }
421
Elliott Hughes376a7a02011-10-24 18:35:55 -0700422 int64_t last = QuasiAtomicRead64(&lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423
424 /* initializing or in the middle of something? */
425 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800426 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427 return 0;
428 }
429
430 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700431 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800432 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800434 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 return now - last;
436}
437
438static const char* kTransportNames[] = {
439 "Unknown",
440 "Socket",
441 "AndroidAdb",
442};
443std::ostream& operator<<(std::ostream& os, const JdwpTransportType& value) {
444 int32_t int_value = static_cast<int32_t>(value);
445 if (value >= kJdwpTransportUnknown && value <= kJdwpTransportAndroidAdb) {
446 os << kTransportNames[int_value];
447 } else {
448 os << "JdwpTransportType[" << int_value << "]";
449 }
450 return os;
451}
452
Elliott Hughes03181a82011-11-17 17:22:21 -0800453std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800454 os << "JdwpLocation["
455 << Dbg::GetClassDescriptor(rhs.classId) << "." << Dbg::GetMethodName(rhs.classId, rhs.methodId)
456 << "@" << rhs.idx << " " << rhs.typeTag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800457 return os;
458}
459
Elliott Hughesdbb40792011-11-18 17:05:22 -0800460std::ostream& operator<<(std::ostream& os, const JdwpTag& value) {
461 switch (value) {
462 case JT_ARRAY: os << "JT_ARRAY"; break;
463 case JT_BYTE: os << "JT_BYTE"; break;
464 case JT_CHAR: os << "JT_CHAR"; break;
465 case JT_OBJECT: os << "JT_OBJECT"; break;
466 case JT_FLOAT: os << "JT_FLOAT"; break;
467 case JT_DOUBLE: os << "JT_DOUBLE"; break;
468 case JT_INT: os << "JT_INT"; break;
469 case JT_LONG: os << "JT_LONG"; break;
470 case JT_SHORT: os << "JT_SHORT"; break;
471 case JT_VOID: os << "JT_VOID"; break;
472 case JT_BOOLEAN: os << "JT_BOOLEAN"; break;
473 case JT_STRING: os << "JT_STRING"; break;
474 case JT_THREAD: os << "JT_THREAD"; break;
475 case JT_THREAD_GROUP: os << "JT_THREAD_GROUP"; break;
476 case JT_CLASS_LOADER: os << "JT_CLASS_LOADER"; break;
477 case JT_CLASS_OBJECT: os << "JT_CLASS_OBJECT"; break;
478 default:
479 os << "JdwpTag[" << static_cast<int32_t>(value) << "]";
480 }
481 return os;
482}
483
Elliott Hughesd07986f2011-12-06 18:27:45 -0800484std::ostream& operator<<(std::ostream& os, const JdwpTypeTag& value) {
485 switch (value) {
486 case TT_CLASS: os << "TT_CLASS"; break;
487 case TT_INTERFACE: os << "TT_INTERFACE"; break;
488 case TT_ARRAY: os << "TT_ARRAY"; break;
489 default:
490 os << "JdwpTypeTag[" << static_cast<int32_t>(value) << "]";
491 }
492 return os;
493}
494
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495} // namespace JDWP
496
497} // namespace art