blob: 5b65aa4bab0a59476f03c869dcb01afb934d8f24 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070016
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include "jdwp/jdwp_event.h"
18
19#include <stddef.h> /* for offsetof() */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070020#include <stdlib.h>
21#include <string.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070022#include <unistd.h>
23
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "debugger.h"
27#include "jdwp/jdwp_constants.h"
28#include "jdwp/jdwp_expand_buf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080029#include "jdwp/jdwp_priv.h"
Ian Rogers693ff612013-02-01 10:56:12 -080030#include "thread-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031
Elliott Hughes872d4ec2011-10-21 17:07:15 -070032/*
33General notes:
34
35The event add/remove stuff usually happens from the debugger thread,
36in response to requests from the debugger, but can also happen as the
37result of an event in an arbitrary thread (e.g. an event with a "count"
38mod expires). It's important to keep the event list locked when processing
39events.
40
41Event posting can happen from any thread. The JDWP thread will not usually
42post anything but VM start/death, but if a JDWP request causes a class
43to be loaded, the ClassPrepare event will come from the JDWP thread.
44
45
46We can have serialization issues when we post an event to the debugger.
47For example, a thread could send an "I hit a breakpoint and am suspending
48myself" message to the debugger. Before it manages to suspend itself, the
49debugger's response ("not interested, resume thread") arrives and is
50processed. We try to resume a thread that hasn't yet suspended.
51
52This means that, after posting an event to the debugger, we need to wait
53for the event thread to suspend itself (and, potentially, all other threads)
54before processing any additional requests from the debugger. While doing
55so we need to be aware that multiple threads may be hitting breakpoints
56or other events simultaneously, so we either need to wait for all of them
57or serialize the events with each other.
58
59The current mechanism works like this:
60 Event thread:
61 - If I'm going to suspend, grab the "I am posting an event" token. Wait
62 for it if it's not currently available.
63 - Post the event to the debugger.
64 - If appropriate, suspend others and then myself. As part of suspending
65 myself, release the "I am posting" token.
66 JDWP thread:
67 - When an event arrives, see if somebody is posting an event. If so,
68 sleep until we can acquire the "I am posting an event" token. Release
69 it immediately and continue processing -- the event we have already
70 received should not interfere with other events that haven't yet
71 been posted.
72
73Some care must be taken to avoid deadlock:
74
75 - thread A and thread B exit near-simultaneously, and post thread-death
76 events with a "suspend all" clause
77 - thread A gets the event token, thread B sits and waits for it
78 - thread A wants to suspend all other threads, but thread B is waiting
79 for the token and can't be suspended
80
81So we need to mark thread B in such a way that thread A doesn't wait for it.
82
83If we just bracket the "grab event token" call with a change to VMWAIT
84before sleeping, the switch back to RUNNING state when we get the token
85will cause thread B to suspend (remember, thread A's global suspend is
86still in force, even after it releases the token). Suspending while
87holding the event token is very bad, because it prevents the JDWP thread
88from processing incoming messages.
89
90We need to change to VMWAIT state at the *start* of posting an event,
91and stay there until we either finish posting the event or decide to
92put ourselves to sleep. That way we don't interfere with anyone else and
93don't allow anyone else to interfere with us.
94*/
95
96
97#define kJdwpEventCommandSet 64
98#define kJdwpCompositeCommand 100
99
100namespace art {
101
102namespace JDWP {
103
104/*
105 * Stuff to compare against when deciding if a mod matches. Only the
106 * values for mods valid for the event being evaluated will be filled in.
107 * The rest will be zeroed.
108 */
109struct ModBasket {
jeffhao162fd332013-01-08 16:21:01 -0800110 ModBasket() : pLoc(NULL), threadId(0), classId(0), excepClassId(0),
111 caught(false), field(0), thisPtr(0) { }
112
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113 const JdwpLocation* pLoc; /* LocationOnly */
Elliott Hughesa2155262011-11-16 16:26:58 -0800114 std::string className; /* ClassMatch/ClassExclude */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700115 ObjectId threadId; /* ThreadOnly */
116 RefTypeId classId; /* ClassOnly */
117 RefTypeId excepClassId; /* ExceptionOnly */
118 bool caught; /* ExceptionOnly */
119 FieldId field; /* FieldOnly */
120 ObjectId thisPtr; /* InstanceOnly */
121 /* nothing for StepOnly -- handled differently */
122};
123
124/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 * Dump an event to the log file.
126 */
127static void dumpEvent(const JdwpEvent* pEvent) {
128 LOG(INFO) << StringPrintf("Event id=0x%4x %p (prev=%p next=%p):", pEvent->requestId, pEvent, pEvent->prev, pEvent->next);
Elliott Hughesf8349362012-06-18 15:00:06 -0700129 LOG(INFO) << " kind=" << pEvent->eventKind << " susp=" << pEvent->suspend_policy << " modCount=" << pEvent->modCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130
131 for (int i = 0; i < pEvent->modCount; i++) {
132 const JdwpEventMod* pMod = &pEvent->mods[i];
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800133 LOG(INFO) << " " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700134 /* TODO - show details */
135 }
136}
137
138/*
139 * Add an event to the list. Ordering is not important.
140 *
141 * If something prevents the event from being registered, e.g. it's a
142 * single-step request on a thread that doesn't exist, the event will
143 * not be added to the list, and an appropriate error will be returned.
144 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800145JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700146 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700148 CHECK(pEvent != NULL);
149 CHECK(pEvent->prev == NULL);
150 CHECK(pEvent->next == NULL);
151
152 /*
153 * If one or more "break"-type mods are used, register them with
154 * the interpreter.
155 */
156 for (int i = 0; i < pEvent->modCount; i++) {
157 const JdwpEventMod* pMod = &pEvent->mods[i];
158 if (pMod->modKind == MK_LOCATION_ONLY) {
159 /* should only be for Breakpoint, Step, and Exception */
160 Dbg::WatchLocation(&pMod->locationOnly.loc);
161 } else if (pMod->modKind == MK_STEP) {
162 /* should only be for EK_SINGLE_STEP; should only be one */
163 JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size);
164 JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth);
Elliott Hughes2435a572012-02-17 16:07:41 -0800165 JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth);
166 if (status != ERR_NONE) {
167 return status;
168 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169 } else if (pMod->modKind == MK_FIELD_ONLY) {
170 /* should be for EK_FIELD_ACCESS or EK_FIELD_MODIFICATION */
171 dumpEvent(pEvent); /* TODO - need for field watches */
172 }
173 }
174
175 /*
176 * Add to list.
177 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700178 if (event_list_ != NULL) {
179 pEvent->next = event_list_;
180 event_list_->prev = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700182 event_list_ = pEvent;
183 ++event_list_size_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700184
185 return ERR_NONE;
186}
187
188/*
189 * Remove an event from the list. This will also remove the event from
190 * any optimization tables, e.g. breakpoints.
191 *
192 * Does not free the JdwpEvent.
193 *
194 * Grab the eventLock before calling here.
195 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800196void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197 if (pEvent->prev == NULL) {
198 /* head of the list */
Elliott Hughesf8349362012-06-18 15:00:06 -0700199 CHECK(event_list_ == pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200
Elliott Hughesf8349362012-06-18 15:00:06 -0700201 event_list_ = pEvent->next;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202 } else {
203 pEvent->prev->next = pEvent->next;
204 }
205
206 if (pEvent->next != NULL) {
207 pEvent->next->prev = pEvent->prev;
208 pEvent->next = NULL;
209 }
210 pEvent->prev = NULL;
211
212 /*
213 * Unhook us from the interpreter, if necessary.
214 */
215 for (int i = 0; i < pEvent->modCount; i++) {
216 JdwpEventMod* pMod = &pEvent->mods[i];
217 if (pMod->modKind == MK_LOCATION_ONLY) {
218 /* should only be for Breakpoint, Step, and Exception */
219 Dbg::UnwatchLocation(&pMod->locationOnly.loc);
220 }
221 if (pMod->modKind == MK_STEP) {
222 /* should only be for EK_SINGLE_STEP; should only be one */
223 Dbg::UnconfigureStep(pMod->step.threadId);
224 }
225 }
226
Elliott Hughesf8349362012-06-18 15:00:06 -0700227 --event_list_size_;
228 CHECK(event_list_size_ != 0 || event_list_ == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700229}
230
231/*
232 * Remove the event with the given ID from the list.
233 *
234 * Failure to find the event isn't really an error, but it is a little
235 * weird. (It looks like Eclipse will try to be extra careful and will
236 * explicitly remove one-off single-step events.)
237 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800238void JdwpState::UnregisterEventById(uint32_t requestId) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700239 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700240
Elliott Hughesf8349362012-06-18 15:00:06 -0700241 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700242 while (pEvent != NULL) {
243 if (pEvent->requestId == requestId) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800244 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245 EventFree(pEvent);
Elliott Hughes761928d2011-11-16 18:33:03 -0800246 return; /* there can be only one with a given ID */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247 }
248
249 pEvent = pEvent->next;
250 }
251
252 //LOGD("Odd: no match when removing event reqId=0x%04x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253}
254
255/*
256 * Remove all entries from the event list.
257 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800258void JdwpState::UnregisterAll() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700259 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260
Elliott Hughesf8349362012-06-18 15:00:06 -0700261 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262 while (pEvent != NULL) {
263 JdwpEvent* pNextEvent = pEvent->next;
264
Elliott Hughes761928d2011-11-16 18:33:03 -0800265 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700266 EventFree(pEvent);
267 pEvent = pNextEvent;
268 }
269
Elliott Hughesf8349362012-06-18 15:00:06 -0700270 event_list_ = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700271}
272
273/*
274 * Allocate a JdwpEvent struct with enough space to hold the specified
275 * number of mod records.
276 */
277JdwpEvent* EventAlloc(int numMods) {
278 JdwpEvent* newEvent;
279 int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]);
280 newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize));
281 memset(newEvent, 0, allocSize);
282 return newEvent;
283}
284
285/*
286 * Free a JdwpEvent.
287 *
288 * Do not call this until the event has been removed from the list.
289 */
290void EventFree(JdwpEvent* pEvent) {
291 if (pEvent == NULL) {
292 return;
293 }
294
295 /* make sure it was removed from the list */
296 CHECK(pEvent->prev == NULL);
297 CHECK(pEvent->next == NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700298 /* want to check state->event_list_ != pEvent */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700299
300 /*
301 * Free any hairy bits in the mods.
302 */
303 for (int i = 0; i < pEvent->modCount; i++) {
304 if (pEvent->mods[i].modKind == MK_CLASS_MATCH) {
305 free(pEvent->mods[i].classMatch.classPattern);
306 pEvent->mods[i].classMatch.classPattern = NULL;
307 }
308 if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) {
309 free(pEvent->mods[i].classExclude.classPattern);
310 pEvent->mods[i].classExclude.classPattern = NULL;
311 }
312 }
313
314 free(pEvent);
315}
316
317/*
318 * Allocate storage for matching events. To keep things simple we
319 * use an array with enough storage for the entire list.
320 *
321 * The state->eventLock should be held before calling.
322 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800323static JdwpEvent** AllocMatchList(size_t event_count) {
324 return new JdwpEvent*[event_count];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325}
326
327/*
328 * Run through the list and remove any entries with an expired "count" mod
329 * from the event list, then free the match list.
330 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700331void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
332 JdwpEvent** ppEvent = match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800334 while (match_count--) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700335 JdwpEvent* pEvent = *ppEvent;
336
337 for (int i = 0; i < pEvent->modCount; i++) {
338 if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800339 VLOG(jdwp) << "##### Removing expired event";
Elliott Hughes761928d2011-11-16 18:33:03 -0800340 UnregisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700341 EventFree(pEvent);
342 break;
343 }
344 }
345
346 ppEvent++;
347 }
348
Elliott Hughesf8349362012-06-18 15:00:06 -0700349 delete[] match_list;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700350}
351
352/*
353 * Match a string against a "restricted regular expression", which is just
354 * a string that may start or end with '*' (e.g. "*.Foo" or "java.*").
355 *
356 * ("Restricted name globbing" might have been a better term.)
357 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800358static bool PatternMatch(const char* pattern, const std::string& target) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800359 size_t patLen = strlen(pattern);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700360 if (pattern[0] == '*') {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361 patLen--;
Elliott Hughesa2155262011-11-16 16:26:58 -0800362 if (target.size() < patLen) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700363 return false;
364 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800365 return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700366 } else if (pattern[patLen-1] == '*') {
Elliott Hughesa2155262011-11-16 16:26:58 -0800367 return strncmp(pattern, target.c_str(), patLen-1) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -0800369 return strcmp(pattern, target.c_str()) == 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700370 }
371}
372
373/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700374 * See if the event's mods match up with the contents of "basket".
375 *
376 * If we find a Count mod before rejecting an event, we decrement it. We
377 * need to do this even if later mods cause us to ignore the event.
378 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 JdwpEventMod* pMod = pEvent->mods;
382
383 for (int i = pEvent->modCount; i > 0; i--, pMod++) {
384 switch (pMod->modKind) {
385 case MK_COUNT:
386 CHECK_GT(pMod->count.count, 0);
387 pMod->count.count--;
388 break;
389 case MK_CONDITIONAL:
390 CHECK(false); // should not be getting these
391 break;
392 case MK_THREAD_ONLY:
393 if (pMod->threadOnly.threadId != basket->threadId) {
394 return false;
395 }
396 break;
397 case MK_CLASS_ONLY:
398 if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) {
399 return false;
400 }
401 break;
402 case MK_CLASS_MATCH:
Elliott Hughes761928d2011-11-16 18:33:03 -0800403 if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404 return false;
405 }
406 break;
407 case MK_CLASS_EXCLUDE:
Elliott Hughes761928d2011-11-16 18:33:03 -0800408 if (PatternMatch(pMod->classMatch.classPattern, basket->className)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700409 return false;
410 }
411 break;
412 case MK_LOCATION_ONLY:
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800413 if (pMod->locationOnly.loc != *basket->pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414 return false;
415 }
416 break;
417 case MK_EXCEPTION_ONLY:
418 if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) {
419 return false;
420 }
421 if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) {
422 return false;
423 }
424 break;
425 case MK_FIELD_ONLY:
426 if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) {
427 return false;
428 }
429 break;
430 case MK_STEP:
431 if (pMod->step.threadId != basket->threadId) {
432 return false;
433 }
434 break;
435 case MK_INSTANCE_ONLY:
436 if (pMod->instanceOnly.objectId != basket->thisPtr) {
437 return false;
438 }
439 break;
440 default:
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800441 LOG(FATAL) << "unknown mod kind " << pMod->modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442 break;
443 }
444 }
445 return true;
446}
447
448/*
449 * Find all events of type "eventKind" with mods that match up with the
450 * rest of the arguments.
451 *
Elliott Hughesf8349362012-06-18 15:00:06 -0700452 * Found events are appended to "match_list", and "*pMatchCount" is advanced,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453 * so this may be called multiple times for grouped events.
454 *
455 * DO NOT call this multiple times for the same eventKind, as Count mods are
456 * decremented during the scan.
457 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700458void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket,
459 JdwpEvent** match_list, int* pMatchCount) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700460 /* start after the existing entries */
Elliott Hughesf8349362012-06-18 15:00:06 -0700461 match_list += *pMatchCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462
Elliott Hughesf8349362012-06-18 15:00:06 -0700463 JdwpEvent* pEvent = event_list_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 while (pEvent != NULL) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800465 if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700466 *match_list++ = pEvent;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467 (*pMatchCount)++;
468 }
469
470 pEvent = pEvent->next;
471 }
472}
473
474/*
475 * Scan through the list of matches and determine the most severe
476 * suspension policy.
477 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700478static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 JdwpSuspendPolicy policy = SP_NONE;
480
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800481 while (match_count--) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700482 if ((*match_list)->suspend_policy > policy) {
483 policy = (*match_list)->suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484 }
Elliott Hughesf8349362012-06-18 15:00:06 -0700485 match_list++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486 }
487
488 return policy;
489}
490
491/*
492 * Three possibilities:
493 * SP_NONE - do nothing
494 * SP_EVENT_THREAD - suspend ourselves
495 * SP_ALL - suspend everybody except JDWP support thread
496 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700497void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700498 VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
499 if (suspend_policy == SP_NONE) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 return;
501 }
502
Elliott Hughesf8349362012-06-18 15:00:06 -0700503 if (suspend_policy == SP_ALL) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700504 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700505 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700506 CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700507 }
508
509 /* this is rare but possible -- see CLASS_PREPARE handling */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700510 if (thread_self_id == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800511 LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512 return;
513 }
514
515 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
516 while (true) {
517 pReq->ready = true;
518 Dbg::SuspendSelf();
519 pReq->ready = false;
520
521 /*
522 * The JDWP thread has told us (and possibly all other threads) to
523 * resume. See if it has left anything in our DebugInvokeReq mailbox.
524 */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800525 if (!pReq->invoke_needed_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800526 /*LOGD("SuspendByPolicy: no invoke needed");*/
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 break;
528 }
529
530 /* grab this before posting/suspending again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700531 SetWaitForEventThread(thread_self_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700532
Elliott Hughesd07986f2011-12-06 18:27:45 -0800533 /* leave pReq->invoke_needed_ raised so we can check reentrancy */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700534 Dbg::ExecuteMethod(pReq);
535
Elliott Hughes475fc232011-10-25 15:00:35 -0700536 pReq->error = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537
538 /* clear this before signaling */
Elliott Hughesd07986f2011-12-06 18:27:45 -0800539 pReq->invoke_needed_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800541 VLOG(jdwp) << "invoke complete, signaling and self-suspending";
Ian Rogersc604d732012-10-14 16:09:54 -0700542 Thread* self = Thread::Current();
543 MutexLock mu(self, pReq->lock_);
544 pReq->cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545 }
546}
547
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700548void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
549 ObjectId threadId) {
550 Thread* self = Thread::Current();
551 self->AssertThreadSuspensionIsAllowable();
552 /* send request and possibly suspend ourselves */
553 if (pReq != NULL) {
554 JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId();
555 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
556 if (suspend_policy != SP_NONE) {
557 SetWaitForEventThread(threadId);
558 }
559 EventFinish(pReq);
560 SuspendByPolicy(suspend_policy, thread_self_id);
561 self->TransitionFromSuspendedToRunnable();
562 }
563}
564
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700565/*
566 * Determine if there is a method invocation in progress in the current
567 * thread.
568 *
Elliott Hughes475fc232011-10-25 15:00:35 -0700569 * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570 * state. If set, we're in the process of invoking a method.
571 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800572bool JdwpState::InvokeInProgress() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573 DebugInvokeReq* pReq = Dbg::GetInvokeReq();
Elliott Hughesd07986f2011-12-06 18:27:45 -0800574 return pReq->invoke_needed_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575}
576
577/*
578 * We need the JDWP thread to hold off on doing stuff while we post an
579 * event and then suspend ourselves.
580 *
581 * Call this with a threadId of zero if you just want to wait for the
582 * current thread operation to complete.
583 *
584 * This could go to sleep waiting for another thread, so it's important
585 * that the thread be marked as VMWAIT before calling here.
586 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700587void JdwpState::SetWaitForEventThread(ObjectId threadId) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588 bool waited = false;
589
590 /* this is held for very brief periods; contention is unlikely */
Ian Rogers81d425b2012-09-27 16:03:43 -0700591 Thread* self = Thread::Current();
592 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700593
594 /*
595 * If another thread is already doing stuff, wait for it. This can
596 * go to sleep indefinitely.
597 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700598 while (event_thread_id_ != 0) {
599 VLOG(jdwp) << StringPrintf("event in progress (%#llx), %#llx sleeping", event_thread_id_, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700600 waited = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700601 event_thread_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700602 }
603
604 if (waited || threadId != 0) {
Elliott Hughes229feb72012-02-23 13:33:29 -0800605 VLOG(jdwp) << StringPrintf("event token grabbed (%#llx)", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700606 }
607 if (threadId != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700608 event_thread_id_ = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609 }
610}
611
612/*
613 * Clear the threadId and signal anybody waiting.
614 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700615void JdwpState::ClearWaitForEventThread() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616 /*
617 * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this
618 * function is called by dvmSuspendSelf(), and the transition back
619 * to RUNNING would confuse it.
620 */
Ian Rogersc604d732012-10-14 16:09:54 -0700621 Thread* self = Thread::Current();
622 MutexLock mu(self, event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700623
Elliott Hughesa21039c2012-06-21 12:09:25 -0700624 CHECK_NE(event_thread_id_, 0U);
625 VLOG(jdwp) << StringPrintf("cleared event token (%#llx)", event_thread_id_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700626
Elliott Hughesa21039c2012-06-21 12:09:25 -0700627 event_thread_id_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700628
Ian Rogersc604d732012-10-14 16:09:54 -0700629 event_thread_cond_.Signal(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700630}
631
632
633/*
634 * Prep an event. Allocates storage for the message and leaves space for
635 * the header.
636 */
637static ExpandBuf* eventPrep() {
638 ExpandBuf* pReq = expandBufAlloc();
639 expandBufAddSpace(pReq, kJDWPHeaderLen);
640 return pReq;
641}
642
643/*
644 * Write the header into the buffer and send the packet off to the debugger.
645 *
646 * Takes ownership of "pReq" (currently discards it).
647 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800648void JdwpState::EventFinish(ExpandBuf* pReq) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700649 uint8_t* buf = expandBufGetBuffer(pReq);
650
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700651 Set4BE(buf, expandBufGetLength(pReq));
Elliott Hughes761928d2011-11-16 18:33:03 -0800652 Set4BE(buf+4, NextRequestSerial());
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700653 Set1(buf+8, 0); /* flags */
654 Set1(buf+9, kJdwpEventCommandSet);
655 Set1(buf+10, kJdwpCompositeCommand);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700656
Elliott Hughes761928d2011-11-16 18:33:03 -0800657 SendRequest(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658
659 expandBufFree(pReq);
660}
661
662
663/*
664 * Tell the debugger that we have finished initializing. This is always
665 * sent, even if the debugger hasn't requested it.
666 *
667 * This should be sent "before the main thread is started and before
668 * any application code has been executed". The thread ID in the message
669 * must be for the main thread.
670 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700671bool JdwpState::PostVMStart() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700672 JdwpSuspendPolicy suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700673 ObjectId threadId = Dbg::GetThreadSelfId();
674
Elliott Hughes376a7a02011-10-24 18:35:55 -0700675 if (options_->suspend) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700676 suspend_policy = SP_ALL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700677 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700678 suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679 }
680
Elliott Hughes761928d2011-11-16 18:33:03 -0800681 ExpandBuf* pReq = eventPrep();
682 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700683 MutexLock mu(Thread::Current(), event_list_lock_); // probably don't need this here
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700684
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800685 VLOG(jdwp) << "EVENT: " << EK_VM_START;
Elliott Hughesf8349362012-06-18 15:00:06 -0700686 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700687
Elliott Hughesf8349362012-06-18 15:00:06 -0700688 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700689 expandBufAdd4BE(pReq, 1);
690
691 expandBufAdd1(pReq, EK_VM_START);
692 expandBufAdd4BE(pReq, 0); /* requestId */
693 expandBufAdd8BE(pReq, threadId);
694 }
695
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700696 /* send request and possibly suspend ourselves */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698
699 return true;
700}
701
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700702/*
703 * A location of interest has been reached. This handles:
704 * Breakpoint
705 * SingleStep
706 * MethodEntry
707 * MethodExit
708 * These four types must be grouped together in a single response. The
709 * "eventFlags" indicates the type of event(s) that have happened.
710 *
711 * Valid mods:
712 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly
713 * LocationOnly (for breakpoint/step only)
714 * Step (for step only)
715 *
716 * Interesting test cases:
717 * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY
718 * and METHOD_EXIT events with a ClassOnly mod on the method's class.
719 * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1.
720 * - Single-step to a line with a breakpoint. Should get a single
721 * event message with both events in it.
722 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800723bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700725 basket.pLoc = pLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700726 basket.classId = pLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727 basket.thisPtr = thisPtr;
728 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughes74847412012-06-20 18:10:21 -0700729 basket.className = Dbg::GetClassName(pLoc->class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730
731 /*
732 * On rare occasions we may need to execute interpreted code in the VM
733 * while handling a request from the debugger. Don't fire breakpoints
734 * while doing so. (I don't think we currently do this at all, so
735 * this is mostly paranoia.)
736 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700737 if (basket.threadId == debug_thread_id_) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800738 VLOG(jdwp) << "Ignoring location event in JDWP thread";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700739 return false;
740 }
741
742 /*
743 * The debugger variable display tab may invoke the interpreter to format
744 * complex objects. We want to ignore breakpoints and method entry/exit
745 * traps while working on behalf of the debugger.
746 *
747 * If we don't ignore them, the VM will get hung up, because we'll
748 * suspend on a breakpoint while the debugger is still waiting for its
749 * method invocation to complete.
750 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800751 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800752 VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753 return false;
754 }
755
Elliott Hughesf8349362012-06-18 15:00:06 -0700756 JdwpEvent** match_list = NULL;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800757 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700758 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700759 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700760
Elliott Hughes761928d2011-11-16 18:33:03 -0800761 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700762 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700763 match_list = AllocMatchList(event_list_size_);
Elliott Hughes86964332012-02-15 19:37:42 -0800764 if ((eventFlags & Dbg::kBreakpoint) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700765 FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700766 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800767 if ((eventFlags & Dbg::kSingleStep) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700768 FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800769 }
770 if ((eventFlags & Dbg::kMethodEntry) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700771 FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800772 }
773 if ((eventFlags & Dbg::kMethodExit) != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700774 FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
Elliott Hughes86964332012-02-15 19:37:42 -0800775
776 // TODO: match EK_METHOD_EXIT_WITH_RETURN_VALUE too; we need to include the 'value', though.
Elliott Hughesf8349362012-06-18 15:00:06 -0700777 //FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800778 }
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800779 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700780 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughesa96836a2013-01-17 12:27:49 -0800781 << basket.className << "." << Dbg::GetMethodName(pLoc->method_id)
Elliott Hughes229feb72012-02-23 13:33:29 -0800782 << StringPrintf(" thread=%#llx dex_pc=%#llx)", basket.threadId, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700783
Elliott Hughesf8349362012-06-18 15:00:06 -0700784 suspend_policy = scanSuspendPolicy(match_list, match_count);
785 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800786
787 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700788 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800789 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800790
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800791 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700792 expandBufAdd1(pReq, match_list[i]->eventKind);
793 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800794 expandBufAdd8BE(pReq, basket.threadId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700795 expandBufAddLocation(pReq, *pLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800796 }
797 }
798
Elliott Hughesf8349362012-06-18 15:00:06 -0700799 CleanupMatchList(match_list, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800800 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800803 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700804}
805
806/*
807 * A thread is starting or stopping.
808 *
809 * Valid mods:
810 * Count, ThreadOnly
811 */
Elliott Hughes234ab152011-10-26 14:02:26 -0700812bool JdwpState::PostThreadChange(ObjectId threadId, bool start) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700813 CHECK_EQ(threadId, Dbg::GetThreadSelfId());
814
815 /*
816 * I don't think this can happen.
817 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800818 if (InvokeInProgress()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700819 LOG(WARNING) << "Not posting thread change during invoke";
820 return false;
821 }
822
823 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824 basket.threadId = threadId;
825
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700826 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700827 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800828 int match_count = 0;
Elliott Hughes234ab152011-10-26 14:02:26 -0700829 {
830 // Don't allow the list to be updated while we scan it.
Ian Rogers50b35e22012-10-04 10:09:15 -0700831 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700832 JdwpEvent** match_list = AllocMatchList(event_list_size_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833
Elliott Hughes234ab152011-10-26 14:02:26 -0700834 if (start) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700835 FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700836 } else {
Elliott Hughesf8349362012-06-18 15:00:06 -0700837 FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700839
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800840 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700841 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -0800842 << StringPrintf("thread=%#llx", basket.threadId) << ")";
Elliott Hughes234ab152011-10-26 14:02:26 -0700843
Elliott Hughesf8349362012-06-18 15:00:06 -0700844 suspend_policy = scanSuspendPolicy(match_list, match_count);
845 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes234ab152011-10-26 14:02:26 -0700846
847 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700848 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800849 expandBufAdd4BE(pReq, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700850
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800851 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700852 expandBufAdd1(pReq, match_list[i]->eventKind);
853 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes234ab152011-10-26 14:02:26 -0700854 expandBufAdd8BE(pReq, basket.threadId);
855 }
856 }
857
Elliott Hughesf8349362012-06-18 15:00:06 -0700858 CleanupMatchList(match_list, match_count);
Elliott Hughes234ab152011-10-26 14:02:26 -0700859 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700860
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700862
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800863 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700864}
865
866/*
867 * Send a polite "VM is dying" message to the debugger.
868 *
869 * Skips the usual "event token" stuff.
870 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700871bool JdwpState::PostVMDeath() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800872 VLOG(jdwp) << "EVENT: " << EK_VM_DEATH;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873
874 ExpandBuf* pReq = eventPrep();
875 expandBufAdd1(pReq, SP_NONE);
876 expandBufAdd4BE(pReq, 1);
877
878 expandBufAdd1(pReq, EK_VM_DEATH);
879 expandBufAdd4BE(pReq, 0);
Elliott Hughes761928d2011-11-16 18:33:03 -0800880 EventFinish(pReq);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700881 return true;
882}
883
884/*
885 * An exception has been thrown. It may or may not have been caught.
886 *
887 * Valid mods:
888 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly,
889 * ExceptionOnly, InstanceOnly
890 *
891 * The "exceptionId" has not been added to the GC-visible object registry,
892 * because there's a pretty good chance that we're not going to send it
893 * up the debugger.
894 */
Elliott Hughes761928d2011-11-16 18:33:03 -0800895bool JdwpState::PostException(const JdwpLocation* pThrowLoc,
Elliott Hughes74847412012-06-20 18:10:21 -0700896 ObjectId exceptionId, RefTypeId exceptionClassId,
897 const JdwpLocation* pCatchLoc, ObjectId thisPtr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700898 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700900 basket.pLoc = pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700901 basket.classId = pThrowLoc->class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700902 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800903 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700904 basket.excepClassId = exceptionClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700905 basket.caught = (pCatchLoc->class_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700906 basket.thisPtr = thisPtr;
907
908 /* don't try to post an exception caused by the debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800909 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800910 VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700911 return false;
912 }
913
Elliott Hughesf8349362012-06-18 15:00:06 -0700914 JdwpEvent** match_list = NULL;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800915 int match_count = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700916 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700917 JdwpSuspendPolicy suspend_policy = SP_NONE;
Elliott Hughes761928d2011-11-16 18:33:03 -0800918 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700919 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700920 match_list = AllocMatchList(event_list_size_);
921 FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800922 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700923 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)"
Elliott Hughes0cf74332012-02-23 23:14:00 -0800924 << StringPrintf(" thread=%#llx", basket.threadId)
925 << StringPrintf(" exceptId=%#llx", exceptionId)
Elliott Hughes436e3722012-02-17 20:01:47 -0800926 << " caught=" << basket.caught << ")"
927 << " throw: " << *pThrowLoc;
Elliott Hughes74847412012-06-20 18:10:21 -0700928 if (pCatchLoc->class_id == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800929 VLOG(jdwp) << " catch: (not caught)";
Elliott Hughes761928d2011-11-16 18:33:03 -0800930 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800931 VLOG(jdwp) << " catch: " << *pCatchLoc;
Elliott Hughes761928d2011-11-16 18:33:03 -0800932 }
933
Elliott Hughesf8349362012-06-18 15:00:06 -0700934 suspend_policy = scanSuspendPolicy(match_list, match_count);
935 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes761928d2011-11-16 18:33:03 -0800936
937 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -0700938 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800939 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -0800940
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800941 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700942 expandBufAdd1(pReq, match_list[i]->eventKind);
943 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -0800944 expandBufAdd8BE(pReq, basket.threadId);
945
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700946 expandBufAddLocation(pReq, *pThrowLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800947 expandBufAdd1(pReq, JT_OBJECT);
948 expandBufAdd8BE(pReq, exceptionId);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700949 expandBufAddLocation(pReq, *pCatchLoc);
Elliott Hughes761928d2011-11-16 18:33:03 -0800950 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700951 }
952
Elliott Hughesf8349362012-06-18 15:00:06 -0700953 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700954 }
955
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700957
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800958 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700959}
960
961/*
962 * Announce that a class has been loaded.
963 *
964 * Valid mods:
965 * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude
966 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700967bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
968 int status) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700969 ModBasket basket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700970
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971 basket.classId = refTypeId;
972 basket.threadId = Dbg::GetThreadSelfId();
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800973 basket.className = Dbg::GetClassName(basket.classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974
975 /* suppress class prep caused by debugger */
Elliott Hughes761928d2011-11-16 18:33:03 -0800976 if (InvokeInProgress()) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800977 VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700978 return false;
979 }
980
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700981 ExpandBuf* pReq = NULL;
Elliott Hughesf8349362012-06-18 15:00:06 -0700982 JdwpSuspendPolicy suspend_policy = SP_NONE;
983 int match_count = 0;
Elliott Hughes761928d2011-11-16 18:33:03 -0800984 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700985 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700986 JdwpEvent** match_list = AllocMatchList(event_list_size_);
987 FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800988 if (match_count != 0) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700989 VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
Elliott Hughes0cf74332012-02-23 23:14:00 -0800990 << StringPrintf("thread=%#llx", basket.threadId) << ") " << signature;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700991
Elliott Hughesf8349362012-06-18 15:00:06 -0700992 suspend_policy = scanSuspendPolicy(match_list, match_count);
993 VLOG(jdwp) << " suspend_policy=" << suspend_policy;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700994
Elliott Hughesa21039c2012-06-21 12:09:25 -0700995 if (basket.threadId == debug_thread_id_) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800996 /*
997 * JDWP says that, for a class prep in the debugger thread, we
998 * should set threadId to null and if any threads were supposed
999 * to be suspended then we suspend all other threads.
1000 */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001001 VLOG(jdwp) << " NOTE: class prepare in debugger thread!";
Elliott Hughes761928d2011-11-16 18:33:03 -08001002 basket.threadId = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -07001003 if (suspend_policy == SP_EVENT_THREAD) {
1004 suspend_policy = SP_ALL;
Elliott Hughes761928d2011-11-16 18:33:03 -08001005 }
1006 }
1007
1008 pReq = eventPrep();
Elliott Hughesf8349362012-06-18 15:00:06 -07001009 expandBufAdd1(pReq, suspend_policy);
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001010 expandBufAdd4BE(pReq, match_count);
Elliott Hughes761928d2011-11-16 18:33:03 -08001011
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001012 for (int i = 0; i < match_count; i++) {
Elliott Hughesf8349362012-06-18 15:00:06 -07001013 expandBufAdd1(pReq, match_list[i]->eventKind);
1014 expandBufAdd4BE(pReq, match_list[i]->requestId);
Elliott Hughes761928d2011-11-16 18:33:03 -08001015 expandBufAdd8BE(pReq, basket.threadId);
1016
1017 expandBufAdd1(pReq, tag);
1018 expandBufAdd8BE(pReq, refTypeId);
1019 expandBufAddUtf8String(pReq, signature);
1020 expandBufAdd4BE(pReq, status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001021 }
1022 }
Elliott Hughesf8349362012-06-18 15:00:06 -07001023 CleanupMatchList(match_list, match_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001024 }
1025
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026 SendRequestAndPossiblySuspend(pReq, suspend_policy, basket.threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001027
Elliott Hughes2aa2e392012-02-17 17:15:43 -08001028 return match_count != 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001029}
1030
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031/*
1032 * Send up a chunk of DDM data.
1033 *
1034 * While this takes the form of a JDWP "event", it doesn't interact with
1035 * other debugger traffic, and can't suspend the VM, so we skip all of
1036 * the fun event token gymnastics.
1037 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001038void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001039 uint8_t header[kJDWPHeaderLen + 8];
1040 size_t dataLen = 0;
1041
1042 CHECK(iov != NULL);
Elliott Hughescccd84f2011-12-05 16:51:54 -08001043 CHECK_GT(iov_count, 0);
1044 CHECK_LT(iov_count, 10);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001045
1046 /*
1047 * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do
1048 * this by creating a new copy of the vector with space for the header.
1049 */
Elliott Hughescccd84f2011-12-05 16:51:54 -08001050 iovec wrapiov[iov_count+1];
1051 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001052 wrapiov[i+1].iov_base = iov[i].iov_base;
1053 wrapiov[i+1].iov_len = iov[i].iov_len;
1054 dataLen += iov[i].iov_len;
1055 }
1056
1057 /* form the header (JDWP plus DDMS) */
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001058 Set4BE(header, sizeof(header) + dataLen);
1059 Set4BE(header+4, NextRequestSerial());
1060 Set1(header+8, 0); /* flags */
1061 Set1(header+9, kJDWPDdmCmdSet);
1062 Set1(header+10, kJDWPDdmCmd);
1063 Set4BE(header+11, type);
1064 Set4BE(header+15, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001065
1066 wrapiov[0].iov_base = header;
1067 wrapiov[0].iov_len = sizeof(header);
1068
Ian Rogers15bf2d32012-08-28 17:33:04 -07001069 // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level
1070 // than mutator for lock ordering reasons.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08001072 bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self);
1073 if (safe_to_release_mutator_lock_over_send) {
1074 for (size_t i=0; i < kMutatorLock; ++i) {
1075 if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
1076 safe_to_release_mutator_lock_over_send = false;
1077 break;
1078 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07001079 }
1080 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001081 bool success;
Ian Rogers15bf2d32012-08-28 17:33:04 -07001082 if (safe_to_release_mutator_lock_over_send) {
1083 // Change state to waiting to allow GC, ... while we're sending.
1084 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Ian Rogers62d6c772013-02-27 08:32:07 -08001085 success = (*transport_->sendBufferedRequest)(this, wrapiov, iov_count + 1);
Ian Rogers15bf2d32012-08-28 17:33:04 -07001086 self->TransitionFromSuspendedToRunnable();
1087 } else {
1088 // Send and possibly block GC...
Ian Rogers62d6c772013-02-27 08:32:07 -08001089 success = (*transport_->sendBufferedRequest)(this, wrapiov, iov_count + 1);
1090 }
1091 if (!success) {
1092 LOG(INFO) << StringPrintf("JDWP send of type %c%c%c%c failed.",
1093 static_cast<uint8_t>(type >> 24),
1094 static_cast<uint8_t>(type >> 16),
1095 static_cast<uint8_t>(type >> 8),
1096 static_cast<uint8_t>(type));
Ian Rogers15bf2d32012-08-28 17:33:04 -07001097 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001098}
1099
1100} // namespace JDWP
1101
1102} // namespace art