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.
17 * Send events to the debugger.
19 #include "jdwp/JdwpPriv.h"
20 #include "jdwp/JdwpConstants.h"
21 #include "jdwp/JdwpHandler.h"
22 #include "jdwp/JdwpEvent.h"
23 #include "jdwp/ExpandBuf.h"
27 #include <stddef.h> /* for offsetof() */
33 The event add/remove stuff usually happens from the debugger thread,
34 in response to requests from the debugger, but can also happen as the
35 result of an event in an arbitrary thread (e.g. an event with a "count"
36 mod expires). It's important to keep the event list locked when processing
39 Event posting can happen from any thread. The JDWP thread will not usually
40 post anything but VM start/death, but if a JDWP request causes a class
41 to be loaded, the ClassPrepare event will come from the JDWP thread.
44 We can have serialization issues when we post an event to the debugger.
45 For example, a thread could send an "I hit a breakpoint and am suspending
46 myself" message to the debugger. Before it manages to suspend itself, the
47 debugger's response ("not interested, resume thread") arrives and is
48 processed. We try to resume a thread that hasn't yet suspended.
50 This means that, after posting an event to the debugger, we need to wait
51 for the event thread to suspend itself (and, potentially, all other threads)
52 before processing any additional requests from the debugger. While doing
53 so we need to be aware that multiple threads may be hitting breakpoints
54 or other events simultaneously, so we either need to wait for all of them
55 or serialize the events with each other.
57 The current mechanism works like this:
59 - If I'm going to suspend, grab the "I am posting an event" token. Wait
60 for it if it's not currently available.
61 - Post the event to the debugger.
62 - If appropriate, suspend others and then myself. As part of suspending
63 myself, release the "I am posting" token.
65 - When an event arrives, see if somebody is posting an event. If so,
66 sleep until we can acquire the "I am posting an event" token. Release
67 it immediately and continue processing -- the event we have already
68 received should not interfere with other events that haven't yet
71 Some care must be taken to avoid deadlock:
73 - thread A and thread B exit near-simultaneously, and post thread-death
74 events with a "suspend all" clause
75 - thread A gets the event token, thread B sits and waits for it
76 - thread A wants to suspend all other threads, but thread B is waiting
77 for the token and can't be suspended
79 So we need to mark thread B in such a way that thread A doesn't wait for it.
81 If we just bracket the "grab event token" call with a change to VMWAIT
82 before sleeping, the switch back to RUNNING state when we get the token
83 will cause thread B to suspend (remember, thread A's global suspend is
84 still in force, even after it releases the token). Suspending while
85 holding the event token is very bad, because it prevents the JDWP thread
86 from processing incoming messages.
88 We need to change to VMWAIT state at the *start* of posting an event,
89 and stay there until we either finish posting the event or decide to
90 put ourselves to sleep. That way we don't interfere with anyone else and
91 don't allow anyone else to interfere with us.
95 #define kJdwpEventCommandSet 64
96 #define kJdwpCompositeCommand 100
99 * Stuff to compare against when deciding if a mod matches. Only the
100 * values for mods valid for the event being evaluated will be filled in.
101 * The rest will be zeroed.
103 typedef struct ModBasket {
104 const JdwpLocation* pLoc; /* LocationOnly */
105 const char* className; /* ClassMatch/ClassExclude */
106 ObjectId threadId; /* ThreadOnly */
107 RefTypeId classId; /* ClassOnly */
108 RefTypeId excepClassId; /* ExceptionOnly */
109 bool caught; /* ExceptionOnly */
110 FieldId field; /* FieldOnly */
111 ObjectId thisPtr; /* InstanceOnly */
112 /* nothing for StepOnly -- handled differently */
116 * Get the next "request" serial number. We use this when sending
117 * packets to the debugger.
119 u4 dvmJdwpNextRequestSerial(JdwpState* state)
123 dvmDbgLockMutex(&state->serialLock);
124 result = state->requestSerial++;
125 dvmDbgUnlockMutex(&state->serialLock);
131 * Get the next "event" serial number. We use this in the response to
132 * message type EventRequest.Set.
134 u4 dvmJdwpNextEventSerial(JdwpState* state)
138 dvmDbgLockMutex(&state->serialLock);
139 result = state->eventSerial++;
140 dvmDbgUnlockMutex(&state->serialLock);
146 * Lock the "event" mutex, which guards the list of registered events.
148 static void lockEventMutex(JdwpState* state)
150 //dvmDbgThreadWaiting();
151 dvmDbgLockMutex(&state->eventLock);
152 //dvmDbgThreadRunning();
156 * Unlock the "event" mutex.
158 static void unlockEventMutex(JdwpState* state)
160 dvmDbgUnlockMutex(&state->eventLock);
164 * Add an event to the list. Ordering is not important.
166 * If something prevents the event from being registered, e.g. it's a
167 * single-step request on a thread that doesn't exist, the event will
168 * not be added to the list, and an appropriate error will be returned.
170 JdwpError dvmJdwpRegisterEvent(JdwpState* state, JdwpEvent* pEvent)
172 JdwpError err = ERR_NONE;
175 lockEventMutex(state);
177 assert(state != NULL);
178 assert(pEvent != NULL);
179 assert(pEvent->prev == NULL);
180 assert(pEvent->next == NULL);
183 * If one or more LocationOnly mods are used, register them with
186 for (i = 0; i < pEvent->modCount; i++) {
187 JdwpEventMod* pMod = &pEvent->mods[i];
188 if (pMod->modKind == MK_LOCATION_ONLY) {
189 /* should only be for Breakpoint, Step, and Exception */
190 dvmDbgWatchLocation(&pMod->locationOnly.loc);
192 if (pMod->modKind == MK_STEP) {
193 /* should only be for EK_SINGLE_STEP; should only be one */
194 dvmDbgConfigureStep(pMod->step.threadId, pMod->step.size,
202 if (state->eventList != NULL) {
203 pEvent->next = state->eventList;
204 state->eventList->prev = pEvent;
206 state->eventList = pEvent;
210 unlockEventMutex(state);
216 * Remove an event from the list. This will also remove the event from
217 * any optimization tables, e.g. breakpoints.
219 * Does not free the JdwpEvent.
221 * Grab the eventLock before calling here.
223 static void unregisterEvent(JdwpState* state, JdwpEvent* pEvent)
227 if (pEvent->prev == NULL) {
228 /* head of the list */
229 assert(state->eventList == pEvent);
231 state->eventList = pEvent->next;
233 pEvent->prev->next = pEvent->next;
236 if (pEvent->next != NULL) {
237 pEvent->next->prev = pEvent->prev;
243 * Unhook us from the interpreter, if necessary.
245 for (i = 0; i < pEvent->modCount; i++) {
246 JdwpEventMod* pMod = &pEvent->mods[i];
247 if (pMod->modKind == MK_LOCATION_ONLY) {
248 /* should only be for Breakpoint, Step, and Exception */
249 dvmDbgUnwatchLocation(&pMod->locationOnly.loc);
251 if (pMod->modKind == MK_STEP) {
252 /* should only be for EK_SINGLE_STEP; should only be one */
253 dvmDbgUnconfigureStep(pMod->step.threadId);
258 assert(state->numEvents != 0 || state->eventList == NULL);
262 * Remove the event with the given ID from the list.
264 * Failure to find the event isn't really an error, but it is a little
265 * weird. (It looks like Eclipse will try to be extra careful and will
266 * explicitly remove one-off single-step events.)
268 void dvmJdwpUnregisterEventById(JdwpState* state, u4 requestId)
272 lockEventMutex(state);
274 pEvent = state->eventList;
275 while (pEvent != NULL) {
276 if (pEvent->requestId == requestId) {
277 unregisterEvent(state, pEvent);
278 dvmJdwpEventFree(pEvent);
279 goto done; /* there can be only one with a given ID */
282 pEvent = pEvent->next;
285 //LOGD("Odd: no match when removing event reqId=0x%04x\n", requestId);
288 unlockEventMutex(state);
292 * Remove all entries from the event list.
294 void dvmJdwpUnregisterAll(JdwpState* state)
297 JdwpEvent* pNextEvent;
299 lockEventMutex(state);
301 pEvent = state->eventList;
302 while (pEvent != NULL) {
303 pNextEvent = pEvent->next;
305 unregisterEvent(state, pEvent);
306 dvmJdwpEventFree(pEvent);
310 state->eventList = NULL;
312 unlockEventMutex(state);
318 * Allocate a JdwpEvent struct with enough space to hold the specified
319 * number of mod records.
321 JdwpEvent* dvmJdwpEventAlloc(int numMods)
324 int allocSize = offsetof(JdwpEvent, mods) +
325 numMods * sizeof(newEvent->mods[0]);
327 newEvent = (JdwpEvent*)malloc(allocSize);
328 memset(newEvent, 0, allocSize);
335 * Do not call this until the event has been removed from the list.
337 void dvmJdwpEventFree(JdwpEvent* pEvent)
344 /* make sure it was removed from the list */
345 assert(pEvent->prev == NULL);
346 assert(pEvent->next == NULL);
347 /* want to assert state->eventList != pEvent */
350 * Free any hairy bits in the mods.
352 for (i = 0; i < pEvent->modCount; i++) {
353 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
354 free(pEvent->mods[i].classMatch.classPattern);
355 pEvent->mods[i].classMatch.classPattern = NULL;
357 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
358 free(pEvent->mods[i].classExclude.classPattern);
359 pEvent->mods[i].classExclude.classPattern = NULL;
367 * Allocate storage for matching events. To keep things simple we
368 * use an array with enough storage for the entire list.
370 * The state->eventLock should be held before calling.
372 static JdwpEvent** allocMatchList(JdwpState* state)
374 return (JdwpEvent**) malloc(sizeof(JdwpEvent*) * state->numEvents);
378 * Run through the list and remove any entries with an expired "count" mod
379 * from the event list, then free the match list.
381 static void cleanupMatchList(JdwpState* state, JdwpEvent** matchList,
384 JdwpEvent** ppEvent = matchList;
386 while (matchCount--) {
387 JdwpEvent* pEvent = *ppEvent;
390 for (i = 0; i < pEvent->modCount; i++) {
391 if (pEvent->mods[i].modKind == MK_COUNT &&
392 pEvent->mods[i].count.count == 0)
394 LOGV("##### Removing expired event\n");
395 unregisterEvent(state, pEvent);
396 dvmJdwpEventFree(pEvent);
408 * Match a string against a "restricted regular expression", which is just
409 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
411 * ("Restricted name globbing" might have been a better term.)
413 static bool patternMatch(const char* pattern, const char* target)
415 int patLen = strlen(pattern);
417 if (pattern[0] == '*') {
418 int targetLen = strlen(target);
420 // TODO: remove printf when we find a test case to verify this
421 LOGE(">>> comparing '%s' to '%s'\n",
422 pattern+1, target + (targetLen-patLen));
424 if (targetLen < patLen)
426 return strcmp(pattern+1, target + (targetLen-patLen)) == 0;
427 } else if (pattern[patLen-1] == '*') {
430 return strncmp(pattern, target, patLen-1) == 0;
432 return strcmp(pattern, target) == 0;
437 * See if two locations are equal.
439 * It's tempting to do a bitwise compare ("struct ==" or memcmp), but if
440 * the storage wasn't zeroed out there could be undefined values in the
441 * padding. Besides, the odds of "idx" being equal while the others aren't
442 * is very small, so this is usually just a simple integer comparison.
444 static inline bool locationMatch(const JdwpLocation* pLoc1,
445 const JdwpLocation* pLoc2)
447 return pLoc1->idx == pLoc2->idx &&
448 pLoc1->methodId == pLoc2->methodId &&
449 pLoc1->classId == pLoc2->classId &&
450 pLoc1->typeTag == pLoc2->typeTag;
454 * See if the event's mods match up with the contents of "basket".
456 * If we find a Count mod before rejecting an event, we decrement it. We
457 * need to do this even if later mods cause us to ignore the event.
459 static bool modsMatch(JdwpState* state, JdwpEvent* pEvent, ModBasket* basket)
461 JdwpEventMod* pMod = pEvent->mods;
464 for (i = pEvent->modCount; i > 0; i--, pMod++) {
465 switch (pMod->modKind) {
467 assert(pMod->count.count > 0);
471 assert(false); // should not be getting these
474 if (pMod->threadOnly.threadId != basket->threadId)
478 if (!dvmDbgMatchType(basket->classId,
479 pMod->classOnly.referenceTypeId))
483 if (!patternMatch(pMod->classMatch.classPattern,
487 case MK_CLASS_EXCLUDE:
488 if (patternMatch(pMod->classMatch.classPattern,
492 case MK_LOCATION_ONLY:
493 if (!locationMatch(&pMod->locationOnly.loc, basket->pLoc))
496 case MK_EXCEPTION_ONLY:
497 if (pMod->exceptionOnly.refTypeId != 0 &&
498 !dvmDbgMatchType(basket->excepClassId,
499 pMod->exceptionOnly.refTypeId))
501 if ((basket->caught && !pMod->exceptionOnly.caught) ||
502 (!basket->caught && !pMod->exceptionOnly.uncaught))
509 if (pMod->step.threadId != basket->threadId)
512 case MK_INSTANCE_ONLY:
513 if (pMod->instanceOnly.objectId != basket->thisPtr)
517 LOGE("unhandled mod kind %d\n", pMod->modKind);
527 * Find all events of type "eventKind" with mods that match up with the
528 * rest of the arguments.
530 * Found events are appended to "matchList", and "*pMatchCount" is advanced,
531 * so this may be called multiple times for grouped events.
533 * DO NOT call this multiple times for the same eventKind, as Count mods are
534 * decremented during the scan.
536 static void findMatchingEvents(JdwpState* state, enum JdwpEventKind eventKind,
537 ModBasket* basket, JdwpEvent** matchList, int* pMatchCount)
541 /* start after the existing entries */
542 matchList += *pMatchCount;
544 pEvent = state->eventList;
545 while (pEvent != NULL) {
546 if (pEvent->eventKind == eventKind && modsMatch(state, pEvent, basket))
548 *matchList++ = pEvent;
552 pEvent = pEvent->next;
557 * Scan through the list of matches and determine the most severe
560 static enum JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** matchList,
563 enum JdwpSuspendPolicy policy = SP_NONE;
565 while (matchCount--) {
566 if ((*matchList)->suspendPolicy > policy)
567 policy = (*matchList)->suspendPolicy;
575 * Three possibilities:
576 * SP_NONE - do nothing
577 * SP_EVENT_THREAD - suspend ourselves
578 * SP_ALL - suspend everybody except JDWP support thread
580 static void suspendByPolicy(JdwpState* state,
581 enum JdwpSuspendPolicy suspendPolicy)
583 if (suspendPolicy == SP_NONE)
586 if (suspendPolicy == SP_ALL) {
587 dvmDbgSuspendVM(true);
589 assert(suspendPolicy == SP_EVENT_THREAD);
592 /* this is rare but possible -- see CLASS_PREPARE handling */
593 if (dvmDbgGetThreadSelfId() == state->debugThreadId) {
594 LOGI("NOTE: suspendByPolicy not suspending JDWP thread\n");
598 DebugInvokeReq* pReq = dvmDbgGetInvokeReq();
605 * The JDWP thread has told us (and possibly all other threads) to
606 * resume. See if it has left anything in our DebugInvokeReq mailbox.
608 if (!pReq->invokeNeeded) {
609 /*LOGD("suspendByPolicy: no invoke needed\n");*/
613 /* grab this before posting/suspending again */
614 dvmJdwpSetWaitForEventThread(state, dvmDbgGetThreadSelfId());
616 /* leave pReq->invokeNeeded raised so we can check reentrancy */
617 LOGV("invoking method...\n");
618 dvmDbgExecuteMethod(pReq);
620 pReq->err = ERR_NONE;
622 /* clear this before signaling */
623 pReq->invokeNeeded = false;
625 LOGV("invoke complete, signaling and self-suspending\n");
626 dvmDbgLockMutex(&pReq->lock);
627 dvmDbgCondSignal(&pReq->cv);
628 dvmDbgUnlockMutex(&pReq->lock);
633 * Determine if there is a method invocation in progress in the current
636 * We look at the "invokeNeeded" flag in the per-thread DebugInvokeReq
637 * state. If set, we're in the process of invoking a method.
639 static bool invokeInProgress(JdwpState* state)
641 DebugInvokeReq* pReq = dvmDbgGetInvokeReq();
642 return pReq->invokeNeeded;
646 * We need the JDWP thread to hold off on doing stuff while we post an
647 * event and then suspend ourselves.
649 * Call this with a threadId of zero if you just want to wait for the
650 * current thread operation to complete.
652 * This could go to sleep waiting for another thread, so it's important
653 * that the thread be marked as VMWAIT before calling here.
655 void dvmJdwpSetWaitForEventThread(JdwpState* state, ObjectId threadId)
659 /* this is held for very brief periods; contention is unlikely */
660 dvmDbgLockMutex(&state->eventThreadLock);
663 * If another thread is already doing stuff, wait for it. This can
664 * go to sleep indefinitely.
666 while (state->eventThreadId != 0) {
667 LOGV("event in progress (0x%llx), 0x%llx sleeping\n",
668 state->eventThreadId, threadId);
670 dvmDbgCondWait(&state->eventThreadCond, &state->eventThreadLock);
673 if (waited || threadId != 0)
674 LOGV("event token grabbed (0x%llx)\n", threadId);
676 state->eventThreadId = threadId;
678 dvmDbgUnlockMutex(&state->eventThreadLock);
682 * Clear the threadId and signal anybody waiting.
684 void dvmJdwpClearWaitForEventThread(JdwpState* state)
687 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
688 * function is called by dvmSuspendSelf(), and the transition back
689 * to RUNNING would confuse it.
691 dvmDbgLockMutex(&state->eventThreadLock);
693 assert(state->eventThreadId != 0);
694 LOGV("cleared event token (0x%llx)\n", state->eventThreadId);
696 state->eventThreadId = 0;
698 dvmDbgCondSignal(&state->eventThreadCond);
700 dvmDbgUnlockMutex(&state->eventThreadLock);
705 * Prep an event. Allocates storage for the message and leaves space for
708 static ExpandBuf* eventPrep(void)
712 pReq = expandBufAlloc();
713 expandBufAddSpace(pReq, kJDWPHeaderLen);
719 * Write the header into the buffer and send the packet off to the debugger.
721 * Takes ownership of "pReq" (currently discards it).
723 static void eventFinish(JdwpState* state, ExpandBuf* pReq)
725 u1* buf = expandBufGetBuffer(pReq);
727 set4BE(buf, expandBufGetLength(pReq));
728 set4BE(buf+4, dvmJdwpNextRequestSerial(state));
729 set1(buf+8, 0); /* flags */
730 set1(buf+9, kJdwpEventCommandSet);
731 set1(buf+10, kJdwpCompositeCommand);
733 dvmJdwpSendRequest(state, pReq);
740 * Tell the debugger that we have finished initializing. This is always
741 * sent, even if the debugger hasn't requested it.
743 * This should be sent "before the main thread is started and before
744 * any application code has been executed". The thread ID in the message
745 * must be for the main thread.
747 bool dvmJdwpPostVMStart(JdwpState* state, bool suspend)
749 enum JdwpSuspendPolicy suspendPolicy;
750 ObjectId threadId = dvmDbgGetThreadSelfId();
753 suspendPolicy = SP_ALL;
755 suspendPolicy = SP_NONE;
757 /* probably don't need this here */
758 lockEventMutex(state);
760 ExpandBuf* pReq = NULL;
762 LOGV("EVENT: %s\n", dvmJdwpEventKindStr(EK_VM_START));
763 LOGV(" suspendPolicy=%s\n", dvmJdwpSuspendPolicyStr(suspendPolicy));
766 expandBufAdd1(pReq, suspendPolicy);
767 expandBufAdd4BE(pReq, 1);
769 expandBufAdd1(pReq, EK_VM_START);
770 expandBufAdd4BE(pReq, 0); /* requestId */
771 expandBufAdd8BE(pReq, threadId);
774 unlockEventMutex(state);
776 /* send request and possibly suspend ourselves */
778 int oldStatus = dvmDbgThreadWaiting();
779 if (suspendPolicy != SP_NONE)
780 dvmJdwpSetWaitForEventThread(state, threadId);
782 eventFinish(state, pReq);
784 suspendByPolicy(state, suspendPolicy);
785 dvmDbgThreadContinuing(oldStatus);
792 * A location of interest has been reached. This handles:
797 * These four types must be grouped together in a single response. The
798 * "eventFlags" indicates the type of event(s) that have happened.
801 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
802 * LocationOnly (for breakpoint/step only)
803 * Step (for step only)
805 * Interesting test cases:
806 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
807 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
808 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
809 * - Single-step to a line with a breakpoint. Should get a single
810 * event message with both events in it.
812 bool dvmJdwpPostLocationEvent(JdwpState* state, const JdwpLocation* pLoc,
813 ObjectId thisPtr, int eventFlags)
815 enum JdwpSuspendPolicy suspendPolicy = SP_NONE;
817 JdwpEvent** matchList;
819 char* nameAlloc = NULL;
821 memset(&basket, 0, sizeof(basket));
823 basket.classId = pLoc->classId;
824 basket.thisPtr = thisPtr;
825 basket.threadId = dvmDbgGetThreadSelfId();
826 basket.className = nameAlloc =
827 dvmDescriptorToName(dvmDbgGetClassDescriptor(pLoc->classId));
830 * On rare occasions we may need to execute interpreted code in the VM
831 * while handling a request from the debugger. Don't fire breakpoints
832 * while doing so. (I don't think we currently do this at all, so
833 * this is mostly paranoia.)
835 if (basket.threadId == state->debugThreadId) {
836 LOGV("Ignoring location event in JDWP thread\n");
842 * The debugger variable display tab may invoke the interpreter to format
843 * complex objects. We want to ignore breakpoints and method entry/exit
844 * traps while working on behalf of the debugger.
846 * If we don't ignore them, the VM will get hung up, because we'll
847 * suspend on a breakpoint while the debugger is still waiting for its
848 * method invocation to complete.
850 if (invokeInProgress(state)) {
851 LOGV("Not checking breakpoints during invoke (%s)\n", basket.className);
856 /* don't allow the list to be updated while we scan it */
857 lockEventMutex(state);
859 matchList = allocMatchList(state);
862 if ((eventFlags & DBG_BREAKPOINT) != 0)
863 findMatchingEvents(state, EK_BREAKPOINT, &basket, matchList,
865 if ((eventFlags & DBG_SINGLE_STEP) != 0)
866 findMatchingEvents(state, EK_SINGLE_STEP, &basket, matchList,
868 if ((eventFlags & DBG_METHOD_ENTRY) != 0)
869 findMatchingEvents(state, EK_METHOD_ENTRY, &basket, matchList,
871 if ((eventFlags & DBG_METHOD_EXIT) != 0)
872 findMatchingEvents(state, EK_METHOD_EXIT, &basket, matchList,
875 ExpandBuf* pReq = NULL;
876 if (matchCount != 0) {
879 LOGV("EVENT: %s(%d total) %s.%s thread=%llx code=%llx)\n",
880 dvmJdwpEventKindStr(matchList[0]->eventKind), matchCount,
882 dvmDbgGetMethodName(pLoc->classId, pLoc->methodId),
883 basket.threadId, pLoc->idx);
885 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
886 LOGV(" suspendPolicy=%s\n",
887 dvmJdwpSuspendPolicyStr(suspendPolicy));
890 expandBufAdd1(pReq, suspendPolicy);
891 expandBufAdd4BE(pReq, matchCount);
893 for (i = 0; i < matchCount; i++) {
894 expandBufAdd1(pReq, matchList[i]->eventKind);
895 expandBufAdd4BE(pReq, matchList[i]->requestId);
896 expandBufAdd8BE(pReq, basket.threadId);
897 dvmJdwpAddLocation(pReq, pLoc);
901 cleanupMatchList(state, matchList, matchCount);
902 unlockEventMutex(state);
904 /* send request and possibly suspend ourselves */
906 int oldStatus = dvmDbgThreadWaiting();
907 if (suspendPolicy != SP_NONE)
908 dvmJdwpSetWaitForEventThread(state, basket.threadId);
910 eventFinish(state, pReq);
912 suspendByPolicy(state, suspendPolicy);
913 dvmDbgThreadContinuing(oldStatus);
917 return matchCount != 0;
921 * A thread is starting or stopping.
926 bool dvmJdwpPostThreadChange(JdwpState* state, ObjectId threadId, bool start)
928 enum JdwpSuspendPolicy suspendPolicy = SP_NONE;
930 JdwpEvent** matchList;
933 assert(threadId = dvmDbgGetThreadSelfId());
936 * I don't think this can happen.
938 if (invokeInProgress(state)) {
939 LOGW("Not posting thread change during invoke\n");
943 memset(&basket, 0, sizeof(basket));
944 basket.threadId = threadId;
946 /* don't allow the list to be updated while we scan it */
947 lockEventMutex(state);
949 matchList = allocMatchList(state);
953 findMatchingEvents(state, EK_THREAD_START, &basket, matchList,
956 findMatchingEvents(state, EK_THREAD_DEATH, &basket, matchList,
959 ExpandBuf* pReq = NULL;
960 if (matchCount != 0) {
963 LOGV("EVENT: %s(%d total) thread=%llx)\n",
964 dvmJdwpEventKindStr(matchList[0]->eventKind), matchCount,
967 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
968 LOGV(" suspendPolicy=%s\n",
969 dvmJdwpSuspendPolicyStr(suspendPolicy));
972 expandBufAdd1(pReq, suspendPolicy);
973 expandBufAdd4BE(pReq, matchCount);
975 for (i = 0; i < matchCount; i++) {
976 expandBufAdd1(pReq, matchList[i]->eventKind);
977 expandBufAdd4BE(pReq, matchList[i]->requestId);
978 expandBufAdd8BE(pReq, basket.threadId);
983 cleanupMatchList(state, matchList, matchCount);
984 unlockEventMutex(state);
986 /* send request and possibly suspend ourselves */
988 int oldStatus = dvmDbgThreadWaiting();
989 if (suspendPolicy != SP_NONE)
990 dvmJdwpSetWaitForEventThread(state, basket.threadId);
992 eventFinish(state, pReq);
994 suspendByPolicy(state, suspendPolicy);
995 dvmDbgThreadContinuing(oldStatus);
998 return matchCount != 0;
1002 * Send a polite "VM is dying" message to the debugger.
1004 * Skips the usual "event token" stuff.
1006 bool dvmJdwpPostVMDeath(JdwpState* state)
1010 LOGV("EVENT: %s\n", dvmJdwpEventKindStr(EK_VM_DEATH));
1013 expandBufAdd1(pReq, SP_NONE);
1014 expandBufAdd4BE(pReq, 1);
1016 expandBufAdd1(pReq, EK_VM_DEATH);
1017 expandBufAdd4BE(pReq, 0);
1018 eventFinish(state, pReq);
1024 * An exception has been thrown. It may or may not have been caught.
1027 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
1028 * ExceptionOnly, InstanceOnly
1030 bool dvmJdwpPostException(JdwpState* state, const JdwpLocation* pThrowLoc,
1031 ObjectId exceptionId, RefTypeId exceptionClassId,
1032 const JdwpLocation* pCatchLoc, ObjectId thisPtr)
1034 enum JdwpSuspendPolicy suspendPolicy = SP_NONE;
1036 JdwpEvent** matchList;
1038 char* nameAlloc = NULL;
1040 memset(&basket, 0, sizeof(basket));
1041 basket.pLoc = pThrowLoc;
1042 basket.classId = pThrowLoc->classId;
1043 basket.threadId = dvmDbgGetThreadSelfId();
1044 basket.className = nameAlloc =
1045 dvmDescriptorToName(dvmDbgGetClassDescriptor(basket.classId));
1046 basket.excepClassId = exceptionClassId;
1047 basket.caught = (pCatchLoc->classId != 0);
1048 basket.thisPtr = thisPtr;
1050 /* don't try to post an exception caused by the debugger */
1051 if (invokeInProgress(state)) {
1052 LOGV("Not posting exception hit during invoke (%s)\n",basket.className);
1057 /* don't allow the list to be updated while we scan it */
1058 lockEventMutex(state);
1060 matchList = allocMatchList(state);
1063 findMatchingEvents(state, EK_EXCEPTION, &basket, matchList, &matchCount);
1065 ExpandBuf* pReq = NULL;
1066 if (matchCount != 0) {
1069 LOGV("EVENT: %s(%d total) thread=%llx exceptId=%llx caught=%d)\n",
1070 dvmJdwpEventKindStr(matchList[0]->eventKind), matchCount,
1071 basket.threadId, exceptionId, basket.caught);
1072 LOGV(" throw: %d %llx %x %lld (%s.%s)\n", pThrowLoc->typeTag,
1073 pThrowLoc->classId, pThrowLoc->methodId, pThrowLoc->idx,
1074 dvmDbgGetClassDescriptor(pThrowLoc->classId),
1075 dvmDbgGetMethodName(pThrowLoc->classId, pThrowLoc->methodId));
1076 if (pCatchLoc->classId == 0) {
1077 LOGV(" catch: (not caught)\n");
1079 LOGV(" catch: %d %llx %x %lld (%s.%s)\n", pCatchLoc->typeTag,
1080 pCatchLoc->classId, pCatchLoc->methodId, pCatchLoc->idx,
1081 dvmDbgGetClassDescriptor(pCatchLoc->classId),
1082 dvmDbgGetMethodName(pCatchLoc->classId, pCatchLoc->methodId));
1085 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
1086 LOGV(" suspendPolicy=%s\n",
1087 dvmJdwpSuspendPolicyStr(suspendPolicy));
1090 expandBufAdd1(pReq, suspendPolicy);
1091 expandBufAdd4BE(pReq, matchCount);
1093 for (i = 0; i < matchCount; i++) {
1094 expandBufAdd1(pReq, matchList[i]->eventKind);
1095 expandBufAdd4BE(pReq, matchList[i]->requestId);
1096 expandBufAdd8BE(pReq, basket.threadId);
1098 dvmJdwpAddLocation(pReq, pThrowLoc);
1099 expandBufAdd1(pReq, JT_OBJECT);
1100 expandBufAdd8BE(pReq, exceptionId);
1101 dvmJdwpAddLocation(pReq, pCatchLoc);
1105 cleanupMatchList(state, matchList, matchCount);
1106 unlockEventMutex(state);
1108 /* send request and possibly suspend ourselves */
1110 int oldStatus = dvmDbgThreadWaiting();
1111 if (suspendPolicy != SP_NONE)
1112 dvmJdwpSetWaitForEventThread(state, basket.threadId);
1114 eventFinish(state, pReq);
1116 suspendByPolicy(state, suspendPolicy);
1117 dvmDbgThreadContinuing(oldStatus);
1121 return matchCount != 0;
1125 * Announce that a class has been loaded.
1128 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1130 bool dvmJdwpPostClassPrepare(JdwpState* state, int tag, RefTypeId refTypeId,
1131 const char* signature, int status)
1133 enum JdwpSuspendPolicy suspendPolicy = SP_NONE;
1135 JdwpEvent** matchList;
1137 char* nameAlloc = NULL;
1139 memset(&basket, 0, sizeof(basket));
1140 basket.classId = refTypeId;
1141 basket.threadId = dvmDbgGetThreadSelfId();
1142 basket.className = nameAlloc =
1143 dvmDescriptorToName(dvmDbgGetClassDescriptor(basket.classId));
1145 /* suppress class prep caused by debugger */
1146 if (invokeInProgress(state)) {
1147 LOGV("Not posting class prep caused by invoke (%s)\n",basket.className);
1152 /* don't allow the list to be updated while we scan it */
1153 lockEventMutex(state);
1155 matchList = allocMatchList(state);
1158 findMatchingEvents(state, EK_CLASS_PREPARE, &basket, matchList,
1161 ExpandBuf* pReq = NULL;
1162 if (matchCount != 0) {
1165 LOGV("EVENT: %s(%d total) thread=%llx)\n",
1166 dvmJdwpEventKindStr(matchList[0]->eventKind), matchCount,
1169 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
1170 LOGV(" suspendPolicy=%s\n",
1171 dvmJdwpSuspendPolicyStr(suspendPolicy));
1173 if (basket.threadId == state->debugThreadId) {
1175 * JDWP says that, for a class prep in the debugger thread, we
1176 * should set threadId to null and if any threads were supposed
1177 * to be suspended then we suspend all other threads.
1179 LOGV(" NOTE: class prepare in debugger thread!\n");
1180 basket.threadId = 0;
1181 if (suspendPolicy == SP_EVENT_THREAD)
1182 suspendPolicy = SP_ALL;
1186 expandBufAdd1(pReq, suspendPolicy);
1187 expandBufAdd4BE(pReq, matchCount);
1189 for (i = 0; i < matchCount; i++) {
1190 expandBufAdd1(pReq, matchList[i]->eventKind);
1191 expandBufAdd4BE(pReq, matchList[i]->requestId);
1192 expandBufAdd8BE(pReq, basket.threadId);
1194 expandBufAdd1(pReq, tag);
1195 expandBufAdd8BE(pReq, refTypeId);
1196 expandBufAddUtf8String(pReq, (const u1*) signature);
1197 expandBufAdd4BE(pReq, status);
1201 cleanupMatchList(state, matchList, matchCount);
1203 unlockEventMutex(state);
1205 /* send request and possibly suspend ourselves */
1207 int oldStatus = dvmDbgThreadWaiting();
1208 if (suspendPolicy != SP_NONE)
1209 dvmJdwpSetWaitForEventThread(state, basket.threadId);
1211 eventFinish(state, pReq);
1213 suspendByPolicy(state, suspendPolicy);
1214 dvmDbgThreadContinuing(oldStatus);
1218 return matchCount != 0;
1225 * Count, ClassMatch, ClassExclude
1227 bool dvmJdwpPostClassUnload(JdwpState* state, RefTypeId refTypeId)
1229 assert(false); // TODO
1234 * Get or set a field.
1237 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, FieldOnly,
1240 bool dvmJdwpPostFieldAccess(JdwpState* state, int STUFF, ObjectId thisPtr,
1243 assert(false); // TODO
1248 * Send up a chunk of DDM data.
1250 * While this takes the form of a JDWP "event", it doesn't interact with
1251 * other debugger traffic, and can't suspend the VM, so we skip all of
1252 * the fun event token gymnastics.
1254 void dvmJdwpDdmSendChunk(JdwpState* state, int type, int len, const u1* buf)
1260 * Write the chunk header and data into the ExpandBuf.
1262 pReq = expandBufAlloc();
1263 expandBufAddSpace(pReq, kJDWPHeaderLen);
1264 expandBufAdd4BE(pReq, type);
1265 expandBufAdd4BE(pReq, len);
1267 outBuf = expandBufAddSpace(pReq, len);
1268 memcpy(outBuf, buf, len);
1272 * Go back and write the JDWP header.
1274 outBuf = expandBufGetBuffer(pReq);
1276 set4BE(outBuf, expandBufGetLength(pReq));
1277 set4BE(outBuf+4, dvmJdwpNextRequestSerial(state));
1278 set1(outBuf+8, 0); /* flags */
1279 set1(outBuf+9, kJDWPDdmCmdSet);
1280 set1(outBuf+10, kJDWPDdmCmd);
1285 //LOGD("Sending chunk (type=0x%08x len=%d)\n", type, len);
1286 dvmJdwpSendRequest(state, pReq);
1288 expandBufFree(pReq);