blob: df24b8c285abeecab68b121422c2ffe90209783d [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 Hughes761928d2011-11-16 18:33:03 -0800118 UniquePtr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700119 switch (options->transport) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120 case kJdwpTransportSocket:
121 // LOGD("prepping for JDWP over TCP");
122 state->transport = SocketTransport();
123 break;
124#ifdef HAVE_ANDROID_OS
125 case kJdwpTransportAndroidAdb:
126 // LOGD("prepping for JDWP over ADB");
127 state->transport = AndroidAdbTransport();
128 break;
129#endif
130 default:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700131 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132 }
133
Elliott Hughes761928d2011-11-16 18:33:03 -0800134 if (!(*state->transport->startup)(state.get(), options)) {
135 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136 }
137
138 /*
139 * Grab a mutex or two before starting the thread. This ensures they
140 * won't signal the cond var before we're waiting.
141 */
142 state->thread_start_lock_.Lock();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700143 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144 state->attach_lock_.Lock();
145 }
146
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 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800151 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152
153 /*
154 * Wait until the thread finishes basic initialization.
155 * TODO: cond vars should be waited upon in a loop
156 */
157 state->thread_start_cond_.Wait(state->thread_start_lock_);
158 state->thread_start_lock_.Unlock();
159
160 /*
161 * For suspend=y, wait for the debugger to connect to us or for us to
162 * connect to the debugger.
163 *
164 * The JDWP thread will signal us when it connects successfully or
165 * times out (for timeout=xxx), so we have to check to see what happened
166 * when we wake up.
167 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700168 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169 {
Elliott Hughes34e06962012-04-09 13:55:55 -0700170 ScopedThreadStateChange tsc(Thread::Current(), kVmWait);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700171
172 state->attach_cond_.Wait(state->attach_lock_);
173 state->attach_lock_.Unlock();
174 }
175
Elliott Hughes376a7a02011-10-24 18:35:55 -0700176 if (!state->IsActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700177 LOG(ERROR) << "JDWP connection failed";
Elliott Hughes761928d2011-11-16 18:33:03 -0800178 return NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 }
180
181 LOG(INFO) << "JDWP connected";
182
183 /*
184 * Ordinarily we would pause briefly to allow the debugger to set
185 * breakpoints and so on, but for "suspend=y" the VM init code will
186 * pause the VM when it sends the VM_START message.
187 */
188 }
189
Elliott Hughes761928d2011-11-16 18:33:03 -0800190 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191}
192
193/*
194 * Reset all session-related state. There should not be an active connection
195 * to the client at this point. The rest of the VM still thinks there is
196 * a debugger attached.
197 *
198 * This includes freeing up the debugger event list.
199 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700200void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 /* could reset the serial numbers, but no need to */
202
Elliott Hughes761928d2011-11-16 18:33:03 -0800203 UnregisterAll();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700204 CHECK(eventList == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205
206 /*
207 * Should not have one of these in progress. If the debugger went away
208 * mid-request, though, we could see this.
209 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700210 if (eventThreadId != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800211 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212 DCHECK(false);
213 }
214}
215
216/*
217 * Tell the JDWP thread to shut down. Frees "state".
218 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700219JdwpState::~JdwpState() {
220 if (transport != NULL) {
221 if (IsConnected()) {
222 PostVMDeath();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700223 }
224
225 /*
226 * Close down the network to inspire the thread to halt.
227 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800228 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughes376a7a02011-10-24 18:35:55 -0700229 (*transport->shutdown)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700230
Elliott Hughes376a7a02011-10-24 18:35:55 -0700231 if (debug_thread_started_) {
232 run = false;
233 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700234 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235 LOG(WARNING) << "JDWP thread join failed";
236 }
237 }
238
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800239 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughes376a7a02011-10-24 18:35:55 -0700240 (*transport->free)(this);
241 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700242 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700243 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244
Elliott Hughes376a7a02011-10-24 18:35:55 -0700245 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700246}
247
248/*
249 * Are we talking to a debugger?
250 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700251bool JdwpState::IsActive() {
252 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253}
254
255/*
256 * Entry point for JDWP thread. The thread was created through the VM
257 * mechanisms, so there is a java/lang/Thread associated with us.
258 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700259static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
261 CHECK(state != NULL);
262
Elliott Hughes376a7a02011-10-24 18:35:55 -0700263 state->Run();
264 return NULL;
265}
266
267void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700268 Runtime* runtime = Runtime::Current();
Elliott Hughes462c9442012-03-23 18:47:50 -0700269 runtime->AttachCurrentThread("JDWP", true, Thread::GetSystemThreadGroup());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800271 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700272
273 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700274 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700275 * we're running.
276 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700277 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700278 run = true;
279 android_atomic_release_store(true, &debug_thread_started_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280
Elliott Hughes376a7a02011-10-24 18:35:55 -0700281 thread_start_lock_.Lock();
282 thread_start_cond_.Broadcast();
283 thread_start_lock_.Unlock();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284
285 /* set the thread state to VMWAIT so GCs don't wait for us */
286 Dbg::ThreadWaiting();
287
288 /*
289 * Loop forever if we're in server mode, processing connections. In
290 * non-server mode, we bail out of the thread when the debugger drops
291 * us.
292 *
293 * We broadcast a notification when a debugger attaches, after we
294 * successfully process the handshake.
295 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700296 while (run) {
297 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700298 /*
299 * Block forever, waiting for a connection. To support the
300 * "timeout=xxx" option we'll need to tweak this.
301 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700302 if (!(*transport->accept)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303 break;
304 }
305 } else {
306 /*
307 * If we're not acting as a server, we need to connect out to the
308 * debugger. To support the "timeout=xxx" option we need to
309 * have a timeout if the handshake reply isn't received in a
310 * reasonable amount of time.
311 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700312 if (!(*transport->establish)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313 /* wake anybody who was waiting for us to succeed */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700314 MutexLock mu(attach_lock_);
315 attach_cond_.Broadcast();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700316 break;
317 }
318 }
319
320 /* prep debug code to handle the new connection */
321 Dbg::Connected();
322
323 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700324 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800325 while (!Dbg::IsDisposed()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700326 // sanity check -- shouldn't happen?
Elliott Hughes34e06962012-04-09 13:55:55 -0700327 if (Thread::Current()->GetState() != kVmWait) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328 LOG(ERROR) << "JDWP thread no longer in VMWAIT (now " << Thread::Current()->GetState() << "); resetting";
329 Dbg::ThreadWaiting();
330 }
331
Elliott Hughes376a7a02011-10-24 18:35:55 -0700332 if (!(*transport->processIncoming)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 /* blocking read */
334 break;
335 }
336
Elliott Hughes376a7a02011-10-24 18:35:55 -0700337 if (first && !(*transport->awaitingHandshake)(this)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 /* handshake worked, tell the interpreter that we're active */
339 first = false;
340
341 /* set thread ID; requires object registry to be active */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700342 debugThreadId = Dbg::GetThreadSelfId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700343
344 /* wake anybody who's waiting for us */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700345 MutexLock mu(attach_lock_);
346 attach_cond_.Broadcast();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347 }
348 }
349
Elliott Hughes376a7a02011-10-24 18:35:55 -0700350 (*transport->close)(this);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351
Elliott Hughes376a7a02011-10-24 18:35:55 -0700352 if (ddmActive) {
353 ddmActive = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354
355 /* broadcast the disconnect; must be in RUNNING state */
356 Dbg::ThreadRunning();
357 Dbg::DdmDisconnected();
358 Dbg::ThreadWaiting();
359 }
360
361 /* release session state, e.g. remove breakpoint instructions */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700362 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700363
364 /* tell the interpreter that the debugger is no longer around */
365 Dbg::Disconnected();
366
367 /* if we had threads suspended, resume them now */
368 Dbg::UndoDebuggerSuspensions();
369
370 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700371 if (!options_->server) {
372 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 }
374 }
375
376 /* back to running, for thread shutdown */
377 Dbg::ThreadRunning();
378
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800379 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700380 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381}
382
Elliott Hughes475fc232011-10-25 15:00:35 -0700383Thread* JdwpState::GetDebugThread() {
384 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700385}
386
387/*
388 * Support routines for waitForDebugger().
389 *
390 * We can't have a trivial "waitForDebugger" function that returns the
391 * instant the debugger connects, because we run the risk of executing code
392 * before the debugger has had a chance to configure breakpoints or issue
393 * suspend calls. It would be nice to just sit in the suspended state, but
394 * most debuggers don't expect any threads to be suspended when they attach.
395 *
396 * There's no JDWP event we can post to tell the debugger, "we've stopped,
397 * and we like it that way". We could send a fake breakpoint, which should
398 * cause the debugger to immediately send a resume, but the debugger might
399 * send the resume immediately or might throw an exception of its own upon
400 * receiving a breakpoint event that it didn't ask for.
401 *
402 * What we really want is a "wait until the debugger is done configuring
403 * stuff" event. We can approximate this with a "wait until the debugger
404 * has been idle for a brief period".
405 */
406
407/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408 * Return the time, in milliseconds, since the last debugger activity.
409 *
410 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
411 * processing a debugger request.
412 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700413int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700414 if (!Dbg::IsDebuggerActive()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415 LOG(DEBUG) << "no active debugger";
416 return -1;
417 }
418
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700419 int64_t last = QuasiAtomic::Read64(&lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420
421 /* initializing or in the middle of something? */
422 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800423 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424 return 0;
425 }
426
427 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700428 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800429 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800431 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700432 return now - last;
433}
434
Elliott Hughes03181a82011-11-17 17:22:21 -0800435std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800436 os << "JdwpLocation["
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800437 << Dbg::GetClassName(rhs.classId) << "." << Dbg::GetMethodName(rhs.classId, rhs.methodId)
Elliott Hughes229feb72012-02-23 13:33:29 -0800438 << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.typeTag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800439 return os;
440}
441
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800442bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes972a47b2012-02-21 18:16:06 -0800443 return lhs.dex_pc == rhs.dex_pc && lhs.methodId == rhs.methodId &&
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800444 lhs.classId == rhs.classId && lhs.typeTag == rhs.typeTag;
445}
446
447bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
448 return !(lhs == rhs);
449}
450
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451} // namespace JDWP
452
453} // namespace art