blob: 12370a470b58e3918492f18ae36bb52b2acc6c1c [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 */
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100166 DeoptimizationRequest req;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167 for (int i = 0; i < pEvent->modCount; i++) {
168 const JdwpEventMod* pMod = &pEvent->mods[i];
169 if (pMod->modKind == MK_LOCATION_ONLY) {
170 /* should only be for Breakpoint, Step, and Exception */
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100171 Dbg::WatchLocation(&pMod->locationOnly.loc, &req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 } else if (pMod->modKind == MK_STEP) {
173 /* should only be for EK_SINGLE_STEP; should only be one */
174 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
175 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
Elliott Hughes2435a572012-02-17 16:07:41 -0800176 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
177 if (status != ERR_NONE) {
178 return status;
179 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 } else if (pMod->modKind == MK_FIELD_ONLY) {
181 /* should be for EK_FIELD_ACCESS or EK_FIELD_MODIFICATION */
182 dumpEvent(pEvent); /* TODO - need for field watches */
183 }
184 }
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100185 if (NeedsFullDeoptimization(pEvent->eventKind)) {
186 CHECK_EQ(req.kind, DeoptimizationRequest::kNothing);
187 CHECK(req.method == nullptr);
188 req.kind = DeoptimizationRequest::kFullDeoptimization;
189 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100191 {
192 /*
193 * Add to list.
194 */
195 MutexLock mu(Thread::Current(), event_list_lock_);
196 if (event_list_ != NULL) {
197 pEvent->next = event_list_;
198 event_list_->prev = pEvent;
199 }
200 event_list_ = pEvent;
201 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100203
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100204 // TODO we can do better job here since we should process only one request: the one we just
205 // created.
206 Dbg::RequestDeoptimization(req);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100207 Dbg::ManageDeoptimization();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208
209 return ERR_NONE;
210}
211
212/*
213 * Remove an event from the list. This will also remove the event from
214 * any optimization tables, e.g. breakpoints.
215 *
216 * Does not free the JdwpEvent.
217 *
218 * Grab the eventLock before calling here.
219 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800220void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221 if (pEvent->prev == NULL) {
222 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700223 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224
Elliott Hughesf8349362012-06-18 15:00:06 -0700225 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 } else {
227 pEvent->prev->next = pEvent->next;
228 }
229
230 if (pEvent->next != NULL) {
231 pEvent->next->prev = pEvent->prev;
232 pEvent->next = NULL;
233 }
234 pEvent->prev = NULL;
235
236 /*
237 * Unhook us from the interpreter, if necessary.
238 */
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100239 DeoptimizationRequest req;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700240 for (int i = 0; i < pEvent->modCount; i++) {
241 JdwpEventMod* pMod = &pEvent->mods[i];
242 if (pMod->modKind == MK_LOCATION_ONLY) {
243 /* should only be for Breakpoint, Step, and Exception */
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100244 Dbg::UnwatchLocation(&pMod->locationOnly.loc, &req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245 }
246 if (pMod->modKind == MK_STEP) {
247 /* should only be for EK_SINGLE_STEP; should only be one */
248 Dbg::UnconfigureStep(pMod->step.threadId);
249 }
250 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100251 if (pEvent->eventKind == EK_SINGLE_STEP) {
252 // Special case for single-steps where we want to avoid the slow pattern deoptimize/undeoptimize
253 // loop between each single-step. In a IDE, this would happens each time the user click on the
254 // "single-step" button. Here we delay the full undeoptimization to the next resume
255 // (VM.Resume or ThreadReference.Resume) or the end of the debugging session (VM.Dispose or
256 // runtime shutdown).
257 // Therefore, in a singles-stepping sequence, only the first single-step will trigger a full
258 // deoptimization and only the last single-step will trigger a full undeoptimization.
259 Dbg::DelayFullUndeoptimization();
260 } else if (NeedsFullDeoptimization(pEvent->eventKind)) {
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100261 CHECK_EQ(req.kind, DeoptimizationRequest::kNothing);
262 CHECK(req.method == nullptr);
263 req.kind = DeoptimizationRequest::kFullUndeoptimization;
264 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265
Elliott Hughesf8349362012-06-18 15:00:06 -0700266 --event_list_size_;
267 CHECK(event_list_size_ != 0 || event_list_ == NULL);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100268
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100269 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270}
271
272/*
273 * Remove the event with the given ID from the list.
274 *
275 * Failure to find the event isn't really an error, but it is a little
276 * weird. (It looks like Eclipse will try to be extra careful and will
277 * explicitly remove one-off single-step events.)
278 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800279void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100280 bool found = false;
281 {
282 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700283
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100284 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
285 if (pEvent->requestId == requestId) {
286 found = true;
287 UnregisterEvent(pEvent);
288 EventFree(pEvent);
289 break; /* there can be only one with a given ID */
290 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700291 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700292 }
293
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100294 if (found) {
295 Dbg::ManageDeoptimization();
296 } else {
297 LOG(DEBUG) << StringPrintf("Odd: no match when removing event reqId=0x%04x", requestId);
298 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700299}
300
301/*
302 * Remove all entries from the event list.
303 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800304void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700305 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306
Elliott Hughesf8349362012-06-18 15:00:06 -0700307 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308 while (pEvent != NULL) {
309 JdwpEvent* pNextEvent = pEvent->next;
310
Elliott Hughes761928d2011-11-16 18:33:03 -0800311 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700312 EventFree(pEvent);
313 pEvent = pNextEvent;
314 }
315
Elliott Hughesf8349362012-06-18 15:00:06 -0700316 event_list_ = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700317}
318
319/*
320 * Allocate a JdwpEvent struct with enough space to hold the specified
321 * number of mod records.
322 */
323JdwpEvent* EventAlloc(int numMods) {
324 JdwpEvent* newEvent;
325 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
326 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
327 memset(newEvent, 0, allocSize);
328 return newEvent;
329}
330
331/*
332 * Free a JdwpEvent.
333 *
334 * Do not call this until the event has been removed from the list.
335 */
336void EventFree(JdwpEvent* pEvent) {
337 if (pEvent == NULL) {
338 return;
339 }
340
341 /* make sure it was removed from the list */
342 CHECK(pEvent->prev == NULL);
343 CHECK(pEvent->next == NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700344 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700345
346 /*
347 * Free any hairy bits in the mods.
348 */
349 for (int i = 0; i < pEvent->modCount; i++) {
350 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
351 free(pEvent->mods[i].classMatch.classPattern);
352 pEvent->mods[i].classMatch.classPattern = NULL;
353 }
354 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
355 free(pEvent->mods[i].classExclude.classPattern);
356 pEvent->mods[i].classExclude.classPattern = NULL;
357 }
358 }
359
360 free(pEvent);
361}
362
363/*
364 * Allocate storage for matching events. To keep things simple we
365 * use an array with enough storage for the entire list.
366 *
367 * The state->eventLock should be held before calling.
368 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800369static JdwpEvent** AllocMatchList(size_t event_count) {
370 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371}
372
373/*
374 * Run through the list and remove any entries with an expired "count" mod
375 * from the event list, then free the match list.
376 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700377void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
378 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800380 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 JdwpEvent* pEvent = *ppEvent;
382
383 for (int i = 0; i < pEvent->modCount; i++) {
384 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800385 VLOG(jdwp) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800386 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700387 EventFree(pEvent);
388 break;
389 }
390 }
391
392 ppEvent++;
393 }
394
Elliott Hughesf8349362012-06-18 15:00:06 -0700395 delete[] match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700396}
397
398/*
399 * Match a string against a "restricted regular expression", which is just
400 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
401 *
402 * ("Restricted name globbing" might have been a better term.)
403 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800404static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800405 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700406 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800408 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700409 return false;
410 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800411 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800413 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800415 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416 }
417}
418
419/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420 * See if the event's mods match up with the contents of "basket".
421 *
422 * If we find a Count mod before rejecting an event, we decrement it. We
423 * need to do this even if later mods cause us to ignore the event.
424 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700425static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427 JdwpEventMod* pMod = pEvent->mods;
428
429 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
430 switch (pMod->modKind) {
431 case MK_COUNT:
432 CHECK_GT(pMod->count.count, 0);
433 pMod->count.count--;
434 break;
435 case MK_CONDITIONAL:
436 CHECK(false); // should not be getting these
437 break;
438 case MK_THREAD_ONLY:
439 if (pMod->threadOnly.threadId != basket->threadId) {
440 return false;
441 }
442 break;
443 case MK_CLASS_ONLY:
444 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
445 return false;
446 }
447 break;
448 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800449 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700450 return false;
451 }
452 break;
453 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800454 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700455 return false;
456 }
457 break;
458 case MK_LOCATION_ONLY:
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800459 if (pMod->locationOnly.loc != *basket->pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700460 return false;
461 }
462 break;
463 case MK_EXCEPTION_ONLY:
464 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
465 return false;
466 }
467 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
468 return false;
469 }
470 break;
471 case MK_FIELD_ONLY:
472 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
473 return false;
474 }
475 break;
476 case MK_STEP:
477 if (pMod->step.threadId != basket->threadId) {
478 return false;
479 }
480 break;
481 case MK_INSTANCE_ONLY:
482 if (pMod->instanceOnly.objectId != basket->thisPtr) {
483 return false;
484 }
485 break;
486 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800487 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488 break;
489 }
490 }
491 return true;
492}
493
494/*
495 * Find all events of type "eventKind" with mods that match up with the
496 * rest of the arguments.
497 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700498 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700499 * so this may be called multiple times for grouped events.
500 *
501 * DO NOT call this multiple times for the same eventKind, as Count mods are
502 * decremented during the scan.
503 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700504void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket,
505 JdwpEvent** match_list, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700507 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508
Elliott Hughesf8349362012-06-18 15:00:06 -0700509 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800511 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700512 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700513 (*pMatchCount)++;
514 }
515
516 pEvent = pEvent->next;
517 }
518}
519
520/*
521 * Scan through the list of matches and determine the most severe
522 * suspension policy.
523 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700524static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525 JdwpSuspendPolicy policy = SP_NONE;
526
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800527 while (match_count--) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700528 if ((*match_list)->suspend_policy > policy) {
529 policy = (*match_list)->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700530 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700531 match_list++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700532 }
533
534 return policy;
535}
536
537/*
538 * Three possibilities:
539 * SP_NONE - do nothing
540 * SP_EVENT_THREAD - suspend ourselves
541 * SP_ALL - suspend everybody except JDWP support thread
542 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700544 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
545 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700546 return;
547 }
548
Elliott Hughesf8349362012-06-18 15:00:06 -0700549 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700550 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700551 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700552 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553 }
554
555 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800557 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700558 return;
559 }
560
561 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
562 while (true) {
563 pReq->ready = true;
564 Dbg::SuspendSelf();
565 pReq->ready = false;
566
567 /*
568 * The JDWP thread has told us (and possibly all other threads) to
569 * resume. See if it has left anything in our DebugInvokeReq mailbox.
570 */
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100571 if (!pReq->invoke_needed) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800572 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573 break;
574 }
575
576 /* grab this before posting/suspending again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577 SetWaitForEventThread(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700578
Elliott Hughesd07986f2011-12-06 18:27:45 -0800579 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580 Dbg::ExecuteMethod(pReq);
581
Elliott Hughes475fc232011-10-25 15:00:35 -0700582 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700583 }
584}
585
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700586void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
587 ObjectId threadId) {
588 Thread* self = Thread::Current();
589 self->AssertThreadSuspensionIsAllowable();
590 /* send request and possibly suspend ourselves */
591 if (pReq != NULL) {
592 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
593 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
594 if (suspend_policy != SP_NONE) {
595 SetWaitForEventThread(threadId);
596 }
597 EventFinish(pReq);
598 SuspendByPolicy(suspend_policy, thread_self_id);
599 self->TransitionFromSuspendedToRunnable();
600 }
601}
602
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700603/*
604 * Determine if there is a method invocation in progress in the current
605 * thread.
606 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700607 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700608 * state. If set, we're in the process of invoking a method.
609 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800610bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100612 return pReq->invoke_needed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613}
614
615/*
616 * We need the JDWP thread to hold off on doing stuff while we post an
617 * event and then suspend ourselves.
618 *
619 * Call this with a threadId of zero if you just want to wait for the
620 * current thread operation to complete.
621 *
622 * This could go to sleep waiting for another thread, so it's important
623 * that the thread be marked as VMWAIT before calling here.
624 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700625void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700626 bool waited = false;
627
628 /* this is held for very brief periods; contention is unlikely */
Ian Rogers81d425b2012-09-27 16:03:43 -0700629 Thread* self = Thread::Current();
630 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700631
632 /*
633 * If another thread is already doing stuff, wait for it. This can
634 * go to sleep indefinitely.
635 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700636 while (event_thread_id_ != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800637 VLOG(jdwp) << StringPrintf("event in progress (%#" PRIx64 "), %#" PRIx64 " sleeping",
638 event_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700639 waited = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700640 event_thread_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700641 }
642
643 if (waited || threadId != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800644 VLOG(jdwp) << StringPrintf("event token grabbed (%#" PRIx64 ")", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700645 }
646 if (threadId != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700647 event_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648 }
649}
650
651/*
652 * Clear the threadId and signal anybody waiting.
653 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700654void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700655 /*
656 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
657 * function is called by dvmSuspendSelf(), and the transition back
658 * to RUNNING would confuse it.
659 */
Ian Rogersc604d732012-10-14 16:09:54 -0700660 Thread* self = Thread::Current();
661 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662
Elliott Hughesa21039c2012-06-21 12:09:25 -0700663 CHECK_NE(event_thread_id_, 0U);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800664 VLOG(jdwp) << StringPrintf("cleared event token (%#" PRIx64 ")", event_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700665
Elliott Hughesa21039c2012-06-21 12:09:25 -0700666 event_thread_id_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700667
Ian Rogersc604d732012-10-14 16:09:54 -0700668 event_thread_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700669}
670
671
672/*
673 * Prep an event. Allocates storage for the message and leaves space for
674 * the header.
675 */
676static ExpandBuf* eventPrep() {
677 ExpandBuf* pReq = expandBufAlloc();
678 expandBufAddSpace(pReq, kJDWPHeaderLen);
679 return pReq;
680}
681
682/*
683 * Write the header into the buffer and send the packet off to the debugger.
684 *
685 * Takes ownership of "pReq" (currently discards it).
686 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800687void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700688 uint8_t* buf = expandBufGetBuffer(pReq);
689
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700690 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800691 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700692 Set1(buf+8, 0); /* flags */
693 Set1(buf+9, kJdwpEventCommandSet);
694 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695
Sebastien Hertz99660e12014-02-19 15:04:42 +0100696 // Prevents from interleaving commands and events. Otherwise we could end up in sending an event
697 // before sending the reply of the command being processed and would lead to bad synchronization
698 // between the debugger and the debuggee.
699 WaitForProcessingRequest();
700
Elliott Hughes761928d2011-11-16 18:33:03 -0800701 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700702
703 expandBufFree(pReq);
704}
705
706
707/*
708 * Tell the debugger that we have finished initializing. This is always
709 * sent, even if the debugger hasn't requested it.
710 *
711 * This should be sent "before the main thread is started and before
712 * any application code has been executed". The thread ID in the message
713 * must be for the main thread.
714 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700715bool JdwpState::PostVMStart() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700716 JdwpSuspendPolicy suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700717 ObjectId threadId = Dbg::GetThreadSelfId();
718
Elliott Hughes376a7a02011-10-24 18:35:55 -0700719 if (options_->suspend) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700720 suspend_policy = SP_ALL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700721 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700722 suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700723 }
724
Elliott Hughes761928d2011-11-16 18:33:03 -0800725 ExpandBuf* pReq = eventPrep();
726 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700727 MutexLock mu(Thread::Current(), event_list_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700728
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800729 VLOG(jdwp) << "EVENT: " << EK_VM_START;
Elliott Hughesf8349362012-06-18 15:00:06 -0700730 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700731
Elliott Hughesf8349362012-06-18 15:00:06 -0700732 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700733 expandBufAdd4BE(pReq, 1);
734
735 expandBufAdd1(pReq, EK_VM_START);
736 expandBufAdd4BE(pReq, 0); /* requestId */
737 expandBufAdd8BE(pReq, threadId);
738 }
739
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100740 Dbg::ManageDeoptimization();
741
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744
745 return true;
746}
747
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700748/*
749 * A location of interest has been reached. This handles:
750 * Breakpoint
751 * SingleStep
752 * MethodEntry
753 * MethodExit
754 * These four types must be grouped together in a single response. The
755 * "eventFlags" indicates the type of event(s) that have happened.
756 *
757 * Valid mods:
758 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
759 * LocationOnly (for breakpoint/step only)
760 * Step (for step only)
761 *
762 * Interesting test cases:
763 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
764 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
765 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
766 * - Single-step to a line with a breakpoint. Should get a single
767 * event message with both events in it.
768 */
Jeff Hao579b0242013-11-18 13:16:49 -0800769bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags,
770 const JValue* returnValue) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700771 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772 basket.pLoc = pLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700773 basket.classId = pLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700774 basket.thisPtr = thisPtr;
775 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes74847412012-06-20 18:10:21 -0700776 basket.className = Dbg::GetClassName(pLoc->class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777
778 /*
779 * On rare occasions we may need to execute interpreted code in the VM
780 * while handling a request from the debugger. Don't fire breakpoints
781 * while doing so. (I don't think we currently do this at all, so
782 * this is mostly paranoia.)
783 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700784 if (basket.threadId == debug_thread_id_) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800785 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700786 return false;
787 }
788
789 /*
790 * The debugger variable display tab may invoke the interpreter to format
791 * complex objects. We want to ignore breakpoints and method entry/exit
792 * traps while working on behalf of the debugger.
793 *
794 * If we don't ignore them, the VM will get hung up, because we'll
795 * suspend on a breakpoint while the debugger is still waiting for its
796 * method invocation to complete.
797 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800798 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800799 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700800 return false;
801 }
802
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800803 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700804 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700805 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800806 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700807 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100808 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes86964332012-02-15 19:37:42 -0800809 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700810 FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700811 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800812 if ((eventFlags & Dbg::kSingleStep) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700813 FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800814 }
815 if ((eventFlags & Dbg::kMethodEntry) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700816 FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800817 }
818 if ((eventFlags & Dbg::kMethodExit) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700819 FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
Jeff Hao579b0242013-11-18 13:16:49 -0800820 FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800821 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800822 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700823 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughesa96836a2013-01-17 12:27:49 -0800824 << basket.className << "." << Dbg::GetMethodName(pLoc->method_id)
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800825 << StringPrintf(" thread=%#" PRIx64 " dex_pc=%#" PRIx64 ")",
826 basket.threadId, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700827
Elliott Hughesf8349362012-06-18 15:00:06 -0700828 suspend_policy = scanSuspendPolicy(match_list, match_count);
829 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800830
831 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700832 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800833 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800834
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800835 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700836 expandBufAdd1(pReq, match_list[i]->eventKind);
837 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800838 expandBufAdd8BE(pReq, basket.threadId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700839 expandBufAddLocation(pReq, *pLoc);
Jeff Hao579b0242013-11-18 13:16:49 -0800840 if (match_list[i]->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
841 Dbg::OutputMethodReturnValue(pLoc->method_id, returnValue, pReq);
842 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800843 }
844 }
845
Elliott Hughesf8349362012-06-18 15:00:06 -0700846 CleanupMatchList(match_list, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800847 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700848
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100849 Dbg::ManageDeoptimization();
850
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800852 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853}
854
855/*
856 * A thread is starting or stopping.
857 *
858 * Valid mods:
859 * Count, ThreadOnly
860 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700861bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700862 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
863
864 /*
865 * I don't think this can happen.
866 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800867 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700868 LOG(WARNING) << "Not posting thread change during invoke";
869 return false;
870 }
871
872 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873 basket.threadId = threadId;
874
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700875 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700876 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800877 int match_count = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -0700878 {
879 // Don't allow the list to be updated while we scan it.
Ian Rogers50b35e22012-10-04 10:09:15 -0700880 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700881 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882
Elliott Hughes234ab152011-10-26 14:02:26 -0700883 if (start) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700884 FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700885 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700886 FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700887 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700888
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800889 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700890 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800891 << StringPrintf("thread=%#" PRIx64, basket.threadId) << ")";
Elliott Hughes234ab152011-10-26 14:02:26 -0700892
Elliott Hughesf8349362012-06-18 15:00:06 -0700893 suspend_policy = scanSuspendPolicy(match_list, match_count);
894 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700895
896 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700897 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800898 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700899
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800900 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700901 expandBufAdd1(pReq, match_list[i]->eventKind);
902 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes234ab152011-10-26 14:02:26 -0700903 expandBufAdd8BE(pReq, basket.threadId);
904 }
905 }
906
Elliott Hughesf8349362012-06-18 15:00:06 -0700907 CleanupMatchList(match_list, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700908 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700909
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100910 Dbg::ManageDeoptimization();
911
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700913
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800914 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700915}
916
917/*
918 * Send a polite "VM is dying" message to the debugger.
919 *
920 * Skips the usual "event token" stuff.
921 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700922bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800923 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700924
925 ExpandBuf* pReq = eventPrep();
926 expandBufAdd1(pReq, SP_NONE);
927 expandBufAdd4BE(pReq, 1);
928
929 expandBufAdd1(pReq, EK_VM_DEATH);
930 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800931 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700932 return true;
933}
934
935/*
936 * An exception has been thrown. It may or may not have been caught.
937 *
938 * Valid mods:
939 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
940 * ExceptionOnly, InstanceOnly
941 *
942 * The "exceptionId" has not been added to the GC-visible object registry,
943 * because there's a pretty good chance that we're not going to send it
944 * up the debugger.
945 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800946bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes74847412012-06-20 18:10:21 -0700947 ObjectId exceptionId, RefTypeId exceptionClassId,
948 const JdwpLocation* pCatchLoc, ObjectId thisPtr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700951 basket.pLoc = pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700952 basket.classId = pThrowLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700953 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800954 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955 basket.excepClassId = exceptionClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700956 basket.caught = (pCatchLoc->class_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700957 basket.thisPtr = thisPtr;
958
959 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800960 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800961 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700962 return false;
963 }
964
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800965 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700967 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800968 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700969 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100970 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700971 FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800972 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700973 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)"
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800974 << StringPrintf(" thread=%#" PRIx64, basket.threadId)
975 << StringPrintf(" exceptId=%#" PRIx64, exceptionId)
Elliott Hughes436e3722012-02-17 20:01:47 -0800976 << " caught=" << basket.caught << ")"
977 << " throw: " << *pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700978 if (pCatchLoc->class_id == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800979 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -0800980 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800981 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800982 }
983
Elliott Hughesf8349362012-06-18 15:00:06 -0700984 suspend_policy = scanSuspendPolicy(match_list, match_count);
985 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800986
987 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700988 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800989 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800990
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800991 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700992 expandBufAdd1(pReq, match_list[i]->eventKind);
993 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800994 expandBufAdd8BE(pReq, basket.threadId);
995
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700996 expandBufAddLocation(pReq, *pThrowLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800997 expandBufAdd1(pReq, JT_OBJECT);
998 expandBufAdd8BE(pReq, exceptionId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700999 expandBufAddLocation(pReq, *pCatchLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -08001000 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001 }
1002
Elliott Hughesf8349362012-06-18 15:00:06 -07001003 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004 }
1005
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001006 Dbg::ManageDeoptimization();
1007
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001009
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001010 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001011}
1012
1013/*
1014 * Announce that a class has been loaded.
1015 *
1016 * Valid mods:
1017 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1018 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
1020 int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001021 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001022
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023 basket.classId = refTypeId;
1024 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001025 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001026
1027 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001028 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001029 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001030 return false;
1031 }
1032
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001033 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001034 JdwpSuspendPolicy suspend_policy = SP_NONE;
1035 int match_count = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -08001036 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001037 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001038 JdwpEvent** match_list = AllocMatchList(event_list_size_);
1039 FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001040 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001041 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001042 << StringPrintf("thread=%#" PRIx64, basket.threadId) << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043
Elliott Hughesf8349362012-06-18 15:00:06 -07001044 suspend_policy = scanSuspendPolicy(match_list, match_count);
1045 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046
Elliott Hughesa21039c2012-06-21 12:09:25 -07001047 if (basket.threadId == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001048 /*
1049 * JDWP says that, for a class prep in the debugger thread, we
1050 * should set threadId to null and if any threads were supposed
1051 * to be suspended then we suspend all other threads.
1052 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001053 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001054 basket.threadId = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001055 if (suspend_policy == SP_EVENT_THREAD) {
1056 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001057 }
1058 }
1059
1060 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001061 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001062 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001063
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001064 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001065 expandBufAdd1(pReq, match_list[i]->eventKind);
1066 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001067 expandBufAdd8BE(pReq, basket.threadId);
1068
1069 expandBufAdd1(pReq, tag);
1070 expandBufAdd8BE(pReq, refTypeId);
1071 expandBufAddUtf8String(pReq, signature);
1072 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001073 }
1074 }
Elliott Hughesf8349362012-06-18 15:00:06 -07001075 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001076 }
1077
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001078 Dbg::ManageDeoptimization();
1079
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001081
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001082 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001083}
1084
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001085/*
1086 * Send up a chunk of DDM data.
1087 *
1088 * While this takes the form of a JDWP "event", it doesn't interact with
1089 * other debugger traffic, and can't suspend the VM, so we skip all of
1090 * the fun event token gymnastics.
1091 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001092void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001093 uint8_t header[kJDWPHeaderLen + 8];
1094 size_t dataLen = 0;
1095
1096 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001097 CHECK_GT(iov_count, 0);
1098 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099
1100 /*
1101 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1102 * this by creating a new copy of the vector with space for the header.
1103 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001104 std::vector<iovec> wrapiov;
1105 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001106 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001107 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001108 dataLen += iov[i].iov_len;
1109 }
1110
1111 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001112 Set4BE(header, sizeof(header) + dataLen);
1113 Set4BE(header+4, NextRequestSerial());
1114 Set1(header+8, 0); /* flags */
1115 Set1(header+9, kJDWPDdmCmdSet);
1116 Set1(header+10, kJDWPDdmCmd);
1117 Set4BE(header+11, type);
1118 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001119
1120 wrapiov[0].iov_base = header;
1121 wrapiov[0].iov_len = sizeof(header);
1122
Ian Rogers15bf2d32012-08-28 17:33:04 -07001123 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1124 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001126 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1127 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001128 for (size_t i = 0; i < kMutatorLock; ++i) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001129 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
1130 safe_to_release_mutator_lock_over_send = false;
1131 break;
1132 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001133 }
1134 }
1135 if (safe_to_release_mutator_lock_over_send) {
1136 // Change state to waiting to allow GC, ... while we're sending.
1137 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001138 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001139 self->TransitionFromSuspendedToRunnable();
1140 } else {
1141 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001142 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001143 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001144}
1145
1146} // namespace JDWP
1147
1148} // namespace art