2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * JDWP initialization.
20 #include "jdwp/JdwpPriv.h"
31 static void* jdwpThreadStart(void* arg);
34 * JdwpNetStateBase class implementation
36 JdwpNetStateBase::JdwpNetStateBase()
39 dvmDbgInitMutex(&socketLock);
43 * Write a packet. Grabs a mutex to assure atomicity.
45 ssize_t JdwpNetStateBase::writePacket(ExpandBuf* pReply)
47 dvmDbgLockMutex(&socketLock);
48 ssize_t cc = TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply),
49 expandBufGetLength(pReply)));
50 dvmDbgUnlockMutex(&socketLock);
56 * Write a buffered packet. Grabs a mutex to assure atomicity.
58 ssize_t JdwpNetStateBase::writeBufferedPacket(const struct iovec* iov,
61 dvmDbgLockMutex(&socketLock);
62 ssize_t actual = TEMP_FAILURE_RETRY(writev(clientSock, iov, iovcnt));
63 dvmDbgUnlockMutex(&socketLock);
71 * Does not return until JDWP thread is running, but may return before
72 * the thread is accepting network connections.
74 JdwpState* dvmJdwpStartup(const JdwpStartupParams* pParams)
76 JdwpState* state = NULL;
78 /* comment this out when debugging JDWP itself */
79 android_setMinPriority(LOG_TAG, ANDROID_LOG_DEBUG);
81 state = (JdwpState*) calloc(1, sizeof(JdwpState));
83 state->params = *pParams;
85 state->requestSerial = 0x10000000;
86 state->eventSerial = 0x20000000;
87 dvmDbgInitMutex(&state->threadStartLock);
88 dvmDbgInitMutex(&state->attachLock);
89 dvmDbgInitMutex(&state->serialLock);
90 dvmDbgInitMutex(&state->eventLock);
91 state->eventThreadId = 0;
92 dvmDbgInitMutex(&state->eventThreadLock);
93 dvmDbgInitCond(&state->threadStartCond);
94 dvmDbgInitCond(&state->attachCond);
95 dvmDbgInitCond(&state->eventThreadCond);
97 switch (pParams->transport) {
98 case kJdwpTransportSocket:
99 // ALOGD("prepping for JDWP over TCP");
100 state->transport = dvmJdwpSocketTransport();
102 case kJdwpTransportAndroidAdb:
103 // ALOGD("prepping for JDWP over ADB");
104 state->transport = dvmJdwpAndroidAdbTransport();
108 ALOGE("Unknown transport %d", pParams->transport);
113 if (!dvmJdwpNetStartup(state, pParams))
117 * Grab a mutex or two before starting the thread. This ensures they
118 * won't signal the cond var before we're waiting.
120 dvmDbgLockMutex(&state->threadStartLock);
121 if (pParams->suspend)
122 dvmDbgLockMutex(&state->attachLock);
125 * We have bound to a port, or are trying to connect outbound to a
126 * debugger. Create the JDWP thread and let it continue the mission.
128 if (!dvmCreateInternalThread(&state->debugThreadHandle, "JDWP",
129 jdwpThreadStart, state))
131 /* state is getting tossed, but unlock these anyway for cleanliness */
132 dvmDbgUnlockMutex(&state->threadStartLock);
133 if (pParams->suspend)
134 dvmDbgUnlockMutex(&state->attachLock);
139 * Wait until the thread finishes basic initialization.
140 * TODO: cond vars should be waited upon in a loop
142 dvmDbgCondWait(&state->threadStartCond, &state->threadStartLock);
143 dvmDbgUnlockMutex(&state->threadStartLock);
147 * For suspend=y, wait for the debugger to connect to us or for us to
148 * connect to the debugger.
150 * The JDWP thread will signal us when it connects successfully or
151 * times out (for timeout=xxx), so we have to check to see what happened
154 if (pParams->suspend) {
155 dvmChangeStatus(NULL, THREAD_VMWAIT);
156 dvmDbgCondWait(&state->attachCond, &state->attachLock);
157 dvmDbgUnlockMutex(&state->attachLock);
158 dvmChangeStatus(NULL, THREAD_RUNNING);
160 if (!dvmJdwpIsActive(state)) {
161 ALOGE("JDWP connection failed");
165 ALOGI("JDWP connected");
168 * Ordinarily we would pause briefly to allow the debugger to set
169 * breakpoints and so on, but for "suspend=y" the VM init code will
170 * pause the VM when it sends the VM_START message.
177 dvmJdwpShutdown(state); // frees state
182 * Reset all session-related state. There should not be an active connection
183 * to the client at this point. The rest of the VM still thinks there is
184 * a debugger attached.
186 * This includes freeing up the debugger event list.
188 void dvmJdwpResetState(JdwpState* state)
190 /* could reset the serial numbers, but no need to */
192 dvmJdwpUnregisterAll(state);
193 assert(state->eventList == NULL);
196 * Should not have one of these in progress. If the debugger went away
197 * mid-request, though, we could see this.
199 if (state->eventThreadId != 0) {
200 ALOGW("WARNING: resetting state while event in progress");
206 * Tell the JDWP thread to shut down. Frees "state".
208 void dvmJdwpShutdown(JdwpState* state)
215 if (dvmJdwpIsTransportDefined(state)) {
216 if (dvmJdwpIsConnected(state))
217 dvmJdwpPostVMDeath(state);
220 * Close down the network to inspire the thread to halt.
222 if (gDvm.verboseShutdown)
223 ALOGD("JDWP shutting down net...");
224 dvmJdwpNetShutdown(state);
226 if (state->debugThreadStarted) {
228 if (pthread_join(state->debugThreadHandle, &threadReturn) != 0) {
229 ALOGW("JDWP thread join failed");
233 if (gDvm.verboseShutdown)
234 ALOGD("JDWP freeing netstate...");
235 dvmJdwpNetFree(state);
236 state->netState = NULL;
238 assert(state->netState == NULL);
240 dvmJdwpResetState(state);
245 * Are we talking to a debugger?
247 bool dvmJdwpIsActive(JdwpState* state)
249 return dvmJdwpIsConnected(state);
253 * Entry point for JDWP thread. The thread was created through the VM
254 * mechanisms, so there is a java/lang/Thread associated with us.
256 static void* jdwpThreadStart(void* arg)
258 JdwpState* state = (JdwpState*) arg;
260 ALOGV("JDWP: thread running");
263 * Finish initializing "state", then notify the creating thread that
266 state->debugThreadHandle = dvmThreadSelf()->handle;
268 android_atomic_release_store(true, &state->debugThreadStarted);
270 dvmDbgLockMutex(&state->threadStartLock);
271 dvmDbgCondBroadcast(&state->threadStartCond);
272 dvmDbgUnlockMutex(&state->threadStartLock);
274 /* set the thread state to VMWAIT so GCs don't wait for us */
275 dvmDbgThreadWaiting();
278 * Loop forever if we're in server mode, processing connections. In
279 * non-server mode, we bail out of the thread when the debugger drops
282 * We broadcast a notification when a debugger attaches, after we
283 * successfully process the handshake.
288 if (state->params.server) {
290 * Block forever, waiting for a connection. To support the
291 * "timeout=xxx" option we'll need to tweak this.
293 if (!dvmJdwpAcceptConnection(state))
297 * If we're not acting as a server, we need to connect out to the
298 * debugger. To support the "timeout=xxx" option we need to
299 * have a timeout if the handshake reply isn't received in a
300 * reasonable amount of time.
302 if (!dvmJdwpEstablishConnection(state)) {
303 /* wake anybody who was waiting for us to succeed */
304 dvmDbgLockMutex(&state->attachLock);
305 dvmDbgCondBroadcast(&state->attachCond);
306 dvmDbgUnlockMutex(&state->attachLock);
311 /* prep debug code to handle the new connection */
314 /* process requests until the debugger drops */
317 // sanity check -- shouldn't happen?
318 if (dvmThreadSelf()->status != THREAD_VMWAIT) {
319 ALOGE("JDWP thread no longer in VMWAIT (now %d); resetting",
320 dvmThreadSelf()->status);
321 dvmDbgThreadWaiting();
324 if (!dvmJdwpProcessIncoming(state)) /* blocking read */
327 if (first && !dvmJdwpAwaitingHandshake(state)) {
328 /* handshake worked, tell the interpreter that we're active */
331 /* set thread ID; requires object registry to be active */
332 state->debugThreadId = dvmDbgGetThreadSelfId();
334 /* wake anybody who's waiting for us */
335 dvmDbgLockMutex(&state->attachLock);
336 dvmDbgCondBroadcast(&state->attachCond);
337 dvmDbgUnlockMutex(&state->attachLock);
341 dvmJdwpCloseConnection(state);
343 if (state->ddmActive) {
344 state->ddmActive = false;
346 /* broadcast the disconnect; must be in RUNNING state */
347 dvmDbgThreadRunning();
348 dvmDbgDdmDisconnected();
349 dvmDbgThreadWaiting();
352 /* release session state, e.g. remove breakpoint instructions */
353 dvmJdwpResetState(state);
355 /* tell the interpreter that the debugger is no longer around */
356 dvmDbgDisconnected();
358 /* if we had threads suspended, resume them now */
359 dvmUndoDebuggerSuspensions();
361 /* if we connected out, this was a one-shot deal */
362 if (!state->params.server)
366 /* back to running, for thread shutdown */
367 dvmDbgThreadRunning();
369 ALOGV("JDWP: thread exiting");
375 * Return the thread handle, or (pthread_t)0 if the debugger isn't running.
377 pthread_t dvmJdwpGetDebugThread(JdwpState* state)
382 return state->debugThreadHandle;
387 * Support routines for waitForDebugger().
389 * We can't have a trivial "waitForDebugger" function that returns the
390 * instant the debugger connects, because we run the risk of executing code
391 * before the debugger has had a chance to configure breakpoints or issue
392 * suspend calls. It would be nice to just sit in the suspended state, but
393 * most debuggers don't expect any threads to be suspended when they attach.
395 * There's no JDWP event we can post to tell the debugger, "we've stopped,
396 * and we like it that way". We could send a fake breakpoint, which should
397 * cause the debugger to immediately send a resume, but the debugger might
398 * send the resume immediately or might throw an exception of its own upon
399 * receiving a breakpoint event that it didn't ask for.
401 * What we really want is a "wait until the debugger is done configuring
402 * stuff" event. We can approximate this with a "wait until the debugger
403 * has been idle for a brief period".
407 * Get a notion of the current time, in milliseconds.
409 s8 dvmJdwpGetNowMsec()
411 #ifdef HAVE_POSIX_CLOCKS
413 clock_gettime(CLOCK_MONOTONIC, &now);
414 return now.tv_sec * 1000LL + now.tv_nsec / 1000000LL;
417 gettimeofday(&now, NULL);
418 return now.tv_sec * 1000LL + now.tv_usec / 1000LL;
423 * Return the time, in milliseconds, since the last debugger activity.
425 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
426 * processing a debugger request.
428 s8 dvmJdwpLastDebuggerActivity(JdwpState* state)
430 if (!gDvm.debuggerActive) {
431 ALOGD("dvmJdwpLastDebuggerActivity: no active debugger");
435 s8 last = dvmQuasiAtomicRead64(&state->lastActivityWhen);
437 /* initializing or in the middle of something? */
439 ALOGV("+++ last=busy");
443 /* now get the current time */
444 s8 now = dvmJdwpGetNowMsec();
447 ALOGV("+++ debugger interval=%lld", now - last);