blob: 9b3ea2e6c788f9a784e9237d6c5c9619a9cb9d99 [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 Hertz4d25df32014-03-21 17:44:46 +0100251 if (NeedsFullDeoptimization(pEvent->eventKind)) {
252 CHECK_EQ(req.kind, DeoptimizationRequest::kNothing);
253 CHECK(req.method == nullptr);
254 req.kind = DeoptimizationRequest::kFullUndeoptimization;
255 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700256
Elliott Hughesf8349362012-06-18 15:00:06 -0700257 --event_list_size_;
258 CHECK(event_list_size_ != 0 || event_list_ == NULL);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100259
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100260 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261}
262
263/*
264 * Remove the event with the given ID from the list.
265 *
266 * Failure to find the event isn't really an error, but it is a little
267 * weird. (It looks like Eclipse will try to be extra careful and will
268 * explicitly remove one-off single-step events.)
269 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800270void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100271 bool found = false;
272 {
273 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700274
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100275 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
276 if (pEvent->requestId == requestId) {
277 found = true;
278 UnregisterEvent(pEvent);
279 EventFree(pEvent);
280 break; /* there can be only one with a given ID */
281 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700283 }
284
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100285 if (found) {
286 Dbg::ManageDeoptimization();
287 } else {
288 LOG(DEBUG) << StringPrintf("Odd: no match when removing event reqId=0x%04x", requestId);
289 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290}
291
292/*
293 * Remove all entries from the event list.
294 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800295void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700296 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297
Elliott Hughesf8349362012-06-18 15:00:06 -0700298 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700299 while (pEvent != NULL) {
300 JdwpEvent* pNextEvent = pEvent->next;
301
Elliott Hughes761928d2011-11-16 18:33:03 -0800302 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303 EventFree(pEvent);
304 pEvent = pNextEvent;
305 }
306
Elliott Hughesf8349362012-06-18 15:00:06 -0700307 event_list_ = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308}
309
310/*
311 * Allocate a JdwpEvent struct with enough space to hold the specified
312 * number of mod records.
313 */
314JdwpEvent* EventAlloc(int numMods) {
315 JdwpEvent* newEvent;
316 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
317 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
318 memset(newEvent, 0, allocSize);
319 return newEvent;
320}
321
322/*
323 * Free a JdwpEvent.
324 *
325 * Do not call this until the event has been removed from the list.
326 */
327void EventFree(JdwpEvent* pEvent) {
328 if (pEvent == NULL) {
329 return;
330 }
331
332 /* make sure it was removed from the list */
333 CHECK(pEvent->prev == NULL);
334 CHECK(pEvent->next == NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700335 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336
337 /*
338 * Free any hairy bits in the mods.
339 */
340 for (int i = 0; i < pEvent->modCount; i++) {
341 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
342 free(pEvent->mods[i].classMatch.classPattern);
343 pEvent->mods[i].classMatch.classPattern = NULL;
344 }
345 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
346 free(pEvent->mods[i].classExclude.classPattern);
347 pEvent->mods[i].classExclude.classPattern = NULL;
348 }
349 }
350
351 free(pEvent);
352}
353
354/*
355 * Allocate storage for matching events. To keep things simple we
356 * use an array with enough storage for the entire list.
357 *
358 * The state->eventLock should be held before calling.
359 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800360static JdwpEvent** AllocMatchList(size_t event_count) {
361 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700362}
363
364/*
365 * Run through the list and remove any entries with an expired "count" mod
366 * from the event list, then free the match list.
367 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700368void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
369 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700370
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800371 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700372 JdwpEvent* pEvent = *ppEvent;
373
374 for (int i = 0; i < pEvent->modCount; i++) {
375 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800376 VLOG(jdwp) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800377 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700378 EventFree(pEvent);
379 break;
380 }
381 }
382
383 ppEvent++;
384 }
385
Elliott Hughesf8349362012-06-18 15:00:06 -0700386 delete[] match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700387}
388
389/*
390 * Match a string against a "restricted regular expression", which is just
391 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
392 *
393 * ("Restricted name globbing" might have been a better term.)
394 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800395static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800396 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700397 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700398 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800399 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700400 return false;
401 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800402 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700403 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800404 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800406 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407 }
408}
409
410/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700411 * See if the event's mods match up with the contents of "basket".
412 *
413 * If we find a Count mod before rejecting an event, we decrement it. We
414 * need to do this even if later mods cause us to ignore the event.
415 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700417 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700418 JdwpEventMod* pMod = pEvent->mods;
419
420 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
421 switch (pMod->modKind) {
422 case MK_COUNT:
423 CHECK_GT(pMod->count.count, 0);
424 pMod->count.count--;
425 break;
426 case MK_CONDITIONAL:
427 CHECK(false); // should not be getting these
428 break;
429 case MK_THREAD_ONLY:
430 if (pMod->threadOnly.threadId != basket->threadId) {
431 return false;
432 }
433 break;
434 case MK_CLASS_ONLY:
435 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
436 return false;
437 }
438 break;
439 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800440 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 return false;
442 }
443 break;
444 case MK_CLASS_EXCLUDE:
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_LOCATION_ONLY:
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800450 if (pMod->locationOnly.loc != *basket->pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 return false;
452 }
453 break;
454 case MK_EXCEPTION_ONLY:
455 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
456 return false;
457 }
458 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
459 return false;
460 }
461 break;
462 case MK_FIELD_ONLY:
463 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
464 return false;
465 }
466 break;
467 case MK_STEP:
468 if (pMod->step.threadId != basket->threadId) {
469 return false;
470 }
471 break;
472 case MK_INSTANCE_ONLY:
473 if (pMod->instanceOnly.objectId != basket->thisPtr) {
474 return false;
475 }
476 break;
477 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800478 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 break;
480 }
481 }
482 return true;
483}
484
485/*
486 * Find all events of type "eventKind" with mods that match up with the
487 * rest of the arguments.
488 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700489 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700490 * so this may be called multiple times for grouped events.
491 *
492 * DO NOT call this multiple times for the same eventKind, as Count mods are
493 * decremented during the scan.
494 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700495void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket,
496 JdwpEvent** match_list, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700498 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700499
Elliott Hughesf8349362012-06-18 15:00:06 -0700500 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700501 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800502 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700503 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 (*pMatchCount)++;
505 }
506
507 pEvent = pEvent->next;
508 }
509}
510
511/*
512 * Scan through the list of matches and determine the most severe
513 * suspension policy.
514 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700515static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516 JdwpSuspendPolicy policy = SP_NONE;
517
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800518 while (match_count--) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700519 if ((*match_list)->suspend_policy > policy) {
520 policy = (*match_list)->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700522 match_list++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523 }
524
525 return policy;
526}
527
528/*
529 * Three possibilities:
530 * SP_NONE - do nothing
531 * SP_EVENT_THREAD - suspend ourselves
532 * SP_ALL - suspend everybody except JDWP support thread
533 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700534void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700535 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
536 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537 return;
538 }
539
Elliott Hughesf8349362012-06-18 15:00:06 -0700540 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700541 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700542 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700543 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544 }
545
546 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700547 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800548 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700549 return;
550 }
551
552 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
553 while (true) {
554 pReq->ready = true;
555 Dbg::SuspendSelf();
556 pReq->ready = false;
557
558 /*
559 * The JDWP thread has told us (and possibly all other threads) to
560 * resume. See if it has left anything in our DebugInvokeReq mailbox.
561 */
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100562 if (!pReq->invoke_needed) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800563 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700564 break;
565 }
566
567 /* grab this before posting/suspending again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700568 SetWaitForEventThread(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700569
Elliott Hughesd07986f2011-12-06 18:27:45 -0800570 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571 Dbg::ExecuteMethod(pReq);
572
Elliott Hughes475fc232011-10-25 15:00:35 -0700573 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700574 }
575}
576
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
578 ObjectId threadId) {
579 Thread* self = Thread::Current();
580 self->AssertThreadSuspensionIsAllowable();
581 /* send request and possibly suspend ourselves */
582 if (pReq != NULL) {
583 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
584 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
585 if (suspend_policy != SP_NONE) {
586 SetWaitForEventThread(threadId);
587 }
588 EventFinish(pReq);
589 SuspendByPolicy(suspend_policy, thread_self_id);
590 self->TransitionFromSuspendedToRunnable();
591 }
592}
593
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700594/*
595 * Determine if there is a method invocation in progress in the current
596 * thread.
597 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700598 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700599 * state. If set, we're in the process of invoking a method.
600 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800601bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700602 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100603 return pReq->invoke_needed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604}
605
606/*
607 * We need the JDWP thread to hold off on doing stuff while we post an
608 * event and then suspend ourselves.
609 *
610 * Call this with a threadId of zero if you just want to wait for the
611 * current thread operation to complete.
612 *
613 * This could go to sleep waiting for another thread, so it's important
614 * that the thread be marked as VMWAIT before calling here.
615 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700616void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617 bool waited = false;
618
619 /* this is held for very brief periods; contention is unlikely */
Ian Rogers81d425b2012-09-27 16:03:43 -0700620 Thread* self = Thread::Current();
621 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622
623 /*
624 * If another thread is already doing stuff, wait for it. This can
625 * go to sleep indefinitely.
626 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700627 while (event_thread_id_ != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800628 VLOG(jdwp) << StringPrintf("event in progress (%#" PRIx64 "), %#" PRIx64 " sleeping",
629 event_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700630 waited = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700631 event_thread_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700632 }
633
634 if (waited || threadId != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800635 VLOG(jdwp) << StringPrintf("event token grabbed (%#" PRIx64 ")", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700636 }
637 if (threadId != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700638 event_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700639 }
640}
641
642/*
643 * Clear the threadId and signal anybody waiting.
644 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700645void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646 /*
647 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
648 * function is called by dvmSuspendSelf(), and the transition back
649 * to RUNNING would confuse it.
650 */
Ian Rogersc604d732012-10-14 16:09:54 -0700651 Thread* self = Thread::Current();
652 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700653
Elliott Hughesa21039c2012-06-21 12:09:25 -0700654 CHECK_NE(event_thread_id_, 0U);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800655 VLOG(jdwp) << StringPrintf("cleared event token (%#" PRIx64 ")", event_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700656
Elliott Hughesa21039c2012-06-21 12:09:25 -0700657 event_thread_id_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658
Ian Rogersc604d732012-10-14 16:09:54 -0700659 event_thread_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700660}
661
662
663/*
664 * Prep an event. Allocates storage for the message and leaves space for
665 * the header.
666 */
667static ExpandBuf* eventPrep() {
668 ExpandBuf* pReq = expandBufAlloc();
669 expandBufAddSpace(pReq, kJDWPHeaderLen);
670 return pReq;
671}
672
673/*
674 * Write the header into the buffer and send the packet off to the debugger.
675 *
676 * Takes ownership of "pReq" (currently discards it).
677 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800678void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679 uint8_t* buf = expandBufGetBuffer(pReq);
680
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700681 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800682 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700683 Set1(buf+8, 0); /* flags */
684 Set1(buf+9, kJdwpEventCommandSet);
685 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700686
Sebastien Hertz99660e12014-02-19 15:04:42 +0100687 // Prevents from interleaving commands and events. Otherwise we could end up in sending an event
688 // before sending the reply of the command being processed and would lead to bad synchronization
689 // between the debugger and the debuggee.
690 WaitForProcessingRequest();
691
Elliott Hughes761928d2011-11-16 18:33:03 -0800692 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700693
694 expandBufFree(pReq);
695}
696
697
698/*
699 * Tell the debugger that we have finished initializing. This is always
700 * sent, even if the debugger hasn't requested it.
701 *
702 * This should be sent "before the main thread is started and before
703 * any application code has been executed". The thread ID in the message
704 * must be for the main thread.
705 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700706bool JdwpState::PostVMStart() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700707 JdwpSuspendPolicy suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700708 ObjectId threadId = Dbg::GetThreadSelfId();
709
Elliott Hughes376a7a02011-10-24 18:35:55 -0700710 if (options_->suspend) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700711 suspend_policy = SP_ALL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700712 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700713 suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700714 }
715
Elliott Hughes761928d2011-11-16 18:33:03 -0800716 ExpandBuf* pReq = eventPrep();
717 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700718 MutexLock mu(Thread::Current(), event_list_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700719
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800720 VLOG(jdwp) << "EVENT: " << EK_VM_START;
Elliott Hughesf8349362012-06-18 15:00:06 -0700721 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700722
Elliott Hughesf8349362012-06-18 15:00:06 -0700723 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724 expandBufAdd4BE(pReq, 1);
725
726 expandBufAdd1(pReq, EK_VM_START);
727 expandBufAdd4BE(pReq, 0); /* requestId */
728 expandBufAdd8BE(pReq, threadId);
729 }
730
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100731 Dbg::ManageDeoptimization();
732
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700733 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700735
736 return true;
737}
738
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700739/*
740 * A location of interest has been reached. This handles:
741 * Breakpoint
742 * SingleStep
743 * MethodEntry
744 * MethodExit
745 * These four types must be grouped together in a single response. The
746 * "eventFlags" indicates the type of event(s) that have happened.
747 *
748 * Valid mods:
749 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
750 * LocationOnly (for breakpoint/step only)
751 * Step (for step only)
752 *
753 * Interesting test cases:
754 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
755 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
756 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
757 * - Single-step to a line with a breakpoint. Should get a single
758 * event message with both events in it.
759 */
Jeff Hao579b0242013-11-18 13:16:49 -0800760bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags,
761 const JValue* returnValue) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700762 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700763 basket.pLoc = pLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700764 basket.classId = pLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700765 basket.thisPtr = thisPtr;
766 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes74847412012-06-20 18:10:21 -0700767 basket.className = Dbg::GetClassName(pLoc->class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700768
769 /*
770 * On rare occasions we may need to execute interpreted code in the VM
771 * while handling a request from the debugger. Don't fire breakpoints
772 * while doing so. (I don't think we currently do this at all, so
773 * this is mostly paranoia.)
774 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700775 if (basket.threadId == debug_thread_id_) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800776 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777 return false;
778 }
779
780 /*
781 * The debugger variable display tab may invoke the interpreter to format
782 * complex objects. We want to ignore breakpoints and method entry/exit
783 * traps while working on behalf of the debugger.
784 *
785 * If we don't ignore them, the VM will get hung up, because we'll
786 * suspend on a breakpoint while the debugger is still waiting for its
787 * method invocation to complete.
788 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800789 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800790 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700791 return false;
792 }
793
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800794 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700796 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800797 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700798 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100799 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes86964332012-02-15 19:37:42 -0800800 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700801 FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800803 if ((eventFlags & Dbg::kSingleStep) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700804 FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800805 }
806 if ((eventFlags & Dbg::kMethodEntry) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700807 FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800808 }
809 if ((eventFlags & Dbg::kMethodExit) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700810 FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
Jeff Hao579b0242013-11-18 13:16:49 -0800811 FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800812 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800813 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700814 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughesa96836a2013-01-17 12:27:49 -0800815 << basket.className << "." << Dbg::GetMethodName(pLoc->method_id)
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800816 << StringPrintf(" thread=%#" PRIx64 " dex_pc=%#" PRIx64 ")",
817 basket.threadId, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818
Elliott Hughesf8349362012-06-18 15:00:06 -0700819 suspend_policy = scanSuspendPolicy(match_list, match_count);
820 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800821
822 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700823 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800824 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800825
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800826 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700827 expandBufAdd1(pReq, match_list[i]->eventKind);
828 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800829 expandBufAdd8BE(pReq, basket.threadId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700830 expandBufAddLocation(pReq, *pLoc);
Jeff Hao579b0242013-11-18 13:16:49 -0800831 if (match_list[i]->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
832 Dbg::OutputMethodReturnValue(pLoc->method_id, returnValue, pReq);
833 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800834 }
835 }
836
Elliott Hughesf8349362012-06-18 15:00:06 -0700837 CleanupMatchList(match_list, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800838 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700839
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100840 Dbg::ManageDeoptimization();
841
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800843 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700844}
845
846/*
847 * A thread is starting or stopping.
848 *
849 * Valid mods:
850 * Count, ThreadOnly
851 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700852bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
854
855 /*
856 * I don't think this can happen.
857 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800858 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700859 LOG(WARNING) << "Not posting thread change during invoke";
860 return false;
861 }
862
863 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700864 basket.threadId = threadId;
865
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700867 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800868 int match_count = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -0700869 {
870 // Don't allow the list to be updated while we scan it.
Ian Rogers50b35e22012-10-04 10:09:15 -0700871 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700872 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873
Elliott Hughes234ab152011-10-26 14:02:26 -0700874 if (start) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700875 FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700876 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700877 FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700878 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800880 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700881 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800882 << StringPrintf("thread=%#" PRIx64, basket.threadId) << ")";
Elliott Hughes234ab152011-10-26 14:02:26 -0700883
Elliott Hughesf8349362012-06-18 15:00:06 -0700884 suspend_policy = scanSuspendPolicy(match_list, match_count);
885 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700886
887 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700888 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800889 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700890
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800891 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700892 expandBufAdd1(pReq, match_list[i]->eventKind);
893 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes234ab152011-10-26 14:02:26 -0700894 expandBufAdd8BE(pReq, basket.threadId);
895 }
896 }
897
Elliott Hughesf8349362012-06-18 15:00:06 -0700898 CleanupMatchList(match_list, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700899 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700900
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100901 Dbg::ManageDeoptimization();
902
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700903 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700904
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800905 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700906}
907
908/*
909 * Send a polite "VM is dying" message to the debugger.
910 *
911 * Skips the usual "event token" stuff.
912 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700913bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800914 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700915
916 ExpandBuf* pReq = eventPrep();
917 expandBufAdd1(pReq, SP_NONE);
918 expandBufAdd4BE(pReq, 1);
919
920 expandBufAdd1(pReq, EK_VM_DEATH);
921 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800922 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700923 return true;
924}
925
926/*
927 * An exception has been thrown. It may or may not have been caught.
928 *
929 * Valid mods:
930 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
931 * ExceptionOnly, InstanceOnly
932 *
933 * The "exceptionId" has not been added to the GC-visible object registry,
934 * because there's a pretty good chance that we're not going to send it
935 * up the debugger.
936 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800937bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes74847412012-06-20 18:10:21 -0700938 ObjectId exceptionId, RefTypeId exceptionClassId,
939 const JdwpLocation* pCatchLoc, ObjectId thisPtr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700940 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700941
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700942 basket.pLoc = pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700943 basket.classId = pThrowLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700944 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800945 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700946 basket.excepClassId = exceptionClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700947 basket.caught = (pCatchLoc->class_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700948 basket.thisPtr = thisPtr;
949
950 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800951 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800952 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700953 return false;
954 }
955
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800956 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700957 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700958 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800959 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700960 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100961 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700962 FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800963 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700964 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)"
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800965 << StringPrintf(" thread=%#" PRIx64, basket.threadId)
966 << StringPrintf(" exceptId=%#" PRIx64, exceptionId)
Elliott Hughes436e3722012-02-17 20:01:47 -0800967 << " caught=" << basket.caught << ")"
968 << " throw: " << *pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700969 if (pCatchLoc->class_id == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800970 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -0800971 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800972 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800973 }
974
Elliott Hughesf8349362012-06-18 15:00:06 -0700975 suspend_policy = scanSuspendPolicy(match_list, match_count);
976 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800977
978 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700979 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800980 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800981
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800982 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700983 expandBufAdd1(pReq, match_list[i]->eventKind);
984 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800985 expandBufAdd8BE(pReq, basket.threadId);
986
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700987 expandBufAddLocation(pReq, *pThrowLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800988 expandBufAdd1(pReq, JT_OBJECT);
989 expandBufAdd8BE(pReq, exceptionId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700990 expandBufAddLocation(pReq, *pCatchLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800991 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700992 }
993
Elliott Hughesf8349362012-06-18 15:00:06 -0700994 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995 }
996
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100997 Dbg::ManageDeoptimization();
998
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001000
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001001 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001002}
1003
1004/*
1005 * Announce that a class has been loaded.
1006 *
1007 * Valid mods:
1008 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1009 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
1011 int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001012 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001013
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001014 basket.classId = refTypeId;
1015 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001016 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001017
1018 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001019 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001020 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001021 return false;
1022 }
1023
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001024 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001025 JdwpSuspendPolicy suspend_policy = SP_NONE;
1026 int match_count = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -08001027 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001028 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001029 JdwpEvent** match_list = AllocMatchList(event_list_size_);
1030 FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001031 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001032 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001033 << StringPrintf("thread=%#" PRIx64, basket.threadId) << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034
Elliott Hughesf8349362012-06-18 15:00:06 -07001035 suspend_policy = scanSuspendPolicy(match_list, match_count);
1036 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001037
Elliott Hughesa21039c2012-06-21 12:09:25 -07001038 if (basket.threadId == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001039 /*
1040 * JDWP says that, for a class prep in the debugger thread, we
1041 * should set threadId to null and if any threads were supposed
1042 * to be suspended then we suspend all other threads.
1043 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001044 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001045 basket.threadId = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001046 if (suspend_policy == SP_EVENT_THREAD) {
1047 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001048 }
1049 }
1050
1051 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001052 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001053 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001054
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001055 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001056 expandBufAdd1(pReq, match_list[i]->eventKind);
1057 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001058 expandBufAdd8BE(pReq, basket.threadId);
1059
1060 expandBufAdd1(pReq, tag);
1061 expandBufAdd8BE(pReq, refTypeId);
1062 expandBufAddUtf8String(pReq, signature);
1063 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001064 }
1065 }
Elliott Hughesf8349362012-06-18 15:00:06 -07001066 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001067 }
1068
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001069 Dbg::ManageDeoptimization();
1070
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001072
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001073 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001074}
1075
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001076/*
1077 * Send up a chunk of DDM data.
1078 *
1079 * While this takes the form of a JDWP "event", it doesn't interact with
1080 * other debugger traffic, and can't suspend the VM, so we skip all of
1081 * the fun event token gymnastics.
1082 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001083void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001084 uint8_t header[kJDWPHeaderLen + 8];
1085 size_t dataLen = 0;
1086
1087 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001088 CHECK_GT(iov_count, 0);
1089 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001090
1091 /*
1092 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1093 * this by creating a new copy of the vector with space for the header.
1094 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001095 std::vector<iovec> wrapiov;
1096 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001097 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001098 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099 dataLen += iov[i].iov_len;
1100 }
1101
1102 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001103 Set4BE(header, sizeof(header) + dataLen);
1104 Set4BE(header+4, NextRequestSerial());
1105 Set1(header+8, 0); /* flags */
1106 Set1(header+9, kJDWPDdmCmdSet);
1107 Set1(header+10, kJDWPDdmCmd);
1108 Set4BE(header+11, type);
1109 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001110
1111 wrapiov[0].iov_base = header;
1112 wrapiov[0].iov_len = sizeof(header);
1113
Ian Rogers15bf2d32012-08-28 17:33:04 -07001114 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1115 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001117 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1118 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001119 for (size_t i = 0; i < kMutatorLock; ++i) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001120 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
1121 safe_to_release_mutator_lock_over_send = false;
1122 break;
1123 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001124 }
1125 }
1126 if (safe_to_release_mutator_lock_over_send) {
1127 // Change state to waiting to allow GC, ... while we're sending.
1128 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001129 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001130 self->TransitionFromSuspendedToRunnable();
1131 } else {
1132 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001133 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001134 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001135}
1136
1137} // namespace JDWP
1138
1139} // namespace art