blob: fc08d232740a52dfc325a2ba2c0136f2998118b7 [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) {
Sebastien Hertzf3928792014-11-17 19:00:37 +0100128 if (!Dbg::RequiresDeoptimization()) {
129 // We don't need deoptimization for debugging.
130 return false;
131 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100132 switch (eventKind) {
133 case EK_METHOD_ENTRY:
134 case EK_METHOD_EXIT:
135 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
136 case EK_SINGLE_STEP:
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200137 case EK_FIELD_ACCESS:
138 case EK_FIELD_MODIFICATION:
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139 return true;
140 default:
141 return false;
142 }
143}
144
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800145static uint32_t GetInstrumentationEventFor(JdwpEventKind eventKind) {
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200146 switch (eventKind) {
147 case EK_BREAKPOINT:
148 case EK_SINGLE_STEP:
149 return instrumentation::Instrumentation::kDexPcMoved;
150 case EK_EXCEPTION:
151 case EK_EXCEPTION_CATCH:
152 return instrumentation::Instrumentation::kExceptionCaught;
153 case EK_METHOD_ENTRY:
154 return instrumentation::Instrumentation::kMethodEntered;
155 case EK_METHOD_EXIT:
156 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
157 return instrumentation::Instrumentation::kMethodExited;
158 case EK_FIELD_ACCESS:
159 return instrumentation::Instrumentation::kFieldRead;
160 case EK_FIELD_MODIFICATION:
161 return instrumentation::Instrumentation::kFieldWritten;
162 default:
163 return 0;
164 }
165}
166
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167/*
168 * Add an event to the list. Ordering is not important.
169 *
170 * If something prevents the event from being registered, e.g. it's a
171 * single-step request on a thread that doesn't exist, the event will
172 * not be added to the list, and an appropriate error will be returned.
173 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800174JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200175 CHECK(pEvent != nullptr);
176 CHECK(pEvent->prev == nullptr);
177 CHECK(pEvent->next == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700178
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200179 {
180 /*
181 * If one or more "break"-type mods are used, register them with
182 * the interpreter.
183 */
184 DeoptimizationRequest req;
185 for (int i = 0; i < pEvent->modCount; i++) {
186 const JdwpEventMod* pMod = &pEvent->mods[i];
187 if (pMod->modKind == MK_LOCATION_ONLY) {
Sebastien Hertz033aabf2014-10-08 13:54:55 +0200188 // Should only concern breakpoint, field access, field modification, step, and exception
189 // events.
190 // However breakpoint requires specific handling. Field access, field modification and step
191 // events need full deoptimization to be reported while exception event is reported during
192 // exception handling.
193 if (pEvent->eventKind == EK_BREAKPOINT) {
194 Dbg::WatchLocation(&pMod->locationOnly.loc, &req);
195 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200196 } else if (pMod->modKind == MK_STEP) {
197 /* should only be for EK_SINGLE_STEP; should only be one */
198 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
199 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
200 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
201 if (status != ERR_NONE) {
202 return status;
203 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800204 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200206 if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700207 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
208 CHECK(req.Method() == nullptr);
209 req.SetKind(DeoptimizationRequest::kFullDeoptimization);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200210 }
211 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200213 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
214 if (instrumentation_event != 0) {
215 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700216 req.SetKind(DeoptimizationRequest::kRegisterForEvent);
217 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200218 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100219 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700220
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100221 {
222 /*
223 * Add to list.
224 */
225 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200226 if (event_list_ != nullptr) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100227 pEvent->next = event_list_;
228 event_list_->prev = pEvent;
229 }
230 event_list_ = pEvent;
231 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700232 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100233
234 Dbg::ManageDeoptimization();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235
236 return ERR_NONE;
237}
238
239/*
240 * Remove an event from the list. This will also remove the event from
241 * any optimization tables, e.g. breakpoints.
242 *
243 * Does not free the JdwpEvent.
244 *
245 * Grab the eventLock before calling here.
246 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800247void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200248 if (pEvent->prev == nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700250 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251
Elliott Hughesf8349362012-06-18 15:00:06 -0700252 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253 } else {
254 pEvent->prev->next = pEvent->next;
255 }
256
Sebastien Hertz7d955652014-10-22 10:57:10 +0200257 if (pEvent->next != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258 pEvent->next->prev = pEvent->prev;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200259 pEvent->next = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200261 pEvent->prev = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200263 {
264 /*
265 * Unhook us from the interpreter, if necessary.
266 */
267 DeoptimizationRequest req;
268 for (int i = 0; i < pEvent->modCount; i++) {
269 JdwpEventMod* pMod = &pEvent->mods[i];
270 if (pMod->modKind == MK_LOCATION_ONLY) {
Sebastien Hertz033aabf2014-10-08 13:54:55 +0200271 // Like in RegisterEvent, we need specific handling for breakpoint only.
272 if (pEvent->eventKind == EK_BREAKPOINT) {
273 Dbg::UnwatchLocation(&pMod->locationOnly.loc, &req);
274 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200275 }
276 if (pMod->modKind == MK_STEP) {
277 /* should only be for EK_SINGLE_STEP; should only be one */
278 Dbg::UnconfigureStep(pMod->step.threadId);
279 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200281 if (pEvent->eventKind == EK_SINGLE_STEP) {
282 // Special case for single-steps where we want to avoid the slow pattern deoptimize/undeoptimize
283 // loop between each single-step. In a IDE, this would happens each time the user click on the
284 // "single-step" button. Here we delay the full undeoptimization to the next resume
285 // (VM.Resume or ThreadReference.Resume) or the end of the debugging session (VM.Dispose or
286 // runtime shutdown).
287 // Therefore, in a singles-stepping sequence, only the first single-step will trigger a full
288 // deoptimization and only the last single-step will trigger a full undeoptimization.
289 Dbg::DelayFullUndeoptimization();
290 } else if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700291 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
292 CHECK(req.Method() == nullptr);
293 req.SetKind(DeoptimizationRequest::kFullUndeoptimization);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700294 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200295 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700296 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200297 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
298 if (instrumentation_event != 0) {
299 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700300 req.SetKind(DeoptimizationRequest::kUnregisterForEvent);
301 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200302 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100303 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700304
Elliott Hughesf8349362012-06-18 15:00:06 -0700305 --event_list_size_;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200306 CHECK(event_list_size_ != 0 || event_list_ == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700307}
308
309/*
310 * Remove the event with the given ID from the list.
311 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700312 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800313void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100314 bool found = false;
315 {
316 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700317
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100318 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
319 if (pEvent->requestId == requestId) {
320 found = true;
321 UnregisterEvent(pEvent);
322 EventFree(pEvent);
323 break; /* there can be only one with a given ID */
324 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700326 }
327
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100328 if (found) {
329 Dbg::ManageDeoptimization();
330 } else {
Sebastien Hertzf272af42014-09-18 10:20:42 +0200331 // Failure to find the event isn't really an error. For instance, it looks like Eclipse will
332 // try to be extra careful and will explicitly remove one-off single-step events (using a
333 // 'count' event modifier of 1). So the event may have already been removed as part of the
334 // event notification (see JdwpState::CleanupMatchList).
335 VLOG(jdwp) << StringPrintf("No match when removing event reqId=0x%04x", requestId);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100336 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700337}
338
339/*
340 * Remove all entries from the event list.
341 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800342void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700343 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700344
Elliott Hughesf8349362012-06-18 15:00:06 -0700345 JdwpEvent* pEvent = event_list_;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200346 while (pEvent != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347 JdwpEvent* pNextEvent = pEvent->next;
348
Elliott Hughes761928d2011-11-16 18:33:03 -0800349 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700350 EventFree(pEvent);
351 pEvent = pNextEvent;
352 }
353
Sebastien Hertz7d955652014-10-22 10:57:10 +0200354 event_list_ = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355}
356
357/*
358 * Allocate a JdwpEvent struct with enough space to hold the specified
359 * number of mod records.
360 */
361JdwpEvent* EventAlloc(int numMods) {
362 JdwpEvent* newEvent;
363 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
364 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
365 memset(newEvent, 0, allocSize);
366 return newEvent;
367}
368
369/*
370 * Free a JdwpEvent.
371 *
372 * Do not call this until the event has been removed from the list.
373 */
374void EventFree(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200375 if (pEvent == nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700376 return;
377 }
378
379 /* make sure it was removed from the list */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200380 CHECK(pEvent->prev == nullptr);
381 CHECK(pEvent->next == nullptr);
Elliott Hughesf8349362012-06-18 15:00:06 -0700382 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383
384 /*
385 * Free any hairy bits in the mods.
386 */
387 for (int i = 0; i < pEvent->modCount; i++) {
388 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
389 free(pEvent->mods[i].classMatch.classPattern);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200390 pEvent->mods[i].classMatch.classPattern = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700391 }
392 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
393 free(pEvent->mods[i].classExclude.classPattern);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200394 pEvent->mods[i].classExclude.classPattern = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700395 }
396 }
397
398 free(pEvent);
399}
400
401/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700402 * Run through the list and remove any entries with an expired "count" mod
Sebastien Hertz7d955652014-10-22 10:57:10 +0200403 * from the event list.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200405void JdwpState::CleanupMatchList(const std::vector<JdwpEvent*>& match_list) {
406 for (JdwpEvent* pEvent : match_list) {
407 for (int i = 0; i < pEvent->modCount; ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200409 VLOG(jdwp) << StringPrintf("##### Removing expired event (requestId=%#" PRIx32 ")",
410 pEvent->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800411 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 EventFree(pEvent);
413 break;
414 }
415 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417}
418
419/*
420 * Match a string against a "restricted regular expression", which is just
421 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
422 *
423 * ("Restricted name globbing" might have been a better term.)
424 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800425static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800426 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800429 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430 return false;
431 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800432 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800434 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800436 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700437 }
438}
439
440/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 * See if the event's mods match up with the contents of "basket".
442 *
443 * If we find a Count mod before rejecting an event, we decrement it. We
444 * need to do this even if later mods cause us to ignore the event.
445 */
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200446static bool ModsMatch(JdwpEvent* pEvent, const ModBasket& basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700447 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700448 JdwpEventMod* pMod = pEvent->mods;
449
450 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
451 switch (pMod->modKind) {
452 case MK_COUNT:
453 CHECK_GT(pMod->count.count, 0);
454 pMod->count.count--;
Sebastien Hertz43207792014-04-15 16:03:27 +0200455 if (pMod->count.count > 0) {
456 return false;
457 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700458 break;
459 case MK_CONDITIONAL:
460 CHECK(false); // should not be getting these
461 break;
462 case MK_THREAD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200463 if (!Dbg::MatchThread(pMod->threadOnly.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 return false;
465 }
466 break;
467 case MK_CLASS_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200468 if (!Dbg::MatchType(basket.locationClass, pMod->classOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 return false;
470 }
471 break;
472 case MK_CLASS_MATCH:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200473 if (!PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474 return false;
475 }
476 break;
477 case MK_CLASS_EXCLUDE:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200478 if (PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 return false;
480 }
481 break;
482 case MK_LOCATION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200483 if (!Dbg::MatchLocation(pMod->locationOnly.loc, *basket.pLoc)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484 return false;
485 }
486 break;
487 case MK_EXCEPTION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200488 if (pMod->exceptionOnly.refTypeId != 0 &&
489 !Dbg::MatchType(basket.exceptionClass, pMod->exceptionOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700490 return false;
491 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200492 if ((basket.caught && !pMod->exceptionOnly.caught) ||
493 (!basket.caught && !pMod->exceptionOnly.uncaught)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700494 return false;
495 }
496 break;
497 case MK_FIELD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200498 if (!Dbg::MatchField(pMod->fieldOnly.refTypeId, pMod->fieldOnly.fieldId, basket.field)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700499 return false;
500 }
501 break;
502 case MK_STEP:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200503 if (!Dbg::MatchThread(pMod->step.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 return false;
505 }
506 break;
507 case MK_INSTANCE_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200508 if (!Dbg::MatchInstance(pMod->instanceOnly.objectId, basket.thisPtr)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 return false;
510 }
511 break;
512 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800513 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514 break;
515 }
516 }
517 return true;
518}
519
520/*
Sebastien Hertz7d955652014-10-22 10:57:10 +0200521 * Find all events of type "event_kind" with mods that match up with the
522 * rest of the arguments while holding the event list lock. This method
523 * is used by FindMatchingEvents below.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524 *
Sebastien Hertz7d955652014-10-22 10:57:10 +0200525 * Found events are appended to "match_list" so this may be called multiple times for grouped
526 * events.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 *
528 * DO NOT call this multiple times for the same eventKind, as Count mods are
529 * decremented during the scan.
530 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200531void JdwpState::FindMatchingEventsLocked(JdwpEventKind event_kind, const ModBasket& basket,
532 std::vector<JdwpEvent*>* match_list) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200533 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200534 if (pEvent->eventKind == event_kind && ModsMatch(pEvent, basket)) {
535 match_list->push_back(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700536 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537 }
538}
539
540/*
Sebastien Hertz7d955652014-10-22 10:57:10 +0200541 * Find all events of type "event_kind" with mods that match up with the
542 * rest of the arguments and return true if at least one event matches,
543 * false otherwise.
544 *
545 * Found events are appended to "match_list" so this may be called multiple
546 * times for grouped events.
547 *
548 * DO NOT call this multiple times for the same eventKind, as Count mods are
549 * decremented during the scan.
550 */
551bool JdwpState::FindMatchingEvents(JdwpEventKind event_kind, const ModBasket& basket,
552 std::vector<JdwpEvent*>* match_list) {
553 MutexLock mu(Thread::Current(), event_list_lock_);
554 match_list->reserve(event_list_size_);
555 FindMatchingEventsLocked(event_kind, basket, match_list);
556 return !match_list->empty();
557}
558
559/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700560 * Scan through the list of matches and determine the most severe
561 * suspension policy.
562 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200563static JdwpSuspendPolicy ScanSuspendPolicy(const std::vector<JdwpEvent*>& match_list) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700564 JdwpSuspendPolicy policy = SP_NONE;
565
Sebastien Hertz7d955652014-10-22 10:57:10 +0200566 for (JdwpEvent* pEvent : match_list) {
567 if (pEvent->suspend_policy > policy) {
568 policy = pEvent->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700569 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570 }
571
572 return policy;
573}
574
575/*
576 * Three possibilities:
577 * SP_NONE - do nothing
578 * SP_EVENT_THREAD - suspend ourselves
579 * SP_ALL - suspend everybody except JDWP support thread
580 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700581void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700582 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
583 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700584 return;
585 }
586
Elliott Hughesf8349362012-06-18 15:00:06 -0700587 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700588 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700589 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700590 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700591 }
592
593 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700594 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800595 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700596 return;
597 }
598
599 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
600 while (true) {
601 pReq->ready = true;
602 Dbg::SuspendSelf();
603 pReq->ready = false;
604
605 /*
606 * The JDWP thread has told us (and possibly all other threads) to
607 * resume. See if it has left anything in our DebugInvokeReq mailbox.
608 */
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100609 if (!pReq->invoke_needed) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800610 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611 break;
612 }
613
614 /* grab this before posting/suspending again */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100615 AcquireJdwpTokenForEvent(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616
Elliott Hughesd07986f2011-12-06 18:27:45 -0800617 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700618 Dbg::ExecuteMethod(pReq);
619
Elliott Hughes475fc232011-10-25 15:00:35 -0700620 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621 }
622}
623
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
625 ObjectId threadId) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200626 Thread* const self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 self->AssertThreadSuspensionIsAllowable();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200628 CHECK(pReq != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 /* send request and possibly suspend ourselves */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200630 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
631 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
632 if (suspend_policy != SP_NONE) {
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100633 AcquireJdwpTokenForEvent(threadId);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200635 EventFinish(pReq);
Sebastien Hertz813b9602015-02-24 14:56:59 +0100636 {
637 // Before suspending, we change our state to kSuspended so the debugger sees us as RUNNING.
638 ScopedThreadStateChange stsc(self, kSuspended);
639 SuspendByPolicy(suspend_policy, thread_self_id);
640 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200641 self->TransitionFromSuspendedToRunnable();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642}
643
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700644/*
645 * Determine if there is a method invocation in progress in the current
646 * thread.
647 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700648 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700649 * state. If set, we're in the process of invoking a method.
650 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800651bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Sebastien Hertzd38667a2013-11-25 15:43:54 +0100653 return pReq->invoke_needed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700654}
655
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100656void JdwpState::AcquireJdwpTokenForCommand() {
657 CHECK_EQ(Thread::Current(), GetDebugThread()) << "Expected debugger thread";
658 SetWaitForJdwpToken(debug_thread_id_);
659}
660
661void JdwpState::ReleaseJdwpTokenForCommand() {
662 CHECK_EQ(Thread::Current(), GetDebugThread()) << "Expected debugger thread";
663 ClearWaitForJdwpToken();
664}
665
666void JdwpState::AcquireJdwpTokenForEvent(ObjectId threadId) {
667 CHECK_NE(Thread::Current(), GetDebugThread()) << "Expected event thread";
668 CHECK_NE(debug_thread_id_, threadId) << "Not expected debug thread";
669 SetWaitForJdwpToken(threadId);
670}
671
672void JdwpState::ReleaseJdwpTokenForEvent() {
673 CHECK_NE(Thread::Current(), GetDebugThread()) << "Expected event thread";
674 ClearWaitForJdwpToken();
675}
676
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700677/*
678 * We need the JDWP thread to hold off on doing stuff while we post an
679 * event and then suspend ourselves.
680 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700681 * This could go to sleep waiting for another thread, so it's important
682 * that the thread be marked as VMWAIT before calling here.
683 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100684void JdwpState::SetWaitForJdwpToken(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700685 bool waited = false;
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100686 Thread* const self = Thread::Current();
687 CHECK_NE(threadId, 0u);
688 CHECK_NE(self->GetState(), kRunnable);
689 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700690
691 /* this is held for very brief periods; contention is unlikely */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100692 MutexLock mu(self, jdwp_token_lock_);
693
694 CHECK_NE(jdwp_token_owner_thread_id_, threadId) << "Thread is already holding event thread lock";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695
696 /*
697 * If another thread is already doing stuff, wait for it. This can
698 * go to sleep indefinitely.
699 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100700 while (jdwp_token_owner_thread_id_ != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800701 VLOG(jdwp) << StringPrintf("event in progress (%#" PRIx64 "), %#" PRIx64 " sleeping",
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100702 jdwp_token_owner_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700703 waited = true;
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100704 jdwp_token_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700705 }
706
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100707 if (waited || threadId != debug_thread_id_) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800708 VLOG(jdwp) << StringPrintf("event token grabbed (%#" PRIx64 ")", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700709 }
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100710 jdwp_token_owner_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700711}
712
713/*
714 * Clear the threadId and signal anybody waiting.
715 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100716void JdwpState::ClearWaitForJdwpToken() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700717 /*
718 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100719 * function is called by Dbg::SuspendSelf(), and the transition back
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720 * to RUNNING would confuse it.
721 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100722 Thread* const self = Thread::Current();
723 MutexLock mu(self, jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100725 CHECK_NE(jdwp_token_owner_thread_id_, 0U);
726 VLOG(jdwp) << StringPrintf("cleared event token (%#" PRIx64 ")", jdwp_token_owner_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100728 jdwp_token_owner_thread_id_ = 0;
729 jdwp_token_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730}
731
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700732/*
733 * Prep an event. Allocates storage for the message and leaves space for
734 * the header.
735 */
736static ExpandBuf* eventPrep() {
737 ExpandBuf* pReq = expandBufAlloc();
738 expandBufAddSpace(pReq, kJDWPHeaderLen);
739 return pReq;
740}
741
742/*
743 * Write the header into the buffer and send the packet off to the debugger.
744 *
745 * Takes ownership of "pReq" (currently discards it).
746 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800747void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700748 uint8_t* buf = expandBufGetBuffer(pReq);
749
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700750 Set4BE(buf, expandBufGetLength(pReq));
Sebastien Hertz7d955652014-10-22 10:57:10 +0200751 Set4BE(buf + 4, NextRequestSerial());
752 Set1(buf + 8, 0); /* flags */
753 Set1(buf + 9, kJdwpEventCommandSet);
754 Set1(buf + 10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700755
Elliott Hughes761928d2011-11-16 18:33:03 -0800756 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757
758 expandBufFree(pReq);
759}
760
761
762/*
763 * Tell the debugger that we have finished initializing. This is always
764 * sent, even if the debugger hasn't requested it.
765 *
766 * This should be sent "before the main thread is started and before
767 * any application code has been executed". The thread ID in the message
768 * must be for the main thread.
769 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200770void JdwpState::PostVMStart() {
771 JdwpSuspendPolicy suspend_policy = (options_->suspend) ? SP_ALL : SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772 ObjectId threadId = Dbg::GetThreadSelfId();
773
Sebastien Hertz7d955652014-10-22 10:57:10 +0200774 VLOG(jdwp) << "EVENT: " << EK_VM_START;
775 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776
Elliott Hughes761928d2011-11-16 18:33:03 -0800777 ExpandBuf* pReq = eventPrep();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200778 expandBufAdd1(pReq, suspend_policy);
779 expandBufAdd4BE(pReq, 1);
780 expandBufAdd1(pReq, EK_VM_START);
781 expandBufAdd4BE(pReq, 0); /* requestId */
782 expandBufAddObjectId(pReq, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700783
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100784 Dbg::ManageDeoptimization();
785
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700786 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700787 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700788}
789
Sebastien Hertz7d955652014-10-22 10:57:10 +0200790static void LogMatchingEventsAndThread(const std::vector<JdwpEvent*> match_list,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200791 ObjectId thread_id)
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200792 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200793 for (size_t i = 0, e = match_list.size(); i < e; ++i) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200794 JdwpEvent* pEvent = match_list[i];
795 VLOG(jdwp) << "EVENT #" << i << ": " << pEvent->eventKind
796 << StringPrintf(" (requestId=%#" PRIx32 ")", pEvent->requestId);
797 }
798 std::string thread_name;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200799 JdwpError error = Dbg::GetThreadName(thread_id, &thread_name);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200800 if (error != JDWP::ERR_NONE) {
801 thread_name = "<unknown>";
802 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200803 VLOG(jdwp) << StringPrintf(" thread=%#" PRIx64, thread_id) << " " << thread_name;
804}
805
806static void SetJdwpLocationFromEventLocation(const JDWP::EventLocation* event_location,
807 JDWP::JdwpLocation* jdwp_location)
808 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
809 DCHECK(event_location != nullptr);
810 DCHECK(jdwp_location != nullptr);
811 Dbg::SetJdwpLocation(jdwp_location, event_location->method, event_location->dex_pc);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200812}
813
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700814/*
815 * A location of interest has been reached. This handles:
816 * Breakpoint
817 * SingleStep
818 * MethodEntry
819 * MethodExit
820 * These four types must be grouped together in a single response. The
821 * "eventFlags" indicates the type of event(s) that have happened.
822 *
823 * Valid mods:
824 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
825 * LocationOnly (for breakpoint/step only)
826 * Step (for step only)
827 *
828 * Interesting test cases:
829 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
830 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
831 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
832 * - Single-step to a line with a breakpoint. Should get a single
833 * event message with both events in it.
834 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200835void JdwpState::PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200836 int eventFlags, const JValue* returnValue) {
837 DCHECK(pLoc != nullptr);
838 DCHECK(pLoc->method != nullptr);
839 DCHECK_EQ(pLoc->method->IsStatic(), thisPtr == nullptr);
840
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700841 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700842 basket.pLoc = pLoc;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200843 basket.locationClass = pLoc->method->GetDeclaringClass();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700844 basket.thisPtr = thisPtr;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200845 basket.thread = Thread::Current();
846 basket.className = Dbg::GetClassName(basket.locationClass);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700847
848 /*
849 * On rare occasions we may need to execute interpreted code in the VM
850 * while handling a request from the debugger. Don't fire breakpoints
851 * while doing so. (I don't think we currently do this at all, so
852 * this is mostly paranoia.)
853 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200854 if (basket.thread == GetDebugThread()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800855 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200856 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700857 }
858
859 /*
860 * The debugger variable display tab may invoke the interpreter to format
861 * complex objects. We want to ignore breakpoints and method entry/exit
862 * traps while working on behalf of the debugger.
863 *
864 * If we don't ignore them, the VM will get hung up, because we'll
865 * suspend on a breakpoint while the debugger is still waiting for its
866 * method invocation to complete.
867 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800868 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800869 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200870 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700871 }
872
Sebastien Hertz7d955652014-10-22 10:57:10 +0200873 std::vector<JdwpEvent*> match_list;
Elliott Hughes761928d2011-11-16 18:33:03 -0800874 {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200875 // We use the locked version because we have multiple possible match events.
876 MutexLock mu(Thread::Current(), event_list_lock_);
877 match_list.reserve(event_list_size_);
878 if ((eventFlags & Dbg::kBreakpoint) != 0) {
879 FindMatchingEventsLocked(EK_BREAKPOINT, basket, &match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800880 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200881 if ((eventFlags & Dbg::kSingleStep) != 0) {
882 FindMatchingEventsLocked(EK_SINGLE_STEP, basket, &match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800883 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200884 if ((eventFlags & Dbg::kMethodEntry) != 0) {
885 FindMatchingEventsLocked(EK_METHOD_ENTRY, basket, &match_list);
Sebastien Hertz6995c602014-09-09 12:10:13 +0200886 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200887 if ((eventFlags & Dbg::kMethodExit) != 0) {
888 FindMatchingEventsLocked(EK_METHOD_EXIT, basket, &match_list);
889 FindMatchingEventsLocked(EK_METHOD_EXIT_WITH_RETURN_VALUE, basket, &match_list);
890 }
891 }
892 if (match_list.empty()) {
893 // No matching event.
894 return;
895 }
896 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
897
898 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
899 JDWP::JdwpLocation jdwp_location;
900 SetJdwpLocationFromEventLocation(pLoc, &jdwp_location);
901
902 if (VLOG_IS_ON(jdwp)) {
903 LogMatchingEventsAndThread(match_list, thread_id);
904 VLOG(jdwp) << " location=" << jdwp_location;
905 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
906 }
907
908 ExpandBuf* pReq = eventPrep();
909 expandBufAdd1(pReq, suspend_policy);
910 expandBufAdd4BE(pReq, match_list.size());
911
912 for (const JdwpEvent* pEvent : match_list) {
913 expandBufAdd1(pReq, pEvent->eventKind);
914 expandBufAdd4BE(pReq, pEvent->requestId);
915 expandBufAddObjectId(pReq, thread_id);
916 expandBufAddLocation(pReq, jdwp_location);
917 if (pEvent->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
918 Dbg::OutputMethodReturnValue(jdwp_location.method_id, returnValue, pReq);
919 }
920 }
921
922 {
923 MutexLock mu(Thread::Current(), event_list_lock_);
924 CleanupMatchList(match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800925 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700926
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100927 Dbg::ManageDeoptimization();
928
Sebastien Hertz6995c602014-09-09 12:10:13 +0200929 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700930}
931
Sebastien Hertz7d955652014-10-22 10:57:10 +0200932void JdwpState::PostFieldEvent(const EventLocation* pLoc, mirror::ArtField* field,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200933 mirror::Object* this_object, const JValue* fieldValue,
934 bool is_modification) {
935 DCHECK(pLoc != nullptr);
936 DCHECK(field != nullptr);
937 DCHECK_EQ(fieldValue != nullptr, is_modification);
938 DCHECK_EQ(field->IsStatic(), this_object == nullptr);
939
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200940 ModBasket basket;
941 basket.pLoc = pLoc;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200942 basket.locationClass = pLoc->method->GetDeclaringClass();
943 basket.thisPtr = this_object;
944 basket.thread = Thread::Current();
945 basket.className = Dbg::GetClassName(basket.locationClass);
946 basket.field = field;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200947
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200948 if (InvokeInProgress()) {
949 VLOG(jdwp) << "Not posting field event during invoke";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200950 return;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200951 }
952
Sebastien Hertz7d955652014-10-22 10:57:10 +0200953 std::vector<JdwpEvent*> match_list;
954 const JdwpEventKind match_kind = (is_modification) ? EK_FIELD_MODIFICATION : EK_FIELD_ACCESS;
955 if (!FindMatchingEvents(match_kind, basket, &match_list)) {
956 // No matching event.
957 return;
958 }
959
960 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
961 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
962 ObjectRegistry* registry = Dbg::GetObjectRegistry();
963 ObjectId instance_id = registry->Add(basket.thisPtr);
964 RefTypeId field_type_id = registry->AddRefType(field->GetDeclaringClass());
965 FieldId field_id = Dbg::ToFieldId(field);
966 JDWP::JdwpLocation jdwp_location;
967 SetJdwpLocationFromEventLocation(pLoc, &jdwp_location);
968
969 if (VLOG_IS_ON(jdwp)) {
970 LogMatchingEventsAndThread(match_list, thread_id);
971 VLOG(jdwp) << " location=" << jdwp_location;
972 VLOG(jdwp) << StringPrintf(" this=%#" PRIx64, instance_id);
973 VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, field_type_id) << " "
974 << Dbg::GetClassName(field_id);
975 VLOG(jdwp) << StringPrintf(" field=%#" PRIx32, field_id) << " "
976 << Dbg::GetFieldName(field_id);
977 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
978 }
979
980 ExpandBuf* pReq = eventPrep();
981 expandBufAdd1(pReq, suspend_policy);
982 expandBufAdd4BE(pReq, match_list.size());
983
984 // Get field's reference type tag.
985 JDWP::JdwpTypeTag type_tag = Dbg::GetTypeTag(field->GetDeclaringClass());
986
987 // Get instance type tag.
988 uint8_t tag;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200989 {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200990 ScopedObjectAccessUnchecked soa(Thread::Current());
991 tag = Dbg::TagFromObject(soa, basket.thisPtr);
992 }
993
994 for (const JdwpEvent* pEvent : match_list) {
995 expandBufAdd1(pReq, pEvent->eventKind);
996 expandBufAdd4BE(pReq, pEvent->requestId);
997 expandBufAddObjectId(pReq, thread_id);
998 expandBufAddLocation(pReq, jdwp_location);
999 expandBufAdd1(pReq, type_tag);
1000 expandBufAddRefTypeId(pReq, field_type_id);
1001 expandBufAddFieldId(pReq, field_id);
1002 expandBufAdd1(pReq, tag);
1003 expandBufAddObjectId(pReq, instance_id);
1004 if (is_modification) {
1005 Dbg::OutputFieldValue(field_id, fieldValue, pReq);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001006 }
Sebastien Hertz7d955652014-10-22 10:57:10 +02001007 }
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001008
Sebastien Hertz7d955652014-10-22 10:57:10 +02001009 {
1010 MutexLock mu(Thread::Current(), event_list_lock_);
1011 CleanupMatchList(match_list);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001012 }
1013
1014 Dbg::ManageDeoptimization();
1015
Sebastien Hertz6995c602014-09-09 12:10:13 +02001016 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001017}
1018
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019/*
1020 * A thread is starting or stopping.
1021 *
1022 * Valid mods:
1023 * Count, ThreadOnly
1024 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001025void JdwpState::PostThreadChange(Thread* thread, bool start) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001026 CHECK_EQ(thread, Thread::Current());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001027
1028 /*
1029 * I don't think this can happen.
1030 */
Elliott Hughes761928d2011-11-16 18:33:03 -08001031 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001032 LOG(WARNING) << "Not posting thread change during invoke";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001033 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034 }
1035
Sebastien Hertz107e7572014-12-18 11:13:15 +01001036 // We need the java.lang.Thread object associated to the starting/ending
1037 // thread to get its JDWP id. Therefore we can't report event if there
1038 // is no Java peer. This happens when the runtime shuts down and re-attaches
1039 // the current thread without creating a Java peer.
1040 if (thread->GetPeer() == nullptr) {
1041 return;
1042 }
1043
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001044 ModBasket basket;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001045 basket.thread = thread;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046
Sebastien Hertz7d955652014-10-22 10:57:10 +02001047 std::vector<JdwpEvent*> match_list;
1048 const JdwpEventKind match_kind = (start) ? EK_THREAD_START : EK_THREAD_DEATH;
1049 if (!FindMatchingEvents(match_kind, basket, &match_list)) {
1050 // No matching event.
1051 return;
1052 }
1053
1054 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1055 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1056
1057 if (VLOG_IS_ON(jdwp)) {
1058 LogMatchingEventsAndThread(match_list, thread_id);
1059 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1060 }
1061
1062 ExpandBuf* pReq = eventPrep();
1063 expandBufAdd1(pReq, suspend_policy);
1064 expandBufAdd4BE(pReq, match_list.size());
1065
1066 for (const JdwpEvent* pEvent : match_list) {
1067 expandBufAdd1(pReq, pEvent->eventKind);
1068 expandBufAdd4BE(pReq, pEvent->requestId);
1069 expandBufAdd8BE(pReq, thread_id);
1070 }
1071
Elliott Hughes234ab152011-10-26 14:02:26 -07001072 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001073 MutexLock mu(Thread::Current(), event_list_lock_);
1074 CleanupMatchList(match_list);
Elliott Hughes234ab152011-10-26 14:02:26 -07001075 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001076
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001077 Dbg::ManageDeoptimization();
1078
Sebastien Hertz6995c602014-09-09 12:10:13 +02001079 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001080}
1081
1082/*
1083 * Send a polite "VM is dying" message to the debugger.
1084 *
1085 * Skips the usual "event token" stuff.
1086 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001087bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001088 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001089
1090 ExpandBuf* pReq = eventPrep();
1091 expandBufAdd1(pReq, SP_NONE);
1092 expandBufAdd4BE(pReq, 1);
1093
1094 expandBufAdd1(pReq, EK_VM_DEATH);
1095 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -08001096 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001097 return true;
1098}
1099
1100/*
1101 * An exception has been thrown. It may or may not have been caught.
1102 *
1103 * Valid mods:
1104 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
1105 * ExceptionOnly, InstanceOnly
1106 *
1107 * The "exceptionId" has not been added to the GC-visible object registry,
1108 * because there's a pretty good chance that we're not going to send it
1109 * up the debugger.
1110 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001111void JdwpState::PostException(const EventLocation* pThrowLoc, mirror::Throwable* exception_object,
Sebastien Hertz6995c602014-09-09 12:10:13 +02001112 const EventLocation* pCatchLoc, mirror::Object* thisPtr) {
1113 DCHECK(exception_object != nullptr);
1114 DCHECK(pThrowLoc != nullptr);
1115 DCHECK(pCatchLoc != nullptr);
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001116 if (pThrowLoc->method != nullptr) {
1117 DCHECK_EQ(pThrowLoc->method->IsStatic(), thisPtr == nullptr);
1118 } else {
1119 VLOG(jdwp) << "Unexpected: exception event with empty throw location";
1120 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001121
Sebastien Hertz6995c602014-09-09 12:10:13 +02001122 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001123 basket.pLoc = pThrowLoc;
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001124 if (pThrowLoc->method != nullptr) {
1125 basket.locationClass = pThrowLoc->method->GetDeclaringClass();
1126 } else {
1127 basket.locationClass = nullptr;
1128 }
Sebastien Hertz6995c602014-09-09 12:10:13 +02001129 basket.thread = Thread::Current();
1130 basket.className = Dbg::GetClassName(basket.locationClass);
1131 basket.exceptionClass = exception_object->GetClass();
1132 basket.caught = (pCatchLoc->method != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001133 basket.thisPtr = thisPtr;
1134
1135 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001136 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001137 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001138 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139 }
1140
Sebastien Hertz7d955652014-10-22 10:57:10 +02001141 std::vector<JdwpEvent*> match_list;
1142 if (!FindMatchingEvents(EK_EXCEPTION, basket, &match_list)) {
1143 // No matching event.
1144 return;
1145 }
1146
1147 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1148 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1149 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1150 ObjectId exceptionId = registry->Add(exception_object);
1151 JDWP::JdwpLocation jdwp_throw_location;
1152 JDWP::JdwpLocation jdwp_catch_location;
1153 SetJdwpLocationFromEventLocation(pThrowLoc, &jdwp_throw_location);
1154 SetJdwpLocationFromEventLocation(pCatchLoc, &jdwp_catch_location);
1155
1156 if (VLOG_IS_ON(jdwp)) {
1157 std::string exceptionClassName(PrettyDescriptor(exception_object->GetClass()));
1158
1159 LogMatchingEventsAndThread(match_list, thread_id);
1160 VLOG(jdwp) << " throwLocation=" << jdwp_throw_location;
1161 if (jdwp_catch_location.class_id == 0) {
1162 VLOG(jdwp) << " catchLocation=uncaught";
1163 } else {
1164 VLOG(jdwp) << " catchLocation=" << jdwp_catch_location;
1165 }
1166 VLOG(jdwp) << StringPrintf(" exception=%#" PRIx64, exceptionId) << " "
1167 << exceptionClassName;
1168 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1169 }
1170
1171 ExpandBuf* pReq = eventPrep();
1172 expandBufAdd1(pReq, suspend_policy);
1173 expandBufAdd4BE(pReq, match_list.size());
1174
1175 for (const JdwpEvent* pEvent : match_list) {
1176 expandBufAdd1(pReq, pEvent->eventKind);
1177 expandBufAdd4BE(pReq, pEvent->requestId);
1178 expandBufAddObjectId(pReq, thread_id);
1179 expandBufAddLocation(pReq, jdwp_throw_location);
1180 expandBufAdd1(pReq, JT_OBJECT);
1181 expandBufAddObjectId(pReq, exceptionId);
1182 expandBufAddLocation(pReq, jdwp_catch_location);
1183 }
1184
Elliott Hughes761928d2011-11-16 18:33:03 -08001185 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001186 MutexLock mu(Thread::Current(), event_list_lock_);
1187 CleanupMatchList(match_list);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188 }
1189
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001190 Dbg::ManageDeoptimization();
1191
Sebastien Hertz6995c602014-09-09 12:10:13 +02001192 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193}
1194
1195/*
1196 * Announce that a class has been loaded.
1197 *
1198 * Valid mods:
1199 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1200 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001201void JdwpState::PostClassPrepare(mirror::Class* klass) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001202 DCHECK(klass != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001203
Sebastien Hertz6995c602014-09-09 12:10:13 +02001204 ModBasket basket;
1205 basket.locationClass = klass;
1206 basket.thread = Thread::Current();
1207 basket.className = Dbg::GetClassName(basket.locationClass);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001208
1209 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001210 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001211 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001212 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001213 }
1214
Sebastien Hertz7d955652014-10-22 10:57:10 +02001215 std::vector<JdwpEvent*> match_list;
1216 if (!FindMatchingEvents(EK_CLASS_PREPARE, basket, &match_list)) {
1217 // No matching event.
1218 return;
1219 }
1220
1221 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1222 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1223 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1224 RefTypeId class_id = registry->AddRefType(basket.locationClass);
1225
1226 // OLD-TODO - we currently always send both "verified" and "prepared" since
1227 // debuggers seem to like that. There might be some advantage to honesty,
1228 // since the class may not yet be verified.
1229 int status = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1230 JDWP::JdwpTypeTag tag = Dbg::GetTypeTag(basket.locationClass);
1231 std::string temp;
1232 std::string signature(basket.locationClass->GetDescriptor(&temp));
1233
1234 if (VLOG_IS_ON(jdwp)) {
1235 LogMatchingEventsAndThread(match_list, thread_id);
1236 VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, class_id) << " " << signature;
1237 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1238 }
1239
1240 if (thread_id == debug_thread_id_) {
1241 /*
1242 * JDWP says that, for a class prep in the debugger thread, we
1243 * should set thread to null and if any threads were supposed
1244 * to be suspended then we suspend all other threads.
1245 */
1246 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
1247 thread_id = 0;
1248 if (suspend_policy == SP_EVENT_THREAD) {
1249 suspend_policy = SP_ALL;
1250 }
1251 }
1252
1253 ExpandBuf* pReq = eventPrep();
1254 expandBufAdd1(pReq, suspend_policy);
1255 expandBufAdd4BE(pReq, match_list.size());
1256
1257 for (const JdwpEvent* pEvent : match_list) {
1258 expandBufAdd1(pReq, pEvent->eventKind);
1259 expandBufAdd4BE(pReq, pEvent->requestId);
1260 expandBufAddObjectId(pReq, thread_id);
1261 expandBufAdd1(pReq, tag);
1262 expandBufAddRefTypeId(pReq, class_id);
1263 expandBufAddUtf8String(pReq, signature);
1264 expandBufAdd4BE(pReq, status);
1265 }
1266
Elliott Hughes761928d2011-11-16 18:33:03 -08001267 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001268 MutexLock mu(Thread::Current(), event_list_lock_);
1269 CleanupMatchList(match_list);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270 }
1271
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001272 Dbg::ManageDeoptimization();
1273
Sebastien Hertz6995c602014-09-09 12:10:13 +02001274 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001275}
1276
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001277/*
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001278 * Setup the header for a chunk of DDM data.
1279 */
1280void JdwpState::SetupChunkHeader(uint32_t type, size_t data_len, size_t header_size,
1281 uint8_t* out_header) {
1282 CHECK_EQ(header_size, static_cast<size_t>(kJDWPHeaderLen + 8));
1283 /* form the header (JDWP plus DDMS) */
1284 Set4BE(out_header, header_size + data_len);
1285 Set4BE(out_header + 4, NextRequestSerial());
1286 Set1(out_header + 8, 0); /* flags */
1287 Set1(out_header + 9, kJDWPDdmCmdSet);
1288 Set1(out_header + 10, kJDWPDdmCmd);
1289 Set4BE(out_header + 11, type);
1290 Set4BE(out_header + 15, data_len);
1291}
1292
1293/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001294 * Send up a chunk of DDM data.
1295 *
1296 * While this takes the form of a JDWP "event", it doesn't interact with
1297 * other debugger traffic, and can't suspend the VM, so we skip all of
1298 * the fun event token gymnastics.
1299 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001300void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001301 uint8_t header[kJDWPHeaderLen + 8] = { 0 };
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001302 size_t dataLen = 0;
1303
Sebastien Hertz7d955652014-10-22 10:57:10 +02001304 CHECK(iov != nullptr);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001305 CHECK_GT(iov_count, 0);
1306 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001307
1308 /*
1309 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1310 * this by creating a new copy of the vector with space for the header.
1311 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001312 std::vector<iovec> wrapiov;
1313 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001314 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001315 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316 dataLen += iov[i].iov_len;
1317 }
1318
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001319 SetupChunkHeader(type, dataLen, sizeof(header), header);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320
1321 wrapiov[0].iov_base = header;
1322 wrapiov[0].iov_len = sizeof(header);
1323
Ian Rogers15bf2d32012-08-28 17:33:04 -07001324 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1325 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001327 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1328 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001329 for (size_t i = 0; i < kMutatorLock; ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001330 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001331 safe_to_release_mutator_lock_over_send = false;
1332 break;
1333 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001334 }
1335 }
1336 if (safe_to_release_mutator_lock_over_send) {
1337 // Change state to waiting to allow GC, ... while we're sending.
1338 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001339 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001340 self->TransitionFromSuspendedToRunnable();
1341 } else {
1342 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001343 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001344 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001345}
1346
1347} // namespace JDWP
1348
1349} // namespace art