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);
37 * Does not return until JDWP thread is running, but may return before
38 * the thread is accepting network connections.
40 JdwpState* dvmJdwpStartup(const JdwpStartupParams* pParams)
42 JdwpState* state = NULL;
44 /* comment this out when debugging JDWP itself */
45 android_setMinPriority(LOG_TAG, ANDROID_LOG_DEBUG);
47 state = (JdwpState*) calloc(1, sizeof(JdwpState));
49 state->params = *pParams;
51 state->requestSerial = 0x10000000;
52 state->eventSerial = 0x20000000;
53 dvmDbgInitMutex(&state->threadStartLock);
54 dvmDbgInitMutex(&state->attachLock);
55 dvmDbgInitMutex(&state->serialLock);
56 dvmDbgInitMutex(&state->eventLock);
57 state->eventThreadId = 0;
58 dvmDbgInitMutex(&state->eventThreadLock);
59 dvmDbgInitCond(&state->threadStartCond);
60 dvmDbgInitCond(&state->attachCond);
61 dvmDbgInitCond(&state->eventThreadCond);
63 switch (pParams->transport) {
64 case kJdwpTransportSocket:
65 // LOGD("prepping for JDWP over TCP\n");
66 state->transport = dvmJdwpSocketTransport();
68 case kJdwpTransportAndroidAdb:
69 // LOGD("prepping for JDWP over ADB\n");
70 state->transport = dvmJdwpAndroidAdbTransport();
74 LOGE("Unknown transport %d\n", pParams->transport);
79 if (!dvmJdwpNetStartup(state, pParams))
83 * Grab a mutex or two before starting the thread. This ensures they
84 * won't signal the cond var before we're waiting.
86 dvmDbgLockMutex(&state->threadStartLock);
88 dvmDbgLockMutex(&state->attachLock);
91 * We have bound to a port, or are trying to connect outbound to a
92 * debugger. Create the JDWP thread and let it continue the mission.
94 if (!dvmCreateInternalThread(&state->debugThreadHandle, "JDWP",
95 jdwpThreadStart, state))
97 /* state is getting tossed, but unlock these anyway for cleanliness */
98 dvmDbgUnlockMutex(&state->threadStartLock);
100 dvmDbgUnlockMutex(&state->attachLock);
105 * Wait until the thread finishes basic initialization.
106 * TODO: cond vars should be waited upon in a loop
108 dvmDbgCondWait(&state->threadStartCond, &state->threadStartLock);
109 dvmDbgUnlockMutex(&state->threadStartLock);
113 * For suspend=y, wait for the debugger to connect to us or for us to
114 * connect to the debugger.
116 * The JDWP thread will signal us when it connects successfully or
117 * times out (for timeout=xxx), so we have to check to see what happened
120 if (pParams->suspend) {
121 dvmChangeStatus(NULL, THREAD_VMWAIT);
122 dvmDbgCondWait(&state->attachCond, &state->attachLock);
123 dvmDbgUnlockMutex(&state->attachLock);
124 dvmChangeStatus(NULL, THREAD_RUNNING);
126 if (!dvmJdwpIsActive(state)) {
127 LOGE("JDWP connection failed\n");
131 LOGI("JDWP connected\n");
134 * Ordinarily we would pause briefly to allow the debugger to set
135 * breakpoints and so on, but for "suspend=y" the VM init code will
136 * pause the VM when it sends the VM_START message.
143 dvmJdwpShutdown(state); // frees state
148 * Reset all session-related state. There should not be an active connection
149 * to the client at this point. The rest of the VM still thinks there is
150 * a debugger attached.
152 * This includes freeing up the debugger event list.
154 void dvmJdwpResetState(JdwpState* state)
156 /* could reset the serial numbers, but no need to */
158 dvmJdwpUnregisterAll(state);
159 assert(state->eventList == NULL);
162 * Should not have one of these in progress. If the debugger went away
163 * mid-request, though, we could see this.
165 if (state->eventThreadId != 0) {
166 LOGW("WARNING: resetting state while event in progress\n");
172 * Tell the JDWP thread to shut down. Frees "state".
174 void dvmJdwpShutdown(JdwpState* state)
181 if (dvmJdwpIsTransportDefined(state)) {
182 if (dvmJdwpIsConnected(state))
183 dvmJdwpPostVMDeath(state);
186 * Close down the network to inspire the thread to halt.
188 if (gDvm.verboseShutdown)
189 LOGD("JDWP shutting down net...\n");
190 dvmJdwpNetShutdown(state);
192 if (state->debugThreadStarted) {
194 if (pthread_join(state->debugThreadHandle, &threadReturn) != 0) {
195 LOGW("JDWP thread join failed\n");
199 if (gDvm.verboseShutdown)
200 LOGD("JDWP freeing netstate...\n");
201 dvmJdwpNetFree(state);
202 state->netState = NULL;
204 assert(state->netState == NULL);
206 dvmJdwpResetState(state);
211 * Are we talking to a debugger?
213 bool dvmJdwpIsActive(JdwpState* state)
215 return dvmJdwpIsConnected(state);
219 * Entry point for JDWP thread. The thread was created through the VM
220 * mechanisms, so there is a java/lang/Thread associated with us.
222 static void* jdwpThreadStart(void* arg)
224 JdwpState* state = (JdwpState*) arg;
226 LOGV("JDWP: thread running\n");
229 * Finish initializing "state", then notify the creating thread that
232 state->debugThreadHandle = dvmThreadSelf()->handle;
234 ANDROID_MEMBAR_FULL();
235 state->debugThreadStarted = true; // touch this last
237 dvmDbgLockMutex(&state->threadStartLock);
238 dvmDbgCondBroadcast(&state->threadStartCond);
239 dvmDbgUnlockMutex(&state->threadStartLock);
241 /* set the thread state to VMWAIT so GCs don't wait for us */
242 dvmDbgThreadWaiting();
245 * Loop forever if we're in server mode, processing connections. In
246 * non-server mode, we bail out of the thread when the debugger drops
249 * We broadcast a notification when a debugger attaches, after we
250 * successfully process the handshake.
255 if (state->params.server) {
257 * Block forever, waiting for a connection. To support the
258 * "timeout=xxx" option we'll need to tweak this.
260 if (!dvmJdwpAcceptConnection(state))
264 * If we're not acting as a server, we need to connect out to the
265 * debugger. To support the "timeout=xxx" option we need to
266 * have a timeout if the handshake reply isn't received in a
267 * reasonable amount of time.
269 if (!dvmJdwpEstablishConnection(state)) {
270 /* wake anybody who was waiting for us to succeed */
271 dvmDbgLockMutex(&state->attachLock);
272 dvmDbgCondBroadcast(&state->attachCond);
273 dvmDbgUnlockMutex(&state->attachLock);
278 /* prep debug code to handle the new connection */
281 /* process requests until the debugger drops */
284 // sanity check -- shouldn't happen?
285 if (dvmThreadSelf()->status != THREAD_VMWAIT) {
286 LOGE("JDWP thread no longer in VMWAIT (now %d); resetting\n",
287 dvmThreadSelf()->status);
288 dvmDbgThreadWaiting();
291 if (!dvmJdwpProcessIncoming(state)) /* blocking read */
294 if (first && !dvmJdwpAwaitingHandshake(state)) {
295 /* handshake worked, tell the interpreter that we're active */
298 /* set thread ID; requires object registry to be active */
299 state->debugThreadId = dvmDbgGetThreadSelfId();
301 /* wake anybody who's waiting for us */
302 dvmDbgLockMutex(&state->attachLock);
303 dvmDbgCondBroadcast(&state->attachCond);
304 dvmDbgUnlockMutex(&state->attachLock);
308 dvmJdwpCloseConnection(state);
310 if (state->ddmActive) {
311 state->ddmActive = false;
313 /* broadcast the disconnect; must be in RUNNING state */
314 dvmDbgThreadRunning();
315 dvmDbgDdmDisconnected();
316 dvmDbgThreadWaiting();
319 /* release session state, e.g. remove breakpoint instructions */
320 dvmJdwpResetState(state);
322 /* tell the interpreter that the debugger is no longer around */
323 dvmDbgDisconnected();
325 /* if we had threads suspended, resume them now */
326 dvmUndoDebuggerSuspensions();
328 /* if we connected out, this was a one-shot deal */
329 if (!state->params.server)
333 /* back to running, for thread shutdown */
334 dvmDbgThreadRunning();
336 LOGV("JDWP: thread exiting\n");
342 * Return the thread handle, or (pthread_t)0 if the debugger isn't running.
344 pthread_t dvmJdwpGetDebugThread(JdwpState* state)
349 return state->debugThreadHandle;
354 * Support routines for waitForDebugger().
356 * We can't have a trivial "waitForDebugger" function that returns the
357 * instant the debugger connects, because we run the risk of executing code
358 * before the debugger has had a chance to configure breakpoints or issue
359 * suspend calls. It would be nice to just sit in the suspended state, but
360 * most debuggers don't expect any threads to be suspended when they attach.
362 * There's no JDWP event we can post to tell the debugger, "we've stopped,
363 * and we like it that way". We could send a fake breakpoint, which should
364 * cause the debugger to immediately send a resume, but the debugger might
365 * send the resume immediately or might throw an exception of its own upon
366 * receiving a breakpoint event that it didn't ask for.
368 * What we really want is a "wait until the debugger is done configuring
369 * stuff" event. We can approximate this with a "wait until the debugger
370 * has been idle for a brief period".
374 * Get a notion of the current time, in milliseconds.
376 s8 dvmJdwpGetNowMsec(void)
378 #ifdef HAVE_POSIX_CLOCKS
380 clock_gettime(CLOCK_MONOTONIC, &now);
381 return now.tv_sec * 1000LL + now.tv_nsec / 1000000LL;
384 gettimeofday(&now, NULL);
385 return now.tv_sec * 1000LL + now.tv_usec / 1000LL;
390 * Return the time, in milliseconds, since the last debugger activity.
392 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
393 * processing a debugger request.
395 s8 dvmJdwpLastDebuggerActivity(JdwpState* state)
397 if (!gDvm.debuggerActive) {
398 LOGD("dvmJdwpLastDebuggerActivity: no active debugger\n");
402 s8 last = dvmQuasiAtomicRead64(&state->lastActivityWhen);
404 /* initializing or in the middle of something? */
406 LOGV("+++ last=busy\n");
410 /* now get the current time */
411 s8 now = dvmJdwpGetNowMsec();
414 LOGV("+++ debugger interval=%lld\n", now - last);