blob: d61660bca79ee1c92692d99415e7b4db4c02d391 [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"
Sebastien Hertz6995c602014-09-09 12:10:13 +020030#include "jdwp/object_registry.h"
31#include "mirror/art_field-inl.h"
32#include "scoped_thread_state_change.h"
Ian Rogers693ff612013-02-01 10:56:12 -080033#include "thread-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080034
Elliott Hughes872d4ec2011-10-21 17:07:15 -070035/*
36General notes:
37
38The event add/remove stuff usually happens from the debugger thread,
39in response to requests from the debugger, but can also happen as the
40result of an event in an arbitrary thread (e.g. an event with a "count"
41mod expires). It's important to keep the event list locked when processing
42events.
43
44Event posting can happen from any thread. The JDWP thread will not usually
45post anything but VM start/death, but if a JDWP request causes a class
46to be loaded, the ClassPrepare event will come from the JDWP thread.
47
48
49We can have serialization issues when we post an event to the debugger.
50For example, a thread could send an "I hit a breakpoint and am suspending
51myself" message to the debugger. Before it manages to suspend itself, the
52debugger's response ("not interested, resume thread") arrives and is
53processed. We try to resume a thread that hasn't yet suspended.
54
55This means that, after posting an event to the debugger, we need to wait
56for the event thread to suspend itself (and, potentially, all other threads)
57before processing any additional requests from the debugger. While doing
58so we need to be aware that multiple threads may be hitting breakpoints
59or other events simultaneously, so we either need to wait for all of them
60or serialize the events with each other.
61
62The current mechanism works like this:
63 Event thread:
64 - If I'm going to suspend, grab the "I am posting an event" token. Wait
65 for it if it's not currently available.
66 - Post the event to the debugger.
67 - If appropriate, suspend others and then myself. As part of suspending
68 myself, release the "I am posting" token.
69 JDWP thread:
70 - When an event arrives, see if somebody is posting an event. If so,
71 sleep until we can acquire the "I am posting an event" token. Release
72 it immediately and continue processing -- the event we have already
73 received should not interfere with other events that haven't yet
74 been posted.
75
76Some care must be taken to avoid deadlock:
77
78 - thread A and thread B exit near-simultaneously, and post thread-death
79 events with a "suspend all" clause
80 - thread A gets the event token, thread B sits and waits for it
81 - thread A wants to suspend all other threads, but thread B is waiting
82 for the token and can't be suspended
83
84So we need to mark thread B in such a way that thread A doesn't wait for it.
85
86If we just bracket the "grab event token" call with a change to VMWAIT
87before sleeping, the switch back to RUNNING state when we get the token
88will cause thread B to suspend (remember, thread A's global suspend is
89still in force, even after it releases the token). Suspending while
90holding the event token is very bad, because it prevents the JDWP thread
91from processing incoming messages.
92
93We need to change to VMWAIT state at the *start* of posting an event,
94and stay there until we either finish posting the event or decide to
95put ourselves to sleep. That way we don't interfere with anyone else and
96don't allow anyone else to interfere with us.
97*/
98
99
100#define kJdwpEventCommandSet 64
101#define kJdwpCompositeCommand 100
102
103namespace art {
104
105namespace JDWP {
106
107/*
108 * Stuff to compare against when deciding if a mod matches. Only the
109 * values for mods valid for the event being evaluated will be filled in.
110 * The rest will be zeroed.
111 */
112struct ModBasket {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200113 ModBasket() : pLoc(nullptr), thread(nullptr), locationClass(nullptr), exceptionClass(nullptr),
114 caught(false), field(nullptr), thisPtr(nullptr) { }
jeffhao162fd332013-01-08 16:21:01 -0800115
Sebastien Hertz6995c602014-09-09 12:10:13 +0200116 const EventLocation* pLoc; /* LocationOnly */
117 std::string className; /* ClassMatch/ClassExclude */
118 Thread* thread; /* ThreadOnly */
119 mirror::Class* locationClass; /* ClassOnly */
120 mirror::Class* exceptionClass; /* ExceptionOnly */
121 bool caught; /* ExceptionOnly */
122 mirror::ArtField* field; /* FieldOnly */
123 mirror::Object* thisPtr; /* InstanceOnly */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124 /* nothing for StepOnly -- handled differently */
125};
126
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100127static bool NeedsFullDeoptimization(JdwpEventKind eventKind) {
128 switch (eventKind) {
129 case EK_METHOD_ENTRY:
130 case EK_METHOD_EXIT:
131 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
132 case EK_SINGLE_STEP:
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200133 case EK_FIELD_ACCESS:
134 case EK_FIELD_MODIFICATION:
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100135 return true;
136 default:
137 return false;
138 }
139}
140
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200141uint32_t GetInstrumentationEventFor(JdwpEventKind eventKind) {
142 switch (eventKind) {
143 case EK_BREAKPOINT:
144 case EK_SINGLE_STEP:
145 return instrumentation::Instrumentation::kDexPcMoved;
146 case EK_EXCEPTION:
147 case EK_EXCEPTION_CATCH:
148 return instrumentation::Instrumentation::kExceptionCaught;
149 case EK_METHOD_ENTRY:
150 return instrumentation::Instrumentation::kMethodEntered;
151 case EK_METHOD_EXIT:
152 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
153 return instrumentation::Instrumentation::kMethodExited;
154 case EK_FIELD_ACCESS:
155 return instrumentation::Instrumentation::kFieldRead;
156 case EK_FIELD_MODIFICATION:
157 return instrumentation::Instrumentation::kFieldWritten;
158 default:
159 return 0;
160 }
161}
162
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700163/*
164 * Add an event to the list. Ordering is not important.
165 *
166 * If something prevents the event from being registered, e.g. it's a
167 * single-step request on a thread that doesn't exist, the event will
168 * not be added to the list, and an appropriate error will be returned.
169 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800170JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700171 CHECK(pEvent != NULL);
172 CHECK(pEvent->prev == NULL);
173 CHECK(pEvent->next == NULL);
174
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200175 {
176 /*
177 * If one or more "break"-type mods are used, register them with
178 * the interpreter.
179 */
180 DeoptimizationRequest req;
181 for (int i = 0; i < pEvent->modCount; i++) {
182 const JdwpEventMod* pMod = &pEvent->mods[i];
183 if (pMod->modKind == MK_LOCATION_ONLY) {
184 /* should only be for Breakpoint, Step, and Exception */
185 Dbg::WatchLocation(&pMod->locationOnly.loc, &req);
186 } else if (pMod->modKind == MK_STEP) {
187 /* should only be for EK_SINGLE_STEP; should only be one */
188 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
189 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
190 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
191 if (status != ERR_NONE) {
192 return status;
193 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800194 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700195 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200196 if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700197 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
198 CHECK(req.Method() == nullptr);
199 req.SetKind(DeoptimizationRequest::kFullDeoptimization);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200200 }
201 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200203 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
204 if (instrumentation_event != 0) {
205 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700206 req.SetKind(DeoptimizationRequest::kRegisterForEvent);
207 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200208 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100209 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100211 {
212 /*
213 * Add to list.
214 */
215 MutexLock mu(Thread::Current(), event_list_lock_);
216 if (event_list_ != NULL) {
217 pEvent->next = event_list_;
218 event_list_->prev = pEvent;
219 }
220 event_list_ = pEvent;
221 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100223
224 Dbg::ManageDeoptimization();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700225
226 return ERR_NONE;
227}
228
229/*
230 * Remove an event from the list. This will also remove the event from
231 * any optimization tables, e.g. breakpoints.
232 *
233 * Does not free the JdwpEvent.
234 *
235 * Grab the eventLock before calling here.
236 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800237void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238 if (pEvent->prev == NULL) {
239 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700240 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241
Elliott Hughesf8349362012-06-18 15:00:06 -0700242 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243 } else {
244 pEvent->prev->next = pEvent->next;
245 }
246
247 if (pEvent->next != NULL) {
248 pEvent->next->prev = pEvent->prev;
249 pEvent->next = NULL;
250 }
251 pEvent->prev = NULL;
252
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200253 {
254 /*
255 * Unhook us from the interpreter, if necessary.
256 */
257 DeoptimizationRequest req;
258 for (int i = 0; i < pEvent->modCount; i++) {
259 JdwpEventMod* pMod = &pEvent->mods[i];
260 if (pMod->modKind == MK_LOCATION_ONLY) {
261 /* should only be for Breakpoint, Step, and Exception */
262 Dbg::UnwatchLocation(&pMod->locationOnly.loc, &req);
263 }
264 if (pMod->modKind == MK_STEP) {
265 /* should only be for EK_SINGLE_STEP; should only be one */
266 Dbg::UnconfigureStep(pMod->step.threadId);
267 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700268 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200269 if (pEvent->eventKind == EK_SINGLE_STEP) {
270 // Special case for single-steps where we want to avoid the slow pattern deoptimize/undeoptimize
271 // loop between each single-step. In a IDE, this would happens each time the user click on the
272 // "single-step" button. Here we delay the full undeoptimization to the next resume
273 // (VM.Resume or ThreadReference.Resume) or the end of the debugging session (VM.Dispose or
274 // runtime shutdown).
275 // Therefore, in a singles-stepping sequence, only the first single-step will trigger a full
276 // deoptimization and only the last single-step will trigger a full undeoptimization.
277 Dbg::DelayFullUndeoptimization();
278 } else if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700279 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
280 CHECK(req.Method() == nullptr);
281 req.SetKind(DeoptimizationRequest::kFullUndeoptimization);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200283 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200285 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
286 if (instrumentation_event != 0) {
287 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700288 req.SetKind(DeoptimizationRequest::kUnregisterForEvent);
289 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200290 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100291 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700292
Elliott Hughesf8349362012-06-18 15:00:06 -0700293 --event_list_size_;
294 CHECK(event_list_size_ != 0 || event_list_ == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700295}
296
297/*
298 * Remove the event with the given ID from the list.
299 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700300 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800301void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100302 bool found = false;
303 {
304 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700305
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100306 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
307 if (pEvent->requestId == requestId) {
308 found = true;
309 UnregisterEvent(pEvent);
310 EventFree(pEvent);
311 break; /* there can be only one with a given ID */
312 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314 }
315
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100316 if (found) {
317 Dbg::ManageDeoptimization();
318 } else {
Sebastien Hertzf272af42014-09-18 10:20:42 +0200319 // Failure to find the event isn't really an error. For instance, it looks like Eclipse will
320 // try to be extra careful and will explicitly remove one-off single-step events (using a
321 // 'count' event modifier of 1). So the event may have already been removed as part of the
322 // event notification (see JdwpState::CleanupMatchList).
323 VLOG(jdwp) << StringPrintf("No match when removing event reqId=0x%04x", requestId);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100324 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325}
326
327/*
328 * Remove all entries from the event list.
329 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800330void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700331 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700332
Elliott Hughesf8349362012-06-18 15:00:06 -0700333 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700334 while (pEvent != NULL) {
335 JdwpEvent* pNextEvent = pEvent->next;
336
Elliott Hughes761928d2011-11-16 18:33:03 -0800337 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 EventFree(pEvent);
339 pEvent = pNextEvent;
340 }
341
Elliott Hughesf8349362012-06-18 15:00:06 -0700342 event_list_ = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700343}
344
345/*
346 * Allocate a JdwpEvent struct with enough space to hold the specified
347 * number of mod records.
348 */
349JdwpEvent* EventAlloc(int numMods) {
350 JdwpEvent* newEvent;
351 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
352 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
353 memset(newEvent, 0, allocSize);
354 return newEvent;
355}
356
357/*
358 * Free a JdwpEvent.
359 *
360 * Do not call this until the event has been removed from the list.
361 */
362void EventFree(JdwpEvent* pEvent) {
363 if (pEvent == NULL) {
364 return;
365 }
366
367 /* make sure it was removed from the list */
368 CHECK(pEvent->prev == NULL);
369 CHECK(pEvent->next == NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700370 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371
372 /*
373 * Free any hairy bits in the mods.
374 */
375 for (int i = 0; i < pEvent->modCount; i++) {
376 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
377 free(pEvent->mods[i].classMatch.classPattern);
378 pEvent->mods[i].classMatch.classPattern = NULL;
379 }
380 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
381 free(pEvent->mods[i].classExclude.classPattern);
382 pEvent->mods[i].classExclude.classPattern = NULL;
383 }
384 }
385
386 free(pEvent);
387}
388
389/*
390 * Allocate storage for matching events. To keep things simple we
391 * use an array with enough storage for the entire list.
392 *
393 * The state->eventLock should be held before calling.
394 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800395static JdwpEvent** AllocMatchList(size_t event_count) {
396 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700397}
398
399/*
400 * Run through the list and remove any entries with an expired "count" mod
401 * from the event list, then free the match list.
402 */
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200403void JdwpState::CleanupMatchList(JdwpEvent** match_list, size_t match_count) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700404 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800406 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407 JdwpEvent* pEvent = *ppEvent;
408
409 for (int i = 0; i < pEvent->modCount; i++) {
410 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200411 VLOG(jdwp) << StringPrintf("##### Removing expired event (requestId=%#" PRIx32 ")",
412 pEvent->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800413 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414 EventFree(pEvent);
415 break;
416 }
417 }
418
419 ppEvent++;
420 }
421
Elliott Hughesf8349362012-06-18 15:00:06 -0700422 delete[] match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423}
424
425/*
426 * Match a string against a "restricted regular expression", which is just
427 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
428 *
429 * ("Restricted name globbing" might have been a better term.)
430 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800431static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800432 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800435 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436 return false;
437 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800438 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700439 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800440 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800442 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700443 }
444}
445
446/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447 * See if the event's mods match up with the contents of "basket".
448 *
449 * If we find a Count mod before rejecting an event, we decrement it. We
450 * need to do this even if later mods cause us to ignore the event.
451 */
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200452static bool ModsMatch(JdwpEvent* pEvent, const ModBasket& basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 JdwpEventMod* pMod = pEvent->mods;
455
456 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
457 switch (pMod->modKind) {
458 case MK_COUNT:
459 CHECK_GT(pMod->count.count, 0);
460 pMod->count.count--;
Sebastien Hertz43207792014-04-15 16:03:27 +0200461 if (pMod->count.count > 0) {
462 return false;
463 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 break;
465 case MK_CONDITIONAL:
466 CHECK(false); // should not be getting these
467 break;
468 case MK_THREAD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200469 if (!Dbg::MatchThread(pMod->threadOnly.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700470 return false;
471 }
472 break;
473 case MK_CLASS_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200474 if (!Dbg::MatchType(basket.locationClass, pMod->classOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475 return false;
476 }
477 break;
478 case MK_CLASS_MATCH:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200479 if (!PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700480 return false;
481 }
482 break;
483 case MK_CLASS_EXCLUDE:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200484 if (PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485 return false;
486 }
487 break;
488 case MK_LOCATION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200489 if (!Dbg::MatchLocation(pMod->locationOnly.loc, *basket.pLoc)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700490 return false;
491 }
492 break;
493 case MK_EXCEPTION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200494 if (pMod->exceptionOnly.refTypeId != 0 &&
495 !Dbg::MatchType(basket.exceptionClass, pMod->exceptionOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700496 return false;
497 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200498 if ((basket.caught && !pMod->exceptionOnly.caught) ||
499 (!basket.caught && !pMod->exceptionOnly.uncaught)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 return false;
501 }
502 break;
503 case MK_FIELD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200504 if (!Dbg::MatchField(pMod->fieldOnly.refTypeId, pMod->fieldOnly.fieldId, basket.field)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700505 return false;
506 }
507 break;
508 case MK_STEP:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200509 if (!Dbg::MatchThread(pMod->step.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510 return false;
511 }
512 break;
513 case MK_INSTANCE_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200514 if (!Dbg::MatchInstance(pMod->instanceOnly.objectId, basket.thisPtr)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700515 return false;
516 }
517 break;
518 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800519 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700520 break;
521 }
522 }
523 return true;
524}
525
526/*
527 * Find all events of type "eventKind" with mods that match up with the
528 * rest of the arguments.
529 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700530 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531 * so this may be called multiple times for grouped events.
532 *
533 * DO NOT call this multiple times for the same eventKind, as Count mods are
534 * decremented during the scan.
535 */
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200536void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, const ModBasket& basket,
537 JdwpEvent** match_list, size_t* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700539 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200541 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800542 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700543 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544 (*pMatchCount)++;
545 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700546 }
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
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200777static void LogMatchingEventsAndThread(JdwpEvent** match_list, size_t match_count,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200778 ObjectId thread_id)
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200779 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
780 for (size_t i = 0; i < match_count; ++i) {
781 JdwpEvent* pEvent = match_list[i];
782 VLOG(jdwp) << "EVENT #" << i << ": " << pEvent->eventKind
783 << StringPrintf(" (requestId=%#" PRIx32 ")", pEvent->requestId);
784 }
785 std::string thread_name;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200786 JdwpError error = Dbg::GetThreadName(thread_id, &thread_name);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200787 if (error != JDWP::ERR_NONE) {
788 thread_name = "<unknown>";
789 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200790 VLOG(jdwp) << StringPrintf(" thread=%#" PRIx64, thread_id) << " " << thread_name;
791}
792
793static void SetJdwpLocationFromEventLocation(const JDWP::EventLocation* event_location,
794 JDWP::JdwpLocation* jdwp_location)
795 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
796 DCHECK(event_location != nullptr);
797 DCHECK(jdwp_location != nullptr);
798 Dbg::SetJdwpLocation(jdwp_location, event_location->method, event_location->dex_pc);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200799}
800
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801/*
802 * A location of interest has been reached. This handles:
803 * Breakpoint
804 * SingleStep
805 * MethodEntry
806 * MethodExit
807 * These four types must be grouped together in a single response. The
808 * "eventFlags" indicates the type of event(s) that have happened.
809 *
810 * Valid mods:
811 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
812 * LocationOnly (for breakpoint/step only)
813 * Step (for step only)
814 *
815 * Interesting test cases:
816 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
817 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
818 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
819 * - Single-step to a line with a breakpoint. Should get a single
820 * event message with both events in it.
821 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200822bool JdwpState::PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr,
823 int eventFlags, const JValue* returnValue) {
824 DCHECK(pLoc != nullptr);
825 DCHECK(pLoc->method != nullptr);
826 DCHECK_EQ(pLoc->method->IsStatic(), thisPtr == nullptr);
827
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700828 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700829 basket.pLoc = pLoc;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200830 basket.locationClass = pLoc->method->GetDeclaringClass();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700831 basket.thisPtr = thisPtr;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200832 basket.thread = Thread::Current();
833 basket.className = Dbg::GetClassName(basket.locationClass);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700834
835 /*
836 * On rare occasions we may need to execute interpreted code in the VM
837 * while handling a request from the debugger. Don't fire breakpoints
838 * while doing so. (I don't think we currently do this at all, so
839 * this is mostly paranoia.)
840 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200841 if (basket.thread == GetDebugThread()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800842 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700843 return false;
844 }
845
846 /*
847 * The debugger variable display tab may invoke the interpreter to format
848 * complex objects. We want to ignore breakpoints and method entry/exit
849 * traps while working on behalf of the debugger.
850 *
851 * If we don't ignore them, the VM will get hung up, because we'll
852 * suspend on a breakpoint while the debugger is still waiting for its
853 * method invocation to complete.
854 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800855 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800856 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700857 return false;
858 }
859
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200860 size_t match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700861 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700862 JdwpSuspendPolicy suspend_policy = SP_NONE;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200863 JdwpEvent** match_list = nullptr;
864 ObjectId thread_id = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -0800865 {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200866 {
867 MutexLock mu(Thread::Current(), event_list_lock_);
868 match_list = AllocMatchList(event_list_size_);
869 if ((eventFlags & Dbg::kBreakpoint) != 0) {
870 FindMatchingEvents(EK_BREAKPOINT, basket, match_list, &match_count);
871 }
872 if ((eventFlags & Dbg::kSingleStep) != 0) {
873 FindMatchingEvents(EK_SINGLE_STEP, basket, match_list, &match_count);
874 }
875 if ((eventFlags & Dbg::kMethodEntry) != 0) {
876 FindMatchingEvents(EK_METHOD_ENTRY, basket, match_list, &match_count);
877 }
878 if ((eventFlags & Dbg::kMethodExit) != 0) {
879 FindMatchingEvents(EK_METHOD_EXIT, basket, match_list, &match_count);
880 FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, basket, match_list, &match_count);
881 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800882 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800883 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700884 suspend_policy = scanSuspendPolicy(match_list, match_count);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200885
Sebastien Hertz6995c602014-09-09 12:10:13 +0200886 thread_id = Dbg::GetThreadId(basket.thread);
887 JDWP::JdwpLocation jdwp_location;
888 SetJdwpLocationFromEventLocation(pLoc, &jdwp_location);
889
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200890 if (VLOG_IS_ON(jdwp)) {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200891 LogMatchingEventsAndThread(match_list, match_count, thread_id);
892 VLOG(jdwp) << " location=" << jdwp_location;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200893 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
894 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800895
896 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700897 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800898 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800899
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200900 for (size_t i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700901 expandBufAdd1(pReq, match_list[i]->eventKind);
902 expandBufAdd4BE(pReq, match_list[i]->requestId);
Sebastien Hertz6995c602014-09-09 12:10:13 +0200903 expandBufAdd8BE(pReq, thread_id);
904 expandBufAddLocation(pReq, jdwp_location);
Jeff Hao579b0242013-11-18 13:16:49 -0800905 if (match_list[i]->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200906 Dbg::OutputMethodReturnValue(jdwp_location.method_id, returnValue, pReq);
Jeff Hao579b0242013-11-18 13:16:49 -0800907 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800908 }
909 }
910
Sebastien Hertz6995c602014-09-09 12:10:13 +0200911 {
912 MutexLock mu(Thread::Current(), event_list_lock_);
913 CleanupMatchList(match_list, match_count);
914 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800915 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700916
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100917 Dbg::ManageDeoptimization();
918
Sebastien Hertz6995c602014-09-09 12:10:13 +0200919 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800920 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921}
922
Sebastien Hertz6995c602014-09-09 12:10:13 +0200923bool JdwpState::PostFieldEvent(const EventLocation* pLoc, mirror::ArtField* field,
924 mirror::Object* this_object, const JValue* fieldValue,
925 bool is_modification) {
926 DCHECK(pLoc != nullptr);
927 DCHECK(field != nullptr);
928 DCHECK_EQ(fieldValue != nullptr, is_modification);
929 DCHECK_EQ(field->IsStatic(), this_object == nullptr);
930
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200931 ModBasket basket;
932 basket.pLoc = pLoc;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200933 basket.locationClass = pLoc->method->GetDeclaringClass();
934 basket.thisPtr = this_object;
935 basket.thread = Thread::Current();
936 basket.className = Dbg::GetClassName(basket.locationClass);
937 basket.field = field;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200938
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200939 if (InvokeInProgress()) {
940 VLOG(jdwp) << "Not posting field event during invoke";
941 return false;
942 }
943
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200944 size_t match_count = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200945 ExpandBuf* pReq = NULL;
946 JdwpSuspendPolicy suspend_policy = SP_NONE;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200947 JdwpEvent** match_list = nullptr;
948 ObjectId thread_id = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200949 {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200950 {
951 MutexLock mu(Thread::Current(), event_list_lock_);
952 match_list = AllocMatchList(event_list_size_);
953 if (is_modification) {
954 FindMatchingEvents(EK_FIELD_MODIFICATION, basket, match_list, &match_count);
955 } else {
956 FindMatchingEvents(EK_FIELD_ACCESS, basket, match_list, &match_count);
957 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200958 }
959 if (match_count != 0) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200960 suspend_policy = scanSuspendPolicy(match_list, match_count);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200961
Sebastien Hertz6995c602014-09-09 12:10:13 +0200962 thread_id = Dbg::GetThreadId(basket.thread);
963 ObjectRegistry* registry = Dbg::GetObjectRegistry();
964 ObjectId instance_id = registry->Add(basket.thisPtr);
965 RefTypeId field_type_id = registry->AddRefType(field->GetDeclaringClass());
966 FieldId field_id = Dbg::ToFieldId(field);
967 JDWP::JdwpLocation jdwp_location;
968 SetJdwpLocationFromEventLocation(pLoc, &jdwp_location);
969
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200970 if (VLOG_IS_ON(jdwp)) {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200971 LogMatchingEventsAndThread(match_list, match_count, thread_id);
972 VLOG(jdwp) << " location=" << jdwp_location;
973 VLOG(jdwp) << StringPrintf(" this=%#" PRIx64, instance_id);
974 VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, field_type_id) << " "
975 << Dbg::GetClassName(field_id);
976 VLOG(jdwp) << StringPrintf(" field=%#" PRIx32, field_id) << " "
977 << Dbg::GetFieldName(field_id);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200978 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
979 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200980
981 pReq = eventPrep();
982 expandBufAdd1(pReq, suspend_policy);
983 expandBufAdd4BE(pReq, match_count);
984
Sebastien Hertz6995c602014-09-09 12:10:13 +0200985 // Get field's reference type tag.
986 JDWP::JdwpTypeTag type_tag = Dbg::GetTypeTag(field->GetDeclaringClass());
987
988 // Get instance type tag.
989 uint8_t tag;
990 {
991 ScopedObjectAccessUnchecked soa(Thread::Current());
992 tag = Dbg::TagFromObject(soa, basket.thisPtr);
993 }
994
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200995 for (size_t i = 0; i < match_count; i++) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200996 expandBufAdd1(pReq, match_list[i]->eventKind);
997 expandBufAdd4BE(pReq, match_list[i]->requestId);
Sebastien Hertz6995c602014-09-09 12:10:13 +0200998 expandBufAdd8BE(pReq, thread_id);
999 expandBufAddLocation(pReq, jdwp_location);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001000 expandBufAdd1(pReq, type_tag);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001001 expandBufAddRefTypeId(pReq, field_type_id);
1002 expandBufAddFieldId(pReq, field_id);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001003 expandBufAdd1(pReq, tag);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001004 expandBufAddObjectId(pReq, instance_id);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001005 if (is_modification) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001006 Dbg::OutputFieldValue(field_id, fieldValue, pReq);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001007 }
1008 }
1009 }
1010
Sebastien Hertz6995c602014-09-09 12:10:13 +02001011 {
1012 MutexLock mu(Thread::Current(), event_list_lock_);
1013 CleanupMatchList(match_list, match_count);
1014 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001015 }
1016
1017 Dbg::ManageDeoptimization();
1018
Sebastien Hertz6995c602014-09-09 12:10:13 +02001019 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001020 return match_count != 0;
1021}
1022
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023/*
1024 * A thread is starting or stopping.
1025 *
1026 * Valid mods:
1027 * Count, ThreadOnly
1028 */
Sebastien Hertz6995c602014-09-09 12:10:13 +02001029bool JdwpState::PostThreadChange(Thread* thread, bool start) {
1030 CHECK_EQ(thread, Thread::Current());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031
1032 /*
1033 * I don't think this can happen.
1034 */
Elliott Hughes761928d2011-11-16 18:33:03 -08001035 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001036 LOG(WARNING) << "Not posting thread change during invoke";
1037 return false;
1038 }
1039
1040 ModBasket basket;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001041 basket.thread = thread;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001042
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001044 JdwpSuspendPolicy suspend_policy = SP_NONE;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001045 JdwpEvent** match_list = nullptr;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001046 size_t match_count = 0;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001047 ObjectId thread_id = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -07001048 {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001049 {
1050 // Don't allow the list to be updated while we scan it.
1051 MutexLock mu(Thread::Current(), event_list_lock_);
1052 match_list = AllocMatchList(event_list_size_);
1053 if (start) {
1054 FindMatchingEvents(EK_THREAD_START, basket, match_list, &match_count);
1055 } else {
1056 FindMatchingEvents(EK_THREAD_DEATH, basket, match_list, &match_count);
1057 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001058 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001059
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001060 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001061 suspend_policy = scanSuspendPolicy(match_list, match_count);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001062
Sebastien Hertz6995c602014-09-09 12:10:13 +02001063 thread_id = Dbg::GetThreadId(basket.thread);
1064
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001065 if (VLOG_IS_ON(jdwp)) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001066 LogMatchingEventsAndThread(match_list, match_count, thread_id);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001067 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1068 }
Elliott Hughes234ab152011-10-26 14:02:26 -07001069
1070 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001071 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001072 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -07001073
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001074 for (size_t i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001075 expandBufAdd1(pReq, match_list[i]->eventKind);
1076 expandBufAdd4BE(pReq, match_list[i]->requestId);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001077 expandBufAdd8BE(pReq, thread_id);
Elliott Hughes234ab152011-10-26 14:02:26 -07001078 }
1079 }
1080
Sebastien Hertz6995c602014-09-09 12:10:13 +02001081 {
1082 MutexLock mu(Thread::Current(), event_list_lock_);
1083 CleanupMatchList(match_list, match_count);
1084 }
Elliott Hughes234ab152011-10-26 14:02:26 -07001085 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001086
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001087 Dbg::ManageDeoptimization();
1088
Sebastien Hertz6995c602014-09-09 12:10:13 +02001089 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001090
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001091 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001092}
1093
1094/*
1095 * Send a polite "VM is dying" message to the debugger.
1096 *
1097 * Skips the usual "event token" stuff.
1098 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001099bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001100 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001101
1102 ExpandBuf* pReq = eventPrep();
1103 expandBufAdd1(pReq, SP_NONE);
1104 expandBufAdd4BE(pReq, 1);
1105
1106 expandBufAdd1(pReq, EK_VM_DEATH);
1107 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -08001108 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001109 return true;
1110}
1111
1112/*
1113 * An exception has been thrown. It may or may not have been caught.
1114 *
1115 * Valid mods:
1116 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
1117 * ExceptionOnly, InstanceOnly
1118 *
1119 * The "exceptionId" has not been added to the GC-visible object registry,
1120 * because there's a pretty good chance that we're not going to send it
1121 * up the debugger.
1122 */
Sebastien Hertz6995c602014-09-09 12:10:13 +02001123bool JdwpState::PostException(const EventLocation* pThrowLoc, mirror::Throwable* exception_object,
1124 const EventLocation* pCatchLoc, mirror::Object* thisPtr) {
1125 DCHECK(exception_object != nullptr);
1126 DCHECK(pThrowLoc != nullptr);
1127 DCHECK(pCatchLoc != nullptr);
1128 DCHECK(pThrowLoc->method != nullptr);
1129 DCHECK_EQ(pThrowLoc->method->IsStatic(), thisPtr == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001130
Sebastien Hertz6995c602014-09-09 12:10:13 +02001131 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132 basket.pLoc = pThrowLoc;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001133 basket.locationClass = pThrowLoc->method->GetDeclaringClass();
1134 basket.thread = Thread::Current();
1135 basket.className = Dbg::GetClassName(basket.locationClass);
1136 basket.exceptionClass = exception_object->GetClass();
1137 basket.caught = (pCatchLoc->method != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001138 basket.thisPtr = thisPtr;
1139
1140 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001141 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001142 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001143 return false;
1144 }
1145
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001146 size_t match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001147 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001148 JdwpSuspendPolicy suspend_policy = SP_NONE;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001149 JdwpEvent** match_list = nullptr;
1150 ObjectId thread_id = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -08001151 {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001152 {
1153 MutexLock mu(Thread::Current(), event_list_lock_);
1154 match_list = AllocMatchList(event_list_size_);
1155 FindMatchingEvents(EK_EXCEPTION, basket, match_list, &match_count);
1156 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001157 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001158 suspend_policy = scanSuspendPolicy(match_list, match_count);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001159
Sebastien Hertz6995c602014-09-09 12:10:13 +02001160 thread_id = Dbg::GetThreadId(basket.thread);
1161 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1162 ObjectId exceptionId = registry->Add(exception_object);
1163 JDWP::JdwpLocation jdwp_throw_location;
1164 JDWP::JdwpLocation jdwp_catch_location;
1165 SetJdwpLocationFromEventLocation(pThrowLoc, &jdwp_throw_location);
1166 SetJdwpLocationFromEventLocation(pCatchLoc, &jdwp_catch_location);
1167
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001168 if (VLOG_IS_ON(jdwp)) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001169 std::string exceptionClassName(PrettyDescriptor(exception_object->GetClass()));
1170
1171 LogMatchingEventsAndThread(match_list, match_count, thread_id);
1172 VLOG(jdwp) << " throwLocation=" << jdwp_throw_location;
1173 if (jdwp_catch_location.class_id == 0) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001174 VLOG(jdwp) << " catchLocation=uncaught";
1175 } else {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001176 VLOG(jdwp) << " catchLocation=" << jdwp_catch_location;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001177 }
Sebastien Hertz6995c602014-09-09 12:10:13 +02001178 VLOG(jdwp) << StringPrintf(" exception=%#" PRIx64, exceptionId) << " "
1179 << exceptionClassName;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001180 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1181 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001182
1183 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001184 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001185 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001186
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001187 for (size_t i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001188 expandBufAdd1(pReq, match_list[i]->eventKind);
1189 expandBufAdd4BE(pReq, match_list[i]->requestId);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001190 expandBufAdd8BE(pReq, thread_id);
1191 expandBufAddLocation(pReq, jdwp_throw_location);
Elliott Hughes761928d2011-11-16 18:33:03 -08001192 expandBufAdd1(pReq, JT_OBJECT);
1193 expandBufAdd8BE(pReq, exceptionId);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001194 expandBufAddLocation(pReq, jdwp_catch_location);
Elliott Hughes761928d2011-11-16 18:33:03 -08001195 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001196 }
1197
Sebastien Hertz6995c602014-09-09 12:10:13 +02001198 {
1199 MutexLock mu(Thread::Current(), event_list_lock_);
1200 CleanupMatchList(match_list, match_count);
1201 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001202 }
1203
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001204 Dbg::ManageDeoptimization();
1205
Sebastien Hertz6995c602014-09-09 12:10:13 +02001206 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001207
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001208 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001209}
1210
1211/*
1212 * Announce that a class has been loaded.
1213 *
1214 * Valid mods:
1215 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1216 */
Sebastien Hertz6995c602014-09-09 12:10:13 +02001217bool JdwpState::PostClassPrepare(mirror::Class* klass) {
1218 DCHECK(klass != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001219
Sebastien Hertz6995c602014-09-09 12:10:13 +02001220 ModBasket basket;
1221 basket.locationClass = klass;
1222 basket.thread = Thread::Current();
1223 basket.className = Dbg::GetClassName(basket.locationClass);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001224
1225 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001226 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001227 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001228 return false;
1229 }
1230
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001231 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -07001232 JdwpSuspendPolicy suspend_policy = SP_NONE;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001233 JdwpEvent** match_list = nullptr;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001234 size_t match_count = 0;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001235 ObjectId thread_id = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -08001236 {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001237 {
1238 MutexLock mu(Thread::Current(), event_list_lock_);
1239 match_list = AllocMatchList(event_list_size_);
1240 FindMatchingEvents(EK_CLASS_PREPARE, basket, match_list, &match_count);
1241 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001242 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001243 suspend_policy = scanSuspendPolicy(match_list, match_count);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001244
Sebastien Hertz6995c602014-09-09 12:10:13 +02001245 thread_id = Dbg::GetThreadId(basket.thread);
1246 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1247 RefTypeId class_id = registry->AddRefType(basket.locationClass);
1248
1249 // OLD-TODO - we currently always send both "verified" and "prepared" since
1250 // debuggers seem to like that. There might be some advantage to honesty,
1251 // since the class may not yet be verified.
1252 int status = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1253 JDWP::JdwpTypeTag tag = Dbg::GetTypeTag(basket.locationClass);
1254 std::string temp;
1255 std::string signature(basket.locationClass->GetDescriptor(&temp));
1256
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001257 if (VLOG_IS_ON(jdwp)) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001258 LogMatchingEventsAndThread(match_list, match_count, thread_id);
1259 VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, class_id) << " " << signature;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001260 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1261 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001262
Sebastien Hertz6995c602014-09-09 12:10:13 +02001263 if (thread_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -08001264 /*
1265 * JDWP says that, for a class prep in the debugger thread, we
Sebastien Hertz6995c602014-09-09 12:10:13 +02001266 * should set thread to null and if any threads were supposed
Elliott Hughes761928d2011-11-16 18:33:03 -08001267 * to be suspended then we suspend all other threads.
1268 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001269 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Sebastien Hertz6995c602014-09-09 12:10:13 +02001270 thread_id = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001271 if (suspend_policy == SP_EVENT_THREAD) {
1272 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001273 }
1274 }
1275
1276 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001277 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001278 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001279
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001280 for (size_t i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001281 expandBufAdd1(pReq, match_list[i]->eventKind);
1282 expandBufAdd4BE(pReq, match_list[i]->requestId);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001283 expandBufAdd8BE(pReq, thread_id);
Elliott Hughes761928d2011-11-16 18:33:03 -08001284 expandBufAdd1(pReq, tag);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001285 expandBufAdd8BE(pReq, class_id);
Elliott Hughes761928d2011-11-16 18:33:03 -08001286 expandBufAddUtf8String(pReq, signature);
1287 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001288 }
1289 }
Sebastien Hertz6995c602014-09-09 12:10:13 +02001290
1291 {
1292 MutexLock mu(Thread::Current(), event_list_lock_);
1293 CleanupMatchList(match_list, match_count);
1294 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001295 }
1296
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001297 Dbg::ManageDeoptimization();
1298
Sebastien Hertz6995c602014-09-09 12:10:13 +02001299 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001301 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001302}
1303
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001304/*
1305 * Send up a chunk of DDM data.
1306 *
1307 * While this takes the form of a JDWP "event", it doesn't interact with
1308 * other debugger traffic, and can't suspend the VM, so we skip all of
1309 * the fun event token gymnastics.
1310 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001311void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 uint8_t header[kJDWPHeaderLen + 8];
1313 size_t dataLen = 0;
1314
1315 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001316 CHECK_GT(iov_count, 0);
1317 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001318
1319 /*
1320 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1321 * this by creating a new copy of the vector with space for the header.
1322 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001323 std::vector<iovec> wrapiov;
1324 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001325 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001326 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001327 dataLen += iov[i].iov_len;
1328 }
1329
1330 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001331 Set4BE(header, sizeof(header) + dataLen);
1332 Set4BE(header+4, NextRequestSerial());
1333 Set1(header+8, 0); /* flags */
1334 Set1(header+9, kJDWPDdmCmdSet);
1335 Set1(header+10, kJDWPDdmCmd);
1336 Set4BE(header+11, type);
1337 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001338
1339 wrapiov[0].iov_base = header;
1340 wrapiov[0].iov_len = sizeof(header);
1341
Ian Rogers15bf2d32012-08-28 17:33:04 -07001342 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1343 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001344 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001345 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1346 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001347 for (size_t i = 0; i < kMutatorLock; ++i) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001348 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
1349 safe_to_release_mutator_lock_over_send = false;
1350 break;
1351 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001352 }
1353 }
1354 if (safe_to_release_mutator_lock_over_send) {
1355 // Change state to waiting to allow GC, ... while we're sending.
1356 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001357 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001358 self->TransitionFromSuspendedToRunnable();
1359 } else {
1360 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001361 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001362 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001363}
1364
1365} // namespace JDWP
1366
1367} // namespace art