blob: 19f10eb4c3ecd2db8a781631aeb4fcf6d3d90340 [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);
Elliott Hughesf8349362012-06-18 15:00:06 -0700127 LOG(INFO) << " kind=" << pEvent->eventKind << " susp=" << pEvent->suspend_policy << " modCount=" << pEvent->modCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128
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) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700144 MutexLock mu(event_list_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 Hughesf8349362012-06-18 15:00:06 -0700176 if (event_list_ != NULL) {
177 pEvent->next = event_list_;
178 event_list_->prev = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700180 event_list_ = pEvent;
181 ++event_list_size_;
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 Hughesf8349362012-06-18 15:00:06 -0700197 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198
Elliott Hughesf8349362012-06-18 15:00:06 -0700199 event_list_ = 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 Hughesf8349362012-06-18 15:00:06 -0700225 --event_list_size_;
226 CHECK(event_list_size_ != 0 || event_list_ == 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) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700237 MutexLock mu(event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238
Elliott Hughesf8349362012-06-18 15:00:06 -0700239 JdwpEvent* pEvent = event_list_;
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() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700257 MutexLock mu(event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258
Elliott Hughesf8349362012-06-18 15:00:06 -0700259 JdwpEvent* pEvent = event_list_;
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 Hughesf8349362012-06-18 15:00:06 -0700268 event_list_ = 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);
Elliott Hughesf8349362012-06-18 15:00:06 -0700296 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297
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 Hughesf8349362012-06-18 15:00:06 -0700329void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
330 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800332 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 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 Hughesf8349362012-06-18 15:00:06 -0700347 delete[] match_list;
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/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700372 * See if the event's mods match up with the contents of "basket".
373 *
374 * If we find a Count mod before rejecting an event, we decrement it. We
375 * need to do this even if later mods cause us to ignore the event.
376 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379 JdwpEventMod* pMod = pEvent->mods;
380
381 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
382 switch (pMod->modKind) {
383 case MK_COUNT:
384 CHECK_GT(pMod->count.count, 0);
385 pMod->count.count--;
386 break;
387 case MK_CONDITIONAL:
388 CHECK(false); // should not be getting these
389 break;
390 case MK_THREAD_ONLY:
391 if (pMod->threadOnly.threadId != basket->threadId) {
392 return false;
393 }
394 break;
395 case MK_CLASS_ONLY:
396 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
397 return false;
398 }
399 break;
400 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800401 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700402 return false;
403 }
404 break;
405 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800406 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407 return false;
408 }
409 break;
410 case MK_LOCATION_ONLY:
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800411 if (pMod->locationOnly.loc != *basket->pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 return false;
413 }
414 break;
415 case MK_EXCEPTION_ONLY:
416 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
417 return false;
418 }
419 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
420 return false;
421 }
422 break;
423 case MK_FIELD_ONLY:
424 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
425 return false;
426 }
427 break;
428 case MK_STEP:
429 if (pMod->step.threadId != basket->threadId) {
430 return false;
431 }
432 break;
433 case MK_INSTANCE_ONLY:
434 if (pMod->instanceOnly.objectId != basket->thisPtr) {
435 return false;
436 }
437 break;
438 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800439 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 break;
441 }
442 }
443 return true;
444}
445
446/*
447 * Find all events of type "eventKind" with mods that match up with the
448 * rest of the arguments.
449 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700450 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 * so this may be called multiple times for grouped events.
452 *
453 * DO NOT call this multiple times for the same eventKind, as Count mods are
454 * decremented during the scan.
455 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700456void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket,
457 JdwpEvent** match_list, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700458 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700459 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700460
Elliott Hughesf8349362012-06-18 15:00:06 -0700461 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800463 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700464 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700465 (*pMatchCount)++;
466 }
467
468 pEvent = pEvent->next;
469 }
470}
471
472/*
473 * Scan through the list of matches and determine the most severe
474 * suspension policy.
475 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700476static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700477 JdwpSuspendPolicy policy = SP_NONE;
478
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800479 while (match_count--) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700480 if ((*match_list)->suspend_policy > policy) {
481 policy = (*match_list)->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700483 match_list++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484 }
485
486 return policy;
487}
488
489/*
490 * Three possibilities:
491 * SP_NONE - do nothing
492 * SP_EVENT_THREAD - suspend ourselves
493 * SP_ALL - suspend everybody except JDWP support thread
494 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700495void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700496 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
497 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700498 return;
499 }
500
Elliott Hughesf8349362012-06-18 15:00:06 -0700501 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700502 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700503 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700504 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700505 }
506
507 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700508 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800509 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510 return;
511 }
512
513 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
514 while (true) {
515 pReq->ready = true;
516 Dbg::SuspendSelf();
517 pReq->ready = false;
518
519 /*
520 * The JDWP thread has told us (and possibly all other threads) to
521 * resume. See if it has left anything in our DebugInvokeReq mailbox.
522 */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800523 if (!pReq->invoke_needed_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800524 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525 break;
526 }
527
528 /* grab this before posting/suspending again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529 SetWaitForEventThread(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700530
Elliott Hughesd07986f2011-12-06 18:27:45 -0800531 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700532 Dbg::ExecuteMethod(pReq);
533
Elliott Hughes475fc232011-10-25 15:00:35 -0700534 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700535
536 /* clear this before signaling */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800537 pReq->invoke_needed_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800539 VLOG(jdwp) << "invoke complete, signaling and self-suspending";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540 MutexLock mu(pReq->lock_);
541 pReq->cond_.Signal();
542 }
543}
544
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700545void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
546 ObjectId threadId) {
547 Thread* self = Thread::Current();
548 self->AssertThreadSuspensionIsAllowable();
549 /* send request and possibly suspend ourselves */
550 if (pReq != NULL) {
551 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
552 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
553 if (suspend_policy != SP_NONE) {
554 SetWaitForEventThread(threadId);
555 }
556 EventFinish(pReq);
557 SuspendByPolicy(suspend_policy, thread_self_id);
558 self->TransitionFromSuspendedToRunnable();
559 }
560}
561
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700562/*
563 * Determine if there is a method invocation in progress in the current
564 * thread.
565 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700566 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700567 * state. If set, we're in the process of invoking a method.
568 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800569bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Elliott Hughesd07986f2011-12-06 18:27:45 -0800571 return pReq->invoke_needed_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700572}
573
574/*
575 * We need the JDWP thread to hold off on doing stuff while we post an
576 * event and then suspend ourselves.
577 *
578 * Call this with a threadId of zero if you just want to wait for the
579 * current thread operation to complete.
580 *
581 * This could go to sleep waiting for another thread, so it's important
582 * that the thread be marked as VMWAIT before calling here.
583 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700584void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700585 bool waited = false;
586
587 /* this is held for very brief periods; contention is unlikely */
Ian Rogers81d425b2012-09-27 16:03:43 -0700588 Thread* self = Thread::Current();
589 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700590
591 /*
592 * If another thread is already doing stuff, wait for it. This can
593 * go to sleep indefinitely.
594 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700595 while (event_thread_id_ != 0) {
596 VLOG(jdwp) << StringPrintf("event in progress (%#llx), %#llx sleeping", event_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700597 waited = true;
Ian Rogers81d425b2012-09-27 16:03:43 -0700598 event_thread_cond_.Wait(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700599 }
600
601 if (waited || threadId != 0) {
Elliott Hughes229feb72012-02-23 13:33:29 -0800602 VLOG(jdwp) << StringPrintf("event token grabbed (%#llx)", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700603 }
604 if (threadId != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700605 event_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700606 }
607}
608
609/*
610 * Clear the threadId and signal anybody waiting.
611 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700612void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613 /*
614 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
615 * function is called by dvmSuspendSelf(), and the transition back
616 * to RUNNING would confuse it.
617 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700618 MutexLock mu(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619
Elliott Hughesa21039c2012-06-21 12:09:25 -0700620 CHECK_NE(event_thread_id_, 0U);
621 VLOG(jdwp) << StringPrintf("cleared event token (%#llx)", event_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622
Elliott Hughesa21039c2012-06-21 12:09:25 -0700623 event_thread_id_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700624
Elliott Hughes376a7a02011-10-24 18:35:55 -0700625 event_thread_cond_.Signal();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700626}
627
628
629/*
630 * Prep an event. Allocates storage for the message and leaves space for
631 * the header.
632 */
633static ExpandBuf* eventPrep() {
634 ExpandBuf* pReq = expandBufAlloc();
635 expandBufAddSpace(pReq, kJDWPHeaderLen);
636 return pReq;
637}
638
639/*
640 * Write the header into the buffer and send the packet off to the debugger.
641 *
642 * Takes ownership of "pReq" (currently discards it).
643 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800644void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700645 uint8_t* buf = expandBufGetBuffer(pReq);
646
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700647 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800648 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700649 Set1(buf+8, 0); /* flags */
650 Set1(buf+9, kJdwpEventCommandSet);
651 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652
Elliott Hughes761928d2011-11-16 18:33:03 -0800653 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700654
655 expandBufFree(pReq);
656}
657
658
659/*
660 * Tell the debugger that we have finished initializing. This is always
661 * sent, even if the debugger hasn't requested it.
662 *
663 * This should be sent "before the main thread is started and before
664 * any application code has been executed". The thread ID in the message
665 * must be for the main thread.
666 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700667bool JdwpState::PostVMStart() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700668 JdwpSuspendPolicy suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700669 ObjectId threadId = Dbg::GetThreadSelfId();
670
Elliott Hughes376a7a02011-10-24 18:35:55 -0700671 if (options_->suspend) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700672 suspend_policy = SP_ALL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700673 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700674 suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700675 }
676
Elliott Hughes761928d2011-11-16 18:33:03 -0800677 ExpandBuf* pReq = eventPrep();
678 {
Elliott Hughesf8349362012-06-18 15:00:06 -0700679 MutexLock mu(event_list_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700680
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800681 VLOG(jdwp) << "EVENT: " << EK_VM_START;
Elliott Hughesf8349362012-06-18 15:00:06 -0700682 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700683
Elliott Hughesf8349362012-06-18 15:00:06 -0700684 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700685 expandBufAdd4BE(pReq, 1);
686
687 expandBufAdd1(pReq, EK_VM_START);
688 expandBufAdd4BE(pReq, 0); /* requestId */
689 expandBufAdd8BE(pReq, threadId);
690 }
691
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700694
695 return true;
696}
697
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698/*
699 * A location of interest has been reached. This handles:
700 * Breakpoint
701 * SingleStep
702 * MethodEntry
703 * MethodExit
704 * These four types must be grouped together in a single response. The
705 * "eventFlags" indicates the type of event(s) that have happened.
706 *
707 * Valid mods:
708 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
709 * LocationOnly (for breakpoint/step only)
710 * Step (for step only)
711 *
712 * Interesting test cases:
713 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
714 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
715 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
716 * - Single-step to a line with a breakpoint. Should get a single
717 * event message with both events in it.
718 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800719bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700721
722 memset(&basket, 0, sizeof(basket));
723 basket.pLoc = pLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700724 basket.classId = pLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700725 basket.thisPtr = thisPtr;
726 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes74847412012-06-20 18:10:21 -0700727 basket.className = Dbg::GetClassName(pLoc->class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700728
729 /*
730 * On rare occasions we may need to execute interpreted code in the VM
731 * while handling a request from the debugger. Don't fire breakpoints
732 * while doing so. (I don't think we currently do this at all, so
733 * this is mostly paranoia.)
734 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700735 if (basket.threadId == debug_thread_id_) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800736 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700737 return false;
738 }
739
740 /*
741 * The debugger variable display tab may invoke the interpreter to format
742 * complex objects. We want to ignore breakpoints and method entry/exit
743 * traps while working on behalf of the debugger.
744 *
745 * If we don't ignore them, the VM will get hung up, because we'll
746 * suspend on a breakpoint while the debugger is still waiting for its
747 * method invocation to complete.
748 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800749 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800750 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751 return false;
752 }
753
Elliott Hughesf8349362012-06-18 15:00:06 -0700754 JdwpEvent** match_list = NULL;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800755 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700756 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700757 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700758
Elliott Hughes761928d2011-11-16 18:33:03 -0800759 {
Elliott Hughesf8349362012-06-18 15:00:06 -0700760 MutexLock mu(event_list_lock_);
761 match_list = AllocMatchList(event_list_size_);
Elliott Hughes86964332012-02-15 19:37:42 -0800762 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700763 FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700764 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800765 if ((eventFlags & Dbg::kSingleStep) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700766 FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800767 }
768 if ((eventFlags & Dbg::kMethodEntry) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700769 FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800770 }
771 if ((eventFlags & Dbg::kMethodExit) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700772 FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
Elliott Hughes86964332012-02-15 19:37:42 -0800773
774 // TODO: match EK_METHOD_EXIT_WITH_RETURN_VALUE too; we need to include the 'value', though.
Elliott Hughesf8349362012-06-18 15:00:06 -0700775 //FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800776 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800777 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700778 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes74847412012-06-20 18:10:21 -0700779 << basket.className << "." << Dbg::GetMethodName(pLoc->class_id, pLoc->method_id)
Elliott Hughes229feb72012-02-23 13:33:29 -0800780 << StringPrintf(" thread=%#llx dex_pc=%#llx)", basket.threadId, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700781
Elliott Hughesf8349362012-06-18 15:00:06 -0700782 suspend_policy = scanSuspendPolicy(match_list, match_count);
783 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800784
785 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700786 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800787 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800788
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800789 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700790 expandBufAdd1(pReq, match_list[i]->eventKind);
791 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800792 expandBufAdd8BE(pReq, basket.threadId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700793 expandBufAddLocation(pReq, *pLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800794 }
795 }
796
Elliott Hughesf8349362012-06-18 15:00:06 -0700797 CleanupMatchList(match_list, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800798 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700799
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800802 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700803}
804
805/*
806 * A thread is starting or stopping.
807 *
808 * Valid mods:
809 * Count, ThreadOnly
810 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700811bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700812 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
813
814 /*
815 * I don't think this can happen.
816 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800817 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818 LOG(WARNING) << "Not posting thread change during invoke";
819 return false;
820 }
821
822 ModBasket basket;
823 memset(&basket, 0, sizeof(basket));
824 basket.threadId = threadId;
825
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700826 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700827 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800828 int match_count = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -0700829 {
830 // Don't allow the list to be updated while we scan it.
Elliott Hughesf8349362012-06-18 15:00:06 -0700831 MutexLock mu(event_list_lock_);
832 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833
Elliott Hughes234ab152011-10-26 14:02:26 -0700834 if (start) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700835 FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700836 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700837 FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700839
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800840 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700841 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -0800842 << StringPrintf("thread=%#llx", basket.threadId) << ")";
Elliott Hughes234ab152011-10-26 14:02:26 -0700843
Elliott Hughesf8349362012-06-18 15:00:06 -0700844 suspend_policy = scanSuspendPolicy(match_list, match_count);
845 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700846
847 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700848 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800849 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700850
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800851 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700852 expandBufAdd1(pReq, match_list[i]->eventKind);
853 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes234ab152011-10-26 14:02:26 -0700854 expandBufAdd8BE(pReq, basket.threadId);
855 }
856 }
857
Elliott Hughesf8349362012-06-18 15:00:06 -0700858 CleanupMatchList(match_list, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700859 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700860
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700862
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800863 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700864}
865
866/*
867 * Send a polite "VM is dying" message to the debugger.
868 *
869 * Skips the usual "event token" stuff.
870 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700871bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800872 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873
874 ExpandBuf* pReq = eventPrep();
875 expandBufAdd1(pReq, SP_NONE);
876 expandBufAdd4BE(pReq, 1);
877
878 expandBufAdd1(pReq, EK_VM_DEATH);
879 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800880 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700881 return true;
882}
883
884/*
885 * An exception has been thrown. It may or may not have been caught.
886 *
887 * Valid mods:
888 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
889 * ExceptionOnly, InstanceOnly
890 *
891 * The "exceptionId" has not been added to the GC-visible object registry,
892 * because there's a pretty good chance that we're not going to send it
893 * up the debugger.
894 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800895bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes74847412012-06-20 18:10:21 -0700896 ObjectId exceptionId, RefTypeId exceptionClassId,
897 const JdwpLocation* pCatchLoc, ObjectId thisPtr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700898 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899
900 memset(&basket, 0, sizeof(basket));
901 basket.pLoc = pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700902 basket.classId = pThrowLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800904 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700905 basket.excepClassId = exceptionClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700906 basket.caught = (pCatchLoc->class_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700907 basket.thisPtr = thisPtr;
908
909 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800910 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800911 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912 return false;
913 }
914
Elliott Hughesf8349362012-06-18 15:00:06 -0700915 JdwpEvent** match_list = NULL;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800916 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700917 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700918 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800919 {
Elliott Hughesf8349362012-06-18 15:00:06 -0700920 MutexLock mu(event_list_lock_);
921 match_list = AllocMatchList(event_list_size_);
922 FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800923 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700924 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)"
Elliott Hughes0cf74332012-02-23 23:14:00 -0800925 << StringPrintf(" thread=%#llx", basket.threadId)
926 << StringPrintf(" exceptId=%#llx", exceptionId)
Elliott Hughes436e3722012-02-17 20:01:47 -0800927 << " caught=" << basket.caught << ")"
928 << " throw: " << *pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700929 if (pCatchLoc->class_id == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800930 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -0800931 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800932 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800933 }
934
Elliott Hughesf8349362012-06-18 15:00:06 -0700935 suspend_policy = scanSuspendPolicy(match_list, match_count);
936 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800937
938 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700939 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800940 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800941
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800942 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700943 expandBufAdd1(pReq, match_list[i]->eventKind);
944 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800945 expandBufAdd8BE(pReq, basket.threadId);
946
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700947 expandBufAddLocation(pReq, *pThrowLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800948 expandBufAdd1(pReq, JT_OBJECT);
949 expandBufAdd8BE(pReq, exceptionId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700950 expandBufAddLocation(pReq, *pCatchLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800951 }
952
953 /* don't let the GC discard it */
954 Dbg::RegisterObjectId(exceptionId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955 }
956
Elliott Hughesf8349362012-06-18 15:00:06 -0700957 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958 }
959
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700961
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800962 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700963}
964
965/*
966 * Announce that a class has been loaded.
967 *
968 * Valid mods:
969 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
970 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
972 int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700973 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974
975 memset(&basket, 0, sizeof(basket));
976 basket.classId = refTypeId;
977 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800978 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979
980 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800981 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800982 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700983 return false;
984 }
985
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700987 JdwpSuspendPolicy suspend_policy = SP_NONE;
988 int match_count = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -0800989 {
Elliott Hughesf8349362012-06-18 15:00:06 -0700990 MutexLock mu(event_list_lock_);
991 JdwpEvent** match_list = AllocMatchList(event_list_size_);
992 FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800993 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700994 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -0800995 << StringPrintf("thread=%#llx", basket.threadId) << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700996
Elliott Hughesf8349362012-06-18 15:00:06 -0700997 suspend_policy = scanSuspendPolicy(match_list, match_count);
998 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700999
Elliott Hughesa21039c2012-06-21 12:09:25 -07001000 if (basket.threadId == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001001 /*
1002 * JDWP says that, for a class prep in the debugger thread, we
1003 * should set threadId to null and if any threads were supposed
1004 * to be suspended then we suspend all other threads.
1005 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001006 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001007 basket.threadId = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001008 if (suspend_policy == SP_EVENT_THREAD) {
1009 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001010 }
1011 }
1012
1013 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001014 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001015 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001016
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001017 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001018 expandBufAdd1(pReq, match_list[i]->eventKind);
1019 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001020 expandBufAdd8BE(pReq, basket.threadId);
1021
1022 expandBufAdd1(pReq, tag);
1023 expandBufAdd8BE(pReq, refTypeId);
1024 expandBufAddUtf8String(pReq, signature);
1025 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001026 }
1027 }
Elliott Hughesf8349362012-06-18 15:00:06 -07001028 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001029 }
1030
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001032
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001033 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034}
1035
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001036/*
1037 * Send up a chunk of DDM data.
1038 *
1039 * While this takes the form of a JDWP "event", it doesn't interact with
1040 * other debugger traffic, and can't suspend the VM, so we skip all of
1041 * the fun event token gymnastics.
1042 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001043void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001044 uint8_t header[kJDWPHeaderLen + 8];
1045 size_t dataLen = 0;
1046
1047 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001048 CHECK_GT(iov_count, 0);
1049 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050
1051 /*
1052 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1053 * this by creating a new copy of the vector with space for the header.
1054 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001055 iovec wrapiov[iov_count+1];
1056 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001057 wrapiov[i+1].iov_base = iov[i].iov_base;
1058 wrapiov[i+1].iov_len = iov[i].iov_len;
1059 dataLen += iov[i].iov_len;
1060 }
1061
1062 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001063 Set4BE(header, sizeof(header) + dataLen);
1064 Set4BE(header+4, NextRequestSerial());
1065 Set1(header+8, 0); /* flags */
1066 Set1(header+9, kJDWPDdmCmdSet);
1067 Set1(header+10, kJDWPDdmCmd);
1068 Set4BE(header+11, type);
1069 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001070
1071 wrapiov[0].iov_base = header;
1072 wrapiov[0].iov_len = sizeof(header);
1073
Ian Rogers15bf2d32012-08-28 17:33:04 -07001074 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1075 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 Thread* self = Thread::Current();
Ian Rogers15bf2d32012-08-28 17:33:04 -07001077 bool safe_to_release_mutator_lock_over_send;
1078 for (size_t i=0; i < kMutatorLock; ++i) {
Ian Rogers81d425b2012-09-27 16:03:43 -07001079 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07001080 safe_to_release_mutator_lock_over_send = false;
1081 break;
1082 }
1083 }
1084 if (safe_to_release_mutator_lock_over_send) {
1085 // Change state to waiting to allow GC, ... while we're sending.
1086 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
1087 (*transport_->sendBufferedRequest)(this, wrapiov, iov_count + 1);
1088 self->TransitionFromSuspendedToRunnable();
1089 } else {
1090 // Send and possibly block GC...
1091 (*transport_->sendBufferedRequest)(this, wrapiov, iov_count + 1);
1092 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001093}
1094
1095} // namespace JDWP
1096
1097} // namespace art