blob: 612af8bc9989b7509892a3f0a617c156ac9c7068 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080027#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "debugger.h"
29#include "jdwp/jdwp_constants.h"
30#include "jdwp/jdwp_expand_buf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031#include "jdwp/jdwp_priv.h"
Sebastien Hertz6995c602014-09-09 12:10:13 +020032#include "jdwp/object_registry.h"
Sebastien Hertz6995c602014-09-09 12:10:13 +020033#include "scoped_thread_state_change.h"
Ian Rogers693ff612013-02-01 10:56:12 -080034#include "thread-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080035
Sebastien Hertz261bc042015-04-08 09:36:07 +020036#include "handle_scope-inl.h"
37
Elliott Hughes872d4ec2011-10-21 17:07:15 -070038/*
39General notes:
40
41The event add/remove stuff usually happens from the debugger thread,
42in response to requests from the debugger, but can also happen as the
43result of an event in an arbitrary thread (e.g. an event with a "count"
44mod expires). It's important to keep the event list locked when processing
45events.
46
47Event posting can happen from any thread. The JDWP thread will not usually
48post anything but VM start/death, but if a JDWP request causes a class
49to be loaded, the ClassPrepare event will come from the JDWP thread.
50
51
52We can have serialization issues when we post an event to the debugger.
53For example, a thread could send an "I hit a breakpoint and am suspending
54myself" message to the debugger. Before it manages to suspend itself, the
55debugger's response ("not interested, resume thread") arrives and is
56processed. We try to resume a thread that hasn't yet suspended.
57
58This means that, after posting an event to the debugger, we need to wait
59for the event thread to suspend itself (and, potentially, all other threads)
60before processing any additional requests from the debugger. While doing
61so we need to be aware that multiple threads may be hitting breakpoints
62or other events simultaneously, so we either need to wait for all of them
63or serialize the events with each other.
64
65The current mechanism works like this:
66 Event thread:
67 - If I'm going to suspend, grab the "I am posting an event" token. Wait
68 for it if it's not currently available.
69 - Post the event to the debugger.
70 - If appropriate, suspend others and then myself. As part of suspending
71 myself, release the "I am posting" token.
72 JDWP thread:
73 - When an event arrives, see if somebody is posting an event. If so,
74 sleep until we can acquire the "I am posting an event" token. Release
75 it immediately and continue processing -- the event we have already
76 received should not interfere with other events that haven't yet
77 been posted.
78
79Some care must be taken to avoid deadlock:
80
81 - thread A and thread B exit near-simultaneously, and post thread-death
82 events with a "suspend all" clause
83 - thread A gets the event token, thread B sits and waits for it
84 - thread A wants to suspend all other threads, but thread B is waiting
85 for the token and can't be suspended
86
87So we need to mark thread B in such a way that thread A doesn't wait for it.
88
89If we just bracket the "grab event token" call with a change to VMWAIT
90before sleeping, the switch back to RUNNING state when we get the token
91will cause thread B to suspend (remember, thread A's global suspend is
92still in force, even after it releases the token). Suspending while
93holding the event token is very bad, because it prevents the JDWP thread
94from processing incoming messages.
95
96We need to change to VMWAIT state at the *start* of posting an event,
97and stay there until we either finish posting the event or decide to
98put ourselves to sleep. That way we don't interfere with anyone else and
99don't allow anyone else to interfere with us.
100*/
101
102
103#define kJdwpEventCommandSet 64
104#define kJdwpCompositeCommand 100
105
106namespace art {
107
108namespace JDWP {
109
110/*
111 * Stuff to compare against when deciding if a mod matches. Only the
112 * values for mods valid for the event being evaluated will be filled in.
113 * The rest will be zeroed.
Sebastien Hertz261bc042015-04-08 09:36:07 +0200114 * Must be allocated on the stack only. This is enforced by removing the
115 * operator new.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700116 */
117struct ModBasket {
Sebastien Hertz261bc042015-04-08 09:36:07 +0200118 explicit ModBasket(Thread* self)
119 : hs(self), pLoc(nullptr), thread(self),
120 locationClass(hs.NewHandle<mirror::Class>(nullptr)),
121 exceptionClass(hs.NewHandle<mirror::Class>(nullptr)),
122 caught(false),
123 field(nullptr),
124 thisPtr(hs.NewHandle<mirror::Object>(nullptr)) { }
jeffhao162fd332013-01-08 16:21:01 -0800125
Sebastien Hertz261bc042015-04-08 09:36:07 +0200126 StackHandleScope<3> hs;
127 const EventLocation* pLoc; /* LocationOnly */
128 std::string className; /* ClassMatch/ClassExclude */
129 Thread* const thread; /* ThreadOnly */
130 MutableHandle<mirror::Class> locationClass; /* ClassOnly */
131 MutableHandle<mirror::Class> exceptionClass; /* ExceptionOnly */
132 bool caught; /* ExceptionOnly */
133 ArtField* field; /* FieldOnly */
134 MutableHandle<mirror::Object> thisPtr; /* InstanceOnly */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135 /* nothing for StepOnly -- handled differently */
Sebastien Hertz261bc042015-04-08 09:36:07 +0200136
137 private:
138 DISALLOW_ALLOCATION(); // forbids allocation on the heap.
139 DISALLOW_IMPLICIT_CONSTRUCTORS(ModBasket);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140};
141
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100142static bool NeedsFullDeoptimization(JdwpEventKind eventKind) {
Sebastien Hertzf3928792014-11-17 19:00:37 +0100143 if (!Dbg::RequiresDeoptimization()) {
144 // We don't need deoptimization for debugging.
145 return false;
146 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100147 switch (eventKind) {
148 case EK_METHOD_ENTRY:
149 case EK_METHOD_EXIT:
150 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200151 case EK_FIELD_ACCESS:
152 case EK_FIELD_MODIFICATION:
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100153 return true;
154 default:
155 return false;
156 }
157}
158
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200159// Returns the instrumentation event the DebugInstrumentationListener must
160// listen to in order to properly report the given JDWP event to the debugger.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800161static uint32_t GetInstrumentationEventFor(JdwpEventKind eventKind) {
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200162 switch (eventKind) {
163 case EK_BREAKPOINT:
164 case EK_SINGLE_STEP:
165 return instrumentation::Instrumentation::kDexPcMoved;
166 case EK_EXCEPTION:
167 case EK_EXCEPTION_CATCH:
168 return instrumentation::Instrumentation::kExceptionCaught;
169 case EK_METHOD_ENTRY:
170 return instrumentation::Instrumentation::kMethodEntered;
171 case EK_METHOD_EXIT:
172 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
173 return instrumentation::Instrumentation::kMethodExited;
174 case EK_FIELD_ACCESS:
175 return instrumentation::Instrumentation::kFieldRead;
176 case EK_FIELD_MODIFICATION:
177 return instrumentation::Instrumentation::kFieldWritten;
178 default:
179 return 0;
180 }
181}
182
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700183/*
184 * Add an event to the list. Ordering is not important.
185 *
186 * If something prevents the event from being registered, e.g. it's a
187 * single-step request on a thread that doesn't exist, the event will
188 * not be added to the list, and an appropriate error will be returned.
189 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800190JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200191 CHECK(pEvent != nullptr);
192 CHECK(pEvent->prev == nullptr);
193 CHECK(pEvent->next == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200195 {
196 /*
197 * If one or more "break"-type mods are used, register them with
198 * the interpreter.
199 */
200 DeoptimizationRequest req;
201 for (int i = 0; i < pEvent->modCount; i++) {
202 const JdwpEventMod* pMod = &pEvent->mods[i];
203 if (pMod->modKind == MK_LOCATION_ONLY) {
Sebastien Hertz033aabf2014-10-08 13:54:55 +0200204 // Should only concern breakpoint, field access, field modification, step, and exception
205 // events.
206 // However breakpoint requires specific handling. Field access, field modification and step
207 // events need full deoptimization to be reported while exception event is reported during
208 // exception handling.
209 if (pEvent->eventKind == EK_BREAKPOINT) {
210 Dbg::WatchLocation(&pMod->locationOnly.loc, &req);
211 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200212 } else if (pMod->modKind == MK_STEP) {
213 /* should only be for EK_SINGLE_STEP; should only be one */
214 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
215 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
216 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
217 if (status != ERR_NONE) {
218 return status;
219 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800220 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200222 if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700223 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
224 CHECK(req.Method() == nullptr);
225 req.SetKind(DeoptimizationRequest::kFullDeoptimization);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200226 }
227 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200229 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
230 if (instrumentation_event != 0) {
231 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700232 req.SetKind(DeoptimizationRequest::kRegisterForEvent);
233 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200234 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100235 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700236
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100237 {
238 /*
239 * Add to list.
240 */
241 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200242 if (event_list_ != nullptr) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100243 pEvent->next = event_list_;
244 event_list_->prev = pEvent;
245 }
246 event_list_ = pEvent;
247 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100249
250 Dbg::ManageDeoptimization();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251
252 return ERR_NONE;
253}
254
255/*
256 * Remove an event from the list. This will also remove the event from
257 * any optimization tables, e.g. breakpoints.
258 *
259 * Does not free the JdwpEvent.
260 *
261 * Grab the eventLock before calling here.
262 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800263void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200264 if (pEvent->prev == nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700266 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700267
Elliott Hughesf8349362012-06-18 15:00:06 -0700268 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269 } else {
270 pEvent->prev->next = pEvent->next;
271 }
272
Sebastien Hertz7d955652014-10-22 10:57:10 +0200273 if (pEvent->next != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700274 pEvent->next->prev = pEvent->prev;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200275 pEvent->next = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700276 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200277 pEvent->prev = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700278
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200279 {
280 /*
281 * Unhook us from the interpreter, if necessary.
282 */
283 DeoptimizationRequest req;
284 for (int i = 0; i < pEvent->modCount; i++) {
285 JdwpEventMod* pMod = &pEvent->mods[i];
286 if (pMod->modKind == MK_LOCATION_ONLY) {
Sebastien Hertz033aabf2014-10-08 13:54:55 +0200287 // Like in RegisterEvent, we need specific handling for breakpoint only.
288 if (pEvent->eventKind == EK_BREAKPOINT) {
289 Dbg::UnwatchLocation(&pMod->locationOnly.loc, &req);
290 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200291 }
292 if (pMod->modKind == MK_STEP) {
293 /* should only be for EK_SINGLE_STEP; should only be one */
294 Dbg::UnconfigureStep(pMod->step.threadId);
295 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700296 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200297 if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700298 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
299 CHECK(req.Method() == nullptr);
300 req.SetKind(DeoptimizationRequest::kFullUndeoptimization);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700301 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200302 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200304 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
305 if (instrumentation_event != 0) {
306 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700307 req.SetKind(DeoptimizationRequest::kUnregisterForEvent);
308 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200309 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100310 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700311
Elliott Hughesf8349362012-06-18 15:00:06 -0700312 --event_list_size_;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200313 CHECK(event_list_size_ != 0 || event_list_ == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314}
315
316/*
317 * Remove the event with the given ID from the list.
318 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800320void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100321 bool found = false;
322 {
323 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100325 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
326 if (pEvent->requestId == requestId) {
327 found = true;
328 UnregisterEvent(pEvent);
329 EventFree(pEvent);
330 break; /* there can be only one with a given ID */
331 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700332 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 }
334
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100335 if (found) {
336 Dbg::ManageDeoptimization();
337 } else {
Sebastien Hertzf272af42014-09-18 10:20:42 +0200338 // Failure to find the event isn't really an error. For instance, it looks like Eclipse will
339 // try to be extra careful and will explicitly remove one-off single-step events (using a
340 // 'count' event modifier of 1). So the event may have already been removed as part of the
341 // event notification (see JdwpState::CleanupMatchList).
342 VLOG(jdwp) << StringPrintf("No match when removing event reqId=0x%04x", requestId);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100343 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700344}
345
346/*
347 * Remove all entries from the event list.
348 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800349void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700350 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351
Elliott Hughesf8349362012-06-18 15:00:06 -0700352 JdwpEvent* pEvent = event_list_;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200353 while (pEvent != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354 JdwpEvent* pNextEvent = pEvent->next;
355
Elliott Hughes761928d2011-11-16 18:33:03 -0800356 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700357 EventFree(pEvent);
358 pEvent = pNextEvent;
359 }
360
Sebastien Hertz7d955652014-10-22 10:57:10 +0200361 event_list_ = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700362}
363
364/*
365 * Allocate a JdwpEvent struct with enough space to hold the specified
366 * number of mod records.
367 */
368JdwpEvent* EventAlloc(int numMods) {
369 JdwpEvent* newEvent;
370 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
371 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
372 memset(newEvent, 0, allocSize);
373 return newEvent;
374}
375
376/*
377 * Free a JdwpEvent.
378 *
379 * Do not call this until the event has been removed from the list.
380 */
381void EventFree(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200382 if (pEvent == nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383 return;
384 }
385
386 /* make sure it was removed from the list */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200387 CHECK(pEvent->prev == nullptr);
388 CHECK(pEvent->next == nullptr);
Elliott Hughesf8349362012-06-18 15:00:06 -0700389 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700390
391 /*
392 * Free any hairy bits in the mods.
393 */
394 for (int i = 0; i < pEvent->modCount; i++) {
395 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
396 free(pEvent->mods[i].classMatch.classPattern);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200397 pEvent->mods[i].classMatch.classPattern = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700398 }
399 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
400 free(pEvent->mods[i].classExclude.classPattern);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200401 pEvent->mods[i].classExclude.classPattern = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700402 }
403 }
404
405 free(pEvent);
406}
407
408/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700409 * Run through the list and remove any entries with an expired "count" mod
Sebastien Hertz7d955652014-10-22 10:57:10 +0200410 * from the event list.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700411 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200412void JdwpState::CleanupMatchList(const std::vector<JdwpEvent*>& match_list) {
413 for (JdwpEvent* pEvent : match_list) {
414 for (int i = 0; i < pEvent->modCount; ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200416 VLOG(jdwp) << StringPrintf("##### Removing expired event (requestId=%#" PRIx32 ")",
417 pEvent->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800418 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700419 EventFree(pEvent);
420 break;
421 }
422 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424}
425
426/*
427 * Match a string against a "restricted regular expression", which is just
428 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
429 *
430 * ("Restricted name globbing" might have been a better term.)
431 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800432static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800433 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800436 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700437 return false;
438 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800439 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800441 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800443 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700444 }
445}
446
447/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700448 * See if the event's mods match up with the contents of "basket".
449 *
450 * If we find a Count mod before rejecting an event, we decrement it. We
451 * need to do this even if later mods cause us to ignore the event.
452 */
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200453static bool ModsMatch(JdwpEvent* pEvent, const ModBasket& basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700455 JdwpEventMod* pMod = pEvent->mods;
456
457 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
458 switch (pMod->modKind) {
459 case MK_COUNT:
460 CHECK_GT(pMod->count.count, 0);
461 pMod->count.count--;
Sebastien Hertz43207792014-04-15 16:03:27 +0200462 if (pMod->count.count > 0) {
463 return false;
464 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700465 break;
466 case MK_CONDITIONAL:
467 CHECK(false); // should not be getting these
468 break;
469 case MK_THREAD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200470 if (!Dbg::MatchThread(pMod->threadOnly.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700471 return false;
472 }
473 break;
474 case MK_CLASS_ONLY:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200475 if (!Dbg::MatchType(basket.locationClass.Get(), pMod->classOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700476 return false;
477 }
478 break;
479 case MK_CLASS_MATCH:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200480 if (!PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700481 return false;
482 }
483 break;
484 case MK_CLASS_EXCLUDE:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200485 if (PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486 return false;
487 }
488 break;
489 case MK_LOCATION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200490 if (!Dbg::MatchLocation(pMod->locationOnly.loc, *basket.pLoc)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700491 return false;
492 }
493 break;
494 case MK_EXCEPTION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200495 if (pMod->exceptionOnly.refTypeId != 0 &&
Sebastien Hertz261bc042015-04-08 09:36:07 +0200496 !Dbg::MatchType(basket.exceptionClass.Get(), pMod->exceptionOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497 return false;
498 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200499 if ((basket.caught && !pMod->exceptionOnly.caught) ||
500 (!basket.caught && !pMod->exceptionOnly.uncaught)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700501 return false;
502 }
503 break;
504 case MK_FIELD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200505 if (!Dbg::MatchField(pMod->fieldOnly.refTypeId, pMod->fieldOnly.fieldId, basket.field)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 return false;
507 }
508 break;
509 case MK_STEP:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200510 if (!Dbg::MatchThread(pMod->step.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700511 return false;
512 }
513 break;
514 case MK_INSTANCE_ONLY:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200515 if (!Dbg::MatchInstance(pMod->instanceOnly.objectId, basket.thisPtr.Get())) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516 return false;
517 }
518 break;
519 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800520 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 break;
522 }
523 }
524 return true;
525}
526
527/*
Sebastien Hertz7d955652014-10-22 10:57:10 +0200528 * Find all events of type "event_kind" with mods that match up with the
529 * rest of the arguments while holding the event list lock. This method
530 * is used by FindMatchingEvents below.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531 *
Sebastien Hertz7d955652014-10-22 10:57:10 +0200532 * Found events are appended to "match_list" so this may be called multiple times for grouped
533 * events.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700534 *
535 * DO NOT call this multiple times for the same eventKind, as Count mods are
536 * decremented during the scan.
537 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200538void JdwpState::FindMatchingEventsLocked(JdwpEventKind event_kind, const ModBasket& basket,
539 std::vector<JdwpEvent*>* match_list) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200540 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200541 if (pEvent->eventKind == event_kind && ModsMatch(pEvent, basket)) {
542 match_list->push_back(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700543 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544 }
545}
546
547/*
Sebastien Hertz7d955652014-10-22 10:57:10 +0200548 * Find all events of type "event_kind" with mods that match up with the
549 * rest of the arguments and return true if at least one event matches,
550 * false otherwise.
551 *
552 * Found events are appended to "match_list" so this may be called multiple
553 * times for grouped events.
554 *
555 * DO NOT call this multiple times for the same eventKind, as Count mods are
556 * decremented during the scan.
557 */
558bool JdwpState::FindMatchingEvents(JdwpEventKind event_kind, const ModBasket& basket,
559 std::vector<JdwpEvent*>* match_list) {
560 MutexLock mu(Thread::Current(), event_list_lock_);
561 match_list->reserve(event_list_size_);
562 FindMatchingEventsLocked(event_kind, basket, match_list);
563 return !match_list->empty();
564}
565
566/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700567 * Scan through the list of matches and determine the most severe
568 * suspension policy.
569 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200570static JdwpSuspendPolicy ScanSuspendPolicy(const std::vector<JdwpEvent*>& match_list) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571 JdwpSuspendPolicy policy = SP_NONE;
572
Sebastien Hertz7d955652014-10-22 10:57:10 +0200573 for (JdwpEvent* pEvent : match_list) {
574 if (pEvent->suspend_policy > policy) {
575 policy = pEvent->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700576 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700577 }
578
579 return policy;
580}
581
582/*
583 * Three possibilities:
584 * SP_NONE - do nothing
585 * SP_EVENT_THREAD - suspend ourselves
586 * SP_ALL - suspend everybody except JDWP support thread
587 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700589 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
590 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700591 return;
592 }
593
Elliott Hughesf8349362012-06-18 15:00:06 -0700594 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700595 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700596 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700597 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700598 }
599
600 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700601 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800602 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700603 return;
604 }
605
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700606 while (true) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607 Dbg::SuspendSelf();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700608
609 /*
610 * The JDWP thread has told us (and possibly all other threads) to
611 * resume. See if it has left anything in our DebugInvokeReq mailbox.
612 */
Sebastien Hertz1558b572015-02-25 15:05:59 +0100613 DebugInvokeReq* const pReq = Dbg::GetInvokeReq();
614 if (pReq == nullptr) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800615 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616 break;
617 }
618
619 /* grab this before posting/suspending again */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100620 AcquireJdwpTokenForEvent(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622 Dbg::ExecuteMethod(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700623 }
624}
625
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
627 ObjectId threadId) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200628 Thread* const self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 self->AssertThreadSuspensionIsAllowable();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200630 CHECK(pReq != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 /* send request and possibly suspend ourselves */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200632 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
633 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
634 if (suspend_policy != SP_NONE) {
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100635 AcquireJdwpTokenForEvent(threadId);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200637 EventFinish(pReq);
Sebastien Hertz813b9602015-02-24 14:56:59 +0100638 {
639 // Before suspending, we change our state to kSuspended so the debugger sees us as RUNNING.
640 ScopedThreadStateChange stsc(self, kSuspended);
641 SuspendByPolicy(suspend_policy, thread_self_id);
642 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200643 self->TransitionFromSuspendedToRunnable();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644}
645
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646/*
647 * Determine if there is a method invocation in progress in the current
648 * thread.
649 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700650 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651 * state. If set, we're in the process of invoking a method.
652 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800653bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700654 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Sebastien Hertz1558b572015-02-25 15:05:59 +0100655 return pReq != nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700656}
657
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100658void JdwpState::AcquireJdwpTokenForCommand() {
659 CHECK_EQ(Thread::Current(), GetDebugThread()) << "Expected debugger thread";
660 SetWaitForJdwpToken(debug_thread_id_);
661}
662
663void JdwpState::ReleaseJdwpTokenForCommand() {
664 CHECK_EQ(Thread::Current(), GetDebugThread()) << "Expected debugger thread";
665 ClearWaitForJdwpToken();
666}
667
668void JdwpState::AcquireJdwpTokenForEvent(ObjectId threadId) {
669 CHECK_NE(Thread::Current(), GetDebugThread()) << "Expected event thread";
670 CHECK_NE(debug_thread_id_, threadId) << "Not expected debug thread";
671 SetWaitForJdwpToken(threadId);
672}
673
674void JdwpState::ReleaseJdwpTokenForEvent() {
675 CHECK_NE(Thread::Current(), GetDebugThread()) << "Expected event thread";
676 ClearWaitForJdwpToken();
677}
678
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679/*
680 * We need the JDWP thread to hold off on doing stuff while we post an
681 * event and then suspend ourselves.
682 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700683 * This could go to sleep waiting for another thread, so it's important
684 * that the thread be marked as VMWAIT before calling here.
685 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100686void JdwpState::SetWaitForJdwpToken(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700687 bool waited = false;
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100688 Thread* const self = Thread::Current();
689 CHECK_NE(threadId, 0u);
690 CHECK_NE(self->GetState(), kRunnable);
691 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692
693 /* this is held for very brief periods; contention is unlikely */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100694 MutexLock mu(self, jdwp_token_lock_);
695
696 CHECK_NE(jdwp_token_owner_thread_id_, threadId) << "Thread is already holding event thread lock";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700697
698 /*
699 * If another thread is already doing stuff, wait for it. This can
700 * go to sleep indefinitely.
701 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100702 while (jdwp_token_owner_thread_id_ != 0) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800703 VLOG(jdwp) << StringPrintf("event in progress (%#" PRIx64 "), %#" PRIx64 " sleeping",
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100704 jdwp_token_owner_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700705 waited = true;
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100706 jdwp_token_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707 }
708
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100709 if (waited || threadId != debug_thread_id_) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800710 VLOG(jdwp) << StringPrintf("event token grabbed (%#" PRIx64 ")", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700711 }
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100712 jdwp_token_owner_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700713}
714
715/*
716 * Clear the threadId and signal anybody waiting.
717 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100718void JdwpState::ClearWaitForJdwpToken() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700719 /*
720 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100721 * function is called by Dbg::SuspendSelf(), and the transition back
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700722 * to RUNNING would confuse it.
723 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100724 Thread* const self = Thread::Current();
725 MutexLock mu(self, jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100727 CHECK_NE(jdwp_token_owner_thread_id_, 0U);
728 VLOG(jdwp) << StringPrintf("cleared event token (%#" PRIx64 ")", jdwp_token_owner_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700729
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100730 jdwp_token_owner_thread_id_ = 0;
731 jdwp_token_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700732}
733
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700734/*
735 * Prep an event. Allocates storage for the message and leaves space for
736 * the header.
737 */
738static ExpandBuf* eventPrep() {
739 ExpandBuf* pReq = expandBufAlloc();
740 expandBufAddSpace(pReq, kJDWPHeaderLen);
741 return pReq;
742}
743
744/*
745 * Write the header into the buffer and send the packet off to the debugger.
746 *
747 * Takes ownership of "pReq" (currently discards it).
748 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800749void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750 uint8_t* buf = expandBufGetBuffer(pReq);
751
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700752 Set4BE(buf, expandBufGetLength(pReq));
Sebastien Hertz7d955652014-10-22 10:57:10 +0200753 Set4BE(buf + 4, NextRequestSerial());
754 Set1(buf + 8, 0); /* flags */
755 Set1(buf + 9, kJdwpEventCommandSet);
756 Set1(buf + 10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757
Elliott Hughes761928d2011-11-16 18:33:03 -0800758 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700759
760 expandBufFree(pReq);
761}
762
763
764/*
765 * Tell the debugger that we have finished initializing. This is always
766 * sent, even if the debugger hasn't requested it.
767 *
768 * This should be sent "before the main thread is started and before
769 * any application code has been executed". The thread ID in the message
770 * must be for the main thread.
771 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200772void JdwpState::PostVMStart() {
773 JdwpSuspendPolicy suspend_policy = (options_->suspend) ? SP_ALL : SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700774 ObjectId threadId = Dbg::GetThreadSelfId();
775
Sebastien Hertz7d955652014-10-22 10:57:10 +0200776 VLOG(jdwp) << "EVENT: " << EK_VM_START;
777 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700778
Elliott Hughes761928d2011-11-16 18:33:03 -0800779 ExpandBuf* pReq = eventPrep();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200780 expandBufAdd1(pReq, suspend_policy);
781 expandBufAdd4BE(pReq, 1);
782 expandBufAdd1(pReq, EK_VM_START);
783 expandBufAdd4BE(pReq, 0); /* requestId */
784 expandBufAddObjectId(pReq, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700785
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100786 Dbg::ManageDeoptimization();
787
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700788 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700790}
791
Sebastien Hertz7d955652014-10-22 10:57:10 +0200792static void LogMatchingEventsAndThread(const std::vector<JdwpEvent*> match_list,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200793 ObjectId thread_id)
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200794 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200795 for (size_t i = 0, e = match_list.size(); i < e; ++i) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200796 JdwpEvent* pEvent = match_list[i];
797 VLOG(jdwp) << "EVENT #" << i << ": " << pEvent->eventKind
798 << StringPrintf(" (requestId=%#" PRIx32 ")", pEvent->requestId);
799 }
800 std::string thread_name;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200801 JdwpError error = Dbg::GetThreadName(thread_id, &thread_name);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200802 if (error != JDWP::ERR_NONE) {
803 thread_name = "<unknown>";
804 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200805 VLOG(jdwp) << StringPrintf(" thread=%#" PRIx64, thread_id) << " " << thread_name;
806}
807
808static void SetJdwpLocationFromEventLocation(const JDWP::EventLocation* event_location,
809 JDWP::JdwpLocation* jdwp_location)
810 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
811 DCHECK(event_location != nullptr);
812 DCHECK(jdwp_location != nullptr);
813 Dbg::SetJdwpLocation(jdwp_location, event_location->method, event_location->dex_pc);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200814}
815
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700816/*
817 * A location of interest has been reached. This handles:
818 * Breakpoint
819 * SingleStep
820 * MethodEntry
821 * MethodExit
822 * These four types must be grouped together in a single response. The
823 * "eventFlags" indicates the type of event(s) that have happened.
824 *
825 * Valid mods:
826 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
827 * LocationOnly (for breakpoint/step only)
828 * Step (for step only)
829 *
830 * Interesting test cases:
831 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
832 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
833 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
834 * - Single-step to a line with a breakpoint. Should get a single
835 * event message with both events in it.
836 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200837void JdwpState::PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200838 int eventFlags, const JValue* returnValue) {
839 DCHECK(pLoc != nullptr);
840 DCHECK(pLoc->method != nullptr);
841 DCHECK_EQ(pLoc->method->IsStatic(), thisPtr == nullptr);
842
Sebastien Hertz261bc042015-04-08 09:36:07 +0200843 ModBasket basket(Thread::Current());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700844 basket.pLoc = pLoc;
Sebastien Hertz261bc042015-04-08 09:36:07 +0200845 basket.locationClass.Assign(pLoc->method->GetDeclaringClass());
846 basket.thisPtr.Assign(thisPtr);
847 basket.className = Dbg::GetClassName(basket.locationClass.Get());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700848
849 /*
850 * On rare occasions we may need to execute interpreted code in the VM
851 * while handling a request from the debugger. Don't fire breakpoints
852 * while doing so. (I don't think we currently do this at all, so
853 * this is mostly paranoia.)
854 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200855 if (basket.thread == GetDebugThread()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800856 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200857 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700858 }
859
860 /*
861 * The debugger variable display tab may invoke the interpreter to format
862 * complex objects. We want to ignore breakpoints and method entry/exit
863 * traps while working on behalf of the debugger.
864 *
865 * If we don't ignore them, the VM will get hung up, because we'll
866 * suspend on a breakpoint while the debugger is still waiting for its
867 * method invocation to complete.
868 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800869 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800870 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200871 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872 }
873
Sebastien Hertz7d955652014-10-22 10:57:10 +0200874 std::vector<JdwpEvent*> match_list;
Elliott Hughes761928d2011-11-16 18:33:03 -0800875 {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200876 // We use the locked version because we have multiple possible match events.
877 MutexLock mu(Thread::Current(), event_list_lock_);
878 match_list.reserve(event_list_size_);
879 if ((eventFlags & Dbg::kBreakpoint) != 0) {
880 FindMatchingEventsLocked(EK_BREAKPOINT, basket, &match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800881 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200882 if ((eventFlags & Dbg::kSingleStep) != 0) {
883 FindMatchingEventsLocked(EK_SINGLE_STEP, basket, &match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800884 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200885 if ((eventFlags & Dbg::kMethodEntry) != 0) {
886 FindMatchingEventsLocked(EK_METHOD_ENTRY, basket, &match_list);
Sebastien Hertz6995c602014-09-09 12:10:13 +0200887 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200888 if ((eventFlags & Dbg::kMethodExit) != 0) {
889 FindMatchingEventsLocked(EK_METHOD_EXIT, basket, &match_list);
890 FindMatchingEventsLocked(EK_METHOD_EXIT_WITH_RETURN_VALUE, basket, &match_list);
891 }
892 }
893 if (match_list.empty()) {
894 // No matching event.
895 return;
896 }
897 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
898
899 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
900 JDWP::JdwpLocation jdwp_location;
901 SetJdwpLocationFromEventLocation(pLoc, &jdwp_location);
902
903 if (VLOG_IS_ON(jdwp)) {
904 LogMatchingEventsAndThread(match_list, thread_id);
905 VLOG(jdwp) << " location=" << jdwp_location;
906 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
907 }
908
909 ExpandBuf* pReq = eventPrep();
910 expandBufAdd1(pReq, suspend_policy);
911 expandBufAdd4BE(pReq, match_list.size());
912
913 for (const JdwpEvent* pEvent : match_list) {
914 expandBufAdd1(pReq, pEvent->eventKind);
915 expandBufAdd4BE(pReq, pEvent->requestId);
916 expandBufAddObjectId(pReq, thread_id);
917 expandBufAddLocation(pReq, jdwp_location);
918 if (pEvent->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
919 Dbg::OutputMethodReturnValue(jdwp_location.method_id, returnValue, pReq);
920 }
921 }
922
923 {
924 MutexLock mu(Thread::Current(), event_list_lock_);
925 CleanupMatchList(match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800926 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700927
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100928 Dbg::ManageDeoptimization();
929
Sebastien Hertz6995c602014-09-09 12:10:13 +0200930 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700931}
932
Mathieu Chartierc7853442015-03-27 14:35:38 -0700933void JdwpState::PostFieldEvent(const EventLocation* pLoc, ArtField* field,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200934 mirror::Object* this_object, const JValue* fieldValue,
935 bool is_modification) {
936 DCHECK(pLoc != nullptr);
937 DCHECK(field != nullptr);
938 DCHECK_EQ(fieldValue != nullptr, is_modification);
939 DCHECK_EQ(field->IsStatic(), this_object == nullptr);
940
Sebastien Hertz261bc042015-04-08 09:36:07 +0200941 ModBasket basket(Thread::Current());
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200942 basket.pLoc = pLoc;
Sebastien Hertz261bc042015-04-08 09:36:07 +0200943 basket.locationClass.Assign(pLoc->method->GetDeclaringClass());
944 basket.thisPtr.Assign(this_object);
945 basket.className = Dbg::GetClassName(basket.locationClass.Get());
Sebastien Hertz6995c602014-09-09 12:10:13 +0200946 basket.field = field;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200947
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200948 if (InvokeInProgress()) {
Sebastien Hertz261bc042015-04-08 09:36:07 +0200949 VLOG(jdwp) << "Not posting field event during invoke (" << basket.className << ")";
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);
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700975 VLOG(jdwp) << StringPrintf(" field=%#" PRIx64, field_id) << " "
Sebastien Hertz7d955652014-10-22 10:57:10 +0200976 << 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());
Sebastien Hertz261bc042015-04-08 09:36:07 +0200991 tag = Dbg::TagFromObject(soa, basket.thisPtr.Get());
Sebastien Hertz7d955652014-10-22 10:57:10 +0200992 }
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
Sebastien Hertz261bc042015-04-08 09:36:07 +02001044 ModBasket basket(thread);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001045
Sebastien Hertz7d955652014-10-22 10:57:10 +02001046 std::vector<JdwpEvent*> match_list;
1047 const JdwpEventKind match_kind = (start) ? EK_THREAD_START : EK_THREAD_DEATH;
1048 if (!FindMatchingEvents(match_kind, basket, &match_list)) {
1049 // No matching event.
1050 return;
1051 }
1052
1053 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1054 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1055
1056 if (VLOG_IS_ON(jdwp)) {
1057 LogMatchingEventsAndThread(match_list, thread_id);
1058 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1059 }
1060
1061 ExpandBuf* pReq = eventPrep();
1062 expandBufAdd1(pReq, suspend_policy);
1063 expandBufAdd4BE(pReq, match_list.size());
1064
1065 for (const JdwpEvent* pEvent : match_list) {
1066 expandBufAdd1(pReq, pEvent->eventKind);
1067 expandBufAdd4BE(pReq, pEvent->requestId);
1068 expandBufAdd8BE(pReq, thread_id);
1069 }
1070
Elliott Hughes234ab152011-10-26 14:02:26 -07001071 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001072 MutexLock mu(Thread::Current(), event_list_lock_);
1073 CleanupMatchList(match_list);
Elliott Hughes234ab152011-10-26 14:02:26 -07001074 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001075
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001076 Dbg::ManageDeoptimization();
1077
Sebastien Hertz6995c602014-09-09 12:10:13 +02001078 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001079}
1080
1081/*
1082 * Send a polite "VM is dying" message to the debugger.
1083 *
1084 * Skips the usual "event token" stuff.
1085 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001086bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001087 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001088
1089 ExpandBuf* pReq = eventPrep();
1090 expandBufAdd1(pReq, SP_NONE);
1091 expandBufAdd4BE(pReq, 1);
1092
1093 expandBufAdd1(pReq, EK_VM_DEATH);
1094 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -08001095 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001096 return true;
1097}
1098
1099/*
1100 * An exception has been thrown. It may or may not have been caught.
1101 *
1102 * Valid mods:
1103 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
1104 * ExceptionOnly, InstanceOnly
1105 *
1106 * The "exceptionId" has not been added to the GC-visible object registry,
1107 * because there's a pretty good chance that we're not going to send it
1108 * up the debugger.
1109 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001110void JdwpState::PostException(const EventLocation* pThrowLoc, mirror::Throwable* exception_object,
Sebastien Hertz6995c602014-09-09 12:10:13 +02001111 const EventLocation* pCatchLoc, mirror::Object* thisPtr) {
1112 DCHECK(exception_object != nullptr);
1113 DCHECK(pThrowLoc != nullptr);
1114 DCHECK(pCatchLoc != nullptr);
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001115 if (pThrowLoc->method != nullptr) {
1116 DCHECK_EQ(pThrowLoc->method->IsStatic(), thisPtr == nullptr);
1117 } else {
1118 VLOG(jdwp) << "Unexpected: exception event with empty throw location";
1119 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001120
Sebastien Hertz261bc042015-04-08 09:36:07 +02001121 ModBasket basket(Thread::Current());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122 basket.pLoc = pThrowLoc;
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001123 if (pThrowLoc->method != nullptr) {
Sebastien Hertz261bc042015-04-08 09:36:07 +02001124 basket.locationClass.Assign(pThrowLoc->method->GetDeclaringClass());
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001125 }
Sebastien Hertz261bc042015-04-08 09:36:07 +02001126 basket.className = Dbg::GetClassName(basket.locationClass.Get());
1127 basket.exceptionClass.Assign(exception_object->GetClass());
Sebastien Hertz6995c602014-09-09 12:10:13 +02001128 basket.caught = (pCatchLoc->method != 0);
Sebastien Hertz261bc042015-04-08 09:36:07 +02001129 basket.thisPtr.Assign(thisPtr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001130
1131 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001132 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001133 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001134 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001135 }
1136
Sebastien Hertz7d955652014-10-22 10:57:10 +02001137 std::vector<JdwpEvent*> match_list;
1138 if (!FindMatchingEvents(EK_EXCEPTION, basket, &match_list)) {
1139 // No matching event.
1140 return;
1141 }
1142
1143 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1144 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1145 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1146 ObjectId exceptionId = registry->Add(exception_object);
1147 JDWP::JdwpLocation jdwp_throw_location;
1148 JDWP::JdwpLocation jdwp_catch_location;
1149 SetJdwpLocationFromEventLocation(pThrowLoc, &jdwp_throw_location);
1150 SetJdwpLocationFromEventLocation(pCatchLoc, &jdwp_catch_location);
1151
1152 if (VLOG_IS_ON(jdwp)) {
1153 std::string exceptionClassName(PrettyDescriptor(exception_object->GetClass()));
1154
1155 LogMatchingEventsAndThread(match_list, thread_id);
1156 VLOG(jdwp) << " throwLocation=" << jdwp_throw_location;
1157 if (jdwp_catch_location.class_id == 0) {
1158 VLOG(jdwp) << " catchLocation=uncaught";
1159 } else {
1160 VLOG(jdwp) << " catchLocation=" << jdwp_catch_location;
1161 }
1162 VLOG(jdwp) << StringPrintf(" exception=%#" PRIx64, exceptionId) << " "
1163 << exceptionClassName;
1164 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1165 }
1166
1167 ExpandBuf* pReq = eventPrep();
1168 expandBufAdd1(pReq, suspend_policy);
1169 expandBufAdd4BE(pReq, match_list.size());
1170
1171 for (const JdwpEvent* pEvent : match_list) {
1172 expandBufAdd1(pReq, pEvent->eventKind);
1173 expandBufAdd4BE(pReq, pEvent->requestId);
1174 expandBufAddObjectId(pReq, thread_id);
1175 expandBufAddLocation(pReq, jdwp_throw_location);
1176 expandBufAdd1(pReq, JT_OBJECT);
1177 expandBufAddObjectId(pReq, exceptionId);
1178 expandBufAddLocation(pReq, jdwp_catch_location);
1179 }
1180
Elliott Hughes761928d2011-11-16 18:33:03 -08001181 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001182 MutexLock mu(Thread::Current(), event_list_lock_);
1183 CleanupMatchList(match_list);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001184 }
1185
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001186 Dbg::ManageDeoptimization();
1187
Sebastien Hertz6995c602014-09-09 12:10:13 +02001188 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189}
1190
1191/*
1192 * Announce that a class has been loaded.
1193 *
1194 * Valid mods:
1195 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1196 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001197void JdwpState::PostClassPrepare(mirror::Class* klass) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001198 DCHECK(klass != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001199
Sebastien Hertz261bc042015-04-08 09:36:07 +02001200 ModBasket basket(Thread::Current());
1201 basket.locationClass.Assign(klass);
1202 basket.className = Dbg::GetClassName(basket.locationClass.Get());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001203
1204 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001205 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001206 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001207 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001208 }
1209
Sebastien Hertz7d955652014-10-22 10:57:10 +02001210 std::vector<JdwpEvent*> match_list;
1211 if (!FindMatchingEvents(EK_CLASS_PREPARE, basket, &match_list)) {
1212 // No matching event.
1213 return;
1214 }
1215
1216 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1217 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1218 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1219 RefTypeId class_id = registry->AddRefType(basket.locationClass);
1220
1221 // OLD-TODO - we currently always send both "verified" and "prepared" since
1222 // debuggers seem to like that. There might be some advantage to honesty,
1223 // since the class may not yet be verified.
1224 int status = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
Sebastien Hertz261bc042015-04-08 09:36:07 +02001225 JDWP::JdwpTypeTag tag = Dbg::GetTypeTag(basket.locationClass.Get());
Sebastien Hertz7d955652014-10-22 10:57:10 +02001226 std::string temp;
1227 std::string signature(basket.locationClass->GetDescriptor(&temp));
1228
1229 if (VLOG_IS_ON(jdwp)) {
1230 LogMatchingEventsAndThread(match_list, thread_id);
1231 VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, class_id) << " " << signature;
1232 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1233 }
1234
1235 if (thread_id == debug_thread_id_) {
1236 /*
1237 * JDWP says that, for a class prep in the debugger thread, we
1238 * should set thread to null and if any threads were supposed
1239 * to be suspended then we suspend all other threads.
1240 */
1241 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
1242 thread_id = 0;
1243 if (suspend_policy == SP_EVENT_THREAD) {
1244 suspend_policy = SP_ALL;
1245 }
1246 }
1247
1248 ExpandBuf* pReq = eventPrep();
1249 expandBufAdd1(pReq, suspend_policy);
1250 expandBufAdd4BE(pReq, match_list.size());
1251
1252 for (const JdwpEvent* pEvent : match_list) {
1253 expandBufAdd1(pReq, pEvent->eventKind);
1254 expandBufAdd4BE(pReq, pEvent->requestId);
1255 expandBufAddObjectId(pReq, thread_id);
1256 expandBufAdd1(pReq, tag);
1257 expandBufAddRefTypeId(pReq, class_id);
1258 expandBufAddUtf8String(pReq, signature);
1259 expandBufAdd4BE(pReq, status);
1260 }
1261
Elliott Hughes761928d2011-11-16 18:33:03 -08001262 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001263 MutexLock mu(Thread::Current(), event_list_lock_);
1264 CleanupMatchList(match_list);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 }
1266
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001267 Dbg::ManageDeoptimization();
1268
Sebastien Hertz6995c602014-09-09 12:10:13 +02001269 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270}
1271
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001272/*
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001273 * Setup the header for a chunk of DDM data.
1274 */
1275void JdwpState::SetupChunkHeader(uint32_t type, size_t data_len, size_t header_size,
1276 uint8_t* out_header) {
1277 CHECK_EQ(header_size, static_cast<size_t>(kJDWPHeaderLen + 8));
1278 /* form the header (JDWP plus DDMS) */
1279 Set4BE(out_header, header_size + data_len);
1280 Set4BE(out_header + 4, NextRequestSerial());
1281 Set1(out_header + 8, 0); /* flags */
1282 Set1(out_header + 9, kJDWPDdmCmdSet);
1283 Set1(out_header + 10, kJDWPDdmCmd);
1284 Set4BE(out_header + 11, type);
1285 Set4BE(out_header + 15, data_len);
1286}
1287
1288/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289 * Send up a chunk of DDM data.
1290 *
1291 * While this takes the form of a JDWP "event", it doesn't interact with
1292 * other debugger traffic, and can't suspend the VM, so we skip all of
1293 * the fun event token gymnastics.
1294 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001295void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001296 uint8_t header[kJDWPHeaderLen + 8] = { 0 };
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297 size_t dataLen = 0;
1298
Sebastien Hertz7d955652014-10-22 10:57:10 +02001299 CHECK(iov != nullptr);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001300 CHECK_GT(iov_count, 0);
1301 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001302
1303 /*
1304 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1305 * this by creating a new copy of the vector with space for the header.
1306 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001307 std::vector<iovec> wrapiov;
1308 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001309 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001310 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001311 dataLen += iov[i].iov_len;
1312 }
1313
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001314 SetupChunkHeader(type, dataLen, sizeof(header), header);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001315
1316 wrapiov[0].iov_base = header;
1317 wrapiov[0].iov_len = sizeof(header);
1318
Ian Rogers15bf2d32012-08-28 17:33:04 -07001319 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1320 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001322 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1323 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001324 for (size_t i = 0; i < kMutatorLock; ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001325 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001326 safe_to_release_mutator_lock_over_send = false;
1327 break;
1328 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001329 }
1330 }
1331 if (safe_to_release_mutator_lock_over_send) {
1332 // Change state to waiting to allow GC, ... while we're sending.
1333 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001334 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001335 self->TransitionFromSuspendedToRunnable();
1336 } else {
1337 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001338 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001339 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001340}
1341
1342} // namespace JDWP
1343
1344} // namespace art