blob: d68d01179df91083f4332abe25115035e8f34fab [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);
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) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800334 VLOG(jdwp) << "##### 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 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800357 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700358 return false;
359 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800360 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800362 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700363 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800364 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700365 }
366}
367
368/*
369 * See if two locations are equal.
370 *
371 * It's tempting to do a bitwise compare ("struct ==" or memcmp), but if
372 * the storage wasn't zeroed out there could be undefined values in the
373 * padding. Besides, the odds of "idx" being equal while the others aren't
374 * is very small, so this is usually just a simple integer comparison.
375 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800376static inline bool LocationMatch(const JdwpLocation* pLoc1, const JdwpLocation* pLoc2) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700377 return pLoc1->idx == pLoc2->idx &&
378 pLoc1->methodId == pLoc2->methodId &&
379 pLoc1->classId == pLoc2->classId &&
380 pLoc1->typeTag == pLoc2->typeTag;
381}
382
383/*
384 * See if the event's mods match up with the contents of "basket".
385 *
386 * If we find a Count mod before rejecting an event, we decrement it. We
387 * need to do this even if later mods cause us to ignore the event.
388 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800389static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700390 JdwpEventMod* pMod = pEvent->mods;
391
392 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
393 switch (pMod->modKind) {
394 case MK_COUNT:
395 CHECK_GT(pMod->count.count, 0);
396 pMod->count.count--;
397 break;
398 case MK_CONDITIONAL:
399 CHECK(false); // should not be getting these
400 break;
401 case MK_THREAD_ONLY:
402 if (pMod->threadOnly.threadId != basket->threadId) {
403 return false;
404 }
405 break;
406 case MK_CLASS_ONLY:
407 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
408 return false;
409 }
410 break;
411 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800412 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700413 return false;
414 }
415 break;
416 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800417 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700418 return false;
419 }
420 break;
421 case MK_LOCATION_ONLY:
Elliott Hughes761928d2011-11-16 18:33:03 -0800422 if (!LocationMatch(&pMod->locationOnly.loc, basket->pLoc)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423 return false;
424 }
425 break;
426 case MK_EXCEPTION_ONLY:
427 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
428 return false;
429 }
430 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
431 return false;
432 }
433 break;
434 case MK_FIELD_ONLY:
435 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
436 return false;
437 }
438 break;
439 case MK_STEP:
440 if (pMod->step.threadId != basket->threadId) {
441 return false;
442 }
443 break;
444 case MK_INSTANCE_ONLY:
445 if (pMod->instanceOnly.objectId != basket->thisPtr) {
446 return false;
447 }
448 break;
449 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800450 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 break;
452 }
453 }
454 return true;
455}
456
457/*
458 * Find all events of type "eventKind" with mods that match up with the
459 * rest of the arguments.
460 *
461 * Found events are appended to "matchList", and "*pMatchCount" is advanced,
462 * so this may be called multiple times for grouped events.
463 *
464 * DO NOT call this multiple times for the same eventKind, as Count mods are
465 * decremented during the scan.
466 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800467void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** matchList, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700468 /* start after the existing entries */
469 matchList += *pMatchCount;
470
Elliott Hughes761928d2011-11-16 18:33:03 -0800471 JdwpEvent* pEvent = eventList;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800473 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474 *matchList++ = pEvent;
475 (*pMatchCount)++;
476 }
477
478 pEvent = pEvent->next;
479 }
480}
481
482/*
483 * Scan through the list of matches and determine the most severe
484 * suspension policy.
485 */
486static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** matchList, int matchCount) {
487 JdwpSuspendPolicy policy = SP_NONE;
488
489 while (matchCount--) {
490 if ((*matchList)->suspendPolicy > policy) {
491 policy = (*matchList)->suspendPolicy;
492 }
493 matchList++;
494 }
495
496 return policy;
497}
498
499/*
500 * Three possibilities:
501 * SP_NONE - do nothing
502 * SP_EVENT_THREAD - suspend ourselves
503 * SP_ALL - suspend everybody except JDWP support thread
504 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800505void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspendPolicy) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800506 VLOG(jdwp) << "SuspendByPolicy(" << suspendPolicy << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700507 if (suspendPolicy == SP_NONE) {
508 return;
509 }
510
511 if (suspendPolicy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700512 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700513 } else {
514 CHECK_EQ(suspendPolicy, SP_EVENT_THREAD);
515 }
516
517 /* this is rare but possible -- see CLASS_PREPARE handling */
Elliott Hughes761928d2011-11-16 18:33:03 -0800518 if (Dbg::GetThreadSelfId() == debugThreadId) {
519 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700520 return;
521 }
522
523 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
524 while (true) {
525 pReq->ready = true;
526 Dbg::SuspendSelf();
527 pReq->ready = false;
528
529 /*
530 * The JDWP thread has told us (and possibly all other threads) to
531 * resume. See if it has left anything in our DebugInvokeReq mailbox.
532 */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800533 if (!pReq->invoke_needed_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800534 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700535 break;
536 }
537
538 /* grab this before posting/suspending again */
Elliott Hughes761928d2011-11-16 18:33:03 -0800539 SetWaitForEventThread(Dbg::GetThreadSelfId());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540
Elliott Hughesd07986f2011-12-06 18:27:45 -0800541 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700542 Dbg::ExecuteMethod(pReq);
543
Elliott Hughes475fc232011-10-25 15:00:35 -0700544 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545
546 /* clear this before signaling */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800547 pReq->invoke_needed_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700548
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800549 VLOG(jdwp) << "invoke complete, signaling and self-suspending";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700550 MutexLock mu(pReq->lock_);
551 pReq->cond_.Signal();
552 }
553}
554
555/*
556 * Determine if there is a method invocation in progress in the current
557 * thread.
558 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700559 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700560 * state. If set, we're in the process of invoking a method.
561 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800562bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Elliott Hughesd07986f2011-12-06 18:27:45 -0800564 return pReq->invoke_needed_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700565}
566
567/*
568 * We need the JDWP thread to hold off on doing stuff while we post an
569 * event and then suspend ourselves.
570 *
571 * Call this with a threadId of zero if you just want to wait for the
572 * current thread operation to complete.
573 *
574 * This could go to sleep waiting for another thread, so it's important
575 * that the thread be marked as VMWAIT before calling here.
576 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700577void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700578 bool waited = false;
579
580 /* this is held for very brief periods; contention is unlikely */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700581 MutexLock mu(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582
583 /*
584 * If another thread is already doing stuff, wait for it. This can
585 * go to sleep indefinitely.
586 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700587 while (eventThreadId != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800588 VLOG(jdwp) << StringPrintf("event in progress (0x%llx), 0x%llx sleeping", eventThreadId, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700589 waited = true;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700590 event_thread_cond_.Wait(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700591 }
592
593 if (waited || threadId != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800594 VLOG(jdwp) << StringPrintf("event token grabbed (0x%llx)", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700595 }
596 if (threadId != 0) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700597 eventThreadId = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700598 }
599}
600
601/*
602 * Clear the threadId and signal anybody waiting.
603 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700604void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605 /*
606 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
607 * function is called by dvmSuspendSelf(), and the transition back
608 * to RUNNING would confuse it.
609 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700610 MutexLock mu(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611
Elliott Hughes376a7a02011-10-24 18:35:55 -0700612 CHECK_NE(eventThreadId, 0U);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800613 VLOG(jdwp) << StringPrintf("cleared event token (0x%llx)", eventThreadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700614
Elliott Hughes376a7a02011-10-24 18:35:55 -0700615 eventThreadId = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616
Elliott Hughes376a7a02011-10-24 18:35:55 -0700617 event_thread_cond_.Signal();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700618}
619
620
621/*
622 * Prep an event. Allocates storage for the message and leaves space for
623 * the header.
624 */
625static ExpandBuf* eventPrep() {
626 ExpandBuf* pReq = expandBufAlloc();
627 expandBufAddSpace(pReq, kJDWPHeaderLen);
628 return pReq;
629}
630
631/*
632 * Write the header into the buffer and send the packet off to the debugger.
633 *
634 * Takes ownership of "pReq" (currently discards it).
635 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800636void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700637 uint8_t* buf = expandBufGetBuffer(pReq);
638
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700639 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800640 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700641 Set1(buf+8, 0); /* flags */
642 Set1(buf+9, kJdwpEventCommandSet);
643 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700644
Elliott Hughes761928d2011-11-16 18:33:03 -0800645 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646
647 expandBufFree(pReq);
648}
649
650
651/*
652 * Tell the debugger that we have finished initializing. This is always
653 * sent, even if the debugger hasn't requested it.
654 *
655 * This should be sent "before the main thread is started and before
656 * any application code has been executed". The thread ID in the message
657 * must be for the main thread.
658 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700659bool JdwpState::PostVMStart() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700660 JdwpSuspendPolicy suspendPolicy;
661 ObjectId threadId = Dbg::GetThreadSelfId();
662
Elliott Hughes376a7a02011-10-24 18:35:55 -0700663 if (options_->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700664 suspendPolicy = SP_ALL;
665 } else {
666 suspendPolicy = SP_NONE;
667 }
668
Elliott Hughes761928d2011-11-16 18:33:03 -0800669 ExpandBuf* pReq = eventPrep();
670 {
671 MutexLock mu(event_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800673 VLOG(jdwp) << "EVENT: " << EK_VM_START;
674 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700675
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700676 expandBufAdd1(pReq, suspendPolicy);
677 expandBufAdd4BE(pReq, 1);
678
679 expandBufAdd1(pReq, EK_VM_START);
680 expandBufAdd4BE(pReq, 0); /* requestId */
681 expandBufAdd8BE(pReq, threadId);
682 }
683
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700684 /* send request and possibly suspend ourselves */
685 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700686 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700687 if (suspendPolicy != SP_NONE) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700688 SetWaitForEventThread(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700689 }
690
Elliott Hughes761928d2011-11-16 18:33:03 -0800691 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692
Elliott Hughes761928d2011-11-16 18:33:03 -0800693 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700694 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695 }
696
697 return true;
698}
699
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700700/*
701 * A location of interest has been reached. This handles:
702 * Breakpoint
703 * SingleStep
704 * MethodEntry
705 * MethodExit
706 * These four types must be grouped together in a single response. The
707 * "eventFlags" indicates the type of event(s) that have happened.
708 *
709 * Valid mods:
710 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
711 * LocationOnly (for breakpoint/step only)
712 * Step (for step only)
713 *
714 * Interesting test cases:
715 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
716 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
717 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
718 * - Single-step to a line with a breakpoint. Should get a single
719 * event message with both events in it.
720 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800721bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700722 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700723
724 memset(&basket, 0, sizeof(basket));
725 basket.pLoc = pLoc;
726 basket.classId = pLoc->classId;
727 basket.thisPtr = thisPtr;
728 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800729 basket.className = DescriptorToName(Dbg::GetClassDescriptor(pLoc->classId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730
731 /*
732 * On rare occasions we may need to execute interpreted code in the VM
733 * while handling a request from the debugger. Don't fire breakpoints
734 * while doing so. (I don't think we currently do this at all, so
735 * this is mostly paranoia.)
736 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800737 if (basket.threadId == debugThreadId) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800738 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700739 return false;
740 }
741
742 /*
743 * The debugger variable display tab may invoke the interpreter to format
744 * complex objects. We want to ignore breakpoints and method entry/exit
745 * traps while working on behalf of the debugger.
746 *
747 * If we don't ignore them, the VM will get hung up, because we'll
748 * suspend on a breakpoint while the debugger is still waiting for its
749 * method invocation to complete.
750 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800751 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800752 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753 return false;
754 }
755
Elliott Hughes761928d2011-11-16 18:33:03 -0800756 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700758 ExpandBuf* pReq = NULL;
759 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700760
Elliott Hughes761928d2011-11-16 18:33:03 -0800761 {
762 MutexLock mu(event_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800763 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800764 FindMatchingEvents(EK_BREAKPOINT, &basket, matchList, &matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700765 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800766 if ((eventFlags & Dbg::kSingleStep) != 0) {
767 FindMatchingEvents(EK_SINGLE_STEP, &basket, matchList, &matchCount);
768 }
769 if ((eventFlags & Dbg::kMethodEntry) != 0) {
770 FindMatchingEvents(EK_METHOD_ENTRY, &basket, matchList, &matchCount);
771 }
772 if ((eventFlags & Dbg::kMethodExit) != 0) {
773 FindMatchingEvents(EK_METHOD_EXIT, &basket, matchList, &matchCount);
Elliott Hughes86964332012-02-15 19:37:42 -0800774
775 // TODO: match EK_METHOD_EXIT_WITH_RETURN_VALUE too; we need to include the 'value', though.
776 //FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, matchList, &matchCount);
Elliott Hughes761928d2011-11-16 18:33:03 -0800777 }
778 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800779 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
Elliott Hughes86964332012-02-15 19:37:42 -0800780 << basket.className << "." << Dbg::GetMethodName(pLoc->classId, pLoc->methodId)
781 << " thread=" << (void*) basket.threadId << " code=" << (void*) pLoc->idx << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700782
Elliott Hughes761928d2011-11-16 18:33:03 -0800783 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800784 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800785
786 pReq = eventPrep();
787 expandBufAdd1(pReq, suspendPolicy);
788 expandBufAdd4BE(pReq, matchCount);
789
790 for (int i = 0; i < matchCount; i++) {
791 expandBufAdd1(pReq, matchList[i]->eventKind);
792 expandBufAdd4BE(pReq, matchList[i]->requestId);
793 expandBufAdd8BE(pReq, basket.threadId);
794 AddLocation(pReq, pLoc);
795 }
796 }
797
798 CleanupMatchList(matchList, matchCount);
799 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700800
801 /* send request and possibly suspend ourselves */
802 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700803 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700804 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800805 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700806 }
807
Elliott Hughes761928d2011-11-16 18:33:03 -0800808 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809
Elliott Hughes761928d2011-11-16 18:33:03 -0800810 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700811 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700812 }
813
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700814 return matchCount != 0;
815}
816
817/*
818 * A thread is starting or stopping.
819 *
820 * Valid mods:
821 * Count, ThreadOnly
822 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700823bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
825
826 /*
827 * I don't think this can happen.
828 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800829 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700830 LOG(WARNING) << "Not posting thread change during invoke";
831 return false;
832 }
833
834 ModBasket basket;
835 memset(&basket, 0, sizeof(basket));
836 basket.threadId = threadId;
837
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838 ExpandBuf* pReq = NULL;
839 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes234ab152011-10-26 14:02:26 -0700840 int matchCount = 0;
841 {
842 // Don't allow the list to be updated while we scan it.
843 MutexLock mu(event_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800844 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700845
Elliott Hughes234ab152011-10-26 14:02:26 -0700846 if (start) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800847 FindMatchingEvents(EK_THREAD_START, &basket, matchList, &matchCount);
Elliott Hughes234ab152011-10-26 14:02:26 -0700848 } else {
Elliott Hughes761928d2011-11-16 18:33:03 -0800849 FindMatchingEvents(EK_THREAD_DEATH, &basket, matchList, &matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700850 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700851
Elliott Hughes234ab152011-10-26 14:02:26 -0700852 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800853 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
Elliott Hughes234ab152011-10-26 14:02:26 -0700854 << "thread=" << (void*) basket.threadId << ")";
855
856 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800857 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700858
859 pReq = eventPrep();
860 expandBufAdd1(pReq, suspendPolicy);
861 expandBufAdd4BE(pReq, matchCount);
862
863 for (int i = 0; i < matchCount; i++) {
864 expandBufAdd1(pReq, matchList[i]->eventKind);
865 expandBufAdd4BE(pReq, matchList[i]->requestId);
866 expandBufAdd8BE(pReq, basket.threadId);
867 }
868 }
869
Elliott Hughes761928d2011-11-16 18:33:03 -0800870 CleanupMatchList(matchList, matchCount);
Elliott Hughes234ab152011-10-26 14:02:26 -0700871 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872
873 /* send request and possibly suspend ourselves */
874 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700875 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700876 if (suspendPolicy != SP_NONE) {
Elliott Hughes234ab152011-10-26 14:02:26 -0700877 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700878 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800879 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700880
Elliott Hughes761928d2011-11-16 18:33:03 -0800881 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700882 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700883 }
884
885 return matchCount != 0;
886}
887
888/*
889 * Send a polite "VM is dying" message to the debugger.
890 *
891 * Skips the usual "event token" stuff.
892 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700893bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800894 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700895
896 ExpandBuf* pReq = eventPrep();
897 expandBufAdd1(pReq, SP_NONE);
898 expandBufAdd4BE(pReq, 1);
899
900 expandBufAdd1(pReq, EK_VM_DEATH);
901 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800902 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903 return true;
904}
905
906/*
907 * An exception has been thrown. It may or may not have been caught.
908 *
909 * Valid mods:
910 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
911 * ExceptionOnly, InstanceOnly
912 *
913 * The "exceptionId" has not been added to the GC-visible object registry,
914 * because there's a pretty good chance that we're not going to send it
915 * up the debugger.
916 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800917bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700918 ObjectId exceptionId, RefTypeId exceptionClassId,
919 const JdwpLocation* pCatchLoc, ObjectId thisPtr)
920{
921 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700922
923 memset(&basket, 0, sizeof(basket));
924 basket.pLoc = pThrowLoc;
925 basket.classId = pThrowLoc->classId;
926 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800927 basket.className = DescriptorToName(Dbg::GetClassDescriptor(basket.classId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700928 basket.excepClassId = exceptionClassId;
929 basket.caught = (pCatchLoc->classId != 0);
930 basket.thisPtr = thisPtr;
931
932 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800933 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800934 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700935 return false;
936 }
937
Elliott Hughes761928d2011-11-16 18:33:03 -0800938 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700940 ExpandBuf* pReq = NULL;
941 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800942 {
943 MutexLock mu(event_lock_);
944 FindMatchingEvents(EK_EXCEPTION, &basket, matchList, &matchCount);
945 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800946 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total)"
Elliott Hughes761928d2011-11-16 18:33:03 -0800947 << " thread=" << (void*) basket.threadId
948 << " exceptId=" << (void*) exceptionId
949 << " caught=" << basket.caught << ")";
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800950 VLOG(jdwp) << " throw: " << *pThrowLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800951 if (pCatchLoc->classId == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800952 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -0800953 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800954 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800955 }
956
957 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800958 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800959
960 pReq = eventPrep();
961 expandBufAdd1(pReq, suspendPolicy);
962 expandBufAdd4BE(pReq, matchCount);
963
964 for (int i = 0; i < matchCount; i++) {
965 expandBufAdd1(pReq, matchList[i]->eventKind);
966 expandBufAdd4BE(pReq, matchList[i]->requestId);
967 expandBufAdd8BE(pReq, basket.threadId);
968
969 AddLocation(pReq, pThrowLoc);
970 expandBufAdd1(pReq, JT_OBJECT);
971 expandBufAdd8BE(pReq, exceptionId);
972 AddLocation(pReq, pCatchLoc);
973 }
974
975 /* don't let the GC discard it */
976 Dbg::RegisterObjectId(exceptionId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700977 }
978
Elliott Hughes761928d2011-11-16 18:33:03 -0800979 CleanupMatchList(matchList, matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980 }
981
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982 /* send request and possibly suspend ourselves */
983 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700984 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800986 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700987 }
988
Elliott Hughes761928d2011-11-16 18:33:03 -0800989 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990
Elliott Hughes761928d2011-11-16 18:33:03 -0800991 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700992 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700993 }
994
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995 return matchCount != 0;
996}
997
998/*
999 * Announce that a class has been loaded.
1000 *
1001 * Valid mods:
1002 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1003 */
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001004bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature, int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001005 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001006
1007 memset(&basket, 0, sizeof(basket));
1008 basket.classId = refTypeId;
1009 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001010 basket.className = DescriptorToName(Dbg::GetClassDescriptor(basket.classId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001011
1012 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001013 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001014 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001015 return false;
1016 }
1017
Elliott Hughes761928d2011-11-16 18:33:03 -08001018 JdwpEvent** matchList = AllocMatchList(numEvents);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019 int matchCount = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020 ExpandBuf* pReq = NULL;
1021 JdwpSuspendPolicy suspendPolicy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -08001022 {
1023 MutexLock mu(event_lock_);
1024 FindMatchingEvents(EK_CLASS_PREPARE, &basket, matchList, &matchCount);
1025 if (matchCount != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001026 VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << matchCount << " total) "
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001027 << "thread=" << (void*) basket.threadId << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001028
Elliott Hughes761928d2011-11-16 18:33:03 -08001029 suspendPolicy = scanSuspendPolicy(matchList, matchCount);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001030 VLOG(jdwp) << " suspendPolicy=" << suspendPolicy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031
Elliott Hughes761928d2011-11-16 18:33:03 -08001032 if (basket.threadId == debugThreadId) {
1033 /*
1034 * JDWP says that, for a class prep in the debugger thread, we
1035 * should set threadId to null and if any threads were supposed
1036 * to be suspended then we suspend all other threads.
1037 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001038 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001039 basket.threadId = 0;
1040 if (suspendPolicy == SP_EVENT_THREAD) {
1041 suspendPolicy = SP_ALL;
1042 }
1043 }
1044
1045 pReq = eventPrep();
1046 expandBufAdd1(pReq, suspendPolicy);
1047 expandBufAdd4BE(pReq, matchCount);
1048
1049 for (int i = 0; i < matchCount; i++) {
1050 expandBufAdd1(pReq, matchList[i]->eventKind);
1051 expandBufAdd4BE(pReq, matchList[i]->requestId);
1052 expandBufAdd8BE(pReq, basket.threadId);
1053
1054 expandBufAdd1(pReq, tag);
1055 expandBufAdd8BE(pReq, refTypeId);
1056 expandBufAddUtf8String(pReq, signature);
1057 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001058 }
1059 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001060 CleanupMatchList(matchList, matchCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061 }
1062
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001063 /* send request and possibly suspend ourselves */
1064 if (pReq != NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -07001065 int old_state = Dbg::ThreadWaiting();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001066 if (suspendPolicy != SP_NONE) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001067 SetWaitForEventThread(basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001068 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001069 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001070
Elliott Hughes761928d2011-11-16 18:33:03 -08001071 SuspendByPolicy(suspendPolicy);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001072 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001073 }
1074
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001075 return matchCount != 0;
1076}
1077
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001078/*
1079 * Send up a chunk of DDM data.
1080 *
1081 * While this takes the form of a JDWP "event", it doesn't interact with
1082 * other debugger traffic, and can't suspend the VM, so we skip all of
1083 * the fun event token gymnastics.
1084 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001085void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001086 uint8_t header[kJDWPHeaderLen + 8];
1087 size_t dataLen = 0;
1088
1089 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001090 CHECK_GT(iov_count, 0);
1091 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001092
1093 /*
1094 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1095 * this by creating a new copy of the vector with space for the header.
1096 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001097 iovec wrapiov[iov_count+1];
1098 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099 wrapiov[i+1].iov_base = iov[i].iov_base;
1100 wrapiov[i+1].iov_len = iov[i].iov_len;
1101 dataLen += iov[i].iov_len;
1102 }
1103
1104 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001105 Set4BE(header, sizeof(header) + dataLen);
1106 Set4BE(header+4, NextRequestSerial());
1107 Set1(header+8, 0); /* flags */
1108 Set1(header+9, kJDWPDdmCmdSet);
1109 Set1(header+10, kJDWPDdmCmd);
1110 Set4BE(header+11, type);
1111 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001112
1113 wrapiov[0].iov_base = header;
1114 wrapiov[0].iov_len = sizeof(header);
1115
1116 /*
1117 * Make sure we're in VMWAIT in case the write blocks.
1118 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001119 int old_state = Dbg::ThreadWaiting();
Elliott Hughescccd84f2011-12-05 16:51:54 -08001120 (*transport->sendBufferedRequest)(this, wrapiov, iov_count + 1);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001121 Dbg::ThreadContinuing(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122}
1123
1124} // namespace JDWP
1125
1126} // namespace art