blob: 5101f2f0bc3d12ef30b31445a7557ab0288d17ce [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];
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800131 LOG(INFO) << " " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132 /* 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);
Elliott Hughes2435a572012-02-17 16:07:41 -0800163 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
164 if (status != ERR_NONE) {
165 return status;
166 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167 } else if (pMod->modKind == MK_FIELD_ONLY) {
168 /* should be for EK_FIELD_ACCESS or EK_FIELD_MODIFICATION */
169 dumpEvent(pEvent); /* TODO - need for field watches */
170 }
171 }
172
173 /*
174 * Add to list.
175 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800176 if (eventList != NULL) {
177 pEvent->next = eventList;
178 eventList->prev = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800180 eventList = pEvent;
181 numEvents++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182
183 return ERR_NONE;
184}
185
186/*
187 * Remove an event from the list. This will also remove the event from
188 * any optimization tables, e.g. breakpoints.
189 *
190 * Does not free the JdwpEvent.
191 *
192 * Grab the eventLock before calling here.
193 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800194void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700195 if (pEvent->prev == NULL) {
196 /* head of the list */
Elliott Hughes761928d2011-11-16 18:33:03 -0800197 CHECK(eventList == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198
Elliott Hughes761928d2011-11-16 18:33:03 -0800199 eventList = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200 } else {
201 pEvent->prev->next = pEvent->next;
202 }
203
204 if (pEvent->next != NULL) {
205 pEvent->next->prev = pEvent->prev;
206 pEvent->next = NULL;
207 }
208 pEvent->prev = NULL;
209
210 /*
211 * Unhook us from the interpreter, if necessary.
212 */
213 for (int i = 0; i < pEvent->modCount; i++) {
214 JdwpEventMod* pMod = &pEvent->mods[i];
215 if (pMod->modKind == MK_LOCATION_ONLY) {
216 /* should only be for Breakpoint, Step, and Exception */
217 Dbg::UnwatchLocation(&pMod->locationOnly.loc);
218 }
219 if (pMod->modKind == MK_STEP) {
220 /* should only be for EK_SINGLE_STEP; should only be one */
221 Dbg::UnconfigureStep(pMod->step.threadId);
222 }
223 }
224
Elliott Hughes761928d2011-11-16 18:33:03 -0800225 numEvents--;
226 CHECK(numEvents != 0 || eventList == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227}
228
229/*
230 * Remove the event with the given ID from the list.
231 *
232 * Failure to find the event isn't really an error, but it is a little
233 * weird. (It looks like Eclipse will try to be extra careful and will
234 * explicitly remove one-off single-step events.)
235 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800236void JdwpState::UnregisterEventById(uint32_t requestId) {
237 MutexLock mu(event_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238
Elliott Hughes761928d2011-11-16 18:33:03 -0800239 JdwpEvent* pEvent = eventList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700240 while (pEvent != NULL) {
241 if (pEvent->requestId == requestId) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800242 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243 EventFree(pEvent);
Elliott Hughes761928d2011-11-16 18:33:03 -0800244 return; /* there can be only one with a given ID */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245 }
246
247 pEvent = pEvent->next;
248 }
249
250 //LOGD("Odd: no match when removing event reqId=0x%04x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251}
252
253/*
254 * Remove all entries from the event list.
255 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800256void JdwpState::UnregisterAll() {
257 MutexLock mu(event_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258
Elliott Hughes761928d2011-11-16 18:33:03 -0800259 JdwpEvent* pEvent = eventList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260 while (pEvent != NULL) {
261 JdwpEvent* pNextEvent = pEvent->next;
262
Elliott Hughes761928d2011-11-16 18:33:03 -0800263 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700264 EventFree(pEvent);
265 pEvent = pNextEvent;
266 }
267
Elliott Hughes761928d2011-11-16 18:33:03 -0800268 eventList = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269}
270
271/*
272 * Allocate a JdwpEvent struct with enough space to hold the specified
273 * number of mod records.
274 */
275JdwpEvent* EventAlloc(int numMods) {
276 JdwpEvent* newEvent;
277 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
278 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
279 memset(newEvent, 0, allocSize);
280 return newEvent;
281}
282
283/*
284 * Free a JdwpEvent.
285 *
286 * Do not call this until the event has been removed from the list.
287 */
288void EventFree(JdwpEvent* pEvent) {
289 if (pEvent == NULL) {
290 return;
291 }
292
293 /* make sure it was removed from the list */
294 CHECK(pEvent->prev == NULL);
295 CHECK(pEvent->next == NULL);
296 /* want to check state->eventList != pEvent */
297
298 /*
299 * Free any hairy bits in the mods.
300 */
301 for (int i = 0; i < pEvent->modCount; i++) {
302 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
303 free(pEvent->mods[i].classMatch.classPattern);
304 pEvent->mods[i].classMatch.classPattern = NULL;
305 }
306 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
307 free(pEvent->mods[i].classExclude.classPattern);
308 pEvent->mods[i].classExclude.classPattern = NULL;
309 }
310 }
311
312 free(pEvent);
313}
314
315/*
316 * Allocate storage for matching events. To keep things simple we
317 * use an array with enough storage for the entire list.
318 *
319 * The state->eventLock should be held before calling.
320 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800321static JdwpEvent** AllocMatchList(size_t event_count) {
322 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323}
324
325/*
326 * Run through the list and remove any entries with an expired "count" mod
327 * from the event list, then free the match list.
328 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800329void JdwpState::CleanupMatchList(JdwpEvent** matchList, int matchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330 JdwpEvent** ppEvent = matchList;
331
332 while (matchCount--) {
333 JdwpEvent* pEvent = *ppEvent;
334
335 for (int i = 0; i < pEvent->modCount; i++) {
336 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800337 VLOG(jdwp) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800338 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700339 EventFree(pEvent);
340 break;
341 }
342 }
343
344 ppEvent++;
345 }
346
Elliott Hughes761928d2011-11-16 18:33:03 -0800347 delete[] matchList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700348}
349
350/*
351 * Match a string against a "restricted regular expression", which is just
352 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
353 *
354 * ("Restricted name globbing" might have been a better term.)
355 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800356static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800357 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700358 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800360 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361 return false;
362 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800363 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700364 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800365 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700366 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800367 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368 }
369}
370
371/*
372 * See if two locations are equal.
373 *
374 * It's tempting to do a bitwise compare ("struct ==" or memcmp), but if
375 * the storage wasn't zeroed out there could be undefined values in the
376 * padding. Besides, the odds of "idx" being equal while the others aren't
377 * is very small, so this is usually just a simple integer comparison.
378 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800379static inline bool LocationMatch(const JdwpLocation* pLoc1, const JdwpLocation* pLoc2) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700380 return pLoc1->idx == pLoc2->idx &&
381 pLoc1->methodId == pLoc2->methodId &&
382 pLoc1->classId == pLoc2->classId &&
383 pLoc1->typeTag == pLoc2->typeTag;
384}
385
386/*
387 * See if the event's mods match up with the contents of "basket".
388 *
389 * If we find a Count mod before rejecting an event, we decrement it. We
390 * need to do this even if later mods cause us to ignore the event.
391 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800392static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700393 JdwpEventMod* pMod = pEvent->mods;
394
395 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
396 switch (pMod->modKind) {
397 case MK_COUNT:
398 CHECK_GT(pMod->count.count, 0);
399 pMod->count.count--;
400 break;
401 case MK_CONDITIONAL:
402 CHECK(false); // should not be getting these
403 break;
404 case MK_THREAD_ONLY:
405 if (pMod->threadOnly.threadId != basket->threadId) {
406 return false;
407 }
408 break;
409 case MK_CLASS_ONLY:
410 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
411 return false;
412 }
413 break;
414 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800415 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416 return false;
417 }
418 break;
419 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800420 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700421 return false;
422 }
423 break;
424 case MK_LOCATION_ONLY:
Elliott Hughes761928d2011-11-16 18:33:03 -0800425 if (!LocationMatch(&pMod->locationOnly.loc, basket->pLoc)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426 return false;
427 }
428 break;
429 case MK_EXCEPTION_ONLY:
430 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
431 return false;
432 }
433 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
434 return false;
435 }
436 break;
437 case MK_FIELD_ONLY:
438 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
439 return false;
440 }
441 break;
442 case MK_STEP:
443 if (pMod->step.threadId != basket->threadId) {
444 return false;
445 }
446 break;
447 case MK_INSTANCE_ONLY:
448 if (pMod->instanceOnly.objectId != basket->thisPtr) {
449 return false;
450 }
451 break;
452 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800453 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 break;
455 }
456 }
457 return true;
458}
459
460/*
461 * Find all events of type "eventKind" with mods that match up with the
462 * rest of the arguments.
463 *
464 * Found events are appended to "matchList", and "*pMatchCount" is advanced,
465 * so this may be called multiple times for grouped events.
466 *
467 * DO NOT call this multiple times for the same eventKind, as Count mods are
468 * decremented during the scan.
469 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800470void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** matchList, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700471 /* start after the existing entries */
472 matchList += *pMatchCount;
473
Elliott Hughes761928d2011-11-16 18:33:03 -0800474 JdwpEvent* pEvent = eventList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800476 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700477 *matchList++ = pEvent;
478 (*pMatchCount)++;
479 }
480
481 pEvent = pEvent->next;
482 }
483}
484
485/*
486 * Scan through the list of matches and determine the most severe
487 * suspension policy.
488 */
489static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** matchList, int matchCount) {
490 JdwpSuspendPolicy policy = SP_NONE;
491
492 while (matchCount--) {
493 if ((*matchList)->suspendPolicy > policy) {
494 policy = (*matchList)->suspendPolicy;
495 }
496 matchList++;
497 }
498
499 return policy;
500}
501
502/*
503 * Three possibilities:
504 * SP_NONE - do nothing
505 * SP_EVENT_THREAD - suspend ourselves
506 * SP_ALL - suspend everybody except JDWP support thread
507 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800508void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspendPolicy) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800509 VLOG(jdwp) << "SuspendByPolicy(" << suspendPolicy << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510 if (suspendPolicy == SP_NONE) {
511 return;
512 }
513
514 if (suspendPolicy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700515 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516 } else {
517 CHECK_EQ(suspendPolicy, SP_EVENT_THREAD);
518 }
519
520 /* this is rare but possible -- see CLASS_PREPARE handling */
Elliott Hughes761928d2011-11-16 18:33:03 -0800521 if (Dbg::GetThreadSelfId() == debugThreadId) {
522 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523 return;
524 }
525
526 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
527 while (true) {
528 pReq->ready = true;
529 Dbg::SuspendSelf();
530 pReq->ready = false;
531
532 /*
533 * The JDWP thread has told us (and possibly all other threads) to
534 * resume. See if it has left anything in our DebugInvokeReq mailbox.
535 */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800536 if (!pReq->invoke_needed_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800537 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538 break;
539 }
540
541 /* grab this before posting/suspending again */
Elliott Hughes761928d2011-11-16 18:33:03 -0800542 SetWaitForEventThread(Dbg::GetThreadSelfId());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700543
Elliott Hughesd07986f2011-12-06 18:27:45 -0800544 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545 Dbg::ExecuteMethod(pReq);
546
Elliott Hughes475fc232011-10-25 15:00:35 -0700547 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700548
549 /* clear this before signaling */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800550 pReq->invoke_needed_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700551
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800552 VLOG(jdwp) << "invoke complete, signaling and self-suspending";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553 MutexLock mu(pReq->lock_);
554 pReq->cond_.Signal();
555 }
556}
557
558/*
559 * Determine if there is a method invocation in progress in the current
560 * thread.
561 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700562 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563 * state. If set, we're in the process of invoking a method.
564 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800565bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700566 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Elliott Hughesd07986f2011-12-06 18:27:45 -0800567 return pReq->invoke_needed_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700568}
569
570/*
571 * We need the JDWP thread to hold off on doing stuff while we post an
572 * event and then suspend ourselves.
573 *
574 * Call this with a threadId of zero if you just want to wait for the
575 * current thread operation to complete.
576 *
577 * This could go to sleep waiting for another thread, so it's important
578 * that the thread be marked as VMWAIT before calling here.
579 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700580void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700581 bool waited = false;
582
583 /* this is held for very brief periods; contention is unlikely */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700584 MutexLock mu(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700585
586 /*
587 * If another thread is already doing stuff, wait for it. This can
588 * go to sleep indefinitely.
589 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700590 while (eventThreadId != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800591 VLOG(jdwp) << StringPrintf("event in progress (0x%llx), 0x%llx sleeping", eventThreadId, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700592 waited = true;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700593 event_thread_cond_.Wait(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700594 }
595
596 if (waited || threadId != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800597 VLOG(jdwp) << StringPrintf("event token grabbed (0x%llx)", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700598 }
599 if (threadId != 0) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700600 eventThreadId = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700601 }
602}
603
604/*
605 * Clear the threadId and signal anybody waiting.
606 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700607void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700608 /*
609 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
610 * function is called by dvmSuspendSelf(), and the transition back
611 * to RUNNING would confuse it.
612 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700613 MutexLock mu(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700614
Elliott Hughes376a7a02011-10-24 18:35:55 -0700615 CHECK_NE(eventThreadId, 0U);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800616 VLOG(jdwp) << StringPrintf("cleared event token (0x%llx)", eventThreadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
Elliott Hughes376a7a02011-10-24 18:35:55 -0700618 eventThreadId = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619
Elliott Hughes376a7a02011-10-24 18:35:55 -0700620 event_thread_cond_.Signal();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621}
622
623
624/*
625 * Prep an event. Allocates storage for the message and leaves space for
626 * the header.
627 */
628static ExpandBuf* eventPrep() {
629 ExpandBuf* pReq = expandBufAlloc();
630 expandBufAddSpace(pReq, kJDWPHeaderLen);
631 return pReq;
632}
633
634/*
635 * Write the header into the buffer and send the packet off to the debugger.
636 *
637 * Takes ownership of "pReq" (currently discards it).
638 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800639void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640 uint8_t* buf = expandBufGetBuffer(pReq);
641
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700642 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800643 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700644 Set1(buf+8, 0); /* flags */
645 Set1(buf+9, kJdwpEventCommandSet);
646 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700647
Elliott Hughes761928d2011-11-16 18:33:03 -0800648 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700649
650 expandBufFree(pReq);
651}
652
653
654/*
655 * Tell the debugger that we have finished initializing. This is always
656 * sent, even if the debugger hasn't requested it.
657 *
658 * This should be sent "before the main thread is started and before
659 * any application code has been executed". The thread ID in the message
660 * must be for the main thread.
661 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700662bool JdwpState::PostVMStart() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700663 JdwpSuspendPolicy suspendPolicy;
664 ObjectId threadId = Dbg::GetThreadSelfId();
665
Elliott Hughes376a7a02011-10-24 18:35:55 -0700666 if (options_->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700667 suspendPolicy = SP_ALL;
668 } else {
669 suspendPolicy = SP_NONE;
670 }
671
Elliott Hughes761928d2011-11-16 18:33:03 -0800672 ExpandBuf* pReq = eventPrep();
673 {
674 MutexLock mu(event_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700675
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800676 VLOG(jdwp) << "EVENT: " << EK_VM_START;
677 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700678
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679 expandBufAdd1(pReq, suspendPolicy);
680 expandBufAdd4BE(pReq, 1);
681
682 expandBufAdd1(pReq, EK_VM_START);
683 expandBufAdd4BE(pReq, 0); /* requestId */
684 expandBufAdd8BE(pReq, threadId);
685 }
686
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700687 /* send request and possibly suspend ourselves */
688 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700689 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700690 if (suspendPolicy != SP_NONE) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700691 SetWaitForEventThread(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692 }
693
Elliott Hughes761928d2011-11-16 18:33:03 -0800694 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695
Elliott Hughes761928d2011-11-16 18:33:03 -0800696 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700697 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698 }
699
700 return true;
701}
702
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700703/*
704 * A location of interest has been reached. This handles:
705 * Breakpoint
706 * SingleStep
707 * MethodEntry
708 * MethodExit
709 * These four types must be grouped together in a single response. The
710 * "eventFlags" indicates the type of event(s) that have happened.
711 *
712 * Valid mods:
713 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
714 * LocationOnly (for breakpoint/step only)
715 * Step (for step only)
716 *
717 * Interesting test cases:
718 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
719 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
720 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
721 * - Single-step to a line with a breakpoint. Should get a single
722 * event message with both events in it.
723 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800724bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700725 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726
727 memset(&basket, 0, sizeof(basket));
728 basket.pLoc = pLoc;
729 basket.classId = pLoc->classId;
730 basket.thisPtr = thisPtr;
731 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800732 basket.className = Dbg::GetClassName(pLoc->classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700733
734 /*
735 * On rare occasions we may need to execute interpreted code in the VM
736 * while handling a request from the debugger. Don't fire breakpoints
737 * while doing so. (I don't think we currently do this at all, so
738 * this is mostly paranoia.)
739 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800740 if (basket.threadId == debugThreadId) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800741 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742 return false;
743 }
744
745 /*
746 * The debugger variable display tab may invoke the interpreter to format
747 * complex objects. We want to ignore breakpoints and method entry/exit
748 * traps while working on behalf of the debugger.
749 *
750 * If we don't ignore them, the VM will get hung up, because we'll
751 * suspend on a breakpoint while the debugger is still waiting for its
752 * method invocation to complete.
753 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800754 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800755 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700756 return false;
757 }
758
Elliott Hughes761928d2011-11-16 18:33:03 -0800759 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700760 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700761 ExpandBuf* pReq = NULL;
762 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700763
Elliott Hughes761928d2011-11-16 18:33:03 -0800764 {
765 MutexLock mu(event_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800766 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800767 FindMatchingEvents(EK_BREAKPOINT, &basket, matchList, &matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700768 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800769 if ((eventFlags & Dbg::kSingleStep) != 0) {
770 FindMatchingEvents(EK_SINGLE_STEP, &basket, matchList, &matchCount);
771 }
772 if ((eventFlags & Dbg::kMethodEntry) != 0) {
773 FindMatchingEvents(EK_METHOD_ENTRY, &basket, matchList, &matchCount);
774 }
775 if ((eventFlags & Dbg::kMethodExit) != 0) {
776 FindMatchingEvents(EK_METHOD_EXIT, &basket, matchList, &matchCount);
Elliott Hughes86964332012-02-15 19:37:42 -0800777
778 // TODO: match EK_METHOD_EXIT_WITH_RETURN_VALUE too; we need to include the 'value', though.
779 //FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, matchList, &matchCount);
Elliott Hughes761928d2011-11-16 18:33:03 -0800780 }
781 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800782 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
Elliott Hughes86964332012-02-15 19:37:42 -0800783 << basket.className << "." << Dbg::GetMethodName(pLoc->classId, pLoc->methodId)
784 << " thread=" << (void*) basket.threadId << " code=" << (void*) pLoc->idx << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700785
Elliott Hughes761928d2011-11-16 18:33:03 -0800786 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800787 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800788
789 pReq = eventPrep();
790 expandBufAdd1(pReq, suspendPolicy);
791 expandBufAdd4BE(pReq, matchCount);
792
793 for (int i = 0; i < matchCount; i++) {
794 expandBufAdd1(pReq, matchList[i]->eventKind);
795 expandBufAdd4BE(pReq, matchList[i]->requestId);
796 expandBufAdd8BE(pReq, basket.threadId);
797 AddLocation(pReq, pLoc);
798 }
799 }
800
801 CleanupMatchList(matchList, matchCount);
802 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700803
804 /* send request and possibly suspend ourselves */
805 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700806 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700807 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800808 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809 }
810
Elliott Hughes761928d2011-11-16 18:33:03 -0800811 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700812
Elliott Hughes761928d2011-11-16 18:33:03 -0800813 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700814 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700815 }
816
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700817 return matchCount != 0;
818}
819
820/*
821 * A thread is starting or stopping.
822 *
823 * Valid mods:
824 * Count, ThreadOnly
825 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700826bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700827 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
828
829 /*
830 * I don't think this can happen.
831 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800832 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833 LOG(WARNING) << "Not posting thread change during invoke";
834 return false;
835 }
836
837 ModBasket basket;
838 memset(&basket, 0, sizeof(basket));
839 basket.threadId = threadId;
840
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700841 ExpandBuf* pReq = NULL;
842 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes234ab152011-10-26 14:02:26 -0700843 int matchCount = 0;
844 {
845 // Don't allow the list to be updated while we scan it.
846 MutexLock mu(event_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800847 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700848
Elliott Hughes234ab152011-10-26 14:02:26 -0700849 if (start) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800850 FindMatchingEvents(EK_THREAD_START, &basket, matchList, &matchCount);
Elliott Hughes234ab152011-10-26 14:02:26 -0700851 } else {
Elliott Hughes761928d2011-11-16 18:33:03 -0800852 FindMatchingEvents(EK_THREAD_DEATH, &basket, matchList, &matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700854
Elliott Hughes234ab152011-10-26 14:02:26 -0700855 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800856 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
Elliott Hughes234ab152011-10-26 14:02:26 -0700857 << "thread=" << (void*) basket.threadId << ")";
858
859 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800860 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700861
862 pReq = eventPrep();
863 expandBufAdd1(pReq, suspendPolicy);
864 expandBufAdd4BE(pReq, matchCount);
865
866 for (int i = 0; i < matchCount; i++) {
867 expandBufAdd1(pReq, matchList[i]->eventKind);
868 expandBufAdd4BE(pReq, matchList[i]->requestId);
869 expandBufAdd8BE(pReq, basket.threadId);
870 }
871 }
872
Elliott Hughes761928d2011-11-16 18:33:03 -0800873 CleanupMatchList(matchList, matchCount);
Elliott Hughes234ab152011-10-26 14:02:26 -0700874 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700875
876 /* send request and possibly suspend ourselves */
877 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700878 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879 if (suspendPolicy != SP_NONE) {
Elliott Hughes234ab152011-10-26 14:02:26 -0700880 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700881 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800882 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700883
Elliott Hughes761928d2011-11-16 18:33:03 -0800884 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700885 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700886 }
887
888 return matchCount != 0;
889}
890
891/*
892 * Send a polite "VM is dying" message to the debugger.
893 *
894 * Skips the usual "event token" stuff.
895 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700896bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800897 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700898
899 ExpandBuf* pReq = eventPrep();
900 expandBufAdd1(pReq, SP_NONE);
901 expandBufAdd4BE(pReq, 1);
902
903 expandBufAdd1(pReq, EK_VM_DEATH);
904 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800905 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700906 return true;
907}
908
909/*
910 * An exception has been thrown. It may or may not have been caught.
911 *
912 * Valid mods:
913 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
914 * ExceptionOnly, InstanceOnly
915 *
916 * The "exceptionId" has not been added to the GC-visible object registry,
917 * because there's a pretty good chance that we're not going to send it
918 * up the debugger.
919 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800920bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921 ObjectId exceptionId, RefTypeId exceptionClassId,
922 const JdwpLocation* pCatchLoc, ObjectId thisPtr)
923{
924 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700925
926 memset(&basket, 0, sizeof(basket));
927 basket.pLoc = pThrowLoc;
928 basket.classId = pThrowLoc->classId;
929 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800930 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700931 basket.excepClassId = exceptionClassId;
932 basket.caught = (pCatchLoc->classId != 0);
933 basket.thisPtr = thisPtr;
934
935 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800936 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800937 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700938 return false;
939 }
940
Elliott Hughes761928d2011-11-16 18:33:03 -0800941 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700942 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700943 ExpandBuf* pReq = NULL;
944 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800945 {
946 MutexLock mu(event_lock_);
947 FindMatchingEvents(EK_EXCEPTION, &basket, matchList, &matchCount);
948 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800949 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total)"
Elliott Hughes761928d2011-11-16 18:33:03 -0800950 << " thread=" << (void*) basket.threadId
951 << " exceptId=" << (void*) exceptionId
952 << " caught=" << basket.caught << ")";
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800953 VLOG(jdwp) << " throw: " << *pThrowLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800954 if (pCatchLoc->classId == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800955 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -0800956 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800957 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800958 }
959
960 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800961 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800962
963 pReq = eventPrep();
964 expandBufAdd1(pReq, suspendPolicy);
965 expandBufAdd4BE(pReq, matchCount);
966
967 for (int i = 0; i < matchCount; i++) {
968 expandBufAdd1(pReq, matchList[i]->eventKind);
969 expandBufAdd4BE(pReq, matchList[i]->requestId);
970 expandBufAdd8BE(pReq, basket.threadId);
971
972 AddLocation(pReq, pThrowLoc);
973 expandBufAdd1(pReq, JT_OBJECT);
974 expandBufAdd8BE(pReq, exceptionId);
975 AddLocation(pReq, pCatchLoc);
976 }
977
978 /* don't let the GC discard it */
979 Dbg::RegisterObjectId(exceptionId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980 }
981
Elliott Hughes761928d2011-11-16 18:33:03 -0800982 CleanupMatchList(matchList, matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700983 }
984
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985 /* send request and possibly suspend ourselves */
986 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700987 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700988 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800989 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990 }
991
Elliott Hughes761928d2011-11-16 18:33:03 -0800992 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700993
Elliott Hughes761928d2011-11-16 18:33:03 -0800994 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700995 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700996 }
997
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700998 return matchCount != 0;
999}
1000
1001/*
1002 * Announce that a class has been loaded.
1003 *
1004 * Valid mods:
1005 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1006 */
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001007bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature, int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001008 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001009
1010 memset(&basket, 0, sizeof(basket));
1011 basket.classId = refTypeId;
1012 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001013 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001014
1015 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001016 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001017 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018 return false;
1019 }
1020
Elliott Hughes761928d2011-11-16 18:33:03 -08001021 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001022 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023 ExpandBuf* pReq = NULL;
1024 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -08001025 {
1026 MutexLock mu(event_lock_);
1027 FindMatchingEvents(EK_CLASS_PREPARE, &basket, matchList, &matchCount);
1028 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001029 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001030 << "thread=" << (void*) basket.threadId << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031
Elliott Hughes761928d2011-11-16 18:33:03 -08001032 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001033 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034
Elliott Hughes761928d2011-11-16 18:33:03 -08001035 if (basket.threadId == debugThreadId) {
1036 /*
1037 * JDWP says that, for a class prep in the debugger thread, we
1038 * should set threadId to null and if any threads were supposed
1039 * to be suspended then we suspend all other threads.
1040 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001041 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001042 basket.threadId = 0;
1043 if (suspendPolicy == SP_EVENT_THREAD) {
1044 suspendPolicy = SP_ALL;
1045 }
1046 }
1047
1048 pReq = eventPrep();
1049 expandBufAdd1(pReq, suspendPolicy);
1050 expandBufAdd4BE(pReq, matchCount);
1051
1052 for (int i = 0; i < matchCount; i++) {
1053 expandBufAdd1(pReq, matchList[i]->eventKind);
1054 expandBufAdd4BE(pReq, matchList[i]->requestId);
1055 expandBufAdd8BE(pReq, basket.threadId);
1056
1057 expandBufAdd1(pReq, tag);
1058 expandBufAdd8BE(pReq, refTypeId);
1059 expandBufAddUtf8String(pReq, signature);
1060 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061 }
1062 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001063 CleanupMatchList(matchList, matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001064 }
1065
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001066 /* send request and possibly suspend ourselves */
1067 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -07001068 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001069 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001070 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001071 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001072 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001073
Elliott Hughes761928d2011-11-16 18:33:03 -08001074 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001075 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001076 }
1077
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001078 return matchCount != 0;
1079}
1080
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001081/*
1082 * Send up a chunk of DDM data.
1083 *
1084 * While this takes the form of a JDWP "event", it doesn't interact with
1085 * other debugger traffic, and can't suspend the VM, so we skip all of
1086 * the fun event token gymnastics.
1087 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001088void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001089 uint8_t header[kJDWPHeaderLen + 8];
1090 size_t dataLen = 0;
1091
1092 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001093 CHECK_GT(iov_count, 0);
1094 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001095
1096 /*
1097 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1098 * this by creating a new copy of the vector with space for the header.
1099 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001100 iovec wrapiov[iov_count+1];
1101 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001102 wrapiov[i+1].iov_base = iov[i].iov_base;
1103 wrapiov[i+1].iov_len = iov[i].iov_len;
1104 dataLen += iov[i].iov_len;
1105 }
1106
1107 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001108 Set4BE(header, sizeof(header) + dataLen);
1109 Set4BE(header+4, NextRequestSerial());
1110 Set1(header+8, 0); /* flags */
1111 Set1(header+9, kJDWPDdmCmdSet);
1112 Set1(header+10, kJDWPDdmCmd);
1113 Set4BE(header+11, type);
1114 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001115
1116 wrapiov[0].iov_base = header;
1117 wrapiov[0].iov_len = sizeof(header);
1118
1119 /*
1120 * Make sure we're in VMWAIT in case the write blocks.
1121 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001122 int old_state = Dbg::ThreadWaiting();
Elliott Hughescccd84f2011-12-05 16:51:54 -08001123 (*transport->sendBufferedRequest)(this, wrapiov, iov_count + 1);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001124 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001125}
1126
1127} // namespace JDWP
1128
1129} // namespace art