blob: 5574a117a7bbccbfe08f61dc253579ffa32fc38a [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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_thread_state_change-inl.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
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102namespace art {
103
104namespace JDWP {
105
106/*
107 * Stuff to compare against when deciding if a mod matches. Only the
108 * values for mods valid for the event being evaluated will be filled in.
109 * The rest will be zeroed.
Sebastien Hertz261bc042015-04-08 09:36:07 +0200110 * Must be allocated on the stack only. This is enforced by removing the
111 * operator new.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700112 */
113struct ModBasket {
Sebastien Hertz261bc042015-04-08 09:36:07 +0200114 explicit ModBasket(Thread* self)
115 : hs(self), pLoc(nullptr), thread(self),
116 locationClass(hs.NewHandle<mirror::Class>(nullptr)),
117 exceptionClass(hs.NewHandle<mirror::Class>(nullptr)),
118 caught(false),
119 field(nullptr),
120 thisPtr(hs.NewHandle<mirror::Object>(nullptr)) { }
jeffhao162fd332013-01-08 16:21:01 -0800121
Sebastien Hertz261bc042015-04-08 09:36:07 +0200122 StackHandleScope<3> hs;
123 const EventLocation* pLoc; /* LocationOnly */
124 std::string className; /* ClassMatch/ClassExclude */
125 Thread* const thread; /* ThreadOnly */
126 MutableHandle<mirror::Class> locationClass; /* ClassOnly */
127 MutableHandle<mirror::Class> exceptionClass; /* ExceptionOnly */
128 bool caught; /* ExceptionOnly */
129 ArtField* field; /* FieldOnly */
130 MutableHandle<mirror::Object> thisPtr; /* InstanceOnly */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131 /* nothing for StepOnly -- handled differently */
Sebastien Hertz261bc042015-04-08 09:36:07 +0200132
133 private:
134 DISALLOW_ALLOCATION(); // forbids allocation on the heap.
135 DISALLOW_IMPLICIT_CONSTRUCTORS(ModBasket);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136};
137
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100138static bool NeedsFullDeoptimization(JdwpEventKind eventKind) {
Sebastien Hertzf3928792014-11-17 19:00:37 +0100139 if (!Dbg::RequiresDeoptimization()) {
140 // We don't need deoptimization for debugging.
141 return false;
142 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100143 switch (eventKind) {
144 case EK_METHOD_ENTRY:
145 case EK_METHOD_EXIT:
146 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200147 case EK_FIELD_ACCESS:
148 case EK_FIELD_MODIFICATION:
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100149 return true;
150 default:
151 return false;
152 }
153}
154
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200155// Returns the instrumentation event the DebugInstrumentationListener must
156// listen to in order to properly report the given JDWP event to the debugger.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800157static uint32_t GetInstrumentationEventFor(JdwpEventKind eventKind) {
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200158 switch (eventKind) {
159 case EK_BREAKPOINT:
160 case EK_SINGLE_STEP:
161 return instrumentation::Instrumentation::kDexPcMoved;
162 case EK_EXCEPTION:
163 case EK_EXCEPTION_CATCH:
164 return instrumentation::Instrumentation::kExceptionCaught;
165 case EK_METHOD_ENTRY:
166 return instrumentation::Instrumentation::kMethodEntered;
167 case EK_METHOD_EXIT:
168 case EK_METHOD_EXIT_WITH_RETURN_VALUE:
169 return instrumentation::Instrumentation::kMethodExited;
170 case EK_FIELD_ACCESS:
171 return instrumentation::Instrumentation::kFieldRead;
172 case EK_FIELD_MODIFICATION:
173 return instrumentation::Instrumentation::kFieldWritten;
174 default:
175 return 0;
176 }
177}
178
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179/*
180 * Add an event to the list. Ordering is not important.
181 *
182 * If something prevents the event from being registered, e.g. it's a
183 * single-step request on a thread that doesn't exist, the event will
184 * not be added to the list, and an appropriate error will be returned.
185 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800186JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200187 CHECK(pEvent != nullptr);
188 CHECK(pEvent->prev == nullptr);
189 CHECK(pEvent->next == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200191 {
192 /*
193 * If one or more "break"-type mods are used, register them with
194 * the interpreter.
195 */
196 DeoptimizationRequest req;
197 for (int i = 0; i < pEvent->modCount; i++) {
198 const JdwpEventMod* pMod = &pEvent->mods[i];
199 if (pMod->modKind == MK_LOCATION_ONLY) {
Sebastien Hertz033aabf2014-10-08 13:54:55 +0200200 // Should only concern breakpoint, field access, field modification, step, and exception
201 // events.
202 // However breakpoint requires specific handling. Field access, field modification and step
203 // events need full deoptimization to be reported while exception event is reported during
204 // exception handling.
205 if (pEvent->eventKind == EK_BREAKPOINT) {
206 Dbg::WatchLocation(&pMod->locationOnly.loc, &req);
207 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200208 } else if (pMod->modKind == MK_STEP) {
209 /* should only be for EK_SINGLE_STEP; should only be one */
210 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
211 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
212 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
213 if (status != ERR_NONE) {
214 return status;
215 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800216 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700217 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200218 if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700219 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
220 CHECK(req.Method() == nullptr);
221 req.SetKind(DeoptimizationRequest::kFullDeoptimization);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200222 }
223 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200225 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
226 if (instrumentation_event != 0) {
227 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700228 req.SetKind(DeoptimizationRequest::kRegisterForEvent);
229 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200230 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100231 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700232
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100233 {
234 /*
235 * Add to list.
236 */
237 MutexLock mu(Thread::Current(), event_list_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200238 if (event_list_ != nullptr) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100239 pEvent->next = event_list_;
240 event_list_->prev = pEvent;
241 }
242 event_list_ = pEvent;
243 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100245
246 Dbg::ManageDeoptimization();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247
248 return ERR_NONE;
249}
250
251/*
252 * Remove an event from the list. This will also remove the event from
253 * any optimization tables, e.g. breakpoints.
254 *
255 * Does not free the JdwpEvent.
256 *
257 * Grab the eventLock before calling here.
258 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800259void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200260 if (pEvent->prev == nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700262 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700263
Elliott Hughesf8349362012-06-18 15:00:06 -0700264 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 } else {
266 pEvent->prev->next = pEvent->next;
267 }
268
Sebastien Hertz7d955652014-10-22 10:57:10 +0200269 if (pEvent->next != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270 pEvent->next->prev = pEvent->prev;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200271 pEvent->next = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700272 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200273 pEvent->prev = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700274
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200275 {
276 /*
277 * Unhook us from the interpreter, if necessary.
278 */
279 DeoptimizationRequest req;
280 for (int i = 0; i < pEvent->modCount; i++) {
281 JdwpEventMod* pMod = &pEvent->mods[i];
282 if (pMod->modKind == MK_LOCATION_ONLY) {
Sebastien Hertz033aabf2014-10-08 13:54:55 +0200283 // Like in RegisterEvent, we need specific handling for breakpoint only.
284 if (pEvent->eventKind == EK_BREAKPOINT) {
285 Dbg::UnwatchLocation(&pMod->locationOnly.loc, &req);
286 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200287 }
288 if (pMod->modKind == MK_STEP) {
289 /* should only be for EK_SINGLE_STEP; should only be one */
290 Dbg::UnconfigureStep(pMod->step.threadId);
291 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700292 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200293 if (NeedsFullDeoptimization(pEvent->eventKind)) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700294 CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing);
295 CHECK(req.Method() == nullptr);
296 req.SetKind(DeoptimizationRequest::kFullUndeoptimization);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200298 Dbg::RequestDeoptimization(req);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700299 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200300 uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind);
301 if (instrumentation_event != 0) {
302 DeoptimizationRequest req;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700303 req.SetKind(DeoptimizationRequest::kUnregisterForEvent);
304 req.SetInstrumentationEvent(instrumentation_event);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200305 Dbg::RequestDeoptimization(req);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100306 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700307
Elliott Hughesf8349362012-06-18 15:00:06 -0700308 --event_list_size_;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200309 CHECK(event_list_size_ != 0 || event_list_ == nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310}
311
312/*
313 * Remove the event with the given ID from the list.
314 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700315 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800316void JdwpState::UnregisterEventById(uint32_t requestId) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100317 bool found = false;
318 {
319 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100321 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
322 if (pEvent->requestId == requestId) {
323 found = true;
324 UnregisterEvent(pEvent);
325 EventFree(pEvent);
326 break; /* there can be only one with a given ID */
327 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700329 }
330
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100331 if (found) {
332 Dbg::ManageDeoptimization();
333 } else {
Sebastien Hertzf272af42014-09-18 10:20:42 +0200334 // Failure to find the event isn't really an error. For instance, it looks like Eclipse will
335 // try to be extra careful and will explicitly remove one-off single-step events (using a
336 // 'count' event modifier of 1). So the event may have already been removed as part of the
337 // event notification (see JdwpState::CleanupMatchList).
338 VLOG(jdwp) << StringPrintf("No match when removing event reqId=0x%04x", requestId);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100339 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700340}
341
342/*
343 * Remove all entries from the event list.
344 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800345void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700346 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347
Elliott Hughesf8349362012-06-18 15:00:06 -0700348 JdwpEvent* pEvent = event_list_;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200349 while (pEvent != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700350 JdwpEvent* pNextEvent = pEvent->next;
351
Elliott Hughes761928d2011-11-16 18:33:03 -0800352 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700353 EventFree(pEvent);
354 pEvent = pNextEvent;
355 }
356
Sebastien Hertz7d955652014-10-22 10:57:10 +0200357 event_list_ = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700358}
359
360/*
361 * Allocate a JdwpEvent struct with enough space to hold the specified
362 * number of mod records.
363 */
364JdwpEvent* EventAlloc(int numMods) {
365 JdwpEvent* newEvent;
366 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
367 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
368 memset(newEvent, 0, allocSize);
369 return newEvent;
370}
371
372/*
373 * Free a JdwpEvent.
374 *
375 * Do not call this until the event has been removed from the list.
376 */
377void EventFree(JdwpEvent* pEvent) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200378 if (pEvent == nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379 return;
380 }
381
382 /* make sure it was removed from the list */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200383 CHECK(pEvent->prev == nullptr);
384 CHECK(pEvent->next == nullptr);
Elliott Hughesf8349362012-06-18 15:00:06 -0700385 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700386
387 /*
388 * Free any hairy bits in the mods.
389 */
390 for (int i = 0; i < pEvent->modCount; i++) {
391 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
392 free(pEvent->mods[i].classMatch.classPattern);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200393 pEvent->mods[i].classMatch.classPattern = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700394 }
395 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
396 free(pEvent->mods[i].classExclude.classPattern);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200397 pEvent->mods[i].classExclude.classPattern = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700398 }
399 }
400
401 free(pEvent);
402}
403
404/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405 * Run through the list and remove any entries with an expired "count" mod
Sebastien Hertz7d955652014-10-22 10:57:10 +0200406 * from the event list.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200408void JdwpState::CleanupMatchList(const std::vector<JdwpEvent*>& match_list) {
409 for (JdwpEvent* pEvent : match_list) {
410 for (int i = 0; i < pEvent->modCount; ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700411 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200412 VLOG(jdwp) << StringPrintf("##### Removing expired event (requestId=%#" PRIx32 ")",
413 pEvent->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800414 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415 EventFree(pEvent);
416 break;
417 }
418 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700419 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420}
421
422/*
423 * Match a string against a "restricted regular expression", which is just
424 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
425 *
426 * ("Restricted name globbing" might have been a better term.)
427 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800428static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800429 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700431 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800432 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433 return false;
434 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800435 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800437 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800439 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 }
441}
442
443/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700444 * See if the event's mods match up with the contents of "basket".
445 *
446 * If we find a Count mod before rejecting an event, we decrement it. We
447 * need to do this even if later mods cause us to ignore the event.
448 */
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200449static bool ModsMatch(JdwpEvent* pEvent, const ModBasket& basket)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700450 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 JdwpEventMod* pMod = pEvent->mods;
452
453 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
454 switch (pMod->modKind) {
455 case MK_COUNT:
456 CHECK_GT(pMod->count.count, 0);
457 pMod->count.count--;
Sebastien Hertz43207792014-04-15 16:03:27 +0200458 if (pMod->count.count > 0) {
459 return false;
460 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700461 break;
462 case MK_CONDITIONAL:
463 CHECK(false); // should not be getting these
464 break;
465 case MK_THREAD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200466 if (!Dbg::MatchThread(pMod->threadOnly.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467 return false;
468 }
469 break;
470 case MK_CLASS_ONLY:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200471 if (!Dbg::MatchType(basket.locationClass.Get(), pMod->classOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472 return false;
473 }
474 break;
475 case MK_CLASS_MATCH:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200476 if (!PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700477 return false;
478 }
479 break;
480 case MK_CLASS_EXCLUDE:
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200481 if (PatternMatch(pMod->classMatch.classPattern, basket.className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482 return false;
483 }
484 break;
485 case MK_LOCATION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200486 if (!Dbg::MatchLocation(pMod->locationOnly.loc, *basket.pLoc)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700487 return false;
488 }
489 break;
490 case MK_EXCEPTION_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200491 if (pMod->exceptionOnly.refTypeId != 0 &&
Sebastien Hertz261bc042015-04-08 09:36:07 +0200492 !Dbg::MatchType(basket.exceptionClass.Get(), pMod->exceptionOnly.refTypeId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493 return false;
494 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200495 if ((basket.caught && !pMod->exceptionOnly.caught) ||
496 (!basket.caught && !pMod->exceptionOnly.uncaught)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497 return false;
498 }
499 break;
500 case MK_FIELD_ONLY:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200501 if (!Dbg::MatchField(pMod->fieldOnly.refTypeId, pMod->fieldOnly.fieldId, basket.field)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502 return false;
503 }
504 break;
505 case MK_STEP:
Sebastien Hertz6995c602014-09-09 12:10:13 +0200506 if (!Dbg::MatchThread(pMod->step.threadId, basket.thread)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700507 return false;
508 }
509 break;
510 case MK_INSTANCE_ONLY:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200511 if (!Dbg::MatchInstance(pMod->instanceOnly.objectId, basket.thisPtr.Get())) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512 return false;
513 }
514 break;
515 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800516 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700517 break;
518 }
519 }
520 return true;
521}
522
523/*
Sebastien Hertz7d955652014-10-22 10:57:10 +0200524 * Find all events of type "event_kind" with mods that match up with the
525 * rest of the arguments while holding the event list lock. This method
526 * is used by FindMatchingEvents below.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 *
Sebastien Hertz7d955652014-10-22 10:57:10 +0200528 * Found events are appended to "match_list" so this may be called multiple times for grouped
529 * events.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700530 *
531 * DO NOT call this multiple times for the same eventKind, as Count mods are
532 * decremented during the scan.
533 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200534void JdwpState::FindMatchingEventsLocked(JdwpEventKind event_kind, const ModBasket& basket,
535 std::vector<JdwpEvent*>* match_list) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200536 for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200537 if (pEvent->eventKind == event_kind && ModsMatch(pEvent, basket)) {
538 match_list->push_back(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540 }
541}
542
543/*
Sebastien Hertz7d955652014-10-22 10:57:10 +0200544 * Find all events of type "event_kind" with mods that match up with the
545 * rest of the arguments and return true if at least one event matches,
546 * false otherwise.
547 *
548 * Found events are appended to "match_list" so this may be called multiple
549 * times for grouped events.
550 *
551 * DO NOT call this multiple times for the same eventKind, as Count mods are
552 * decremented during the scan.
553 */
554bool JdwpState::FindMatchingEvents(JdwpEventKind event_kind, const ModBasket& basket,
555 std::vector<JdwpEvent*>* match_list) {
556 MutexLock mu(Thread::Current(), event_list_lock_);
557 match_list->reserve(event_list_size_);
558 FindMatchingEventsLocked(event_kind, basket, match_list);
559 return !match_list->empty();
560}
561
562/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563 * Scan through the list of matches and determine the most severe
564 * suspension policy.
565 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200566static JdwpSuspendPolicy ScanSuspendPolicy(const std::vector<JdwpEvent*>& match_list) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700567 JdwpSuspendPolicy policy = SP_NONE;
568
Sebastien Hertz7d955652014-10-22 10:57:10 +0200569 for (JdwpEvent* pEvent : match_list) {
570 if (pEvent->suspend_policy > policy) {
571 policy = pEvent->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700572 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573 }
574
575 return policy;
576}
577
578/*
579 * Three possibilities:
580 * SP_NONE - do nothing
581 * SP_EVENT_THREAD - suspend ourselves
582 * SP_ALL - suspend everybody except JDWP support thread
583 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700584void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700585 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
586 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587 return;
588 }
589
Elliott Hughesf8349362012-06-18 15:00:06 -0700590 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700591 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700592 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700593 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700594 }
595
596 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800598 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700599 return;
600 }
601
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700602 while (true) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700603 Dbg::SuspendSelf();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604
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 Hertz1558b572015-02-25 15:05:59 +0100609 DebugInvokeReq* const pReq = Dbg::GetInvokeReq();
610 if (pReq == nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611 break;
612 }
613
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200614 // Execute method.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615 Dbg::ExecuteMethod(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616 }
617}
618
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
620 ObjectId threadId) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200621 Thread* const self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 self->AssertThreadSuspensionIsAllowable();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200623 CHECK(pReq != nullptr);
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +0100624 CHECK_EQ(threadId, Dbg::GetThreadSelfId()) << "Only the current thread can suspend itself";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 /* send request and possibly suspend ourselves */
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700626 ScopedThreadSuspension sts(self, kWaitingForDebuggerSend);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200627 if (suspend_policy != SP_NONE) {
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100628 AcquireJdwpTokenForEvent(threadId);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200630 EventFinish(pReq);
Sebastien Hertz813b9602015-02-24 14:56:59 +0100631 {
632 // Before suspending, we change our state to kSuspended so the debugger sees us as RUNNING.
633 ScopedThreadStateChange stsc(self, kSuspended);
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +0100634 SuspendByPolicy(suspend_policy, threadId);
Sebastien Hertz813b9602015-02-24 14:56:59 +0100635 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636}
637
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700638/*
639 * Determine if there is a method invocation in progress in the current
640 * thread.
641 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700642 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700643 * state. If set, we're in the process of invoking a method.
644 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800645bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Sebastien Hertz1558b572015-02-25 15:05:59 +0100647 return pReq != nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648}
649
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100650void JdwpState::AcquireJdwpTokenForCommand() {
651 CHECK_EQ(Thread::Current(), GetDebugThread()) << "Expected debugger thread";
652 SetWaitForJdwpToken(debug_thread_id_);
653}
654
655void JdwpState::ReleaseJdwpTokenForCommand() {
656 CHECK_EQ(Thread::Current(), GetDebugThread()) << "Expected debugger thread";
657 ClearWaitForJdwpToken();
658}
659
660void JdwpState::AcquireJdwpTokenForEvent(ObjectId threadId) {
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100661 SetWaitForJdwpToken(threadId);
662}
663
664void JdwpState::ReleaseJdwpTokenForEvent() {
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100665 ClearWaitForJdwpToken();
666}
667
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700668/*
669 * We need the JDWP thread to hold off on doing stuff while we post an
670 * event and then suspend ourselves.
671 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672 * This could go to sleep waiting for another thread, so it's important
673 * that the thread be marked as VMWAIT before calling here.
674 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100675void JdwpState::SetWaitForJdwpToken(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700676 bool waited = false;
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100677 Thread* const self = Thread::Current();
678 CHECK_NE(threadId, 0u);
679 CHECK_NE(self->GetState(), kRunnable);
680 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700681
682 /* this is held for very brief periods; contention is unlikely */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100683 MutexLock mu(self, jdwp_token_lock_);
684
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +0100685 if (jdwp_token_owner_thread_id_ == threadId) {
686 // Only the debugger thread may already hold the event token. For instance, it may trigger
687 // a CLASS_PREPARE event while processing a command that initializes a class.
688 CHECK_EQ(threadId, debug_thread_id_) << "Non-debugger thread is already holding event token";
689 } else {
690 /*
691 * If another thread is already doing stuff, wait for it. This can
692 * go to sleep indefinitely.
693 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700694
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +0100695 while (jdwp_token_owner_thread_id_ != 0) {
696 VLOG(jdwp) << StringPrintf("event in progress (%#" PRIx64 "), %#" PRIx64 " sleeping",
697 jdwp_token_owner_thread_id_, threadId);
698 waited = true;
699 jdwp_token_cond_.Wait(self);
700 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +0100702 if (waited || threadId != debug_thread_id_) {
703 VLOG(jdwp) << StringPrintf("event token grabbed (%#" PRIx64 ")", threadId);
704 }
705 jdwp_token_owner_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700706 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707}
708
709/*
710 * Clear the threadId and signal anybody waiting.
711 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100712void JdwpState::ClearWaitForJdwpToken() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700713 /*
714 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100715 * function is called by Dbg::SuspendSelf(), and the transition back
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716 * to RUNNING would confuse it.
717 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100718 Thread* const self = Thread::Current();
719 MutexLock mu(self, jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100721 CHECK_NE(jdwp_token_owner_thread_id_, 0U);
722 VLOG(jdwp) << StringPrintf("cleared event token (%#" PRIx64 ")", jdwp_token_owner_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700723
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100724 jdwp_token_owner_thread_id_ = 0;
725 jdwp_token_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726}
727
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700728/*
729 * Prep an event. Allocates storage for the message and leaves space for
730 * the header.
731 */
732static ExpandBuf* eventPrep() {
733 ExpandBuf* pReq = expandBufAlloc();
734 expandBufAddSpace(pReq, kJDWPHeaderLen);
735 return pReq;
736}
737
738/*
739 * Write the header into the buffer and send the packet off to the debugger.
740 *
741 * Takes ownership of "pReq" (currently discards it).
742 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800743void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744 uint8_t* buf = expandBufGetBuffer(pReq);
745
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200746 Set4BE(buf + kJDWPHeaderSizeOffset, expandBufGetLength(pReq));
747 Set4BE(buf + kJDWPHeaderIdOffset, NextRequestSerial());
748 Set1(buf + kJDWPHeaderFlagsOffset, 0); /* flags */
749 Set1(buf + kJDWPHeaderCmdSetOffset, kJDWPEventCmdSet);
750 Set1(buf + kJDWPHeaderCmdOffset, kJDWPEventCompositeCmd);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751
Elliott Hughes761928d2011-11-16 18:33:03 -0800752 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753
754 expandBufFree(pReq);
755}
756
757
758/*
759 * Tell the debugger that we have finished initializing. This is always
760 * sent, even if the debugger hasn't requested it.
761 *
762 * This should be sent "before the main thread is started and before
763 * any application code has been executed". The thread ID in the message
764 * must be for the main thread.
765 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200766void JdwpState::PostVMStart() {
767 JdwpSuspendPolicy suspend_policy = (options_->suspend) ? SP_ALL : SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700768 ObjectId threadId = Dbg::GetThreadSelfId();
769
Sebastien Hertz7d955652014-10-22 10:57:10 +0200770 VLOG(jdwp) << "EVENT: " << EK_VM_START;
771 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772
Elliott Hughes761928d2011-11-16 18:33:03 -0800773 ExpandBuf* pReq = eventPrep();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200774 expandBufAdd1(pReq, suspend_policy);
775 expandBufAdd4BE(pReq, 1);
776 expandBufAdd1(pReq, EK_VM_START);
777 expandBufAdd4BE(pReq, 0); /* requestId */
778 expandBufAddObjectId(pReq, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700779
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100780 Dbg::ManageDeoptimization();
781
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700782 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700784}
785
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000786static void LogMatchingEventsAndThread(const std::vector<JdwpEvent*>& match_list,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200787 ObjectId thread_id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700788 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200789 for (size_t i = 0, e = match_list.size(); i < e; ++i) {
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200790 JdwpEvent* pEvent = match_list[i];
791 VLOG(jdwp) << "EVENT #" << i << ": " << pEvent->eventKind
792 << StringPrintf(" (requestId=%#" PRIx32 ")", pEvent->requestId);
793 }
794 std::string thread_name;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200795 JdwpError error = Dbg::GetThreadName(thread_id, &thread_name);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200796 if (error != JDWP::ERR_NONE) {
797 thread_name = "<unknown>";
798 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200799 VLOG(jdwp) << StringPrintf(" thread=%#" PRIx64, thread_id) << " " << thread_name;
800}
801
802static void SetJdwpLocationFromEventLocation(const JDWP::EventLocation* event_location,
803 JDWP::JdwpLocation* jdwp_location)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700804 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200805 DCHECK(event_location != nullptr);
806 DCHECK(jdwp_location != nullptr);
807 Dbg::SetJdwpLocation(jdwp_location, event_location->method, event_location->dex_pc);
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200808}
809
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700810/*
811 * A location of interest has been reached. This handles:
812 * Breakpoint
813 * SingleStep
814 * MethodEntry
815 * MethodExit
816 * These four types must be grouped together in a single response. The
817 * "eventFlags" indicates the type of event(s) that have happened.
818 *
819 * Valid mods:
820 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
821 * LocationOnly (for breakpoint/step only)
822 * Step (for step only)
823 *
824 * Interesting test cases:
825 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
826 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
827 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
828 * - Single-step to a line with a breakpoint. Should get a single
829 * event message with both events in it.
830 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200831void JdwpState::PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200832 int eventFlags, const JValue* returnValue) {
833 DCHECK(pLoc != nullptr);
834 DCHECK(pLoc->method != nullptr);
835 DCHECK_EQ(pLoc->method->IsStatic(), thisPtr == nullptr);
836
Sebastien Hertz261bc042015-04-08 09:36:07 +0200837 ModBasket basket(Thread::Current());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838 basket.pLoc = pLoc;
Sebastien Hertz261bc042015-04-08 09:36:07 +0200839 basket.locationClass.Assign(pLoc->method->GetDeclaringClass());
840 basket.thisPtr.Assign(thisPtr);
841 basket.className = Dbg::GetClassName(basket.locationClass.Get());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700842
843 /*
844 * On rare occasions we may need to execute interpreted code in the VM
845 * while handling a request from the debugger. Don't fire breakpoints
846 * while doing so. (I don't think we currently do this at all, so
847 * this is mostly paranoia.)
848 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200849 if (basket.thread == GetDebugThread()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800850 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200851 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700852 }
853
854 /*
855 * The debugger variable display tab may invoke the interpreter to format
856 * complex objects. We want to ignore breakpoints and method entry/exit
857 * traps while working on behalf of the debugger.
858 *
859 * If we don't ignore them, the VM will get hung up, because we'll
860 * suspend on a breakpoint while the debugger is still waiting for its
861 * method invocation to complete.
862 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800863 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800864 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200865 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866 }
867
Sebastien Hertz7d955652014-10-22 10:57:10 +0200868 std::vector<JdwpEvent*> match_list;
Elliott Hughes761928d2011-11-16 18:33:03 -0800869 {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200870 // We use the locked version because we have multiple possible match events.
871 MutexLock mu(Thread::Current(), event_list_lock_);
872 match_list.reserve(event_list_size_);
873 if ((eventFlags & Dbg::kBreakpoint) != 0) {
874 FindMatchingEventsLocked(EK_BREAKPOINT, basket, &match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800875 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200876 if ((eventFlags & Dbg::kSingleStep) != 0) {
877 FindMatchingEventsLocked(EK_SINGLE_STEP, basket, &match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800878 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200879 if ((eventFlags & Dbg::kMethodEntry) != 0) {
880 FindMatchingEventsLocked(EK_METHOD_ENTRY, basket, &match_list);
Sebastien Hertz6995c602014-09-09 12:10:13 +0200881 }
Sebastien Hertz7d955652014-10-22 10:57:10 +0200882 if ((eventFlags & Dbg::kMethodExit) != 0) {
883 FindMatchingEventsLocked(EK_METHOD_EXIT, basket, &match_list);
884 FindMatchingEventsLocked(EK_METHOD_EXIT_WITH_RETURN_VALUE, basket, &match_list);
885 }
886 }
887 if (match_list.empty()) {
888 // No matching event.
889 return;
890 }
891 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
892
893 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
894 JDWP::JdwpLocation jdwp_location;
895 SetJdwpLocationFromEventLocation(pLoc, &jdwp_location);
896
897 if (VLOG_IS_ON(jdwp)) {
898 LogMatchingEventsAndThread(match_list, thread_id);
899 VLOG(jdwp) << " location=" << jdwp_location;
900 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
901 }
902
903 ExpandBuf* pReq = eventPrep();
904 expandBufAdd1(pReq, suspend_policy);
905 expandBufAdd4BE(pReq, match_list.size());
906
907 for (const JdwpEvent* pEvent : match_list) {
908 expandBufAdd1(pReq, pEvent->eventKind);
909 expandBufAdd4BE(pReq, pEvent->requestId);
910 expandBufAddObjectId(pReq, thread_id);
911 expandBufAddLocation(pReq, jdwp_location);
912 if (pEvent->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) {
913 Dbg::OutputMethodReturnValue(jdwp_location.method_id, returnValue, pReq);
914 }
915 }
916
917 {
918 MutexLock mu(Thread::Current(), event_list_lock_);
919 CleanupMatchList(match_list);
Elliott Hughes761928d2011-11-16 18:33:03 -0800920 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100922 Dbg::ManageDeoptimization();
923
Sebastien Hertz6995c602014-09-09 12:10:13 +0200924 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700925}
926
Mathieu Chartierc7853442015-03-27 14:35:38 -0700927void JdwpState::PostFieldEvent(const EventLocation* pLoc, ArtField* field,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200928 mirror::Object* this_object, const JValue* fieldValue,
929 bool is_modification) {
930 DCHECK(pLoc != nullptr);
931 DCHECK(field != nullptr);
932 DCHECK_EQ(fieldValue != nullptr, is_modification);
933 DCHECK_EQ(field->IsStatic(), this_object == nullptr);
934
Sebastien Hertz261bc042015-04-08 09:36:07 +0200935 ModBasket basket(Thread::Current());
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200936 basket.pLoc = pLoc;
Sebastien Hertz261bc042015-04-08 09:36:07 +0200937 basket.locationClass.Assign(pLoc->method->GetDeclaringClass());
938 basket.thisPtr.Assign(this_object);
939 basket.className = Dbg::GetClassName(basket.locationClass.Get());
Sebastien Hertz6995c602014-09-09 12:10:13 +0200940 basket.field = field;
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200941
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200942 if (InvokeInProgress()) {
Sebastien Hertz261bc042015-04-08 09:36:07 +0200943 VLOG(jdwp) << "Not posting field event during invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +0200944 return;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200945 }
946
Sebastien Hertz7d955652014-10-22 10:57:10 +0200947 std::vector<JdwpEvent*> match_list;
948 const JdwpEventKind match_kind = (is_modification) ? EK_FIELD_MODIFICATION : EK_FIELD_ACCESS;
949 if (!FindMatchingEvents(match_kind, basket, &match_list)) {
950 // No matching event.
951 return;
952 }
953
954 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
955 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
956 ObjectRegistry* registry = Dbg::GetObjectRegistry();
957 ObjectId instance_id = registry->Add(basket.thisPtr);
958 RefTypeId field_type_id = registry->AddRefType(field->GetDeclaringClass());
959 FieldId field_id = Dbg::ToFieldId(field);
960 JDWP::JdwpLocation jdwp_location;
961 SetJdwpLocationFromEventLocation(pLoc, &jdwp_location);
962
963 if (VLOG_IS_ON(jdwp)) {
964 LogMatchingEventsAndThread(match_list, thread_id);
965 VLOG(jdwp) << " location=" << jdwp_location;
966 VLOG(jdwp) << StringPrintf(" this=%#" PRIx64, instance_id);
967 VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, field_type_id) << " "
968 << Dbg::GetClassName(field_id);
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700969 VLOG(jdwp) << StringPrintf(" field=%#" PRIx64, field_id) << " "
Sebastien Hertz7d955652014-10-22 10:57:10 +0200970 << Dbg::GetFieldName(field_id);
971 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
972 }
973
974 ExpandBuf* pReq = eventPrep();
975 expandBufAdd1(pReq, suspend_policy);
976 expandBufAdd4BE(pReq, match_list.size());
977
978 // Get field's reference type tag.
979 JDWP::JdwpTypeTag type_tag = Dbg::GetTypeTag(field->GetDeclaringClass());
980
981 // Get instance type tag.
982 uint8_t tag;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200983 {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200984 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz261bc042015-04-08 09:36:07 +0200985 tag = Dbg::TagFromObject(soa, basket.thisPtr.Get());
Sebastien Hertz7d955652014-10-22 10:57:10 +0200986 }
987
988 for (const JdwpEvent* pEvent : match_list) {
989 expandBufAdd1(pReq, pEvent->eventKind);
990 expandBufAdd4BE(pReq, pEvent->requestId);
991 expandBufAddObjectId(pReq, thread_id);
992 expandBufAddLocation(pReq, jdwp_location);
993 expandBufAdd1(pReq, type_tag);
994 expandBufAddRefTypeId(pReq, field_type_id);
995 expandBufAddFieldId(pReq, field_id);
996 expandBufAdd1(pReq, tag);
997 expandBufAddObjectId(pReq, instance_id);
998 if (is_modification) {
999 Dbg::OutputFieldValue(field_id, fieldValue, pReq);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001000 }
Sebastien Hertz7d955652014-10-22 10:57:10 +02001001 }
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +02001002
Sebastien Hertz7d955652014-10-22 10:57:10 +02001003 {
1004 MutexLock mu(Thread::Current(), event_list_lock_);
1005 CleanupMatchList(match_list);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001006 }
1007
1008 Dbg::ManageDeoptimization();
1009
Sebastien Hertz6995c602014-09-09 12:10:13 +02001010 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001011}
1012
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001013/*
1014 * A thread is starting or stopping.
1015 *
1016 * Valid mods:
1017 * Count, ThreadOnly
1018 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001019void JdwpState::PostThreadChange(Thread* thread, bool start) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001020 CHECK_EQ(thread, Thread::Current());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001021
1022 /*
1023 * I don't think this can happen.
1024 */
Elliott Hughes761928d2011-11-16 18:33:03 -08001025 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001026 LOG(WARNING) << "Not posting thread change during invoke";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001027 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001028 }
1029
Sebastien Hertz107e7572014-12-18 11:13:15 +01001030 // We need the java.lang.Thread object associated to the starting/ending
1031 // thread to get its JDWP id. Therefore we can't report event if there
1032 // is no Java peer. This happens when the runtime shuts down and re-attaches
1033 // the current thread without creating a Java peer.
1034 if (thread->GetPeer() == nullptr) {
1035 return;
1036 }
1037
Sebastien Hertz261bc042015-04-08 09:36:07 +02001038 ModBasket basket(thread);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001039
Sebastien Hertz7d955652014-10-22 10:57:10 +02001040 std::vector<JdwpEvent*> match_list;
1041 const JdwpEventKind match_kind = (start) ? EK_THREAD_START : EK_THREAD_DEATH;
1042 if (!FindMatchingEvents(match_kind, basket, &match_list)) {
1043 // No matching event.
1044 return;
1045 }
1046
1047 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1048 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1049
1050 if (VLOG_IS_ON(jdwp)) {
1051 LogMatchingEventsAndThread(match_list, thread_id);
1052 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1053 }
1054
1055 ExpandBuf* pReq = eventPrep();
1056 expandBufAdd1(pReq, suspend_policy);
1057 expandBufAdd4BE(pReq, match_list.size());
1058
1059 for (const JdwpEvent* pEvent : match_list) {
1060 expandBufAdd1(pReq, pEvent->eventKind);
1061 expandBufAdd4BE(pReq, pEvent->requestId);
1062 expandBufAdd8BE(pReq, thread_id);
1063 }
1064
Elliott Hughes234ab152011-10-26 14:02:26 -07001065 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001066 MutexLock mu(Thread::Current(), event_list_lock_);
1067 CleanupMatchList(match_list);
Elliott Hughes234ab152011-10-26 14:02:26 -07001068 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001069
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001070 Dbg::ManageDeoptimization();
1071
Sebastien Hertz6995c602014-09-09 12:10:13 +02001072 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001073}
1074
1075/*
1076 * Send a polite "VM is dying" message to the debugger.
1077 *
1078 * Skips the usual "event token" stuff.
1079 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001080bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001081 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082
1083 ExpandBuf* pReq = eventPrep();
1084 expandBufAdd1(pReq, SP_NONE);
1085 expandBufAdd4BE(pReq, 1);
1086
1087 expandBufAdd1(pReq, EK_VM_DEATH);
1088 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -08001089 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001090 return true;
1091}
1092
1093/*
1094 * An exception has been thrown. It may or may not have been caught.
1095 *
1096 * Valid mods:
1097 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
1098 * ExceptionOnly, InstanceOnly
1099 *
1100 * The "exceptionId" has not been added to the GC-visible object registry,
1101 * because there's a pretty good chance that we're not going to send it
1102 * up the debugger.
1103 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001104void JdwpState::PostException(const EventLocation* pThrowLoc, mirror::Throwable* exception_object,
Sebastien Hertz6995c602014-09-09 12:10:13 +02001105 const EventLocation* pCatchLoc, mirror::Object* thisPtr) {
1106 DCHECK(exception_object != nullptr);
1107 DCHECK(pThrowLoc != nullptr);
1108 DCHECK(pCatchLoc != nullptr);
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001109 if (pThrowLoc->method != nullptr) {
1110 DCHECK_EQ(pThrowLoc->method->IsStatic(), thisPtr == nullptr);
1111 } else {
1112 VLOG(jdwp) << "Unexpected: exception event with empty throw location";
1113 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001114
Sebastien Hertz261bc042015-04-08 09:36:07 +02001115 ModBasket basket(Thread::Current());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001116 basket.pLoc = pThrowLoc;
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001117 if (pThrowLoc->method != nullptr) {
Sebastien Hertz261bc042015-04-08 09:36:07 +02001118 basket.locationClass.Assign(pThrowLoc->method->GetDeclaringClass());
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001119 }
Sebastien Hertz261bc042015-04-08 09:36:07 +02001120 basket.className = Dbg::GetClassName(basket.locationClass.Get());
1121 basket.exceptionClass.Assign(exception_object->GetClass());
Sebastien Hertz6995c602014-09-09 12:10:13 +02001122 basket.caught = (pCatchLoc->method != 0);
Sebastien Hertz261bc042015-04-08 09:36:07 +02001123 basket.thisPtr.Assign(thisPtr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001124
1125 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001126 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001127 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001128 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001129 }
1130
Sebastien Hertz7d955652014-10-22 10:57:10 +02001131 std::vector<JdwpEvent*> match_list;
1132 if (!FindMatchingEvents(EK_EXCEPTION, basket, &match_list)) {
1133 // No matching event.
1134 return;
1135 }
1136
1137 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1138 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1139 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1140 ObjectId exceptionId = registry->Add(exception_object);
1141 JDWP::JdwpLocation jdwp_throw_location;
1142 JDWP::JdwpLocation jdwp_catch_location;
1143 SetJdwpLocationFromEventLocation(pThrowLoc, &jdwp_throw_location);
1144 SetJdwpLocationFromEventLocation(pCatchLoc, &jdwp_catch_location);
1145
1146 if (VLOG_IS_ON(jdwp)) {
David Sehr709b0702016-10-13 09:12:37 -07001147 std::string exceptionClassName(mirror::Class::PrettyDescriptor(exception_object->GetClass()));
Sebastien Hertz7d955652014-10-22 10:57:10 +02001148
1149 LogMatchingEventsAndThread(match_list, thread_id);
1150 VLOG(jdwp) << " throwLocation=" << jdwp_throw_location;
1151 if (jdwp_catch_location.class_id == 0) {
1152 VLOG(jdwp) << " catchLocation=uncaught";
1153 } else {
1154 VLOG(jdwp) << " catchLocation=" << jdwp_catch_location;
1155 }
1156 VLOG(jdwp) << StringPrintf(" exception=%#" PRIx64, exceptionId) << " "
1157 << exceptionClassName;
1158 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1159 }
1160
1161 ExpandBuf* pReq = eventPrep();
1162 expandBufAdd1(pReq, suspend_policy);
1163 expandBufAdd4BE(pReq, match_list.size());
1164
1165 for (const JdwpEvent* pEvent : match_list) {
1166 expandBufAdd1(pReq, pEvent->eventKind);
1167 expandBufAdd4BE(pReq, pEvent->requestId);
1168 expandBufAddObjectId(pReq, thread_id);
1169 expandBufAddLocation(pReq, jdwp_throw_location);
1170 expandBufAdd1(pReq, JT_OBJECT);
1171 expandBufAddObjectId(pReq, exceptionId);
1172 expandBufAddLocation(pReq, jdwp_catch_location);
1173 }
1174
Elliott Hughes761928d2011-11-16 18:33:03 -08001175 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001176 MutexLock mu(Thread::Current(), event_list_lock_);
1177 CleanupMatchList(match_list);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001178 }
1179
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001180 Dbg::ManageDeoptimization();
1181
Sebastien Hertz6995c602014-09-09 12:10:13 +02001182 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001183}
1184
1185/*
1186 * Announce that a class has been loaded.
1187 *
1188 * Valid mods:
1189 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
1190 */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001191void JdwpState::PostClassPrepare(mirror::Class* klass) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001192 DCHECK(klass != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193
Sebastien Hertz261bc042015-04-08 09:36:07 +02001194 ModBasket basket(Thread::Current());
1195 basket.locationClass.Assign(klass);
1196 basket.className = Dbg::GetClassName(basket.locationClass.Get());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001197
1198 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -08001199 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001200 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Sebastien Hertz7d955652014-10-22 10:57:10 +02001201 return;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001202 }
1203
Sebastien Hertz7d955652014-10-22 10:57:10 +02001204 std::vector<JdwpEvent*> match_list;
1205 if (!FindMatchingEvents(EK_CLASS_PREPARE, basket, &match_list)) {
1206 // No matching event.
1207 return;
1208 }
1209
1210 JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list);
1211 ObjectId thread_id = Dbg::GetThreadId(basket.thread);
1212 ObjectRegistry* registry = Dbg::GetObjectRegistry();
1213 RefTypeId class_id = registry->AddRefType(basket.locationClass);
1214
1215 // OLD-TODO - we currently always send both "verified" and "prepared" since
1216 // debuggers seem to like that. There might be some advantage to honesty,
1217 // since the class may not yet be verified.
1218 int status = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
Sebastien Hertz261bc042015-04-08 09:36:07 +02001219 JDWP::JdwpTypeTag tag = Dbg::GetTypeTag(basket.locationClass.Get());
Sebastien Hertz7d955652014-10-22 10:57:10 +02001220 std::string temp;
1221 std::string signature(basket.locationClass->GetDescriptor(&temp));
1222
1223 if (VLOG_IS_ON(jdwp)) {
1224 LogMatchingEventsAndThread(match_list, thread_id);
1225 VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, class_id) << " " << signature;
1226 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
1227 }
1228
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +01001229 ObjectId reported_thread_id = thread_id;
1230 if (reported_thread_id == debug_thread_id_) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001231 /*
1232 * JDWP says that, for a class prep in the debugger thread, we
1233 * should set thread to null and if any threads were supposed
1234 * to be suspended then we suspend all other threads.
1235 */
1236 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +01001237 reported_thread_id = 0;
Sebastien Hertz7d955652014-10-22 10:57:10 +02001238 if (suspend_policy == SP_EVENT_THREAD) {
1239 suspend_policy = SP_ALL;
1240 }
1241 }
1242
1243 ExpandBuf* pReq = eventPrep();
1244 expandBufAdd1(pReq, suspend_policy);
1245 expandBufAdd4BE(pReq, match_list.size());
1246
1247 for (const JdwpEvent* pEvent : match_list) {
1248 expandBufAdd1(pReq, pEvent->eventKind);
1249 expandBufAdd4BE(pReq, pEvent->requestId);
Sebastien Hertzaf8bcf82016-11-22 14:55:04 +01001250 expandBufAddObjectId(pReq, reported_thread_id);
Sebastien Hertz7d955652014-10-22 10:57:10 +02001251 expandBufAdd1(pReq, tag);
1252 expandBufAddRefTypeId(pReq, class_id);
1253 expandBufAddUtf8String(pReq, signature);
1254 expandBufAdd4BE(pReq, status);
1255 }
1256
Elliott Hughes761928d2011-11-16 18:33:03 -08001257 {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001258 MutexLock mu(Thread::Current(), event_list_lock_);
1259 CleanupMatchList(match_list);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001260 }
1261
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001262 Dbg::ManageDeoptimization();
1263
Sebastien Hertz6995c602014-09-09 12:10:13 +02001264 SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265}
1266
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001267/*
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001268 * Setup the header for a chunk of DDM data.
1269 */
1270void JdwpState::SetupChunkHeader(uint32_t type, size_t data_len, size_t header_size,
1271 uint8_t* out_header) {
1272 CHECK_EQ(header_size, static_cast<size_t>(kJDWPHeaderLen + 8));
1273 /* form the header (JDWP plus DDMS) */
1274 Set4BE(out_header, header_size + data_len);
1275 Set4BE(out_header + 4, NextRequestSerial());
1276 Set1(out_header + 8, 0); /* flags */
1277 Set1(out_header + 9, kJDWPDdmCmdSet);
1278 Set1(out_header + 10, kJDWPDdmCmd);
1279 Set4BE(out_header + 11, type);
1280 Set4BE(out_header + 15, data_len);
1281}
1282
1283/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284 * Send up a chunk of DDM data.
1285 *
1286 * While this takes the form of a JDWP "event", it doesn't interact with
1287 * other debugger traffic, and can't suspend the VM, so we skip all of
1288 * the fun event token gymnastics.
1289 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001290void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001291 uint8_t header[kJDWPHeaderLen + 8] = { 0 };
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001292 size_t dataLen = 0;
1293
Sebastien Hertz7d955652014-10-22 10:57:10 +02001294 CHECK(iov != nullptr);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001295 CHECK_GT(iov_count, 0);
1296 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297
1298 /*
1299 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1300 * this by creating a new copy of the vector with space for the header.
1301 */
Brian Carlstromf5293522013-07-19 00:24:00 -07001302 std::vector<iovec> wrapiov;
1303 wrapiov.push_back(iovec());
Elliott Hughescccd84f2011-12-05 16:51:54 -08001304 for (int i = 0; i < iov_count; i++) {
Brian Carlstromf5293522013-07-19 00:24:00 -07001305 wrapiov.push_back(iov[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001306 dataLen += iov[i].iov_len;
1307 }
1308
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001309 SetupChunkHeader(type, dataLen, sizeof(header), header);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001310
1311 wrapiov[0].iov_base = header;
1312 wrapiov[0].iov_len = sizeof(header);
1313
Ian Rogers15bf2d32012-08-28 17:33:04 -07001314 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1315 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001316 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001317 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1318 if (safe_to_release_mutator_lock_over_send) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -07001319 for (size_t i = 0; i < kMutatorLock; ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001320 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001321 safe_to_release_mutator_lock_over_send = false;
1322 break;
1323 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001324 }
1325 }
1326 if (safe_to_release_mutator_lock_over_send) {
1327 // Change state to waiting to allow GC, ... while we're sending.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001328 ScopedThreadSuspension sts(self, kWaitingForDebuggerSend);
Brian Carlstromf5293522013-07-19 00:24:00 -07001329 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001330 } else {
1331 // Send and possibly block GC...
Brian Carlstromf5293522013-07-19 00:24:00 -07001332 SendBufferedRequest(type, wrapiov);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001333 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001334}
1335
1336} // namespace JDWP
1337
1338} // namespace art