blob: 61bd1edd5d1bb0625a446b46622c67989ddd64ab [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 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070016
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include "jdwp/jdwp_event.h"
18
19#include <stddef.h> /* for offsetof() */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070020#include <stdlib.h>
21#include <string.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070022#include <unistd.h>
23
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "debugger.h"
27#include "jdwp/jdwp_constants.h"
28#include "jdwp/jdwp_expand_buf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080029#include "jdwp/jdwp_priv.h"
Ian Rogers693ff612013-02-01 10:56:12 -080030#include "thread-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031
Elliott Hughes872d4ec2011-10-21 17:07:15 -070032/*
33General notes:
34
35The event add/remove stuff usually happens from the debugger thread,
36in response to requests from the debugger, but can also happen as the
37result of an event in an arbitrary thread (e.g. an event with a "count"
38mod expires). It's important to keep the event list locked when processing
39events.
40
41Event posting can happen from any thread. The JDWP thread will not usually
42post anything but VM start/death, but if a JDWP request causes a class
43to be loaded, the ClassPrepare event will come from the JDWP thread.
44
45
46We can have serialization issues when we post an event to the debugger.
47For example, a thread could send an "I hit a breakpoint and am suspending
48myself" message to the debugger. Before it manages to suspend itself, the
49debugger's response ("not interested, resume thread") arrives and is
50processed. We try to resume a thread that hasn't yet suspended.
51
52This means that, after posting an event to the debugger, we need to wait
53for the event thread to suspend itself (and, potentially, all other threads)
54before processing any additional requests from the debugger. While doing
55so we need to be aware that multiple threads may be hitting breakpoints
56or other events simultaneously, so we either need to wait for all of them
57or serialize the events with each other.
58
59The current mechanism works like this:
60 Event thread:
61 - If I'm going to suspend, grab the "I am posting an event" token. Wait
62 for it if it's not currently available.
63 - Post the event to the debugger.
64 - If appropriate, suspend others and then myself. As part of suspending
65 myself, release the "I am posting" token.
66 JDWP thread:
67 - When an event arrives, see if somebody is posting an event. If so,
68 sleep until we can acquire the "I am posting an event" token. Release
69 it immediately and continue processing -- the event we have already
70 received should not interfere with other events that haven't yet
71 been posted.
72
73Some care must be taken to avoid deadlock:
74
75 - thread A and thread B exit near-simultaneously, and post thread-death
76 events with a "suspend all" clause
77 - thread A gets the event token, thread B sits and waits for it
78 - thread A wants to suspend all other threads, but thread B is waiting
79 for the token and can't be suspended
80
81So we need to mark thread B in such a way that thread A doesn't wait for it.
82
83If we just bracket the "grab event token" call with a change to VMWAIT
84before sleeping, the switch back to RUNNING state when we get the token
85will cause thread B to suspend (remember, thread A's global suspend is
86still in force, even after it releases the token). Suspending while
87holding the event token is very bad, because it prevents the JDWP thread
88from processing incoming messages.
89
90We need to change to VMWAIT state at the *start* of posting an event,
91and stay there until we either finish posting the event or decide to
92put ourselves to sleep. That way we don't interfere with anyone else and
93don't allow anyone else to interfere with us.
94*/
95
96
97#define kJdwpEventCommandSet 64
98#define kJdwpCompositeCommand 100
99
100namespace art {
101
102namespace JDWP {
103
104/*
105 * Stuff to compare against when deciding if a mod matches. Only the
106 * values for mods valid for the event being evaluated will be filled in.
107 * The rest will be zeroed.
108 */
109struct ModBasket {
jeffhao162fd332013-01-08 16:21:01 -0800110 ModBasket() : pLoc(NULL), threadId(0), classId(0), excepClassId(0),
111 caught(false), field(0), thisPtr(0) { }
112
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113 const JdwpLocation* pLoc; /* LocationOnly */
Elliott Hughesa2155262011-11-16 16:26:58 -0800114 std::string className; /* ClassMatch/ClassExclude */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700115 ObjectId threadId; /* ThreadOnly */
116 RefTypeId classId; /* ClassOnly */
117 RefTypeId excepClassId; /* ExceptionOnly */
118 bool caught; /* ExceptionOnly */
119 FieldId field; /* FieldOnly */
120 ObjectId thisPtr; /* InstanceOnly */
121 /* nothing for StepOnly -- handled differently */
122};
123
124/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 * Dump an event to the log file.
126 */
127static void dumpEvent(const JdwpEvent* pEvent) {
128 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 -0700129 LOG(INFO) << " kind=" << pEvent->eventKind << " susp=" << pEvent->suspend_policy << " modCount=" << pEvent->modCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130
131 for (int i = 0; i < pEvent->modCount; i++) {
132 const JdwpEventMod* pMod = &pEvent->mods[i];
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800133 LOG(INFO) << " " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700134 /* TODO - show details */
135 }
136}
137
138/*
139 * Add an event to the list. Ordering is not important.
140 *
141 * If something prevents the event from being registered, e.g. it's a
142 * single-step request on a thread that doesn't exist, the event will
143 * not be added to the list, and an appropriate error will be returned.
144 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800145JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
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 */
Jeff Hao449db332013-04-12 18:30:52 -0700176 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700177 if (event_list_ != NULL) {
178 pEvent->next = event_list_;
179 event_list_->prev = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700181 event_list_ = pEvent;
182 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700183
184 return ERR_NONE;
185}
186
187/*
188 * Remove an event from the list. This will also remove the event from
189 * any optimization tables, e.g. breakpoints.
190 *
191 * Does not free the JdwpEvent.
192 *
193 * Grab the eventLock before calling here.
194 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800195void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196 if (pEvent->prev == NULL) {
197 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700198 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199
Elliott Hughesf8349362012-06-18 15:00:06 -0700200 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 } else {
202 pEvent->prev->next = pEvent->next;
203 }
204
205 if (pEvent->next != NULL) {
206 pEvent->next->prev = pEvent->prev;
207 pEvent->next = NULL;
208 }
209 pEvent->prev = NULL;
210
211 /*
212 * Unhook us from the interpreter, if necessary.
213 */
214 for (int i = 0; i < pEvent->modCount; i++) {
215 JdwpEventMod* pMod = &pEvent->mods[i];
216 if (pMod->modKind == MK_LOCATION_ONLY) {
217 /* should only be for Breakpoint, Step, and Exception */
218 Dbg::UnwatchLocation(&pMod->locationOnly.loc);
219 }
220 if (pMod->modKind == MK_STEP) {
221 /* should only be for EK_SINGLE_STEP; should only be one */
222 Dbg::UnconfigureStep(pMod->step.threadId);
223 }
224 }
225
Elliott Hughesf8349362012-06-18 15:00:06 -0700226 --event_list_size_;
227 CHECK(event_list_size_ != 0 || event_list_ == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228}
229
230/*
231 * Remove the event with the given ID from the list.
232 *
233 * Failure to find the event isn't really an error, but it is a little
234 * weird. (It looks like Eclipse will try to be extra careful and will
235 * explicitly remove one-off single-step events.)
236 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800237void JdwpState::UnregisterEventById(uint32_t requestId) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700238 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700239
Elliott Hughesf8349362012-06-18 15:00:06 -0700240 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241 while (pEvent != NULL) {
242 if (pEvent->requestId == requestId) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800243 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244 EventFree(pEvent);
Elliott Hughes761928d2011-11-16 18:33:03 -0800245 return; /* there can be only one with a given ID */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700246 }
247
248 pEvent = pEvent->next;
249 }
250
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700251 // ALOGD("Odd: no match when removing event reqId=0x%04x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700252}
253
254/*
255 * Remove all entries from the event list.
256 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800257void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700258 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700259
Elliott Hughesf8349362012-06-18 15:00:06 -0700260 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261 while (pEvent != NULL) {
262 JdwpEvent* pNextEvent = pEvent->next;
263
Elliott Hughes761928d2011-11-16 18:33:03 -0800264 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 EventFree(pEvent);
266 pEvent = pNextEvent;
267 }
268
Elliott Hughesf8349362012-06-18 15:00:06 -0700269 event_list_ = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270}
271
272/*
273 * Allocate a JdwpEvent struct with enough space to hold the specified
274 * number of mod records.
275 */
276JdwpEvent* EventAlloc(int numMods) {
277 JdwpEvent* newEvent;
278 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
279 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
280 memset(newEvent, 0, allocSize);
281 return newEvent;
282}
283
284/*
285 * Free a JdwpEvent.
286 *
287 * Do not call this until the event has been removed from the list.
288 */
289void EventFree(JdwpEvent* pEvent) {
290 if (pEvent == NULL) {
291 return;
292 }
293
294 /* make sure it was removed from the list */
295 CHECK(pEvent->prev == NULL);
296 CHECK(pEvent->next == NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700297 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700298
299 /*
300 * Free any hairy bits in the mods.
301 */
302 for (int i = 0; i < pEvent->modCount; i++) {
303 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
304 free(pEvent->mods[i].classMatch.classPattern);
305 pEvent->mods[i].classMatch.classPattern = NULL;
306 }
307 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
308 free(pEvent->mods[i].classExclude.classPattern);
309 pEvent->mods[i].classExclude.classPattern = NULL;
310 }
311 }
312
313 free(pEvent);
314}
315
316/*
317 * Allocate storage for matching events. To keep things simple we
318 * use an array with enough storage for the entire list.
319 *
320 * The state->eventLock should be held before calling.
321 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800322static JdwpEvent** AllocMatchList(size_t event_count) {
323 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324}
325
326/*
327 * Run through the list and remove any entries with an expired "count" mod
328 * from the event list, then free the match list.
329 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700330void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
331 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700332
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800333 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700334 JdwpEvent* pEvent = *ppEvent;
335
336 for (int i = 0; i < pEvent->modCount; i++) {
337 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800338 VLOG(jdwp) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800339 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700340 EventFree(pEvent);
341 break;
342 }
343 }
344
345 ppEvent++;
346 }
347
Elliott Hughesf8349362012-06-18 15:00:06 -0700348 delete[] match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349}
350
351/*
352 * Match a string against a "restricted regular expression", which is just
353 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
354 *
355 * ("Restricted name globbing" might have been a better term.)
356 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800357static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800358 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700360 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800361 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700362 return false;
363 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800364 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700365 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800366 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800368 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700369 }
370}
371
372/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 * See if the event's mods match up with the contents of "basket".
374 *
375 * If we find a Count mod before rejecting an event, we decrement it. We
376 * need to do this even if later mods cause us to ignore the event.
377 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700379 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700380 JdwpEventMod* pMod = pEvent->mods;
381
382 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
383 switch (pMod->modKind) {
384 case MK_COUNT:
385 CHECK_GT(pMod->count.count, 0);
386 pMod->count.count--;
387 break;
388 case MK_CONDITIONAL:
389 CHECK(false); // should not be getting these
390 break;
391 case MK_THREAD_ONLY:
392 if (pMod->threadOnly.threadId != basket->threadId) {
393 return false;
394 }
395 break;
396 case MK_CLASS_ONLY:
397 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
398 return false;
399 }
400 break;
401 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800402 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700403 return false;
404 }
405 break;
406 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800407 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408 return false;
409 }
410 break;
411 case MK_LOCATION_ONLY:
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800412 if (pMod->locationOnly.loc != *basket->pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700413 return false;
414 }
415 break;
416 case MK_EXCEPTION_ONLY:
417 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
418 return false;
419 }
420 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
421 return false;
422 }
423 break;
424 case MK_FIELD_ONLY:
425 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
426 return false;
427 }
428 break;
429 case MK_STEP:
430 if (pMod->step.threadId != basket->threadId) {
431 return false;
432 }
433 break;
434 case MK_INSTANCE_ONLY:
435 if (pMod->instanceOnly.objectId != basket->thisPtr) {
436 return false;
437 }
438 break;
439 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800440 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 break;
442 }
443 }
444 return true;
445}
446
447/*
448 * Find all events of type "eventKind" with mods that match up with the
449 * rest of the arguments.
450 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700451 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700452 * so this may be called multiple times for grouped events.
453 *
454 * DO NOT call this multiple times for the same eventKind, as Count mods are
455 * decremented during the scan.
456 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket,
458 JdwpEvent** match_list, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700459 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700460 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700461
Elliott Hughesf8349362012-06-18 15:00:06 -0700462 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700463 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800464 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700465 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700466 (*pMatchCount)++;
467 }
468
469 pEvent = pEvent->next;
470 }
471}
472
473/*
474 * Scan through the list of matches and determine the most severe
475 * suspension policy.
476 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700477static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 JdwpSuspendPolicy policy = SP_NONE;
479
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800480 while (match_count--) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700481 if ((*match_list)->suspend_policy > policy) {
482 policy = (*match_list)->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700484 match_list++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485 }
486
487 return policy;
488}
489
490/*
491 * Three possibilities:
492 * SP_NONE - do nothing
493 * SP_EVENT_THREAD - suspend ourselves
494 * SP_ALL - suspend everybody except JDWP support thread
495 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700497 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
498 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700499 return;
500 }
501
Elliott Hughesf8349362012-06-18 15:00:06 -0700502 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700503 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700505 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 }
507
508 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700509 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800510 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700511 return;
512 }
513
514 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
515 while (true) {
516 pReq->ready = true;
517 Dbg::SuspendSelf();
518 pReq->ready = false;
519
520 /*
521 * The JDWP thread has told us (and possibly all other threads) to
522 * resume. See if it has left anything in our DebugInvokeReq mailbox.
523 */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800524 if (!pReq->invoke_needed_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800525 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700526 break;
527 }
528
529 /* grab this before posting/suspending again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 SetWaitForEventThread(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531
Elliott Hughesd07986f2011-12-06 18:27:45 -0800532 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700533 Dbg::ExecuteMethod(pReq);
534
Elliott Hughes475fc232011-10-25 15:00:35 -0700535 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700536
537 /* clear this before signaling */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800538 pReq->invoke_needed_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800540 VLOG(jdwp) << "invoke complete, signaling and self-suspending";
Ian Rogersc604d732012-10-14 16:09:54 -0700541 Thread* self = Thread::Current();
542 MutexLock mu(self, pReq->lock_);
543 pReq->cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544 }
545}
546
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700547void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
548 ObjectId threadId) {
549 Thread* self = Thread::Current();
550 self->AssertThreadSuspensionIsAllowable();
551 /* send request and possibly suspend ourselves */
552 if (pReq != NULL) {
553 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
554 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
555 if (suspend_policy != SP_NONE) {
556 SetWaitForEventThread(threadId);
557 }
558 EventFinish(pReq);
559 SuspendByPolicy(suspend_policy, thread_self_id);
560 self->TransitionFromSuspendedToRunnable();
561 }
562}
563
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700564/*
565 * Determine if there is a method invocation in progress in the current
566 * thread.
567 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700568 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700569 * state. If set, we're in the process of invoking a method.
570 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800571bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700572 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Elliott Hughesd07986f2011-12-06 18:27:45 -0800573 return pReq->invoke_needed_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700574}
575
576/*
577 * We need the JDWP thread to hold off on doing stuff while we post an
578 * event and then suspend ourselves.
579 *
580 * Call this with a threadId of zero if you just want to wait for the
581 * current thread operation to complete.
582 *
583 * This could go to sleep waiting for another thread, so it's important
584 * that the thread be marked as VMWAIT before calling here.
585 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700586void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587 bool waited = false;
588
589 /* this is held for very brief periods; contention is unlikely */
Ian Rogers81d425b2012-09-27 16:03:43 -0700590 Thread* self = Thread::Current();
591 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700592
593 /*
594 * If another thread is already doing stuff, wait for it. This can
595 * go to sleep indefinitely.
596 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700597 while (event_thread_id_ != 0) {
598 VLOG(jdwp) << StringPrintf("event in progress (%#llx), %#llx sleeping", event_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700599 waited = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700600 event_thread_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700601 }
602
603 if (waited || threadId != 0) {
Elliott Hughes229feb72012-02-23 13:33:29 -0800604 VLOG(jdwp) << StringPrintf("event token grabbed (%#llx)", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605 }
606 if (threadId != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700607 event_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700608 }
609}
610
611/*
612 * Clear the threadId and signal anybody waiting.
613 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700614void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615 /*
616 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
617 * function is called by dvmSuspendSelf(), and the transition back
618 * to RUNNING would confuse it.
619 */
Ian Rogersc604d732012-10-14 16:09:54 -0700620 Thread* self = Thread::Current();
621 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622
Elliott Hughesa21039c2012-06-21 12:09:25 -0700623 CHECK_NE(event_thread_id_, 0U);
624 VLOG(jdwp) << StringPrintf("cleared event token (%#llx)", event_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700625
Elliott Hughesa21039c2012-06-21 12:09:25 -0700626 event_thread_id_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700627
Ian Rogersc604d732012-10-14 16:09:54 -0700628 event_thread_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700629}
630
631
632/*
633 * Prep an event. Allocates storage for the message and leaves space for
634 * the header.
635 */
636static ExpandBuf* eventPrep() {
637 ExpandBuf* pReq = expandBufAlloc();
638 expandBufAddSpace(pReq, kJDWPHeaderLen);
639 return pReq;
640}
641
642/*
643 * Write the header into the buffer and send the packet off to the debugger.
644 *
645 * Takes ownership of "pReq" (currently discards it).
646 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800647void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648 uint8_t* buf = expandBufGetBuffer(pReq);
649
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700650 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800651 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700652 Set1(buf+8, 0); /* flags */
653 Set1(buf+9, kJdwpEventCommandSet);
654 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700655
Elliott Hughes761928d2011-11-16 18:33:03 -0800656 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700657
658 expandBufFree(pReq);
659}
660
661
662/*
663 * Tell the debugger that we have finished initializing. This is always
664 * sent, even if the debugger hasn't requested it.
665 *
666 * This should be sent "before the main thread is started and before
667 * any application code has been executed". The thread ID in the message
668 * must be for the main thread.
669 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700670bool JdwpState::PostVMStart() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700671 JdwpSuspendPolicy suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672 ObjectId threadId = Dbg::GetThreadSelfId();
673
Elliott Hughes376a7a02011-10-24 18:35:55 -0700674 if (options_->suspend) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700675 suspend_policy = SP_ALL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700676 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700677 suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700678 }
679
Elliott Hughes761928d2011-11-16 18:33:03 -0800680 ExpandBuf* pReq = eventPrep();
681 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700682 MutexLock mu(Thread::Current(), event_list_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700683
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800684 VLOG(jdwp) << "EVENT: " << EK_VM_START;
Elliott Hughesf8349362012-06-18 15:00:06 -0700685 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700686
Elliott Hughesf8349362012-06-18 15:00:06 -0700687 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700688 expandBufAdd4BE(pReq, 1);
689
690 expandBufAdd1(pReq, EK_VM_START);
691 expandBufAdd4BE(pReq, 0); /* requestId */
692 expandBufAdd8BE(pReq, threadId);
693 }
694
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700697
698 return true;
699}
700
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701/*
702 * A location of interest has been reached. This handles:
703 * Breakpoint
704 * SingleStep
705 * MethodEntry
706 * MethodExit
707 * These four types must be grouped together in a single response. The
708 * "eventFlags" indicates the type of event(s) that have happened.
709 *
710 * Valid mods:
711 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
712 * LocationOnly (for breakpoint/step only)
713 * Step (for step only)
714 *
715 * Interesting test cases:
716 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
717 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
718 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
719 * - Single-step to a line with a breakpoint. Should get a single
720 * event message with both events in it.
721 */
Jeff Hao579b0242013-11-18 13:16:49 -0800722bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags,
723 const JValue* returnValue) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700725 basket.pLoc = pLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700726 basket.classId = pLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727 basket.thisPtr = thisPtr;
728 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes74847412012-06-20 18:10:21 -0700729 basket.className = Dbg::GetClassName(pLoc->class_id);
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 Hughesa21039c2012-06-21 12:09:25 -0700737 if (basket.threadId == debug_thread_id_) {
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 Hughesf8349362012-06-18 15:00:06 -0700756 JdwpEvent** match_list = NULL;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800757 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700758 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700759 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700760
Elliott Hughes761928d2011-11-16 18:33:03 -0800761 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700762 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700763 match_list = AllocMatchList(event_list_size_);
Elliott Hughes86964332012-02-15 19:37:42 -0800764 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700765 FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700766 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800767 if ((eventFlags & Dbg::kSingleStep) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700768 FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800769 }
770 if ((eventFlags & Dbg::kMethodEntry) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700771 FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800772 }
773 if ((eventFlags & Dbg::kMethodExit) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700774 FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
Jeff Hao579b0242013-11-18 13:16:49 -0800775 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 Hughesa96836a2013-01-17 12:27:49 -0800779 << basket.className << "." << Dbg::GetMethodName(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);
Jeff Hao579b0242013-11-18 13:16:49 -0800794 if (match_list[i]->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
795 Dbg::OutputMethodReturnValue(pLoc->method_id, returnValue, pReq);
796 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800797 }
798 }
799
Elliott Hughesf8349362012-06-18 15:00:06 -0700800 CleanupMatchList(match_list, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800801 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800804 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700805}
806
807/*
808 * A thread is starting or stopping.
809 *
810 * Valid mods:
811 * Count, ThreadOnly
812 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700813bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700814 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
815
816 /*
817 * I don't think this can happen.
818 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800819 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700820 LOG(WARNING) << "Not posting thread change during invoke";
821 return false;
822 }
823
824 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700825 basket.threadId = threadId;
826
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700827 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700828 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800829 int match_count = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -0700830 {
831 // Don't allow the list to be updated while we scan it.
Ian Rogers50b35e22012-10-04 10:09:15 -0700832 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700833 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700834
Elliott Hughes234ab152011-10-26 14:02:26 -0700835 if (start) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700836 FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700837 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700838 FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700839 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700840
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800841 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700842 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -0800843 << StringPrintf("thread=%#llx", basket.threadId) << ")";
Elliott Hughes234ab152011-10-26 14:02:26 -0700844
Elliott Hughesf8349362012-06-18 15:00:06 -0700845 suspend_policy = scanSuspendPolicy(match_list, match_count);
846 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700847
848 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700849 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800850 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700851
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800852 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700853 expandBufAdd1(pReq, match_list[i]->eventKind);
854 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes234ab152011-10-26 14:02:26 -0700855 expandBufAdd8BE(pReq, basket.threadId);
856 }
857 }
858
Elliott Hughesf8349362012-06-18 15:00:06 -0700859 CleanupMatchList(match_list, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700860 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700861
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700862 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700863
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800864 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700865}
866
867/*
868 * Send a polite "VM is dying" message to the debugger.
869 *
870 * Skips the usual "event token" stuff.
871 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700872bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800873 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700874
875 ExpandBuf* pReq = eventPrep();
876 expandBufAdd1(pReq, SP_NONE);
877 expandBufAdd4BE(pReq, 1);
878
879 expandBufAdd1(pReq, EK_VM_DEATH);
880 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800881 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882 return true;
883}
884
885/*
886 * An exception has been thrown. It may or may not have been caught.
887 *
888 * Valid mods:
889 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
890 * ExceptionOnly, InstanceOnly
891 *
892 * The "exceptionId" has not been added to the GC-visible object registry,
893 * because there's a pretty good chance that we're not going to send it
894 * up the debugger.
895 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800896bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes74847412012-06-20 18:10:21 -0700897 ObjectId exceptionId, RefTypeId exceptionClassId,
898 const JdwpLocation* pCatchLoc, ObjectId thisPtr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700900
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700901 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 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700920 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700921 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 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700952 }
953
Elliott Hughesf8349362012-06-18 15:00:06 -0700954 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955 }
956
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800959 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960}
961
962/*
963 * Announce that a class has been loaded.
964 *
965 * Valid mods:
966 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
967 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
969 int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700970 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700972 basket.classId = refTypeId;
973 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800974 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700975
976 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800977 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800978 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979 return false;
980 }
981
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700983 JdwpSuspendPolicy suspend_policy = SP_NONE;
984 int match_count = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -0800985 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700986 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700987 JdwpEvent** match_list = AllocMatchList(event_list_size_);
988 FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800989 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700990 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -0800991 << StringPrintf("thread=%#llx", basket.threadId) << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700992
Elliott Hughesf8349362012-06-18 15:00:06 -0700993 suspend_policy = scanSuspendPolicy(match_list, match_count);
994 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
Elliott Hughesa21039c2012-06-21 12:09:25 -0700996 if (basket.threadId == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800997 /*
998 * JDWP says that, for a class prep in the debugger thread, we
999 * should set threadId to null and if any threads were supposed
1000 * to be suspended then we suspend all other threads.
1001 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001002 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001003 basket.threadId = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001004 if (suspend_policy == SP_EVENT_THREAD) {
1005 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001006 }
1007 }
1008
1009 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001010 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001011 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001012
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001013 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001014 expandBufAdd1(pReq, match_list[i]->eventKind);
1015 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001016 expandBufAdd8BE(pReq, basket.threadId);
1017
1018 expandBufAdd1(pReq, tag);
1019 expandBufAdd8BE(pReq, refTypeId);
1020 expandBufAddUtf8String(pReq, signature);
1021 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001022 }
1023 }
Elliott Hughesf8349362012-06-18 15:00:06 -07001024 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001025 }
1026
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001028
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001029 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001030}
1031
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001032/*
1033 * Send up a chunk of DDM data.
1034 *
1035 * While this takes the form of a JDWP "event", it doesn't interact with
1036 * other debugger traffic, and can't suspend the VM, so we skip all of
1037 * the fun event token gymnastics.
1038 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001039void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001040 uint8_t header[kJDWPHeaderLen + 8];
1041 size_t dataLen = 0;
1042
1043 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001044 CHECK_GT(iov_count, 0);
1045 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046
1047 /*
1048 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1049 * this by creating a new copy of the vector with space for the header.
1050 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001051 std::vector<iovec> wrapiov;
1052 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001053 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001054 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055 dataLen += iov[i].iov_len;
1056 }
1057
1058 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001059 Set4BE(header, sizeof(header) + dataLen);
1060 Set4BE(header+4, NextRequestSerial());
1061 Set1(header+8, 0); /* flags */
1062 Set1(header+9, kJDWPDdmCmdSet);
1063 Set1(header+10, kJDWPDdmCmd);
1064 Set4BE(header+11, type);
1065 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001066
1067 wrapiov[0].iov_base = header;
1068 wrapiov[0].iov_len = sizeof(header);
1069
Ian Rogers15bf2d32012-08-28 17:33:04 -07001070 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1071 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001073 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1074 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001075 for (size_t i = 0; i < kMutatorLock; ++i) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001076 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
1077 safe_to_release_mutator_lock_over_send = false;
1078 break;
1079 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001080 }
1081 }
1082 if (safe_to_release_mutator_lock_over_send) {
1083 // Change state to waiting to allow GC, ... while we're sending.
1084 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001085 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001086 self->TransitionFromSuspendedToRunnable();
1087 } else {
1088 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001089 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001090 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001091}
1092
1093} // namespace JDWP
1094
1095} // namespace art