blob: cb2c420dbb2c2ff4a5b57891373a34443b0abbad [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),
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200111 caught(false), fieldTypeID(0), fieldId(0), thisPtr(0) { }
jeffhao162fd332013-01-08 16:21:01 -0800112
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 */
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200119 RefTypeId fieldTypeID; /* FieldOnly */
120 FieldId fieldId; /* FieldOnly */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 ObjectId thisPtr; /* InstanceOnly */
122 /* nothing for StepOnly -- handled differently */
123};
124
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100125static bool NeedsFullDeoptimization(JdwpEventKind eventKind) {
126 switch (eventKind) {
127 case EK_METHOD_ENTRY:
128 case EK_METHOD_EXIT:
129 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
130 case EK_SINGLE_STEP:
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200131 case EK_FIELD_ACCESS:
132 case EK_FIELD_MODIFICATION:
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100133 return true;
134 default:
135 return false;
136 }
137}
138
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200139uint32_t GetInstrumentationEventFor(JdwpEventKind eventKind) {
140 switch (eventKind) {
141 case EK_BREAKPOINT:
142 case EK_SINGLE_STEP:
143 return instrumentation::Instrumentation::kDexPcMoved;
144 case EK_EXCEPTION:
145 case EK_EXCEPTION_CATCH:
146 return instrumentation::Instrumentation::kExceptionCaught;
147 case EK_METHOD_ENTRY:
148 return instrumentation::Instrumentation::kMethodEntered;
149 case EK_METHOD_EXIT:
150 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
151 return instrumentation::Instrumentation::kMethodExited;
152 case EK_FIELD_ACCESS:
153 return instrumentation::Instrumentation::kFieldRead;
154 case EK_FIELD_MODIFICATION:
155 return instrumentation::Instrumentation::kFieldWritten;
156 default:
157 return 0;
158 }
159}
160
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161/*
162 * Add an event to the list. Ordering is not important.
163 *
164 * If something prevents the event from being registered, e.g. it's a
165 * single-step request on a thread that doesn't exist, the event will
166 * not be added to the list, and an appropriate error will be returned.
167 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800168JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169 CHECK(pEvent != NULL);
170 CHECK(pEvent->prev == NULL);
171 CHECK(pEvent->next == NULL);
172
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200173 {
174 /*
175 * If one or more "break"-type mods are used, register them with
176 * the interpreter.
177 */
178 DeoptimizationRequest req;
179 for (int i = 0; i < pEvent->modCount; i++) {
180 const JdwpEventMod* pMod = &pEvent->mods[i];
181 if (pMod->modKind == MK_LOCATION_ONLY) {
182 /* should only be for Breakpoint, Step, and Exception */
183 Dbg::WatchLocation(&pMod->locationOnly.loc, &req);
184 } else if (pMod->modKind == MK_STEP) {
185 /* should only be for EK_SINGLE_STEP; should only be one */
186 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
187 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
188 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
189 if (status != ERR_NONE) {
190 return status;
191 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800192 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200194 if (NeedsFullDeoptimization(pEvent->eventKind)) {
195 CHECK_EQ(req.kind, DeoptimizationRequest::kNothing);
196 CHECK(req.method == nullptr);
197 req.kind = DeoptimizationRequest::kFullDeoptimization;
198 }
199 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200201 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
202 if (instrumentation_event != 0) {
203 DeoptimizationRequest req;
204 req.kind = DeoptimizationRequest::kRegisterForEvent;
205 req.instrumentation_event = instrumentation_event;
206 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100207 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100209 {
210 /*
211 * Add to list.
212 */
213 MutexLock mu(Thread::Current(), event_list_lock_);
214 if (event_list_ != NULL) {
215 pEvent->next = event_list_;
216 event_list_->prev = pEvent;
217 }
218 event_list_ = pEvent;
219 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700220 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100221
222 Dbg::ManageDeoptimization();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700223
224 return ERR_NONE;
225}
226
227/*
228 * Remove an event from the list. This will also remove the event from
229 * any optimization tables, e.g. breakpoints.
230 *
231 * Does not free the JdwpEvent.
232 *
233 * Grab the eventLock before calling here.
234 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800235void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700236 if (pEvent->prev == NULL) {
237 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700238 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700239
Elliott Hughesf8349362012-06-18 15:00:06 -0700240 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241 } else {
242 pEvent->prev->next = pEvent->next;
243 }
244
245 if (pEvent->next != NULL) {
246 pEvent->next->prev = pEvent->prev;
247 pEvent->next = NULL;
248 }
249 pEvent->prev = NULL;
250
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200251 {
252 /*
253 * Unhook us from the interpreter, if necessary.
254 */
255 DeoptimizationRequest req;
256 for (int i = 0; i < pEvent->modCount; i++) {
257 JdwpEventMod* pMod = &pEvent->mods[i];
258 if (pMod->modKind == MK_LOCATION_ONLY) {
259 /* should only be for Breakpoint, Step, and Exception */
260 Dbg::UnwatchLocation(&pMod->locationOnly.loc, &req);
261 }
262 if (pMod->modKind == MK_STEP) {
263 /* should only be for EK_SINGLE_STEP; should only be one */
264 Dbg::UnconfigureStep(pMod->step.threadId);
265 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700266 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200267 if (pEvent->eventKind == EK_SINGLE_STEP) {
268 // Special case for single-steps where we want to avoid the slow pattern deoptimize/undeoptimize
269 // loop between each single-step. In a IDE, this would happens each time the user click on the
270 // "single-step" button. Here we delay the full undeoptimization to the next resume
271 // (VM.Resume or ThreadReference.Resume) or the end of the debugging session (VM.Dispose or
272 // runtime shutdown).
273 // Therefore, in a singles-stepping sequence, only the first single-step will trigger a full
274 // deoptimization and only the last single-step will trigger a full undeoptimization.
275 Dbg::DelayFullUndeoptimization();
276 } else if (NeedsFullDeoptimization(pEvent->eventKind)) {
277 CHECK_EQ(req.kind, DeoptimizationRequest::kNothing);
278 CHECK(req.method == nullptr);
279 req.kind = DeoptimizationRequest::kFullUndeoptimization;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200281 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200283 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
284 if (instrumentation_event != 0) {
285 DeoptimizationRequest req;
286 req.kind = DeoptimizationRequest::kUnregisterForEvent;
287 req.instrumentation_event = instrumentation_event;
288 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100289 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290
Elliott Hughesf8349362012-06-18 15:00:06 -0700291 --event_list_size_;
292 CHECK(event_list_size_ != 0 || event_list_ == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293}
294
295/*
296 * Remove the event with the given ID from the list.
297 *
298 * Failure to find the event isn't really an error, but it is a little
299 * weird. (It looks like Eclipse will try to be extra careful and will
300 * explicitly remove one-off single-step events.)
301 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800302void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100303 bool found = false;
304 {
305 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100307 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
308 if (pEvent->requestId == requestId) {
309 found = true;
310 UnregisterEvent(pEvent);
311 EventFree(pEvent);
312 break; /* there can be only one with a given ID */
313 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700315 }
316
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100317 if (found) {
318 Dbg::ManageDeoptimization();
319 } else {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700320 LOG(WARNING) << StringPrintf("Odd: no match when removing event reqId=0x%04x", requestId);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100321 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700322}
323
324/*
325 * Remove all entries from the event list.
326 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800327void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700328 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700329
Elliott Hughesf8349362012-06-18 15:00:06 -0700330 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331 while (pEvent != NULL) {
332 JdwpEvent* pNextEvent = pEvent->next;
333
Elliott Hughes761928d2011-11-16 18:33:03 -0800334 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700335 EventFree(pEvent);
336 pEvent = pNextEvent;
337 }
338
Elliott Hughesf8349362012-06-18 15:00:06 -0700339 event_list_ = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700340}
341
342/*
343 * Allocate a JdwpEvent struct with enough space to hold the specified
344 * number of mod records.
345 */
346JdwpEvent* EventAlloc(int numMods) {
347 JdwpEvent* newEvent;
348 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
349 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
350 memset(newEvent, 0, allocSize);
351 return newEvent;
352}
353
354/*
355 * Free a JdwpEvent.
356 *
357 * Do not call this until the event has been removed from the list.
358 */
359void EventFree(JdwpEvent* pEvent) {
360 if (pEvent == NULL) {
361 return;
362 }
363
364 /* make sure it was removed from the list */
365 CHECK(pEvent->prev == NULL);
366 CHECK(pEvent->next == NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700367 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368
369 /*
370 * Free any hairy bits in the mods.
371 */
372 for (int i = 0; i < pEvent->modCount; i++) {
373 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
374 free(pEvent->mods[i].classMatch.classPattern);
375 pEvent->mods[i].classMatch.classPattern = NULL;
376 }
377 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
378 free(pEvent->mods[i].classExclude.classPattern);
379 pEvent->mods[i].classExclude.classPattern = NULL;
380 }
381 }
382
383 free(pEvent);
384}
385
386/*
387 * Allocate storage for matching events. To keep things simple we
388 * use an array with enough storage for the entire list.
389 *
390 * The state->eventLock should be held before calling.
391 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800392static JdwpEvent** AllocMatchList(size_t event_count) {
393 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700394}
395
396/*
397 * Run through the list and remove any entries with an expired "count" mod
398 * from the event list, then free the match list.
399 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700400void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
401 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700402
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800403 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404 JdwpEvent* pEvent = *ppEvent;
405
406 for (int i = 0; i < pEvent->modCount; i++) {
407 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800408 VLOG(jdwp) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800409 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700410 EventFree(pEvent);
411 break;
412 }
413 }
414
415 ppEvent++;
416 }
417
Elliott Hughesf8349362012-06-18 15:00:06 -0700418 delete[] match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700419}
420
421/*
422 * Match a string against a "restricted regular expression", which is just
423 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
424 *
425 * ("Restricted name globbing" might have been a better term.)
426 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800427static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800428 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700429 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800431 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700432 return false;
433 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800434 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800436 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700437 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800438 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700439 }
440}
441
442/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700443 * See if the event's mods match up with the contents of "basket".
444 *
445 * If we find a Count mod before rejecting an event, we decrement it. We
446 * need to do this even if later mods cause us to ignore the event.
447 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700449 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700450 JdwpEventMod* pMod = pEvent->mods;
451
452 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
453 switch (pMod->modKind) {
454 case MK_COUNT:
455 CHECK_GT(pMod->count.count, 0);
456 pMod->count.count--;
Sebastien Hertz43207792014-04-15 16:03:27 +0200457 if (pMod->count.count > 0) {
458 return false;
459 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700460 break;
461 case MK_CONDITIONAL:
462 CHECK(false); // should not be getting these
463 break;
464 case MK_THREAD_ONLY:
465 if (pMod->threadOnly.threadId != basket->threadId) {
466 return false;
467 }
468 break;
469 case MK_CLASS_ONLY:
470 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
471 return false;
472 }
473 break;
474 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800475 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700476 return false;
477 }
478 break;
479 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800480 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700481 return false;
482 }
483 break;
484 case MK_LOCATION_ONLY:
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800485 if (pMod->locationOnly.loc != *basket->pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486 return false;
487 }
488 break;
489 case MK_EXCEPTION_ONLY:
490 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
491 return false;
492 }
493 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
494 return false;
495 }
496 break;
497 case MK_FIELD_ONLY:
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200498 if (pMod->fieldOnly.fieldId != basket->fieldId) {
499 return false;
500 }
501 if (!Dbg::MatchType(basket->fieldTypeID, pMod->fieldOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502 return false;
503 }
504 break;
505 case MK_STEP:
506 if (pMod->step.threadId != basket->threadId) {
507 return false;
508 }
509 break;
510 case MK_INSTANCE_ONLY:
511 if (pMod->instanceOnly.objectId != basket->thisPtr) {
512 return false;
513 }
514 break;
515 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800516 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700517 break;
518 }
519 }
520 return true;
521}
522
523/*
524 * Find all events of type "eventKind" with mods that match up with the
525 * rest of the arguments.
526 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700527 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700528 * so this may be called multiple times for grouped events.
529 *
530 * DO NOT call this multiple times for the same eventKind, as Count mods are
531 * decremented during the scan.
532 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket,
534 JdwpEvent** match_list, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700535 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700536 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537
Elliott Hughesf8349362012-06-18 15:00:06 -0700538 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800540 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700541 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700542 (*pMatchCount)++;
543 }
544
545 pEvent = pEvent->next;
546 }
547}
548
549/*
550 * Scan through the list of matches and determine the most severe
551 * suspension policy.
552 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700553static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700554 JdwpSuspendPolicy policy = SP_NONE;
555
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800556 while (match_count--) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700557 if ((*match_list)->suspend_policy > policy) {
558 policy = (*match_list)->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700559 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700560 match_list++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700561 }
562
563 return policy;
564}
565
566/*
567 * Three possibilities:
568 * SP_NONE - do nothing
569 * SP_EVENT_THREAD - suspend ourselves
570 * SP_ALL - suspend everybody except JDWP support thread
571 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700572void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700573 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
574 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575 return;
576 }
577
Elliott Hughesf8349362012-06-18 15:00:06 -0700578 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700579 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700581 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582 }
583
584 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800586 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587 return;
588 }
589
590 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
591 while (true) {
592 pReq->ready = true;
593 Dbg::SuspendSelf();
594 pReq->ready = false;
595
596 /*
597 * The JDWP thread has told us (and possibly all other threads) to
598 * resume. See if it has left anything in our DebugInvokeReq mailbox.
599 */
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100600 if (!pReq->invoke_needed) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800601 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700602 break;
603 }
604
605 /* grab this before posting/suspending again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 SetWaitForEventThread(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607
Elliott Hughesd07986f2011-12-06 18:27:45 -0800608 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609 Dbg::ExecuteMethod(pReq);
610
Elliott Hughes475fc232011-10-25 15:00:35 -0700611 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700612 }
613}
614
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
616 ObjectId threadId) {
617 Thread* self = Thread::Current();
618 self->AssertThreadSuspensionIsAllowable();
619 /* send request and possibly suspend ourselves */
620 if (pReq != NULL) {
621 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
622 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
623 if (suspend_policy != SP_NONE) {
624 SetWaitForEventThread(threadId);
625 }
626 EventFinish(pReq);
627 SuspendByPolicy(suspend_policy, thread_self_id);
628 self->TransitionFromSuspendedToRunnable();
629 }
630}
631
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700632/*
633 * Determine if there is a method invocation in progress in the current
634 * thread.
635 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700636 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700637 * state. If set, we're in the process of invoking a method.
638 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800639bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100641 return pReq->invoke_needed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700642}
643
644/*
645 * We need the JDWP thread to hold off on doing stuff while we post an
646 * event and then suspend ourselves.
647 *
648 * Call this with a threadId of zero if you just want to wait for the
649 * current thread operation to complete.
650 *
651 * This could go to sleep waiting for another thread, so it's important
652 * that the thread be marked as VMWAIT before calling here.
653 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700654void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700655 bool waited = false;
656
657 /* this is held for very brief periods; contention is unlikely */
Ian Rogers81d425b2012-09-27 16:03:43 -0700658 Thread* self = Thread::Current();
659 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700660
661 /*
662 * If another thread is already doing stuff, wait for it. This can
663 * go to sleep indefinitely.
664 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700665 while (event_thread_id_ != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800666 VLOG(jdwp) << StringPrintf("event in progress (%#" PRIx64 "), %#" PRIx64 " sleeping",
667 event_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700668 waited = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700669 event_thread_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700670 }
671
672 if (waited || threadId != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800673 VLOG(jdwp) << StringPrintf("event token grabbed (%#" PRIx64 ")", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700674 }
675 if (threadId != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700676 event_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700677 }
678}
679
680/*
681 * Clear the threadId and signal anybody waiting.
682 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700683void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700684 /*
685 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
686 * function is called by dvmSuspendSelf(), and the transition back
687 * to RUNNING would confuse it.
688 */
Ian Rogersc604d732012-10-14 16:09:54 -0700689 Thread* self = Thread::Current();
690 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700691
Elliott Hughesa21039c2012-06-21 12:09:25 -0700692 CHECK_NE(event_thread_id_, 0U);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800693 VLOG(jdwp) << StringPrintf("cleared event token (%#" PRIx64 ")", event_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700694
Elliott Hughesa21039c2012-06-21 12:09:25 -0700695 event_thread_id_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700696
Ian Rogersc604d732012-10-14 16:09:54 -0700697 event_thread_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698}
699
700
701/*
702 * Prep an event. Allocates storage for the message and leaves space for
703 * the header.
704 */
705static ExpandBuf* eventPrep() {
706 ExpandBuf* pReq = expandBufAlloc();
707 expandBufAddSpace(pReq, kJDWPHeaderLen);
708 return pReq;
709}
710
711/*
712 * Write the header into the buffer and send the packet off to the debugger.
713 *
714 * Takes ownership of "pReq" (currently discards it).
715 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800716void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700717 uint8_t* buf = expandBufGetBuffer(pReq);
718
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700719 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800720 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700721 Set1(buf+8, 0); /* flags */
722 Set1(buf+9, kJdwpEventCommandSet);
723 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724
Sebastien Hertz99660e12014-02-19 15:04:42 +0100725 // Prevents from interleaving commands and events. Otherwise we could end up in sending an event
726 // before sending the reply of the command being processed and would lead to bad synchronization
727 // between the debugger and the debuggee.
728 WaitForProcessingRequest();
729
Elliott Hughes761928d2011-11-16 18:33:03 -0800730 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700731
732 expandBufFree(pReq);
733}
734
735
736/*
737 * Tell the debugger that we have finished initializing. This is always
738 * sent, even if the debugger hasn't requested it.
739 *
740 * This should be sent "before the main thread is started and before
741 * any application code has been executed". The thread ID in the message
742 * must be for the main thread.
743 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700744bool JdwpState::PostVMStart() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700745 JdwpSuspendPolicy suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700746 ObjectId threadId = Dbg::GetThreadSelfId();
747
Elliott Hughes376a7a02011-10-24 18:35:55 -0700748 if (options_->suspend) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700749 suspend_policy = SP_ALL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700751 suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700752 }
753
Elliott Hughes761928d2011-11-16 18:33:03 -0800754 ExpandBuf* pReq = eventPrep();
755 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700756 MutexLock mu(Thread::Current(), event_list_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800758 VLOG(jdwp) << "EVENT: " << EK_VM_START;
Elliott Hughesf8349362012-06-18 15:00:06 -0700759 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700760
Elliott Hughesf8349362012-06-18 15:00:06 -0700761 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700762 expandBufAdd4BE(pReq, 1);
763
764 expandBufAdd1(pReq, EK_VM_START);
765 expandBufAdd4BE(pReq, 0); /* requestId */
766 expandBufAdd8BE(pReq, threadId);
767 }
768
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100769 Dbg::ManageDeoptimization();
770
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700771 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700773
774 return true;
775}
776
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777/*
778 * A location of interest has been reached. This handles:
779 * Breakpoint
780 * SingleStep
781 * MethodEntry
782 * MethodExit
783 * These four types must be grouped together in a single response. The
784 * "eventFlags" indicates the type of event(s) that have happened.
785 *
786 * Valid mods:
787 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
788 * LocationOnly (for breakpoint/step only)
789 * Step (for step only)
790 *
791 * Interesting test cases:
792 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
793 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
794 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
795 * - Single-step to a line with a breakpoint. Should get a single
796 * event message with both events in it.
797 */
Jeff Hao579b0242013-11-18 13:16:49 -0800798bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags,
799 const JValue* returnValue) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700800 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801 basket.pLoc = pLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700802 basket.classId = pLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700803 basket.thisPtr = thisPtr;
804 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes74847412012-06-20 18:10:21 -0700805 basket.className = Dbg::GetClassName(pLoc->class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700806
807 /*
808 * On rare occasions we may need to execute interpreted code in the VM
809 * while handling a request from the debugger. Don't fire breakpoints
810 * while doing so. (I don't think we currently do this at all, so
811 * this is mostly paranoia.)
812 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700813 if (basket.threadId == debug_thread_id_) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800814 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700815 return false;
816 }
817
818 /*
819 * The debugger variable display tab may invoke the interpreter to format
820 * complex objects. We want to ignore breakpoints and method entry/exit
821 * traps while working on behalf of the debugger.
822 *
823 * If we don't ignore them, the VM will get hung up, because we'll
824 * suspend on a breakpoint while the debugger is still waiting for its
825 * method invocation to complete.
826 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800827 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800828 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700829 return false;
830 }
831
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800832 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700834 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800835 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700836 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100837 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes86964332012-02-15 19:37:42 -0800838 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700839 FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700840 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800841 if ((eventFlags & Dbg::kSingleStep) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700842 FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800843 }
844 if ((eventFlags & Dbg::kMethodEntry) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700845 FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800846 }
847 if ((eventFlags & Dbg::kMethodExit) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700848 FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
Jeff Hao579b0242013-11-18 13:16:49 -0800849 FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800850 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800851 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700852 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughesa96836a2013-01-17 12:27:49 -0800853 << basket.className << "." << Dbg::GetMethodName(pLoc->method_id)
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800854 << StringPrintf(" thread=%#" PRIx64 " dex_pc=%#" PRIx64 ")",
855 basket.threadId, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700856
Elliott Hughesf8349362012-06-18 15:00:06 -0700857 suspend_policy = scanSuspendPolicy(match_list, match_count);
858 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800859
860 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700861 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800862 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800863
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800864 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700865 expandBufAdd1(pReq, match_list[i]->eventKind);
866 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800867 expandBufAdd8BE(pReq, basket.threadId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700868 expandBufAddLocation(pReq, *pLoc);
Jeff Hao579b0242013-11-18 13:16:49 -0800869 if (match_list[i]->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
870 Dbg::OutputMethodReturnValue(pLoc->method_id, returnValue, pReq);
871 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800872 }
873 }
874
Elliott Hughesf8349362012-06-18 15:00:06 -0700875 CleanupMatchList(match_list, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800876 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700877
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100878 Dbg::ManageDeoptimization();
879
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800881 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882}
883
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200884bool JdwpState::PostFieldEvent(const JdwpLocation* pLoc, RefTypeId typeId, FieldId fieldId,
885 ObjectId thisPtr, const JValue* fieldValue, bool is_modification) {
886 ModBasket basket;
887 basket.pLoc = pLoc;
888 basket.classId = pLoc->class_id;
889 basket.thisPtr = thisPtr;
890 basket.threadId = Dbg::GetThreadSelfId();
891 basket.className = Dbg::GetClassName(pLoc->class_id);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200892 basket.fieldTypeID = typeId;
893 basket.fieldId = fieldId;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200894
895 if (InvokeInProgress()) {
896 VLOG(jdwp) << "Not posting field event during invoke";
897 return false;
898 }
899
900 // Get field's reference type tag.
901 JDWP::JdwpTypeTag type_tag;
902 uint32_t class_status; // unused here.
903 JdwpError error = Dbg::GetClassInfo(typeId, &type_tag, &class_status, NULL);
904 if (error != ERR_NONE) {
905 return false;
906 }
907
908 // Get instance type tag.
909 uint8_t tag;
910 error = Dbg::GetObjectTag(thisPtr, tag);
911 if (error != ERR_NONE) {
912 return false;
913 }
914
915 int match_count = 0;
916 ExpandBuf* pReq = NULL;
917 JdwpSuspendPolicy suspend_policy = SP_NONE;
918 {
919 MutexLock mu(Thread::Current(), event_list_lock_);
920 JdwpEvent** match_list = AllocMatchList(event_list_size_);
921
922 if (is_modification) {
923 FindMatchingEvents(EK_FIELD_MODIFICATION, &basket, match_list, &match_count);
924 } else {
925 FindMatchingEvents(EK_FIELD_ACCESS, &basket, match_list, &match_count);
926 }
927 if (match_count != 0) {
928 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
929 << basket.className << "." << Dbg::GetMethodName(pLoc->method_id)
930 << StringPrintf(" thread=%#" PRIx64 " dex_pc=%#" PRIx64 ")",
931 basket.threadId, pLoc->dex_pc);
932
933 suspend_policy = scanSuspendPolicy(match_list, match_count);
934 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
935
936 pReq = eventPrep();
937 expandBufAdd1(pReq, suspend_policy);
938 expandBufAdd4BE(pReq, match_count);
939
940 for (int i = 0; i < match_count; i++) {
941 expandBufAdd1(pReq, match_list[i]->eventKind);
942 expandBufAdd4BE(pReq, match_list[i]->requestId);
943 expandBufAdd8BE(pReq, basket.threadId);
944 expandBufAddLocation(pReq, *pLoc);
945 expandBufAdd1(pReq, type_tag);
946 expandBufAddRefTypeId(pReq, typeId);
947 expandBufAddFieldId(pReq, fieldId);
948 expandBufAdd1(pReq, tag);
949 expandBufAddObjectId(pReq, thisPtr);
950 if (is_modification) {
951 Dbg::OutputFieldValue(fieldId, fieldValue, pReq);
952 }
953 }
954 }
955
956 CleanupMatchList(match_list, match_count);
957 }
958
959 Dbg::ManageDeoptimization();
960
961 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
962 return match_count != 0;
963}
964
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700965/*
966 * A thread is starting or stopping.
967 *
968 * Valid mods:
969 * Count, ThreadOnly
970 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700971bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700972 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
973
974 /*
975 * I don't think this can happen.
976 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800977 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700978 LOG(WARNING) << "Not posting thread change during invoke";
979 return false;
980 }
981
982 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700983 basket.threadId = threadId;
984
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700986 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800987 int match_count = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -0700988 {
989 // Don't allow the list to be updated while we scan it.
Ian Rogers50b35e22012-10-04 10:09:15 -0700990 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700991 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700992
Elliott Hughes234ab152011-10-26 14:02:26 -0700993 if (start) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700994 FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700995 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700996 FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700997 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700998
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800999 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001000 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001001 << StringPrintf("thread=%#" PRIx64, basket.threadId) << ")";
Elliott Hughes234ab152011-10-26 14:02:26 -07001002
Elliott Hughesf8349362012-06-18 15:00:06 -07001003 suspend_policy = scanSuspendPolicy(match_list, match_count);
1004 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes234ab152011-10-26 14:02:26 -07001005
1006 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001007 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001008 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -07001009
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001010 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001011 expandBufAdd1(pReq, match_list[i]->eventKind);
1012 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes234ab152011-10-26 14:02:26 -07001013 expandBufAdd8BE(pReq, basket.threadId);
1014 }
1015 }
1016
Elliott Hughesf8349362012-06-18 15:00:06 -07001017 CleanupMatchList(match_list, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -07001018 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001020 Dbg::ManageDeoptimization();
1021
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001024 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001025}
1026
1027/*
1028 * Send a polite "VM is dying" message to the debugger.
1029 *
1030 * Skips the usual "event token" stuff.
1031 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001032bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001033 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034
1035 ExpandBuf* pReq = eventPrep();
1036 expandBufAdd1(pReq, SP_NONE);
1037 expandBufAdd4BE(pReq, 1);
1038
1039 expandBufAdd1(pReq, EK_VM_DEATH);
1040 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -08001041 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001042 return true;
1043}
1044
1045/*
1046 * An exception has been thrown. It may or may not have been caught.
1047 *
1048 * Valid mods:
1049 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
1050 * ExceptionOnly, InstanceOnly
1051 *
1052 * The "exceptionId" has not been added to the GC-visible object registry,
1053 * because there's a pretty good chance that we're not going to send it
1054 * up the debugger.
1055 */
Elliott Hughes761928d2011-11-16 18:33:03 -08001056bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes74847412012-06-20 18:10:21 -07001057 ObjectId exceptionId, RefTypeId exceptionClassId,
1058 const JdwpLocation* pCatchLoc, ObjectId thisPtr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001059 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001060
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061 basket.pLoc = pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -07001062 basket.classId = pThrowLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001063 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001064 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001065 basket.excepClassId = exceptionClassId;
Elliott Hughes74847412012-06-20 18:10:21 -07001066 basket.caught = (pCatchLoc->class_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001067 basket.thisPtr = thisPtr;
1068
1069 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001070 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001071 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001072 return false;
1073 }
1074
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001075 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001076 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001077 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -08001078 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001079 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001080 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001081 FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001082 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001083 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)"
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001084 << StringPrintf(" thread=%#" PRIx64, basket.threadId)
1085 << StringPrintf(" exceptId=%#" PRIx64, exceptionId)
Elliott Hughes436e3722012-02-17 20:01:47 -08001086 << " caught=" << basket.caught << ")"
1087 << " throw: " << *pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -07001088 if (pCatchLoc->class_id == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001089 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -08001090 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001091 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -08001092 }
1093
Elliott Hughesf8349362012-06-18 15:00:06 -07001094 suspend_policy = scanSuspendPolicy(match_list, match_count);
1095 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -08001096
1097 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001098 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001099 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001100
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001101 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001102 expandBufAdd1(pReq, match_list[i]->eventKind);
1103 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001104 expandBufAdd8BE(pReq, basket.threadId);
1105
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001106 expandBufAddLocation(pReq, *pThrowLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -08001107 expandBufAdd1(pReq, JT_OBJECT);
1108 expandBufAdd8BE(pReq, exceptionId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001109 expandBufAddLocation(pReq, *pCatchLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -08001110 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001111 }
1112
Elliott Hughesf8349362012-06-18 15:00:06 -07001113 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001114 }
1115
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001116 Dbg::ManageDeoptimization();
1117
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001119
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001120 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001121}
1122
1123/*
1124 * Announce that a class has been loaded.
1125 *
1126 * Valid mods:
1127 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1128 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
1130 int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001133 basket.classId = refTypeId;
1134 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001135 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136
1137 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001138 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001139 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001140 return false;
1141 }
1142
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001143 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001144 JdwpSuspendPolicy suspend_policy = SP_NONE;
1145 int match_count = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -08001146 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001147 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001148 JdwpEvent** match_list = AllocMatchList(event_list_size_);
1149 FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001150 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001151 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001152 << StringPrintf("thread=%#" PRIx64, basket.threadId) << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001153
Elliott Hughesf8349362012-06-18 15:00:06 -07001154 suspend_policy = scanSuspendPolicy(match_list, match_count);
1155 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001156
Elliott Hughesa21039c2012-06-21 12:09:25 -07001157 if (basket.threadId == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001158 /*
1159 * JDWP says that, for a class prep in the debugger thread, we
1160 * should set threadId to null and if any threads were supposed
1161 * to be suspended then we suspend all other threads.
1162 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001163 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001164 basket.threadId = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001165 if (suspend_policy == SP_EVENT_THREAD) {
1166 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001167 }
1168 }
1169
1170 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001171 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001172 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001173
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001174 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001175 expandBufAdd1(pReq, match_list[i]->eventKind);
1176 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001177 expandBufAdd8BE(pReq, basket.threadId);
1178
1179 expandBufAdd1(pReq, tag);
1180 expandBufAdd8BE(pReq, refTypeId);
1181 expandBufAddUtf8String(pReq, signature);
1182 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001183 }
1184 }
Elliott Hughesf8349362012-06-18 15:00:06 -07001185 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186 }
1187
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001188 Dbg::ManageDeoptimization();
1189
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001191
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001192 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193}
1194
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001195/*
1196 * Send up a chunk of DDM data.
1197 *
1198 * While this takes the form of a JDWP "event", it doesn't interact with
1199 * other debugger traffic, and can't suspend the VM, so we skip all of
1200 * the fun event token gymnastics.
1201 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001202void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001203 uint8_t header[kJDWPHeaderLen + 8];
1204 size_t dataLen = 0;
1205
1206 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001207 CHECK_GT(iov_count, 0);
1208 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001209
1210 /*
1211 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1212 * this by creating a new copy of the vector with space for the header.
1213 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001214 std::vector<iovec> wrapiov;
1215 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001216 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001217 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001218 dataLen += iov[i].iov_len;
1219 }
1220
1221 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001222 Set4BE(header, sizeof(header) + dataLen);
1223 Set4BE(header+4, NextRequestSerial());
1224 Set1(header+8, 0); /* flags */
1225 Set1(header+9, kJDWPDdmCmdSet);
1226 Set1(header+10, kJDWPDdmCmd);
1227 Set4BE(header+11, type);
1228 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001229
1230 wrapiov[0].iov_base = header;
1231 wrapiov[0].iov_len = sizeof(header);
1232
Ian Rogers15bf2d32012-08-28 17:33:04 -07001233 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1234 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001236 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1237 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001238 for (size_t i = 0; i < kMutatorLock; ++i) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001239 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
1240 safe_to_release_mutator_lock_over_send = false;
1241 break;
1242 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001243 }
1244 }
1245 if (safe_to_release_mutator_lock_over_send) {
1246 // Change state to waiting to allow GC, ... while we're sending.
1247 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001248 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001249 self->TransitionFromSuspendedToRunnable();
1250 } else {
1251 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001252 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001253 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001254}
1255
1256} // namespace JDWP
1257
1258} // namespace art