blob: 424d87fcc84c68fc19253098fd986cc75b9de4f9 [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 * Send events to the debugger.
18 */
19#include "debugger.h"
20#include "jdwp/jdwp_priv.h"
21#include "jdwp/jdwp_constants.h"
22#include "jdwp/jdwp_handler.h"
23#include "jdwp/jdwp_event.h"
24#include "jdwp/jdwp_expand_buf.h"
25#include "logging.h"
26#include "stringprintf.h"
27
28#include <stdlib.h>
29#include <string.h>
30#include <stddef.h> /* for offsetof() */
31#include <unistd.h>
32
33/*
34General notes:
35
36The event add/remove stuff usually happens from the debugger thread,
37in response to requests from the debugger, but can also happen as the
38result of an event in an arbitrary thread (e.g. an event with a "count"
39mod expires). It's important to keep the event list locked when processing
40events.
41
42Event posting can happen from any thread. The JDWP thread will not usually
43post anything but VM start/death, but if a JDWP request causes a class
44to be loaded, the ClassPrepare event will come from the JDWP thread.
45
46
47We can have serialization issues when we post an event to the debugger.
48For example, a thread could send an "I hit a breakpoint and am suspending
49myself" message to the debugger. Before it manages to suspend itself, the
50debugger's response ("not interested, resume thread") arrives and is
51processed. We try to resume a thread that hasn't yet suspended.
52
53This means that, after posting an event to the debugger, we need to wait
54for the event thread to suspend itself (and, potentially, all other threads)
55before processing any additional requests from the debugger. While doing
56so we need to be aware that multiple threads may be hitting breakpoints
57or other events simultaneously, so we either need to wait for all of them
58or serialize the events with each other.
59
60The current mechanism works like this:
61 Event thread:
62 - If I'm going to suspend, grab the "I am posting an event" token. Wait
63 for it if it's not currently available.
64 - Post the event to the debugger.
65 - If appropriate, suspend others and then myself. As part of suspending
66 myself, release the "I am posting" token.
67 JDWP thread:
68 - When an event arrives, see if somebody is posting an event. If so,
69 sleep until we can acquire the "I am posting an event" token. Release
70 it immediately and continue processing -- the event we have already
71 received should not interfere with other events that haven't yet
72 been posted.
73
74Some care must be taken to avoid deadlock:
75
76 - thread A and thread B exit near-simultaneously, and post thread-death
77 events with a "suspend all" clause
78 - thread A gets the event token, thread B sits and waits for it
79 - thread A wants to suspend all other threads, but thread B is waiting
80 for the token and can't be suspended
81
82So we need to mark thread B in such a way that thread A doesn't wait for it.
83
84If we just bracket the "grab event token" call with a change to VMWAIT
85before sleeping, the switch back to RUNNING state when we get the token
86will cause thread B to suspend (remember, thread A's global suspend is
87still in force, even after it releases the token). Suspending while
88holding the event token is very bad, because it prevents the JDWP thread
89from processing incoming messages.
90
91We need to change to VMWAIT state at the *start* of posting an event,
92and stay there until we either finish posting the event or decide to
93put ourselves to sleep. That way we don't interfere with anyone else and
94don't allow anyone else to interfere with us.
95*/
96
97
98#define kJdwpEventCommandSet 64
99#define kJdwpCompositeCommand 100
100
101namespace art {
102
103namespace JDWP {
104
105/*
106 * Stuff to compare against when deciding if a mod matches. Only the
107 * values for mods valid for the event being evaluated will be filled in.
108 * The rest will be zeroed.
109 */
110struct ModBasket {
111 const JdwpLocation* pLoc; /* LocationOnly */
Elliott Hughesa2155262011-11-16 16:26:58 -0800112 std::string className; /* ClassMatch/ClassExclude */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113 ObjectId threadId; /* ThreadOnly */
114 RefTypeId classId; /* ClassOnly */
115 RefTypeId excepClassId; /* ExceptionOnly */
116 bool caught; /* ExceptionOnly */
117 FieldId field; /* FieldOnly */
118 ObjectId thisPtr; /* InstanceOnly */
119 /* nothing for StepOnly -- handled differently */
120};
121
122/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 * Dump an event to the log file.
124 */
125static void dumpEvent(const JdwpEvent* pEvent) {
126 LOG(INFO) << StringPrintf("Event id=0x%4x %p (prev=%p next=%p):", pEvent->requestId, pEvent, pEvent->prev, pEvent->next);
127 LOG(INFO) << " kind=" << pEvent->eventKind << " susp=" << pEvent->suspendPolicy << " modCount=" << pEvent->modCount;
128
129 for (int i = 0; i < pEvent->modCount; i++) {
130 const JdwpEventMod* pMod = &pEvent->mods[i];
131 LOG(INFO) << " " << static_cast<JdwpModKind>(pMod->modKind);
132 /* TODO - show details */
133 }
134}
135
136/*
137 * Add an event to the list. Ordering is not important.
138 *
139 * If something prevents the event from being registered, e.g. it's a
140 * single-step request on a thread that doesn't exist, the event will
141 * not be added to the list, and an appropriate error will be returned.
142 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800143JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
144 MutexLock mu(event_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146 CHECK(pEvent != NULL);
147 CHECK(pEvent->prev == NULL);
148 CHECK(pEvent->next == NULL);
149
150 /*
151 * If one or more "break"-type mods are used, register them with
152 * the interpreter.
153 */
154 for (int i = 0; i < pEvent->modCount; i++) {
155 const JdwpEventMod* pMod = &pEvent->mods[i];
156 if (pMod->modKind == MK_LOCATION_ONLY) {
157 /* should only be for Breakpoint, Step, and Exception */
158 Dbg::WatchLocation(&pMod->locationOnly.loc);
159 } else if (pMod->modKind == MK_STEP) {
160 /* should only be for EK_SINGLE_STEP; should only be one */
161 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
162 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
163 Dbg::ConfigureStep(pMod->step.threadId, size, depth);
164 } else if (pMod->modKind == MK_FIELD_ONLY) {
165 /* should be for EK_FIELD_ACCESS or EK_FIELD_MODIFICATION */
166 dumpEvent(pEvent); /* TODO - need for field watches */
167 }
168 }
169
170 /*
171 * Add to list.
172 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800173 if (eventList != NULL) {
174 pEvent->next = eventList;
175 eventList->prev = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800177 eventList = pEvent;
178 numEvents++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179
180 return ERR_NONE;
181}
182
183/*
184 * Remove an event from the list. This will also remove the event from
185 * any optimization tables, e.g. breakpoints.
186 *
187 * Does not free the JdwpEvent.
188 *
189 * Grab the eventLock before calling here.
190 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800191void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700192 if (pEvent->prev == NULL) {
193 /* head of the list */
Elliott Hughes761928d2011-11-16 18:33:03 -0800194 CHECK(eventList == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700195
Elliott Hughes761928d2011-11-16 18:33:03 -0800196 eventList = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197 } else {
198 pEvent->prev->next = pEvent->next;
199 }
200
201 if (pEvent->next != NULL) {
202 pEvent->next->prev = pEvent->prev;
203 pEvent->next = NULL;
204 }
205 pEvent->prev = NULL;
206
207 /*
208 * Unhook us from the interpreter, if necessary.
209 */
210 for (int i = 0; i < pEvent->modCount; i++) {
211 JdwpEventMod* pMod = &pEvent->mods[i];
212 if (pMod->modKind == MK_LOCATION_ONLY) {
213 /* should only be for Breakpoint, Step, and Exception */
214 Dbg::UnwatchLocation(&pMod->locationOnly.loc);
215 }
216 if (pMod->modKind == MK_STEP) {
217 /* should only be for EK_SINGLE_STEP; should only be one */
218 Dbg::UnconfigureStep(pMod->step.threadId);
219 }
220 }
221
Elliott Hughes761928d2011-11-16 18:33:03 -0800222 numEvents--;
223 CHECK(numEvents != 0 || eventList == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224}
225
226/*
227 * Remove the event with the given ID from the list.
228 *
229 * Failure to find the event isn't really an error, but it is a little
230 * weird. (It looks like Eclipse will try to be extra careful and will
231 * explicitly remove one-off single-step events.)
232 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800233void JdwpState::UnregisterEventById(uint32_t requestId) {
234 MutexLock mu(event_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235
Elliott Hughes761928d2011-11-16 18:33:03 -0800236 JdwpEvent* pEvent = eventList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700237 while (pEvent != NULL) {
238 if (pEvent->requestId == requestId) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800239 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700240 EventFree(pEvent);
Elliott Hughes761928d2011-11-16 18:33:03 -0800241 return; /* there can be only one with a given ID */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700242 }
243
244 pEvent = pEvent->next;
245 }
246
247 //LOGD("Odd: no match when removing event reqId=0x%04x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248}
249
250/*
251 * Remove all entries from the event list.
252 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800253void JdwpState::UnregisterAll() {
254 MutexLock mu(event_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700255
Elliott Hughes761928d2011-11-16 18:33:03 -0800256 JdwpEvent* pEvent = eventList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700257 while (pEvent != NULL) {
258 JdwpEvent* pNextEvent = pEvent->next;
259
Elliott Hughes761928d2011-11-16 18:33:03 -0800260 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261 EventFree(pEvent);
262 pEvent = pNextEvent;
263 }
264
Elliott Hughes761928d2011-11-16 18:33:03 -0800265 eventList = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700266}
267
268/*
269 * Allocate a JdwpEvent struct with enough space to hold the specified
270 * number of mod records.
271 */
272JdwpEvent* EventAlloc(int numMods) {
273 JdwpEvent* newEvent;
274 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
275 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
276 memset(newEvent, 0, allocSize);
277 return newEvent;
278}
279
280/*
281 * Free a JdwpEvent.
282 *
283 * Do not call this until the event has been removed from the list.
284 */
285void EventFree(JdwpEvent* pEvent) {
286 if (pEvent == NULL) {
287 return;
288 }
289
290 /* make sure it was removed from the list */
291 CHECK(pEvent->prev == NULL);
292 CHECK(pEvent->next == NULL);
293 /* want to check state->eventList != pEvent */
294
295 /*
296 * Free any hairy bits in the mods.
297 */
298 for (int i = 0; i < pEvent->modCount; i++) {
299 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
300 free(pEvent->mods[i].classMatch.classPattern);
301 pEvent->mods[i].classMatch.classPattern = NULL;
302 }
303 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
304 free(pEvent->mods[i].classExclude.classPattern);
305 pEvent->mods[i].classExclude.classPattern = NULL;
306 }
307 }
308
309 free(pEvent);
310}
311
312/*
313 * Allocate storage for matching events. To keep things simple we
314 * use an array with enough storage for the entire list.
315 *
316 * The state->eventLock should be held before calling.
317 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800318static JdwpEvent** AllocMatchList(size_t event_count) {
319 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320}
321
322/*
323 * Run through the list and remove any entries with an expired "count" mod
324 * from the event list, then free the match list.
325 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800326void JdwpState::CleanupMatchList(JdwpEvent** matchList, int matchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700327 JdwpEvent** ppEvent = matchList;
328
329 while (matchCount--) {
330 JdwpEvent* pEvent = *ppEvent;
331
332 for (int i = 0; i < pEvent->modCount; i++) {
333 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
334 LOG(VERBOSE) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800335 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 EventFree(pEvent);
337 break;
338 }
339 }
340
341 ppEvent++;
342 }
343
Elliott Hughes761928d2011-11-16 18:33:03 -0800344 delete[] matchList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700345}
346
347/*
348 * Match a string against a "restricted regular expression", which is just
349 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
350 *
351 * ("Restricted name globbing" might have been a better term.)
352 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800353static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800354 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355
356 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700357 patLen--;
358 // TODO: remove printf when we find a test case to verify this
Elliott Hughesa2155262011-11-16 16:26:58 -0800359 LOG(ERROR) << ">>> comparing '" << (pattern + 1) << "' to '" << (target.c_str() + (target.size()-patLen)) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700360
Elliott Hughesa2155262011-11-16 16:26:58 -0800361 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700362 return false;
363 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800364 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700365 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800366 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800368 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700369 }
370}
371
372/*
373 * See if two locations are equal.
374 *
375 * It's tempting to do a bitwise compare ("struct ==" or memcmp), but if
376 * the storage wasn't zeroed out there could be undefined values in the
377 * padding. Besides, the odds of "idx" being equal while the others aren't
378 * is very small, so this is usually just a simple integer comparison.
379 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800380static inline bool LocationMatch(const JdwpLocation* pLoc1, const JdwpLocation* pLoc2) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 return pLoc1->idx == pLoc2->idx &&
382 pLoc1->methodId == pLoc2->methodId &&
383 pLoc1->classId == pLoc2->classId &&
384 pLoc1->typeTag == pLoc2->typeTag;
385}
386
387/*
388 * See if the event's mods match up with the contents of "basket".
389 *
390 * If we find a Count mod before rejecting an event, we decrement it. We
391 * need to do this even if later mods cause us to ignore the event.
392 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800393static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700394 JdwpEventMod* pMod = pEvent->mods;
395
396 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
397 switch (pMod->modKind) {
398 case MK_COUNT:
399 CHECK_GT(pMod->count.count, 0);
400 pMod->count.count--;
401 break;
402 case MK_CONDITIONAL:
403 CHECK(false); // should not be getting these
404 break;
405 case MK_THREAD_ONLY:
406 if (pMod->threadOnly.threadId != basket->threadId) {
407 return false;
408 }
409 break;
410 case MK_CLASS_ONLY:
411 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
412 return false;
413 }
414 break;
415 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800416 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417 return false;
418 }
419 break;
420 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800421 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700422 return false;
423 }
424 break;
425 case MK_LOCATION_ONLY:
Elliott Hughes761928d2011-11-16 18:33:03 -0800426 if (!LocationMatch(&pMod->locationOnly.loc, basket->pLoc)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427 return false;
428 }
429 break;
430 case MK_EXCEPTION_ONLY:
431 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
432 return false;
433 }
434 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
435 return false;
436 }
437 break;
438 case MK_FIELD_ONLY:
439 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
440 return false;
441 }
442 break;
443 case MK_STEP:
444 if (pMod->step.threadId != basket->threadId) {
445 return false;
446 }
447 break;
448 case MK_INSTANCE_ONLY:
449 if (pMod->instanceOnly.objectId != basket->thisPtr) {
450 return false;
451 }
452 break;
453 default:
454 LOG(ERROR) << "unhandled mod kind " << pMod->modKind;
455 CHECK(false);
456 break;
457 }
458 }
459 return true;
460}
461
462/*
463 * Find all events of type "eventKind" with mods that match up with the
464 * rest of the arguments.
465 *
466 * Found events are appended to "matchList", and "*pMatchCount" is advanced,
467 * so this may be called multiple times for grouped events.
468 *
469 * DO NOT call this multiple times for the same eventKind, as Count mods are
470 * decremented during the scan.
471 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800472void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** matchList, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700473 /* start after the existing entries */
474 matchList += *pMatchCount;
475
Elliott Hughes761928d2011-11-16 18:33:03 -0800476 JdwpEvent* pEvent = eventList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700477 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800478 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 *matchList++ = pEvent;
480 (*pMatchCount)++;
481 }
482
483 pEvent = pEvent->next;
484 }
485}
486
487/*
488 * Scan through the list of matches and determine the most severe
489 * suspension policy.
490 */
491static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** matchList, int matchCount) {
492 JdwpSuspendPolicy policy = SP_NONE;
493
494 while (matchCount--) {
495 if ((*matchList)->suspendPolicy > policy) {
496 policy = (*matchList)->suspendPolicy;
497 }
498 matchList++;
499 }
500
501 return policy;
502}
503
504/*
505 * Three possibilities:
506 * SP_NONE - do nothing
507 * SP_EVENT_THREAD - suspend ourselves
508 * SP_ALL - suspend everybody except JDWP support thread
509 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800510void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspendPolicy) {
511 LOG(INFO) << "SuspendByPolicy(" << suspendPolicy << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512 if (suspendPolicy == SP_NONE) {
513 return;
514 }
515
516 if (suspendPolicy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700517 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700518 } else {
519 CHECK_EQ(suspendPolicy, SP_EVENT_THREAD);
520 }
521
522 /* this is rare but possible -- see CLASS_PREPARE handling */
Elliott Hughes761928d2011-11-16 18:33:03 -0800523 if (Dbg::GetThreadSelfId() == debugThreadId) {
524 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525 return;
526 }
527
528 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
529 while (true) {
530 pReq->ready = true;
531 Dbg::SuspendSelf();
532 pReq->ready = false;
533
534 /*
535 * The JDWP thread has told us (and possibly all other threads) to
536 * resume. See if it has left anything in our DebugInvokeReq mailbox.
537 */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800538 if (!pReq->invoke_needed_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800539 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540 break;
541 }
542
543 /* grab this before posting/suspending again */
Elliott Hughes761928d2011-11-16 18:33:03 -0800544 SetWaitForEventThread(Dbg::GetThreadSelfId());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545
Elliott Hughesd07986f2011-12-06 18:27:45 -0800546 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700547 LOG(VERBOSE) << "invoking method...";
548 Dbg::ExecuteMethod(pReq);
549
Elliott Hughes475fc232011-10-25 15:00:35 -0700550 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700551
552 /* clear this before signaling */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800553 pReq->invoke_needed_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700554
555 LOG(VERBOSE) << "invoke complete, signaling and self-suspending";
556 MutexLock mu(pReq->lock_);
557 pReq->cond_.Signal();
558 }
559}
560
561/*
562 * Determine if there is a method invocation in progress in the current
563 * thread.
564 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700565 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700566 * state. If set, we're in the process of invoking a method.
567 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800568bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700569 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Elliott Hughesd07986f2011-12-06 18:27:45 -0800570 return pReq->invoke_needed_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571}
572
573/*
574 * We need the JDWP thread to hold off on doing stuff while we post an
575 * event and then suspend ourselves.
576 *
577 * Call this with a threadId of zero if you just want to wait for the
578 * current thread operation to complete.
579 *
580 * This could go to sleep waiting for another thread, so it's important
581 * that the thread be marked as VMWAIT before calling here.
582 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700583void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700584 bool waited = false;
585
586 /* this is held for very brief periods; contention is unlikely */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700587 MutexLock mu(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588
589 /*
590 * If another thread is already doing stuff, wait for it. This can
591 * go to sleep indefinitely.
592 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700593 while (eventThreadId != 0) {
594 LOG(VERBOSE) << StringPrintf("event in progress (0x%llx), 0x%llx sleeping", eventThreadId, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700595 waited = true;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700596 event_thread_cond_.Wait(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700597 }
598
599 if (waited || threadId != 0) {
600 LOG(VERBOSE) << StringPrintf("event token grabbed (0x%llx)", threadId);
601 }
602 if (threadId != 0) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700603 eventThreadId = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604 }
605}
606
607/*
608 * Clear the threadId and signal anybody waiting.
609 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700610void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611 /*
612 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
613 * function is called by dvmSuspendSelf(), and the transition back
614 * to RUNNING would confuse it.
615 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700616 MutexLock mu(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
Elliott Hughes376a7a02011-10-24 18:35:55 -0700618 CHECK_NE(eventThreadId, 0U);
619 LOG(VERBOSE) << StringPrintf("cleared event token (0x%llx)", eventThreadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700620
Elliott Hughes376a7a02011-10-24 18:35:55 -0700621 eventThreadId = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622
Elliott Hughes376a7a02011-10-24 18:35:55 -0700623 event_thread_cond_.Signal();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700624}
625
626
627/*
628 * Prep an event. Allocates storage for the message and leaves space for
629 * the header.
630 */
631static ExpandBuf* eventPrep() {
632 ExpandBuf* pReq = expandBufAlloc();
633 expandBufAddSpace(pReq, kJDWPHeaderLen);
634 return pReq;
635}
636
637/*
638 * Write the header into the buffer and send the packet off to the debugger.
639 *
640 * Takes ownership of "pReq" (currently discards it).
641 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800642void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700643 uint8_t* buf = expandBufGetBuffer(pReq);
644
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700645 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800646 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700647 Set1(buf+8, 0); /* flags */
648 Set1(buf+9, kJdwpEventCommandSet);
649 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700650
Elliott Hughes761928d2011-11-16 18:33:03 -0800651 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652
653 expandBufFree(pReq);
654}
655
656
657/*
658 * Tell the debugger that we have finished initializing. This is always
659 * sent, even if the debugger hasn't requested it.
660 *
661 * This should be sent "before the main thread is started and before
662 * any application code has been executed". The thread ID in the message
663 * must be for the main thread.
664 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700665bool JdwpState::PostVMStart() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700666 JdwpSuspendPolicy suspendPolicy;
667 ObjectId threadId = Dbg::GetThreadSelfId();
668
Elliott Hughes376a7a02011-10-24 18:35:55 -0700669 if (options_->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700670 suspendPolicy = SP_ALL;
671 } else {
672 suspendPolicy = SP_NONE;
673 }
674
Elliott Hughes761928d2011-11-16 18:33:03 -0800675 ExpandBuf* pReq = eventPrep();
676 {
677 MutexLock mu(event_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700678
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679 LOG(VERBOSE) << "EVENT: " << EK_VM_START;
680 LOG(VERBOSE) << " suspendPolicy=" << suspendPolicy;
681
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700682 expandBufAdd1(pReq, suspendPolicy);
683 expandBufAdd4BE(pReq, 1);
684
685 expandBufAdd1(pReq, EK_VM_START);
686 expandBufAdd4BE(pReq, 0); /* requestId */
687 expandBufAdd8BE(pReq, threadId);
688 }
689
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700690 /* send request and possibly suspend ourselves */
691 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700692 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700693 if (suspendPolicy != SP_NONE) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700694 SetWaitForEventThread(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695 }
696
Elliott Hughes761928d2011-11-16 18:33:03 -0800697 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698
Elliott Hughes761928d2011-11-16 18:33:03 -0800699 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700700 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701 }
702
703 return true;
704}
705
706// TODO: This doesn't behave like the real dvmDescriptorToName.
707// I'm hoping this isn't used to communicate with the debugger, and we can just use descriptors.
Elliott Hughesa2155262011-11-16 16:26:58 -0800708std::string dvmDescriptorToName(const std::string& descriptor) {
709 return descriptor;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700710}
711
712/*
713 * A location of interest has been reached. This handles:
714 * Breakpoint
715 * SingleStep
716 * MethodEntry
717 * MethodExit
718 * These four types must be grouped together in a single response. The
719 * "eventFlags" indicates the type of event(s) that have happened.
720 *
721 * Valid mods:
722 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
723 * LocationOnly (for breakpoint/step only)
724 * Step (for step only)
725 *
726 * Interesting test cases:
727 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
728 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
729 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
730 * - Single-step to a line with a breakpoint. Should get a single
731 * event message with both events in it.
732 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800733bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700734 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700735
736 memset(&basket, 0, sizeof(basket));
737 basket.pLoc = pLoc;
738 basket.classId = pLoc->classId;
739 basket.thisPtr = thisPtr;
740 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesa2155262011-11-16 16:26:58 -0800741 basket.className = dvmDescriptorToName(Dbg::GetClassDescriptor(pLoc->classId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
743 /*
744 * On rare occasions we may need to execute interpreted code in the VM
745 * while handling a request from the debugger. Don't fire breakpoints
746 * while doing so. (I don't think we currently do this at all, so
747 * this is mostly paranoia.)
748 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800749 if (basket.threadId == debugThreadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750 LOG(VERBOSE) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751 return false;
752 }
753
754 /*
755 * The debugger variable display tab may invoke the interpreter to format
756 * complex objects. We want to ignore breakpoints and method entry/exit
757 * traps while working on behalf of the debugger.
758 *
759 * If we don't ignore them, the VM will get hung up, because we'll
760 * suspend on a breakpoint while the debugger is still waiting for its
761 * method invocation to complete.
762 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800763 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700764 LOG(VERBOSE) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700765 return false;
766 }
767
Elliott Hughes761928d2011-11-16 18:33:03 -0800768 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700769 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700770 ExpandBuf* pReq = NULL;
771 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772
Elliott Hughes761928d2011-11-16 18:33:03 -0800773 {
774 MutexLock mu(event_lock_);
775 if ((eventFlags & Dbg::kBreakPoint) != 0) {
776 FindMatchingEvents(EK_BREAKPOINT, &basket, matchList, &matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800778 if ((eventFlags & Dbg::kSingleStep) != 0) {
779 FindMatchingEvents(EK_SINGLE_STEP, &basket, matchList, &matchCount);
780 }
781 if ((eventFlags & Dbg::kMethodEntry) != 0) {
782 FindMatchingEvents(EK_METHOD_ENTRY, &basket, matchList, &matchCount);
783 }
784 if ((eventFlags & Dbg::kMethodExit) != 0) {
785 FindMatchingEvents(EK_METHOD_EXIT, &basket, matchList, &matchCount);
786 }
787 if (matchCount != 0) {
788 LOG(VERBOSE) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
789 << basket.className << "." << Dbg::GetMethodName(pLoc->classId, pLoc->methodId)
790 << " thread=" << (void*) basket.threadId << " code=" << (void*) pLoc->idx << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700791
Elliott Hughes761928d2011-11-16 18:33:03 -0800792 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
793 LOG(VERBOSE) << " suspendPolicy=" << suspendPolicy;
794
795 pReq = eventPrep();
796 expandBufAdd1(pReq, suspendPolicy);
797 expandBufAdd4BE(pReq, matchCount);
798
799 for (int i = 0; i < matchCount; i++) {
800 expandBufAdd1(pReq, matchList[i]->eventKind);
801 expandBufAdd4BE(pReq, matchList[i]->requestId);
802 expandBufAdd8BE(pReq, basket.threadId);
803 AddLocation(pReq, pLoc);
804 }
805 }
806
807 CleanupMatchList(matchList, matchCount);
808 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809
810 /* send request and possibly suspend ourselves */
811 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700812 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700813 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800814 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700815 }
816
Elliott Hughes761928d2011-11-16 18:33:03 -0800817 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818
Elliott Hughes761928d2011-11-16 18:33:03 -0800819 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700820 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700821 }
822
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700823 return matchCount != 0;
824}
825
826/*
827 * A thread is starting or stopping.
828 *
829 * Valid mods:
830 * Count, ThreadOnly
831 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700832bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
834
835 /*
836 * I don't think this can happen.
837 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800838 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700839 LOG(WARNING) << "Not posting thread change during invoke";
840 return false;
841 }
842
843 ModBasket basket;
844 memset(&basket, 0, sizeof(basket));
845 basket.threadId = threadId;
846
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700847 ExpandBuf* pReq = NULL;
848 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes234ab152011-10-26 14:02:26 -0700849 int matchCount = 0;
850 {
851 // Don't allow the list to be updated while we scan it.
852 MutexLock mu(event_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800853 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700854
Elliott Hughes234ab152011-10-26 14:02:26 -0700855 if (start) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800856 FindMatchingEvents(EK_THREAD_START, &basket, matchList, &matchCount);
Elliott Hughes234ab152011-10-26 14:02:26 -0700857 } else {
Elliott Hughes761928d2011-11-16 18:33:03 -0800858 FindMatchingEvents(EK_THREAD_DEATH, &basket, matchList, &matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700859 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700860
Elliott Hughes234ab152011-10-26 14:02:26 -0700861 if (matchCount != 0) {
862 LOG(VERBOSE) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
863 << "thread=" << (void*) basket.threadId << ")";
864
865 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
866 LOG(VERBOSE) << " suspendPolicy=" << suspendPolicy;
867
868 pReq = eventPrep();
869 expandBufAdd1(pReq, suspendPolicy);
870 expandBufAdd4BE(pReq, matchCount);
871
872 for (int i = 0; i < matchCount; i++) {
873 expandBufAdd1(pReq, matchList[i]->eventKind);
874 expandBufAdd4BE(pReq, matchList[i]->requestId);
875 expandBufAdd8BE(pReq, basket.threadId);
876 }
877 }
878
Elliott Hughes761928d2011-11-16 18:33:03 -0800879 CleanupMatchList(matchList, matchCount);
Elliott Hughes234ab152011-10-26 14:02:26 -0700880 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700881
882 /* send request and possibly suspend ourselves */
883 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700884 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700885 if (suspendPolicy != SP_NONE) {
Elliott Hughes234ab152011-10-26 14:02:26 -0700886 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700887 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800888 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700889
Elliott Hughes761928d2011-11-16 18:33:03 -0800890 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700891 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700892 }
893
894 return matchCount != 0;
895}
896
897/*
898 * Send a polite "VM is dying" message to the debugger.
899 *
900 * Skips the usual "event token" stuff.
901 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700902bool JdwpState::PostVMDeath() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903 LOG(VERBOSE) << "EVENT: " << EK_VM_DEATH;
904
905 ExpandBuf* pReq = eventPrep();
906 expandBufAdd1(pReq, SP_NONE);
907 expandBufAdd4BE(pReq, 1);
908
909 expandBufAdd1(pReq, EK_VM_DEATH);
910 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800911 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912 return true;
913}
914
915/*
916 * An exception has been thrown. It may or may not have been caught.
917 *
918 * Valid mods:
919 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
920 * ExceptionOnly, InstanceOnly
921 *
922 * The "exceptionId" has not been added to the GC-visible object registry,
923 * because there's a pretty good chance that we're not going to send it
924 * up the debugger.
925 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800926bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700927 ObjectId exceptionId, RefTypeId exceptionClassId,
928 const JdwpLocation* pCatchLoc, ObjectId thisPtr)
929{
930 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700931
932 memset(&basket, 0, sizeof(basket));
933 basket.pLoc = pThrowLoc;
934 basket.classId = pThrowLoc->classId;
935 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesa2155262011-11-16 16:26:58 -0800936 basket.className = dvmDescriptorToName(Dbg::GetClassDescriptor(basket.classId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937 basket.excepClassId = exceptionClassId;
938 basket.caught = (pCatchLoc->classId != 0);
939 basket.thisPtr = thisPtr;
940
941 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800942 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700943 LOG(VERBOSE) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700944 return false;
945 }
946
Elliott Hughes761928d2011-11-16 18:33:03 -0800947 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700948 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949 ExpandBuf* pReq = NULL;
950 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800951 {
952 MutexLock mu(event_lock_);
953 FindMatchingEvents(EK_EXCEPTION, &basket, matchList, &matchCount);
954 if (matchCount != 0) {
955 LOG(VERBOSE) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total)"
956 << " thread=" << (void*) basket.threadId
957 << " exceptId=" << (void*) exceptionId
958 << " caught=" << basket.caught << ")";
Elliott Hughes03181a82011-11-17 17:22:21 -0800959 LOG(VERBOSE) << " throw: " << *pThrowLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800960 if (pCatchLoc->classId == 0) {
961 LOG(VERBOSE) << " catch: (not caught)";
962 } else {
Elliott Hughes03181a82011-11-17 17:22:21 -0800963 LOG(VERBOSE) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800964 }
965
966 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
967 LOG(VERBOSE) << " suspendPolicy=" << suspendPolicy;
968
969 pReq = eventPrep();
970 expandBufAdd1(pReq, suspendPolicy);
971 expandBufAdd4BE(pReq, matchCount);
972
973 for (int i = 0; i < matchCount; i++) {
974 expandBufAdd1(pReq, matchList[i]->eventKind);
975 expandBufAdd4BE(pReq, matchList[i]->requestId);
976 expandBufAdd8BE(pReq, basket.threadId);
977
978 AddLocation(pReq, pThrowLoc);
979 expandBufAdd1(pReq, JT_OBJECT);
980 expandBufAdd8BE(pReq, exceptionId);
981 AddLocation(pReq, pCatchLoc);
982 }
983
984 /* don't let the GC discard it */
985 Dbg::RegisterObjectId(exceptionId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986 }
987
Elliott Hughes761928d2011-11-16 18:33:03 -0800988 CleanupMatchList(matchList, matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700989 }
990
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700991 /* send request and possibly suspend ourselves */
992 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700993 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700994 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800995 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700996 }
997
Elliott Hughes761928d2011-11-16 18:33:03 -0800998 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700999
Elliott Hughes761928d2011-11-16 18:33:03 -08001000 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001001 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001002 }
1003
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004 return matchCount != 0;
1005}
1006
1007/*
1008 * Announce that a class has been loaded.
1009 *
1010 * Valid mods:
1011 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1012 */
Elliott Hughes761928d2011-11-16 18:33:03 -08001013bool JdwpState::PostClassPrepare(int tag, RefTypeId refTypeId, const char* signature, int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001014 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001015
1016 memset(&basket, 0, sizeof(basket));
1017 basket.classId = refTypeId;
1018 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesa2155262011-11-16 16:26:58 -08001019 basket.className = dvmDescriptorToName(Dbg::GetClassDescriptor(basket.classId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020
1021 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001022 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023 LOG(VERBOSE) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001024 return false;
1025 }
1026
Elliott Hughes761928d2011-11-16 18:33:03 -08001027 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001028 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001029 ExpandBuf* pReq = NULL;
1030 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -08001031 {
1032 MutexLock mu(event_lock_);
1033 FindMatchingEvents(EK_CLASS_PREPARE, &basket, matchList, &matchCount);
1034 if (matchCount != 0) {
1035 LOG(VERBOSE) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
1036 << "thread=" << (void*) basket.threadId << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001037
Elliott Hughes761928d2011-11-16 18:33:03 -08001038 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
1039 LOG(VERBOSE) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001040
Elliott Hughes761928d2011-11-16 18:33:03 -08001041 if (basket.threadId == debugThreadId) {
1042 /*
1043 * JDWP says that, for a class prep in the debugger thread, we
1044 * should set threadId to null and if any threads were supposed
1045 * to be suspended then we suspend all other threads.
1046 */
1047 LOG(VERBOSE) << " NOTE: class prepare in debugger thread!";
1048 basket.threadId = 0;
1049 if (suspendPolicy == SP_EVENT_THREAD) {
1050 suspendPolicy = SP_ALL;
1051 }
1052 }
1053
1054 pReq = eventPrep();
1055 expandBufAdd1(pReq, suspendPolicy);
1056 expandBufAdd4BE(pReq, matchCount);
1057
1058 for (int i = 0; i < matchCount; i++) {
1059 expandBufAdd1(pReq, matchList[i]->eventKind);
1060 expandBufAdd4BE(pReq, matchList[i]->requestId);
1061 expandBufAdd8BE(pReq, basket.threadId);
1062
1063 expandBufAdd1(pReq, tag);
1064 expandBufAdd8BE(pReq, refTypeId);
1065 expandBufAddUtf8String(pReq, signature);
1066 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001067 }
1068 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001069 CleanupMatchList(matchList, matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001070 }
1071
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001072 /* send request and possibly suspend ourselves */
1073 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -07001074 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001075 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001076 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001077 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001078 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001079
Elliott Hughes761928d2011-11-16 18:33:03 -08001080 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001081 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082 }
1083
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001084 return matchCount != 0;
1085}
1086
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001087/*
1088 * Send up a chunk of DDM data.
1089 *
1090 * While this takes the form of a JDWP "event", it doesn't interact with
1091 * other debugger traffic, and can't suspend the VM, so we skip all of
1092 * the fun event token gymnastics.
1093 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001094void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001095 uint8_t header[kJDWPHeaderLen + 8];
1096 size_t dataLen = 0;
1097
1098 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001099 CHECK_GT(iov_count, 0);
1100 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001101
1102 /*
1103 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1104 * this by creating a new copy of the vector with space for the header.
1105 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001106 iovec wrapiov[iov_count+1];
1107 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001108 wrapiov[i+1].iov_base = iov[i].iov_base;
1109 wrapiov[i+1].iov_len = iov[i].iov_len;
1110 dataLen += iov[i].iov_len;
1111 }
1112
1113 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001114 Set4BE(header, sizeof(header) + dataLen);
1115 Set4BE(header+4, NextRequestSerial());
1116 Set1(header+8, 0); /* flags */
1117 Set1(header+9, kJDWPDdmCmdSet);
1118 Set1(header+10, kJDWPDdmCmd);
1119 Set4BE(header+11, type);
1120 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001121
1122 wrapiov[0].iov_base = header;
1123 wrapiov[0].iov_len = sizeof(header);
1124
1125 /*
1126 * Make sure we're in VMWAIT in case the write blocks.
1127 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001128 int old_state = Dbg::ThreadWaiting();
Elliott Hughescccd84f2011-12-05 16:51:54 -08001129 (*transport->sendBufferedRequest)(this, wrapiov, iov_count + 1);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001130 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131}
1132
1133} // namespace JDWP
1134
1135} // namespace art