blob: 4aa7f13254903350c28ccacde19369b90e530b9f [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
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100138static bool NeedsFullDeoptimization(JdwpEventKind eventKind) {
139 switch (eventKind) {
140 case EK_METHOD_ENTRY:
141 case EK_METHOD_EXIT:
142 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
143 case EK_SINGLE_STEP:
144 return true;
145 default:
146 return false;
147 }
148}
149
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150/*
151 * Add an event to the list. Ordering is not important.
152 *
153 * If something prevents the event from being registered, e.g. it's a
154 * single-step request on a thread that doesn't exist, the event will
155 * not be added to the list, and an appropriate error will be returned.
156 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800157JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700158 CHECK(pEvent != NULL);
159 CHECK(pEvent->prev == NULL);
160 CHECK(pEvent->next == NULL);
161
162 /*
163 * If one or more "break"-type mods are used, register them with
164 * the interpreter.
165 */
166 for (int i = 0; i < pEvent->modCount; i++) {
167 const JdwpEventMod* pMod = &pEvent->mods[i];
168 if (pMod->modKind == MK_LOCATION_ONLY) {
169 /* should only be for Breakpoint, Step, and Exception */
170 Dbg::WatchLocation(&pMod->locationOnly.loc);
171 } else if (pMod->modKind == MK_STEP) {
172 /* should only be for EK_SINGLE_STEP; should only be one */
173 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
174 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
Elliott Hughes2435a572012-02-17 16:07:41 -0800175 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
176 if (status != ERR_NONE) {
177 return status;
178 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 } else if (pMod->modKind == MK_FIELD_ONLY) {
180 /* should be for EK_FIELD_ACCESS or EK_FIELD_MODIFICATION */
181 dumpEvent(pEvent); /* TODO - need for field watches */
182 }
183 }
184
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100185 {
186 /*
187 * Add to list.
188 */
189 MutexLock mu(Thread::Current(), event_list_lock_);
190 if (event_list_ != NULL) {
191 pEvent->next = event_list_;
192 event_list_->prev = pEvent;
193 }
194 event_list_ = pEvent;
195 ++event_list_size_;
196
197 /**
198 * Do we need to enable full deoptimization ?
199 */
200 if (NeedsFullDeoptimization(pEvent->eventKind)) {
201 if (full_deoptimization_requests_ == 0) {
202 // This is the first event that needs full deoptimization: enable it.
203 Dbg::EnableFullDeoptimization();
204 }
205 ++full_deoptimization_requests_;
206 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700207 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100208
209 Dbg::ManageDeoptimization();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210
211 return ERR_NONE;
212}
213
214/*
215 * Remove an event from the list. This will also remove the event from
216 * any optimization tables, e.g. breakpoints.
217 *
218 * Does not free the JdwpEvent.
219 *
220 * Grab the eventLock before calling here.
221 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800222void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700223 if (pEvent->prev == NULL) {
224 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700225 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226
Elliott Hughesf8349362012-06-18 15:00:06 -0700227 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 } else {
229 pEvent->prev->next = pEvent->next;
230 }
231
232 if (pEvent->next != NULL) {
233 pEvent->next->prev = pEvent->prev;
234 pEvent->next = NULL;
235 }
236 pEvent->prev = NULL;
237
238 /*
239 * Unhook us from the interpreter, if necessary.
240 */
241 for (int i = 0; i < pEvent->modCount; i++) {
242 JdwpEventMod* pMod = &pEvent->mods[i];
243 if (pMod->modKind == MK_LOCATION_ONLY) {
244 /* should only be for Breakpoint, Step, and Exception */
245 Dbg::UnwatchLocation(&pMod->locationOnly.loc);
246 }
247 if (pMod->modKind == MK_STEP) {
248 /* should only be for EK_SINGLE_STEP; should only be one */
249 Dbg::UnconfigureStep(pMod->step.threadId);
250 }
251 }
252
Elliott Hughesf8349362012-06-18 15:00:06 -0700253 --event_list_size_;
254 CHECK(event_list_size_ != 0 || event_list_ == NULL);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100255
256 /**
257 * Can we disable full deoptimization ?
258 */
259 if (NeedsFullDeoptimization(pEvent->eventKind)) {
260 --full_deoptimization_requests_;
261 if (full_deoptimization_requests_ == 0) {
262 // We no longer need full deoptimization.
263 Dbg::DisableFullDeoptimization();
264 }
265 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700266}
267
268/*
269 * Remove the event with the given ID from the list.
270 *
271 * Failure to find the event isn't really an error, but it is a little
272 * weird. (It looks like Eclipse will try to be extra careful and will
273 * explicitly remove one-off single-step events.)
274 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800275void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100276 bool found = false;
277 {
278 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700279
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100280 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
281 if (pEvent->requestId == requestId) {
282 found = true;
283 UnregisterEvent(pEvent);
284 EventFree(pEvent);
285 break; /* there can be only one with a given ID */
286 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700287 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700288 }
289
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100290 if (found) {
291 Dbg::ManageDeoptimization();
292 } else {
293 LOG(DEBUG) << StringPrintf("Odd: no match when removing event reqId=0x%04x", requestId);
294 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700295}
296
297/*
298 * Remove all entries from the event list.
299 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800300void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700301 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700302
Elliott Hughesf8349362012-06-18 15:00:06 -0700303 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700304 while (pEvent != NULL) {
305 JdwpEvent* pNextEvent = pEvent->next;
306
Elliott Hughes761928d2011-11-16 18:33:03 -0800307 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308 EventFree(pEvent);
309 pEvent = pNextEvent;
310 }
311
Elliott Hughesf8349362012-06-18 15:00:06 -0700312 event_list_ = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313}
314
315/*
316 * Allocate a JdwpEvent struct with enough space to hold the specified
317 * number of mod records.
318 */
319JdwpEvent* EventAlloc(int numMods) {
320 JdwpEvent* newEvent;
321 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
322 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
323 memset(newEvent, 0, allocSize);
324 return newEvent;
325}
326
327/*
328 * Free a JdwpEvent.
329 *
330 * Do not call this until the event has been removed from the list.
331 */
332void EventFree(JdwpEvent* pEvent) {
333 if (pEvent == NULL) {
334 return;
335 }
336
337 /* make sure it was removed from the list */
338 CHECK(pEvent->prev == NULL);
339 CHECK(pEvent->next == NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700340 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700341
342 /*
343 * Free any hairy bits in the mods.
344 */
345 for (int i = 0; i < pEvent->modCount; i++) {
346 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
347 free(pEvent->mods[i].classMatch.classPattern);
348 pEvent->mods[i].classMatch.classPattern = NULL;
349 }
350 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
351 free(pEvent->mods[i].classExclude.classPattern);
352 pEvent->mods[i].classExclude.classPattern = NULL;
353 }
354 }
355
356 free(pEvent);
357}
358
359/*
360 * Allocate storage for matching events. To keep things simple we
361 * use an array with enough storage for the entire list.
362 *
363 * The state->eventLock should be held before calling.
364 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800365static JdwpEvent** AllocMatchList(size_t event_count) {
366 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367}
368
369/*
370 * Run through the list and remove any entries with an expired "count" mod
371 * from the event list, then free the match list.
372 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700373void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
374 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700375
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800376 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700377 JdwpEvent* pEvent = *ppEvent;
378
379 for (int i = 0; i < pEvent->modCount; i++) {
380 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800381 VLOG(jdwp) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800382 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383 EventFree(pEvent);
384 break;
385 }
386 }
387
388 ppEvent++;
389 }
390
Elliott Hughesf8349362012-06-18 15:00:06 -0700391 delete[] match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700392}
393
394/*
395 * Match a string against a "restricted regular expression", which is just
396 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
397 *
398 * ("Restricted name globbing" might have been a better term.)
399 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800400static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800401 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700402 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700403 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800404 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405 return false;
406 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800407 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800409 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700410 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800411 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 }
413}
414
415/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416 * See if the event's mods match up with the contents of "basket".
417 *
418 * If we find a Count mod before rejecting an event, we decrement it. We
419 * need to do this even if later mods cause us to ignore the event.
420 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700421static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423 JdwpEventMod* pMod = pEvent->mods;
424
425 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
426 switch (pMod->modKind) {
427 case MK_COUNT:
428 CHECK_GT(pMod->count.count, 0);
429 pMod->count.count--;
430 break;
431 case MK_CONDITIONAL:
432 CHECK(false); // should not be getting these
433 break;
434 case MK_THREAD_ONLY:
435 if (pMod->threadOnly.threadId != basket->threadId) {
436 return false;
437 }
438 break;
439 case MK_CLASS_ONLY:
440 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
441 return false;
442 }
443 break;
444 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800445 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700446 return false;
447 }
448 break;
449 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800450 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 return false;
452 }
453 break;
454 case MK_LOCATION_ONLY:
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800455 if (pMod->locationOnly.loc != *basket->pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700456 return false;
457 }
458 break;
459 case MK_EXCEPTION_ONLY:
460 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
461 return false;
462 }
463 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
464 return false;
465 }
466 break;
467 case MK_FIELD_ONLY:
468 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
469 return false;
470 }
471 break;
472 case MK_STEP:
473 if (pMod->step.threadId != basket->threadId) {
474 return false;
475 }
476 break;
477 case MK_INSTANCE_ONLY:
478 if (pMod->instanceOnly.objectId != basket->thisPtr) {
479 return false;
480 }
481 break;
482 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800483 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484 break;
485 }
486 }
487 return true;
488}
489
490/*
491 * Find all events of type "eventKind" with mods that match up with the
492 * rest of the arguments.
493 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700494 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495 * so this may be called multiple times for grouped events.
496 *
497 * DO NOT call this multiple times for the same eventKind, as Count mods are
498 * decremented during the scan.
499 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket,
501 JdwpEvent** match_list, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700503 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504
Elliott Hughesf8349362012-06-18 15:00:06 -0700505 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800507 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700508 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 (*pMatchCount)++;
510 }
511
512 pEvent = pEvent->next;
513 }
514}
515
516/*
517 * Scan through the list of matches and determine the most severe
518 * suspension policy.
519 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700520static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 JdwpSuspendPolicy policy = SP_NONE;
522
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800523 while (match_count--) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700524 if ((*match_list)->suspend_policy > policy) {
525 policy = (*match_list)->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700526 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700527 match_list++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700528 }
529
530 return policy;
531}
532
533/*
534 * Three possibilities:
535 * SP_NONE - do nothing
536 * SP_EVENT_THREAD - suspend ourselves
537 * SP_ALL - suspend everybody except JDWP support thread
538 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700539void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700540 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
541 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700542 return;
543 }
544
Elliott Hughesf8349362012-06-18 15:00:06 -0700545 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700546 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700547 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700548 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700549 }
550
551 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700552 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800553 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700554 return;
555 }
556
557 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
558 while (true) {
559 pReq->ready = true;
560 Dbg::SuspendSelf();
561 pReq->ready = false;
562
563 /*
564 * The JDWP thread has told us (and possibly all other threads) to
565 * resume. See if it has left anything in our DebugInvokeReq mailbox.
566 */
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100567 if (!pReq->invoke_needed) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800568 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700569 break;
570 }
571
572 /* grab this before posting/suspending again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573 SetWaitForEventThread(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700574
Elliott Hughesd07986f2011-12-06 18:27:45 -0800575 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700576 Dbg::ExecuteMethod(pReq);
577
Elliott Hughes475fc232011-10-25 15:00:35 -0700578 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579
580 /* clear this before signaling */
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100581 pReq->invoke_needed = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800583 VLOG(jdwp) << "invoke complete, signaling and self-suspending";
Ian Rogersc604d732012-10-14 16:09:54 -0700584 Thread* self = Thread::Current();
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100585 MutexLock mu(self, pReq->lock);
586 pReq->cond.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587 }
588}
589
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
591 ObjectId threadId) {
592 Thread* self = Thread::Current();
593 self->AssertThreadSuspensionIsAllowable();
594 /* send request and possibly suspend ourselves */
595 if (pReq != NULL) {
596 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
597 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
598 if (suspend_policy != SP_NONE) {
599 SetWaitForEventThread(threadId);
600 }
601 EventFinish(pReq);
602 SuspendByPolicy(suspend_policy, thread_self_id);
603 self->TransitionFromSuspendedToRunnable();
604 }
605}
606
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607/*
608 * Determine if there is a method invocation in progress in the current
609 * thread.
610 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700611 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700612 * state. If set, we're in the process of invoking a method.
613 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800614bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100616 return pReq->invoke_needed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617}
618
619/*
620 * We need the JDWP thread to hold off on doing stuff while we post an
621 * event and then suspend ourselves.
622 *
623 * Call this with a threadId of zero if you just want to wait for the
624 * current thread operation to complete.
625 *
626 * This could go to sleep waiting for another thread, so it's important
627 * that the thread be marked as VMWAIT before calling here.
628 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700629void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700630 bool waited = false;
631
632 /* this is held for very brief periods; contention is unlikely */
Ian Rogers81d425b2012-09-27 16:03:43 -0700633 Thread* self = Thread::Current();
634 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700635
636 /*
637 * If another thread is already doing stuff, wait for it. This can
638 * go to sleep indefinitely.
639 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700640 while (event_thread_id_ != 0) {
641 VLOG(jdwp) << StringPrintf("event in progress (%#llx), %#llx sleeping", event_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700642 waited = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700643 event_thread_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700644 }
645
646 if (waited || threadId != 0) {
Elliott Hughes229feb72012-02-23 13:33:29 -0800647 VLOG(jdwp) << StringPrintf("event token grabbed (%#llx)", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648 }
649 if (threadId != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700650 event_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651 }
652}
653
654/*
655 * Clear the threadId and signal anybody waiting.
656 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700657void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658 /*
659 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
660 * function is called by dvmSuspendSelf(), and the transition back
661 * to RUNNING would confuse it.
662 */
Ian Rogersc604d732012-10-14 16:09:54 -0700663 Thread* self = Thread::Current();
664 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700665
Elliott Hughesa21039c2012-06-21 12:09:25 -0700666 CHECK_NE(event_thread_id_, 0U);
667 VLOG(jdwp) << StringPrintf("cleared event token (%#llx)", event_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700668
Elliott Hughesa21039c2012-06-21 12:09:25 -0700669 event_thread_id_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700670
Ian Rogersc604d732012-10-14 16:09:54 -0700671 event_thread_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672}
673
674
675/*
676 * Prep an event. Allocates storage for the message and leaves space for
677 * the header.
678 */
679static ExpandBuf* eventPrep() {
680 ExpandBuf* pReq = expandBufAlloc();
681 expandBufAddSpace(pReq, kJDWPHeaderLen);
682 return pReq;
683}
684
685/*
686 * Write the header into the buffer and send the packet off to the debugger.
687 *
688 * Takes ownership of "pReq" (currently discards it).
689 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800690void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700691 uint8_t* buf = expandBufGetBuffer(pReq);
692
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700693 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800694 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700695 Set1(buf+8, 0); /* flags */
696 Set1(buf+9, kJdwpEventCommandSet);
697 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698
Elliott Hughes761928d2011-11-16 18:33:03 -0800699 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700700
701 expandBufFree(pReq);
702}
703
704
705/*
706 * Tell the debugger that we have finished initializing. This is always
707 * sent, even if the debugger hasn't requested it.
708 *
709 * This should be sent "before the main thread is started and before
710 * any application code has been executed". The thread ID in the message
711 * must be for the main thread.
712 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700713bool JdwpState::PostVMStart() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700714 JdwpSuspendPolicy suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700715 ObjectId threadId = Dbg::GetThreadSelfId();
716
Elliott Hughes376a7a02011-10-24 18:35:55 -0700717 if (options_->suspend) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700718 suspend_policy = SP_ALL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700719 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700720 suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700721 }
722
Elliott Hughes761928d2011-11-16 18:33:03 -0800723 ExpandBuf* pReq = eventPrep();
724 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700725 MutexLock mu(Thread::Current(), event_list_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800727 VLOG(jdwp) << "EVENT: " << EK_VM_START;
Elliott Hughesf8349362012-06-18 15:00:06 -0700728 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700729
Elliott Hughesf8349362012-06-18 15:00:06 -0700730 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700731 expandBufAdd4BE(pReq, 1);
732
733 expandBufAdd1(pReq, EK_VM_START);
734 expandBufAdd4BE(pReq, 0); /* requestId */
735 expandBufAdd8BE(pReq, threadId);
736 }
737
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100738 Dbg::ManageDeoptimization();
739
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
743 return true;
744}
745
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700746/*
747 * A location of interest has been reached. This handles:
748 * Breakpoint
749 * SingleStep
750 * MethodEntry
751 * MethodExit
752 * These four types must be grouped together in a single response. The
753 * "eventFlags" indicates the type of event(s) that have happened.
754 *
755 * Valid mods:
756 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
757 * LocationOnly (for breakpoint/step only)
758 * Step (for step only)
759 *
760 * Interesting test cases:
761 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
762 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
763 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
764 * - Single-step to a line with a breakpoint. Should get a single
765 * event message with both events in it.
766 */
Jeff Hao579b0242013-11-18 13:16:49 -0800767bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags,
768 const JValue* returnValue) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700769 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700770 basket.pLoc = pLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700771 basket.classId = pLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772 basket.thisPtr = thisPtr;
773 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes74847412012-06-20 18:10:21 -0700774 basket.className = Dbg::GetClassName(pLoc->class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700775
776 /*
777 * On rare occasions we may need to execute interpreted code in the VM
778 * while handling a request from the debugger. Don't fire breakpoints
779 * while doing so. (I don't think we currently do this at all, so
780 * this is mostly paranoia.)
781 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700782 if (basket.threadId == debug_thread_id_) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800783 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700784 return false;
785 }
786
787 /*
788 * The debugger variable display tab may invoke the interpreter to format
789 * complex objects. We want to ignore breakpoints and method entry/exit
790 * traps while working on behalf of the debugger.
791 *
792 * If we don't ignore them, the VM will get hung up, because we'll
793 * suspend on a breakpoint while the debugger is still waiting for its
794 * method invocation to complete.
795 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800796 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800797 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700798 return false;
799 }
800
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800801 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700803 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800804 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700805 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100806 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes86964332012-02-15 19:37:42 -0800807 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700808 FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800810 if ((eventFlags & Dbg::kSingleStep) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700811 FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800812 }
813 if ((eventFlags & Dbg::kMethodEntry) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700814 FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800815 }
816 if ((eventFlags & Dbg::kMethodExit) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700817 FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
Jeff Hao579b0242013-11-18 13:16:49 -0800818 FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800819 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800820 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700821 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughesa96836a2013-01-17 12:27:49 -0800822 << basket.className << "." << Dbg::GetMethodName(pLoc->method_id)
Elliott Hughes229feb72012-02-23 13:33:29 -0800823 << StringPrintf(" thread=%#llx dex_pc=%#llx)", basket.threadId, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824
Elliott Hughesf8349362012-06-18 15:00:06 -0700825 suspend_policy = scanSuspendPolicy(match_list, match_count);
826 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800827
828 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700829 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800830 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800831
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800832 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700833 expandBufAdd1(pReq, match_list[i]->eventKind);
834 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800835 expandBufAdd8BE(pReq, basket.threadId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700836 expandBufAddLocation(pReq, *pLoc);
Jeff Hao579b0242013-11-18 13:16:49 -0800837 if (match_list[i]->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
838 Dbg::OutputMethodReturnValue(pLoc->method_id, returnValue, pReq);
839 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800840 }
841 }
842
Elliott Hughesf8349362012-06-18 15:00:06 -0700843 CleanupMatchList(match_list, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800844 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700845
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100846 Dbg::ManageDeoptimization();
847
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800849 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700850}
851
852/*
853 * A thread is starting or stopping.
854 *
855 * Valid mods:
856 * Count, ThreadOnly
857 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700858bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700859 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
860
861 /*
862 * I don't think this can happen.
863 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800864 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700865 LOG(WARNING) << "Not posting thread change during invoke";
866 return false;
867 }
868
869 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700870 basket.threadId = threadId;
871
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700873 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800874 int match_count = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -0700875 {
876 // Don't allow the list to be updated while we scan it.
Ian Rogers50b35e22012-10-04 10:09:15 -0700877 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700878 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879
Elliott Hughes234ab152011-10-26 14:02:26 -0700880 if (start) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700881 FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700882 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700883 FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700884 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700885
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800886 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700887 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -0800888 << StringPrintf("thread=%#llx", basket.threadId) << ")";
Elliott Hughes234ab152011-10-26 14:02:26 -0700889
Elliott Hughesf8349362012-06-18 15:00:06 -0700890 suspend_policy = scanSuspendPolicy(match_list, match_count);
891 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700892
893 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700894 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800895 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700896
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800897 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700898 expandBufAdd1(pReq, match_list[i]->eventKind);
899 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes234ab152011-10-26 14:02:26 -0700900 expandBufAdd8BE(pReq, basket.threadId);
901 }
902 }
903
Elliott Hughesf8349362012-06-18 15:00:06 -0700904 CleanupMatchList(match_list, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700905 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700906
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100907 Dbg::ManageDeoptimization();
908
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700910
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800911 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912}
913
914/*
915 * Send a polite "VM is dying" message to the debugger.
916 *
917 * Skips the usual "event token" stuff.
918 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700919bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800920 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921
922 ExpandBuf* pReq = eventPrep();
923 expandBufAdd1(pReq, SP_NONE);
924 expandBufAdd4BE(pReq, 1);
925
926 expandBufAdd1(pReq, EK_VM_DEATH);
927 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800928 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700929 return true;
930}
931
932/*
933 * An exception has been thrown. It may or may not have been caught.
934 *
935 * Valid mods:
936 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
937 * ExceptionOnly, InstanceOnly
938 *
939 * The "exceptionId" has not been added to the GC-visible object registry,
940 * because there's a pretty good chance that we're not going to send it
941 * up the debugger.
942 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800943bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes74847412012-06-20 18:10:21 -0700944 ObjectId exceptionId, RefTypeId exceptionClassId,
945 const JdwpLocation* pCatchLoc, ObjectId thisPtr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700946 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700948 basket.pLoc = pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700949 basket.classId = pThrowLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800951 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700952 basket.excepClassId = exceptionClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700953 basket.caught = (pCatchLoc->class_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700954 basket.thisPtr = thisPtr;
955
956 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800957 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800958 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700959 return false;
960 }
961
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800962 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700963 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700964 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800965 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700966 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100967 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700968 FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800969 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700970 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)"
Elliott Hughes0cf74332012-02-23 23:14:00 -0800971 << StringPrintf(" thread=%#llx", basket.threadId)
972 << StringPrintf(" exceptId=%#llx", exceptionId)
Elliott Hughes436e3722012-02-17 20:01:47 -0800973 << " caught=" << basket.caught << ")"
974 << " throw: " << *pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700975 if (pCatchLoc->class_id == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800976 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -0800977 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800978 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800979 }
980
Elliott Hughesf8349362012-06-18 15:00:06 -0700981 suspend_policy = scanSuspendPolicy(match_list, match_count);
982 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800983
984 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700985 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800986 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800987
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800988 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700989 expandBufAdd1(pReq, match_list[i]->eventKind);
990 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800991 expandBufAdd8BE(pReq, basket.threadId);
992
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700993 expandBufAddLocation(pReq, *pThrowLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800994 expandBufAdd1(pReq, JT_OBJECT);
995 expandBufAdd8BE(pReq, exceptionId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700996 expandBufAddLocation(pReq, *pCatchLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800997 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700998 }
999
Elliott Hughesf8349362012-06-18 15:00:06 -07001000 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001 }
1002
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001003 Dbg::ManageDeoptimization();
1004
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001006
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001007 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001008}
1009
1010/*
1011 * Announce that a class has been loaded.
1012 *
1013 * Valid mods:
1014 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1015 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
1017 int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020 basket.classId = refTypeId;
1021 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001022 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023
1024 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001025 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001026 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001027 return false;
1028 }
1029
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001030 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001031 JdwpSuspendPolicy suspend_policy = SP_NONE;
1032 int match_count = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -08001033 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001034 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001035 JdwpEvent** match_list = AllocMatchList(event_list_size_);
1036 FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001037 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001038 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -08001039 << StringPrintf("thread=%#llx", basket.threadId) << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001040
Elliott Hughesf8349362012-06-18 15:00:06 -07001041 suspend_policy = scanSuspendPolicy(match_list, match_count);
1042 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043
Elliott Hughesa21039c2012-06-21 12:09:25 -07001044 if (basket.threadId == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001045 /*
1046 * JDWP says that, for a class prep in the debugger thread, we
1047 * should set threadId to null and if any threads were supposed
1048 * to be suspended then we suspend all other threads.
1049 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001050 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001051 basket.threadId = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001052 if (suspend_policy == SP_EVENT_THREAD) {
1053 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001054 }
1055 }
1056
1057 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001058 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001059 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001060
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001061 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001062 expandBufAdd1(pReq, match_list[i]->eventKind);
1063 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001064 expandBufAdd8BE(pReq, basket.threadId);
1065
1066 expandBufAdd1(pReq, tag);
1067 expandBufAdd8BE(pReq, refTypeId);
1068 expandBufAddUtf8String(pReq, signature);
1069 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001070 }
1071 }
Elliott Hughesf8349362012-06-18 15:00:06 -07001072 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001073 }
1074
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001075 Dbg::ManageDeoptimization();
1076
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001078
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001079 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001080}
1081
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082/*
1083 * Send up a chunk of DDM data.
1084 *
1085 * While this takes the form of a JDWP "event", it doesn't interact with
1086 * other debugger traffic, and can't suspend the VM, so we skip all of
1087 * the fun event token gymnastics.
1088 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001089void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001090 uint8_t header[kJDWPHeaderLen + 8];
1091 size_t dataLen = 0;
1092
1093 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001094 CHECK_GT(iov_count, 0);
1095 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001096
1097 /*
1098 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1099 * this by creating a new copy of the vector with space for the header.
1100 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001101 std::vector<iovec> wrapiov;
1102 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001103 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001104 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001105 dataLen += iov[i].iov_len;
1106 }
1107
1108 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001109 Set4BE(header, sizeof(header) + dataLen);
1110 Set4BE(header+4, NextRequestSerial());
1111 Set1(header+8, 0); /* flags */
1112 Set1(header+9, kJDWPDdmCmdSet);
1113 Set1(header+10, kJDWPDdmCmd);
1114 Set4BE(header+11, type);
1115 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001116
1117 wrapiov[0].iov_base = header;
1118 wrapiov[0].iov_len = sizeof(header);
1119
Ian Rogers15bf2d32012-08-28 17:33:04 -07001120 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1121 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001123 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1124 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001125 for (size_t i = 0; i < kMutatorLock; ++i) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001126 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
1127 safe_to_release_mutator_lock_over_send = false;
1128 break;
1129 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001130 }
1131 }
1132 if (safe_to_release_mutator_lock_over_send) {
1133 // Change state to waiting to allow GC, ... while we're sending.
1134 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001135 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001136 self->TransitionFromSuspendedToRunnable();
1137 } else {
1138 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001139 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001140 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001141}
1142
1143} // namespace JDWP
1144
1145} // namespace art