Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | /* |
| 17 | * Send events to the debugger. |
| 18 | */ |
| 19 | #include "debugger.h" |
| 20 | #include "jdwp/jdwp_priv.h" |
| 21 | #include "jdwp/jdwp_constants.h" |
| 22 | #include "jdwp/jdwp_handler.h" |
| 23 | #include "jdwp/jdwp_event.h" |
| 24 | #include "jdwp/jdwp_expand_buf.h" |
| 25 | #include "logging.h" |
| 26 | #include "stringprintf.h" |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <stddef.h> /* for offsetof() */ |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | /* |
| 34 | General notes: |
| 35 | |
| 36 | The event add/remove stuff usually happens from the debugger thread, |
| 37 | in response to requests from the debugger, but can also happen as the |
| 38 | result of an event in an arbitrary thread (e.g. an event with a "count" |
| 39 | mod expires). It's important to keep the event list locked when processing |
| 40 | events. |
| 41 | |
| 42 | Event posting can happen from any thread. The JDWP thread will not usually |
| 43 | post anything but VM start/death, but if a JDWP request causes a class |
| 44 | to be loaded, the ClassPrepare event will come from the JDWP thread. |
| 45 | |
| 46 | |
| 47 | We can have serialization issues when we post an event to the debugger. |
| 48 | For example, a thread could send an "I hit a breakpoint and am suspending |
| 49 | myself" message to the debugger. Before it manages to suspend itself, the |
| 50 | debugger's response ("not interested, resume thread") arrives and is |
| 51 | processed. We try to resume a thread that hasn't yet suspended. |
| 52 | |
| 53 | This means that, after posting an event to the debugger, we need to wait |
| 54 | for the event thread to suspend itself (and, potentially, all other threads) |
| 55 | before processing any additional requests from the debugger. While doing |
| 56 | so we need to be aware that multiple threads may be hitting breakpoints |
| 57 | or other events simultaneously, so we either need to wait for all of them |
| 58 | or serialize the events with each other. |
| 59 | |
| 60 | The current mechanism works like this: |
| 61 | Event thread: |
| 62 | - If I'm going to suspend, grab the "I am posting an event" token. Wait |
| 63 | for it if it's not currently available. |
| 64 | - Post the event to the debugger. |
| 65 | - If appropriate, suspend others and then myself. As part of suspending |
| 66 | myself, release the "I am posting" token. |
| 67 | JDWP thread: |
| 68 | - When an event arrives, see if somebody is posting an event. If so, |
| 69 | sleep until we can acquire the "I am posting an event" token. Release |
| 70 | it immediately and continue processing -- the event we have already |
| 71 | received should not interfere with other events that haven't yet |
| 72 | been posted. |
| 73 | |
| 74 | Some care must be taken to avoid deadlock: |
| 75 | |
| 76 | - thread A and thread B exit near-simultaneously, and post thread-death |
| 77 | events with a "suspend all" clause |
| 78 | - thread A gets the event token, thread B sits and waits for it |
| 79 | - thread A wants to suspend all other threads, but thread B is waiting |
| 80 | for the token and can't be suspended |
| 81 | |
| 82 | So we need to mark thread B in such a way that thread A doesn't wait for it. |
| 83 | |
| 84 | If we just bracket the "grab event token" call with a change to VMWAIT |
| 85 | before sleeping, the switch back to RUNNING state when we get the token |
| 86 | will cause thread B to suspend (remember, thread A's global suspend is |
| 87 | still in force, even after it releases the token). Suspending while |
| 88 | holding the event token is very bad, because it prevents the JDWP thread |
| 89 | from processing incoming messages. |
| 90 | |
| 91 | We need to change to VMWAIT state at the *start* of posting an event, |
| 92 | and stay there until we either finish posting the event or decide to |
| 93 | put ourselves to sleep. That way we don't interfere with anyone else and |
| 94 | don't allow anyone else to interfere with us. |
| 95 | */ |
| 96 | |
| 97 | |
| 98 | #define kJdwpEventCommandSet 64 |
| 99 | #define kJdwpCompositeCommand 100 |
| 100 | |
| 101 | namespace art { |
| 102 | |
| 103 | namespace JDWP { |
| 104 | |
| 105 | /* |
| 106 | * Stuff to compare against when deciding if a mod matches. Only the |
| 107 | * values for mods valid for the event being evaluated will be filled in. |
| 108 | * The rest will be zeroed. |
| 109 | */ |
| 110 | struct ModBasket { |
| 111 | const JdwpLocation* pLoc; /* LocationOnly */ |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 112 | std::string className; /* ClassMatch/ClassExclude */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 113 | ObjectId threadId; /* ThreadOnly */ |
| 114 | RefTypeId classId; /* ClassOnly */ |
| 115 | RefTypeId excepClassId; /* ExceptionOnly */ |
| 116 | bool caught; /* ExceptionOnly */ |
| 117 | FieldId field; /* FieldOnly */ |
| 118 | ObjectId thisPtr; /* InstanceOnly */ |
| 119 | /* nothing for StepOnly -- handled differently */ |
| 120 | }; |
| 121 | |
| 122 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 123 | * Dump an event to the log file. |
| 124 | */ |
| 125 | static void dumpEvent(const JdwpEvent* pEvent) { |
| 126 | LOG(INFO) << StringPrintf("Event id=0x%4x %p (prev=%p next=%p):", pEvent->requestId, pEvent, pEvent->prev, pEvent->next); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 127 | LOG(INFO) << " kind=" << pEvent->eventKind << " susp=" << pEvent->suspend_policy << " modCount=" << pEvent->modCount; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 128 | |
| 129 | for (int i = 0; i < pEvent->modCount; i++) { |
| 130 | const JdwpEventMod* pMod = &pEvent->mods[i]; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 131 | LOG(INFO) << " " << pMod->modKind; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 132 | /* TODO - show details */ |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Add an event to the list. Ordering is not important. |
| 138 | * |
| 139 | * If something prevents the event from being registered, e.g. it's a |
| 140 | * single-step request on a thread that doesn't exist, the event will |
| 141 | * not be added to the list, and an appropriate error will be returned. |
| 142 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 143 | JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 144 | MutexLock mu(event_list_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 145 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 146 | CHECK(pEvent != NULL); |
| 147 | CHECK(pEvent->prev == NULL); |
| 148 | CHECK(pEvent->next == NULL); |
| 149 | |
| 150 | /* |
| 151 | * If one or more "break"-type mods are used, register them with |
| 152 | * the interpreter. |
| 153 | */ |
| 154 | for (int i = 0; i < pEvent->modCount; i++) { |
| 155 | const JdwpEventMod* pMod = &pEvent->mods[i]; |
| 156 | if (pMod->modKind == MK_LOCATION_ONLY) { |
| 157 | /* should only be for Breakpoint, Step, and Exception */ |
| 158 | Dbg::WatchLocation(&pMod->locationOnly.loc); |
| 159 | } else if (pMod->modKind == MK_STEP) { |
| 160 | /* should only be for EK_SINGLE_STEP; should only be one */ |
| 161 | JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size); |
| 162 | JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 163 | JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth); |
| 164 | if (status != ERR_NONE) { |
| 165 | return status; |
| 166 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 167 | } else if (pMod->modKind == MK_FIELD_ONLY) { |
| 168 | /* should be for EK_FIELD_ACCESS or EK_FIELD_MODIFICATION */ |
| 169 | dumpEvent(pEvent); /* TODO - need for field watches */ |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Add to list. |
| 175 | */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 176 | if (event_list_ != NULL) { |
| 177 | pEvent->next = event_list_; |
| 178 | event_list_->prev = pEvent; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 179 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 180 | event_list_ = pEvent; |
| 181 | ++event_list_size_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 182 | |
| 183 | return ERR_NONE; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Remove an event from the list. This will also remove the event from |
| 188 | * any optimization tables, e.g. breakpoints. |
| 189 | * |
| 190 | * Does not free the JdwpEvent. |
| 191 | * |
| 192 | * Grab the eventLock before calling here. |
| 193 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 194 | void JdwpState::UnregisterEvent(JdwpEvent* pEvent) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 195 | if (pEvent->prev == NULL) { |
| 196 | /* head of the list */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 197 | CHECK(event_list_ == pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 198 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 199 | event_list_ = pEvent->next; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 200 | } else { |
| 201 | pEvent->prev->next = pEvent->next; |
| 202 | } |
| 203 | |
| 204 | if (pEvent->next != NULL) { |
| 205 | pEvent->next->prev = pEvent->prev; |
| 206 | pEvent->next = NULL; |
| 207 | } |
| 208 | pEvent->prev = NULL; |
| 209 | |
| 210 | /* |
| 211 | * Unhook us from the interpreter, if necessary. |
| 212 | */ |
| 213 | for (int i = 0; i < pEvent->modCount; i++) { |
| 214 | JdwpEventMod* pMod = &pEvent->mods[i]; |
| 215 | if (pMod->modKind == MK_LOCATION_ONLY) { |
| 216 | /* should only be for Breakpoint, Step, and Exception */ |
| 217 | Dbg::UnwatchLocation(&pMod->locationOnly.loc); |
| 218 | } |
| 219 | if (pMod->modKind == MK_STEP) { |
| 220 | /* should only be for EK_SINGLE_STEP; should only be one */ |
| 221 | Dbg::UnconfigureStep(pMod->step.threadId); |
| 222 | } |
| 223 | } |
| 224 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 225 | --event_list_size_; |
| 226 | CHECK(event_list_size_ != 0 || event_list_ == NULL); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Remove the event with the given ID from the list. |
| 231 | * |
| 232 | * Failure to find the event isn't really an error, but it is a little |
| 233 | * weird. (It looks like Eclipse will try to be extra careful and will |
| 234 | * explicitly remove one-off single-step events.) |
| 235 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 236 | void JdwpState::UnregisterEventById(uint32_t requestId) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 237 | MutexLock mu(event_list_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 238 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 239 | JdwpEvent* pEvent = event_list_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 240 | while (pEvent != NULL) { |
| 241 | if (pEvent->requestId == requestId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 242 | UnregisterEvent(pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 243 | EventFree(pEvent); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 244 | return; /* there can be only one with a given ID */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | pEvent = pEvent->next; |
| 248 | } |
| 249 | |
| 250 | //LOGD("Odd: no match when removing event reqId=0x%04x", requestId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Remove all entries from the event list. |
| 255 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 256 | void JdwpState::UnregisterAll() { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 257 | MutexLock mu(event_list_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 258 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 259 | JdwpEvent* pEvent = event_list_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 260 | while (pEvent != NULL) { |
| 261 | JdwpEvent* pNextEvent = pEvent->next; |
| 262 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 263 | UnregisterEvent(pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 264 | EventFree(pEvent); |
| 265 | pEvent = pNextEvent; |
| 266 | } |
| 267 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 268 | event_list_ = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Allocate a JdwpEvent struct with enough space to hold the specified |
| 273 | * number of mod records. |
| 274 | */ |
| 275 | JdwpEvent* EventAlloc(int numMods) { |
| 276 | JdwpEvent* newEvent; |
| 277 | int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]); |
| 278 | newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize)); |
| 279 | memset(newEvent, 0, allocSize); |
| 280 | return newEvent; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Free a JdwpEvent. |
| 285 | * |
| 286 | * Do not call this until the event has been removed from the list. |
| 287 | */ |
| 288 | void EventFree(JdwpEvent* pEvent) { |
| 289 | if (pEvent == NULL) { |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | /* make sure it was removed from the list */ |
| 294 | CHECK(pEvent->prev == NULL); |
| 295 | CHECK(pEvent->next == NULL); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 296 | /* want to check state->event_list_ != pEvent */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Free any hairy bits in the mods. |
| 300 | */ |
| 301 | for (int i = 0; i < pEvent->modCount; i++) { |
| 302 | if (pEvent->mods[i].modKind == MK_CLASS_MATCH) { |
| 303 | free(pEvent->mods[i].classMatch.classPattern); |
| 304 | pEvent->mods[i].classMatch.classPattern = NULL; |
| 305 | } |
| 306 | if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) { |
| 307 | free(pEvent->mods[i].classExclude.classPattern); |
| 308 | pEvent->mods[i].classExclude.classPattern = NULL; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | free(pEvent); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Allocate storage for matching events. To keep things simple we |
| 317 | * use an array with enough storage for the entire list. |
| 318 | * |
| 319 | * The state->eventLock should be held before calling. |
| 320 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 321 | static JdwpEvent** AllocMatchList(size_t event_count) { |
| 322 | return new JdwpEvent*[event_count]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Run through the list and remove any entries with an expired "count" mod |
| 327 | * from the event list, then free the match list. |
| 328 | */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 329 | void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) { |
| 330 | JdwpEvent** ppEvent = match_list; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 331 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 332 | while (match_count--) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 333 | JdwpEvent* pEvent = *ppEvent; |
| 334 | |
| 335 | for (int i = 0; i < pEvent->modCount; i++) { |
| 336 | if (pEvent->mods[i].modKind == MK_COUNT && pEvent->mods[i].count.count == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 337 | VLOG(jdwp) << "##### Removing expired event"; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 338 | UnregisterEvent(pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 339 | EventFree(pEvent); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | ppEvent++; |
| 345 | } |
| 346 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 347 | delete[] match_list; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Match a string against a "restricted regular expression", which is just |
| 352 | * a string that may start or end with '*' (e.g. "*.Foo" or "java.*"). |
| 353 | * |
| 354 | * ("Restricted name globbing" might have been a better term.) |
| 355 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 356 | static bool PatternMatch(const char* pattern, const std::string& target) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 357 | size_t patLen = strlen(pattern); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 358 | if (pattern[0] == '*') { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 359 | patLen--; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 360 | if (target.size() < patLen) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 361 | return false; |
| 362 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 363 | return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 364 | } else if (pattern[patLen-1] == '*') { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 365 | return strncmp(pattern, target.c_str(), patLen-1) == 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 366 | } else { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 367 | return strcmp(pattern, target.c_str()) == 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 372 | * See if the event's mods match up with the contents of "basket". |
| 373 | * |
| 374 | * If we find a Count mod before rejecting an event, we decrement it. We |
| 375 | * need to do this even if later mods cause us to ignore the event. |
| 376 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 377 | static bool ModsMatch(JdwpEvent* pEvent, ModBasket* basket) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 378 | JdwpEventMod* pMod = pEvent->mods; |
| 379 | |
| 380 | for (int i = pEvent->modCount; i > 0; i--, pMod++) { |
| 381 | switch (pMod->modKind) { |
| 382 | case MK_COUNT: |
| 383 | CHECK_GT(pMod->count.count, 0); |
| 384 | pMod->count.count--; |
| 385 | break; |
| 386 | case MK_CONDITIONAL: |
| 387 | CHECK(false); // should not be getting these |
| 388 | break; |
| 389 | case MK_THREAD_ONLY: |
| 390 | if (pMod->threadOnly.threadId != basket->threadId) { |
| 391 | return false; |
| 392 | } |
| 393 | break; |
| 394 | case MK_CLASS_ONLY: |
| 395 | if (!Dbg::MatchType(basket->classId, pMod->classOnly.refTypeId)) { |
| 396 | return false; |
| 397 | } |
| 398 | break; |
| 399 | case MK_CLASS_MATCH: |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 400 | if (!PatternMatch(pMod->classMatch.classPattern, basket->className)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 401 | return false; |
| 402 | } |
| 403 | break; |
| 404 | case MK_CLASS_EXCLUDE: |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 405 | if (PatternMatch(pMod->classMatch.classPattern, basket->className)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | break; |
| 409 | case MK_LOCATION_ONLY: |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 410 | if (pMod->locationOnly.loc != *basket->pLoc) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | break; |
| 414 | case MK_EXCEPTION_ONLY: |
| 415 | if (pMod->exceptionOnly.refTypeId != 0 && !Dbg::MatchType(basket->excepClassId, pMod->exceptionOnly.refTypeId)) { |
| 416 | return false; |
| 417 | } |
| 418 | if ((basket->caught && !pMod->exceptionOnly.caught) || (!basket->caught && !pMod->exceptionOnly.uncaught)) { |
| 419 | return false; |
| 420 | } |
| 421 | break; |
| 422 | case MK_FIELD_ONLY: |
| 423 | if (!Dbg::MatchType(basket->classId, pMod->fieldOnly.refTypeId) || pMod->fieldOnly.fieldId != basket->field) { |
| 424 | return false; |
| 425 | } |
| 426 | break; |
| 427 | case MK_STEP: |
| 428 | if (pMod->step.threadId != basket->threadId) { |
| 429 | return false; |
| 430 | } |
| 431 | break; |
| 432 | case MK_INSTANCE_ONLY: |
| 433 | if (pMod->instanceOnly.objectId != basket->thisPtr) { |
| 434 | return false; |
| 435 | } |
| 436 | break; |
| 437 | default: |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 438 | LOG(FATAL) << "unknown mod kind " << pMod->modKind; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | } |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Find all events of type "eventKind" with mods that match up with the |
| 447 | * rest of the arguments. |
| 448 | * |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 449 | * Found events are appended to "match_list", and "*pMatchCount" is advanced, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 450 | * so this may be called multiple times for grouped events. |
| 451 | * |
| 452 | * DO NOT call this multiple times for the same eventKind, as Count mods are |
| 453 | * decremented during the scan. |
| 454 | */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 455 | void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** match_list, int* pMatchCount) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 456 | /* start after the existing entries */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 457 | match_list += *pMatchCount; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 458 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 459 | JdwpEvent* pEvent = event_list_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 460 | while (pEvent != NULL) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 461 | if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 462 | *match_list++ = pEvent; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 463 | (*pMatchCount)++; |
| 464 | } |
| 465 | |
| 466 | pEvent = pEvent->next; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Scan through the list of matches and determine the most severe |
| 472 | * suspension policy. |
| 473 | */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 474 | static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 475 | JdwpSuspendPolicy policy = SP_NONE; |
| 476 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 477 | while (match_count--) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 478 | if ((*match_list)->suspend_policy > policy) { |
| 479 | policy = (*match_list)->suspend_policy; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 480 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 481 | match_list++; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | return policy; |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Three possibilities: |
| 489 | * SP_NONE - do nothing |
| 490 | * SP_EVENT_THREAD - suspend ourselves |
| 491 | * SP_ALL - suspend everybody except JDWP support thread |
| 492 | */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 493 | void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy) { |
| 494 | VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")"; |
| 495 | if (suspend_policy == SP_NONE) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 496 | return; |
| 497 | } |
| 498 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 499 | if (suspend_policy == SP_ALL) { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 500 | Dbg::SuspendVM(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 501 | } else { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 502 | CHECK_EQ(suspend_policy, SP_EVENT_THREAD); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* this is rare but possible -- see CLASS_PREPARE handling */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 506 | if (Dbg::GetThreadSelfId() == debugThreadId) { |
| 507 | LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 508 | return; |
| 509 | } |
| 510 | |
| 511 | DebugInvokeReq* pReq = Dbg::GetInvokeReq(); |
| 512 | while (true) { |
| 513 | pReq->ready = true; |
| 514 | Dbg::SuspendSelf(); |
| 515 | pReq->ready = false; |
| 516 | |
| 517 | /* |
| 518 | * The JDWP thread has told us (and possibly all other threads) to |
| 519 | * resume. See if it has left anything in our DebugInvokeReq mailbox. |
| 520 | */ |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 521 | if (!pReq->invoke_needed_) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 522 | /*LOGD("SuspendByPolicy: no invoke needed");*/ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 523 | break; |
| 524 | } |
| 525 | |
| 526 | /* grab this before posting/suspending again */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 527 | SetWaitForEventThread(Dbg::GetThreadSelfId()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 528 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 529 | /* leave pReq->invoke_needed_ raised so we can check reentrancy */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 530 | Dbg::ExecuteMethod(pReq); |
| 531 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 532 | pReq->error = ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 533 | |
| 534 | /* clear this before signaling */ |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 535 | pReq->invoke_needed_ = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 536 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 537 | VLOG(jdwp) << "invoke complete, signaling and self-suspending"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 538 | MutexLock mu(pReq->lock_); |
| 539 | pReq->cond_.Signal(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Determine if there is a method invocation in progress in the current |
| 545 | * thread. |
| 546 | * |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 547 | * We look at the "invoke_needed" flag in the per-thread DebugInvokeReq |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 548 | * state. If set, we're in the process of invoking a method. |
| 549 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 550 | bool JdwpState::InvokeInProgress() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 551 | DebugInvokeReq* pReq = Dbg::GetInvokeReq(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 552 | return pReq->invoke_needed_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* |
| 556 | * We need the JDWP thread to hold off on doing stuff while we post an |
| 557 | * event and then suspend ourselves. |
| 558 | * |
| 559 | * Call this with a threadId of zero if you just want to wait for the |
| 560 | * current thread operation to complete. |
| 561 | * |
| 562 | * This could go to sleep waiting for another thread, so it's important |
| 563 | * that the thread be marked as VMWAIT before calling here. |
| 564 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 565 | void JdwpState::SetWaitForEventThread(ObjectId threadId) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 566 | bool waited = false; |
| 567 | |
| 568 | /* this is held for very brief periods; contention is unlikely */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 569 | MutexLock mu(event_thread_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 570 | |
| 571 | /* |
| 572 | * If another thread is already doing stuff, wait for it. This can |
| 573 | * go to sleep indefinitely. |
| 574 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 575 | while (eventThreadId != 0) { |
Elliott Hughes | 229feb7 | 2012-02-23 13:33:29 -0800 | [diff] [blame] | 576 | VLOG(jdwp) << StringPrintf("event in progress (%#llx), %#llx sleeping", eventThreadId, threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 577 | waited = true; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 578 | event_thread_cond_.Wait(event_thread_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | if (waited || threadId != 0) { |
Elliott Hughes | 229feb7 | 2012-02-23 13:33:29 -0800 | [diff] [blame] | 582 | VLOG(jdwp) << StringPrintf("event token grabbed (%#llx)", threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 583 | } |
| 584 | if (threadId != 0) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 585 | eventThreadId = threadId; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Clear the threadId and signal anybody waiting. |
| 591 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 592 | void JdwpState::ClearWaitForEventThread() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 593 | /* |
| 594 | * Grab the mutex. Don't try to go in/out of VMWAIT mode, as this |
| 595 | * function is called by dvmSuspendSelf(), and the transition back |
| 596 | * to RUNNING would confuse it. |
| 597 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 598 | MutexLock mu(event_thread_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 599 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 600 | CHECK_NE(eventThreadId, 0U); |
Elliott Hughes | 229feb7 | 2012-02-23 13:33:29 -0800 | [diff] [blame] | 601 | VLOG(jdwp) << StringPrintf("cleared event token (%#llx)", eventThreadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 602 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 603 | eventThreadId = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 604 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 605 | event_thread_cond_.Signal(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | |
| 609 | /* |
| 610 | * Prep an event. Allocates storage for the message and leaves space for |
| 611 | * the header. |
| 612 | */ |
| 613 | static ExpandBuf* eventPrep() { |
| 614 | ExpandBuf* pReq = expandBufAlloc(); |
| 615 | expandBufAddSpace(pReq, kJDWPHeaderLen); |
| 616 | return pReq; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Write the header into the buffer and send the packet off to the debugger. |
| 621 | * |
| 622 | * Takes ownership of "pReq" (currently discards it). |
| 623 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 624 | void JdwpState::EventFinish(ExpandBuf* pReq) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 625 | uint8_t* buf = expandBufGetBuffer(pReq); |
| 626 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 627 | Set4BE(buf, expandBufGetLength(pReq)); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 628 | Set4BE(buf+4, NextRequestSerial()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 629 | Set1(buf+8, 0); /* flags */ |
| 630 | Set1(buf+9, kJdwpEventCommandSet); |
| 631 | Set1(buf+10, kJdwpCompositeCommand); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 632 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 633 | SendRequest(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 634 | |
| 635 | expandBufFree(pReq); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | /* |
| 640 | * Tell the debugger that we have finished initializing. This is always |
| 641 | * sent, even if the debugger hasn't requested it. |
| 642 | * |
| 643 | * This should be sent "before the main thread is started and before |
| 644 | * any application code has been executed". The thread ID in the message |
| 645 | * must be for the main thread. |
| 646 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 647 | bool JdwpState::PostVMStart() { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 648 | JdwpSuspendPolicy suspend_policy; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 649 | ObjectId threadId = Dbg::GetThreadSelfId(); |
| 650 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 651 | if (options_->suspend) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 652 | suspend_policy = SP_ALL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 653 | } else { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 654 | suspend_policy = SP_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 657 | ExpandBuf* pReq = eventPrep(); |
| 658 | { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 659 | MutexLock mu(event_list_lock_); // probably don't need this here |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 660 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 661 | VLOG(jdwp) << "EVENT: " << EK_VM_START; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 662 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 663 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 664 | expandBufAdd1(pReq, suspend_policy); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 665 | expandBufAdd4BE(pReq, 1); |
| 666 | |
| 667 | expandBufAdd1(pReq, EK_VM_START); |
| 668 | expandBufAdd4BE(pReq, 0); /* requestId */ |
| 669 | expandBufAdd8BE(pReq, threadId); |
| 670 | } |
| 671 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 672 | /* send request and possibly suspend ourselves */ |
| 673 | if (pReq != NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 674 | int old_state = Dbg::ThreadWaiting(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 675 | if (suspend_policy != SP_NONE) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 676 | SetWaitForEventThread(threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 679 | EventFinish(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 680 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 681 | SuspendByPolicy(suspend_policy); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 682 | Dbg::ThreadContinuing(old_state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | return true; |
| 686 | } |
| 687 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 688 | /* |
| 689 | * A location of interest has been reached. This handles: |
| 690 | * Breakpoint |
| 691 | * SingleStep |
| 692 | * MethodEntry |
| 693 | * MethodExit |
| 694 | * These four types must be grouped together in a single response. The |
| 695 | * "eventFlags" indicates the type of event(s) that have happened. |
| 696 | * |
| 697 | * Valid mods: |
| 698 | * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly |
| 699 | * LocationOnly (for breakpoint/step only) |
| 700 | * Step (for step only) |
| 701 | * |
| 702 | * Interesting test cases: |
| 703 | * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY |
| 704 | * and METHOD_EXIT events with a ClassOnly mod on the method's class. |
| 705 | * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1. |
| 706 | * - Single-step to a line with a breakpoint. Should get a single |
| 707 | * event message with both events in it. |
| 708 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 709 | bool JdwpState::PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 710 | ModBasket basket; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 711 | |
| 712 | memset(&basket, 0, sizeof(basket)); |
| 713 | basket.pLoc = pLoc; |
| 714 | basket.classId = pLoc->classId; |
| 715 | basket.thisPtr = thisPtr; |
| 716 | basket.threadId = Dbg::GetThreadSelfId(); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 717 | basket.className = Dbg::GetClassName(pLoc->classId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 718 | |
| 719 | /* |
| 720 | * On rare occasions we may need to execute interpreted code in the VM |
| 721 | * while handling a request from the debugger. Don't fire breakpoints |
| 722 | * while doing so. (I don't think we currently do this at all, so |
| 723 | * this is mostly paranoia.) |
| 724 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 725 | if (basket.threadId == debugThreadId) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 726 | VLOG(jdwp) << "Ignoring location event in JDWP thread"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 727 | return false; |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * The debugger variable display tab may invoke the interpreter to format |
| 732 | * complex objects. We want to ignore breakpoints and method entry/exit |
| 733 | * traps while working on behalf of the debugger. |
| 734 | * |
| 735 | * If we don't ignore them, the VM will get hung up, because we'll |
| 736 | * suspend on a breakpoint while the debugger is still waiting for its |
| 737 | * method invocation to complete. |
| 738 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 739 | if (InvokeInProgress()) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 740 | VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 744 | JdwpEvent** match_list = NULL; |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 745 | int match_count = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 746 | ExpandBuf* pReq = NULL; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 747 | JdwpSuspendPolicy suspend_policy = SP_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 748 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 749 | { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 750 | MutexLock mu(event_list_lock_); |
| 751 | match_list = AllocMatchList(event_list_size_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 752 | if ((eventFlags & Dbg::kBreakpoint) != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 753 | FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 754 | } |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 755 | if ((eventFlags & Dbg::kSingleStep) != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 756 | FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 757 | } |
| 758 | if ((eventFlags & Dbg::kMethodEntry) != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 759 | FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 760 | } |
| 761 | if ((eventFlags & Dbg::kMethodExit) != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 762 | FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 763 | |
| 764 | // TODO: match EK_METHOD_EXIT_WITH_RETURN_VALUE too; we need to include the 'value', though. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 765 | //FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 766 | } |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 767 | if (match_count != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 768 | VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) " |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 769 | << basket.className << "." << Dbg::GetMethodName(pLoc->classId, pLoc->methodId) |
Elliott Hughes | 229feb7 | 2012-02-23 13:33:29 -0800 | [diff] [blame] | 770 | << StringPrintf(" thread=%#llx dex_pc=%#llx)", basket.threadId, pLoc->dex_pc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 771 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 772 | suspend_policy = scanSuspendPolicy(match_list, match_count); |
| 773 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 774 | |
| 775 | pReq = eventPrep(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 776 | expandBufAdd1(pReq, suspend_policy); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 777 | expandBufAdd4BE(pReq, match_count); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 778 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 779 | for (int i = 0; i < match_count; i++) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 780 | expandBufAdd1(pReq, match_list[i]->eventKind); |
| 781 | expandBufAdd4BE(pReq, match_list[i]->requestId); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 782 | expandBufAdd8BE(pReq, basket.threadId); |
| 783 | AddLocation(pReq, pLoc); |
| 784 | } |
| 785 | } |
| 786 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 787 | CleanupMatchList(match_list, match_count); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 788 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 789 | |
| 790 | /* send request and possibly suspend ourselves */ |
| 791 | if (pReq != NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 792 | int old_state = Dbg::ThreadWaiting(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 793 | if (suspend_policy != SP_NONE) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 794 | SetWaitForEventThread(basket.threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 797 | EventFinish(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 798 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 799 | SuspendByPolicy(suspend_policy); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 800 | Dbg::ThreadContinuing(old_state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 803 | return match_count != 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* |
| 807 | * A thread is starting or stopping. |
| 808 | * |
| 809 | * Valid mods: |
| 810 | * Count, ThreadOnly |
| 811 | */ |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 812 | bool JdwpState::PostThreadChange(ObjectId threadId, bool start) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 813 | CHECK_EQ(threadId, Dbg::GetThreadSelfId()); |
| 814 | |
| 815 | /* |
| 816 | * I don't think this can happen. |
| 817 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 818 | if (InvokeInProgress()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 819 | LOG(WARNING) << "Not posting thread change during invoke"; |
| 820 | return false; |
| 821 | } |
| 822 | |
| 823 | ModBasket basket; |
| 824 | memset(&basket, 0, sizeof(basket)); |
| 825 | basket.threadId = threadId; |
| 826 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 827 | ExpandBuf* pReq = NULL; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 828 | JdwpSuspendPolicy suspend_policy = SP_NONE; |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 829 | int match_count = 0; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 830 | { |
| 831 | // Don't allow the list to be updated while we scan it. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 832 | MutexLock mu(event_list_lock_); |
| 833 | JdwpEvent** match_list = AllocMatchList(event_list_size_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 834 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 835 | if (start) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 836 | FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 837 | } else { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 838 | FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 839 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 840 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 841 | if (match_count != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 842 | VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) " |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 843 | << StringPrintf("thread=%#llx", basket.threadId) << ")"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 844 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 845 | suspend_policy = scanSuspendPolicy(match_list, match_count); |
| 846 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 847 | |
| 848 | pReq = eventPrep(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 849 | expandBufAdd1(pReq, suspend_policy); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 850 | expandBufAdd4BE(pReq, match_count); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 851 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 852 | for (int i = 0; i < match_count; i++) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 853 | expandBufAdd1(pReq, match_list[i]->eventKind); |
| 854 | expandBufAdd4BE(pReq, match_list[i]->requestId); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 855 | expandBufAdd8BE(pReq, basket.threadId); |
| 856 | } |
| 857 | } |
| 858 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 859 | CleanupMatchList(match_list, match_count); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 860 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 861 | |
| 862 | /* send request and possibly suspend ourselves */ |
| 863 | if (pReq != NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 864 | int old_state = Dbg::ThreadWaiting(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 865 | if (suspend_policy != SP_NONE) { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 866 | SetWaitForEventThread(basket.threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 867 | } |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 868 | EventFinish(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 869 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 870 | SuspendByPolicy(suspend_policy); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 871 | Dbg::ThreadContinuing(old_state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 874 | return match_count != 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* |
| 878 | * Send a polite "VM is dying" message to the debugger. |
| 879 | * |
| 880 | * Skips the usual "event token" stuff. |
| 881 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 882 | bool JdwpState::PostVMDeath() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 883 | VLOG(jdwp) << "EVENT: " << EK_VM_DEATH; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 884 | |
| 885 | ExpandBuf* pReq = eventPrep(); |
| 886 | expandBufAdd1(pReq, SP_NONE); |
| 887 | expandBufAdd4BE(pReq, 1); |
| 888 | |
| 889 | expandBufAdd1(pReq, EK_VM_DEATH); |
| 890 | expandBufAdd4BE(pReq, 0); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 891 | EventFinish(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 892 | return true; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * An exception has been thrown. It may or may not have been caught. |
| 897 | * |
| 898 | * Valid mods: |
| 899 | * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly, |
| 900 | * ExceptionOnly, InstanceOnly |
| 901 | * |
| 902 | * The "exceptionId" has not been added to the GC-visible object registry, |
| 903 | * because there's a pretty good chance that we're not going to send it |
| 904 | * up the debugger. |
| 905 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 906 | bool JdwpState::PostException(const JdwpLocation* pThrowLoc, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 907 | ObjectId exceptionId, RefTypeId exceptionClassId, |
| 908 | const JdwpLocation* pCatchLoc, ObjectId thisPtr) |
| 909 | { |
| 910 | ModBasket basket; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 911 | |
| 912 | memset(&basket, 0, sizeof(basket)); |
| 913 | basket.pLoc = pThrowLoc; |
| 914 | basket.classId = pThrowLoc->classId; |
| 915 | basket.threadId = Dbg::GetThreadSelfId(); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 916 | basket.className = Dbg::GetClassName(basket.classId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 917 | basket.excepClassId = exceptionClassId; |
| 918 | basket.caught = (pCatchLoc->classId != 0); |
| 919 | basket.thisPtr = thisPtr; |
| 920 | |
| 921 | /* don't try to post an exception caused by the debugger */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 922 | if (InvokeInProgress()) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 923 | VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 924 | return false; |
| 925 | } |
| 926 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 927 | JdwpEvent** match_list = NULL; |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 928 | int match_count = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 929 | ExpandBuf* pReq = NULL; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 930 | JdwpSuspendPolicy suspend_policy = SP_NONE; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 931 | { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 932 | MutexLock mu(event_list_lock_); |
| 933 | match_list = AllocMatchList(event_list_size_); |
| 934 | FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 935 | if (match_count != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 936 | VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)" |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 937 | << StringPrintf(" thread=%#llx", basket.threadId) |
| 938 | << StringPrintf(" exceptId=%#llx", exceptionId) |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 939 | << " caught=" << basket.caught << ")" |
| 940 | << " throw: " << *pThrowLoc; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 941 | if (pCatchLoc->classId == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 942 | VLOG(jdwp) << " catch: (not caught)"; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 943 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 944 | VLOG(jdwp) << " catch: " << *pCatchLoc; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 947 | suspend_policy = scanSuspendPolicy(match_list, match_count); |
| 948 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 949 | |
| 950 | pReq = eventPrep(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 951 | expandBufAdd1(pReq, suspend_policy); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 952 | expandBufAdd4BE(pReq, match_count); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 953 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 954 | for (int i = 0; i < match_count; i++) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 955 | expandBufAdd1(pReq, match_list[i]->eventKind); |
| 956 | expandBufAdd4BE(pReq, match_list[i]->requestId); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 957 | expandBufAdd8BE(pReq, basket.threadId); |
| 958 | |
| 959 | AddLocation(pReq, pThrowLoc); |
| 960 | expandBufAdd1(pReq, JT_OBJECT); |
| 961 | expandBufAdd8BE(pReq, exceptionId); |
| 962 | AddLocation(pReq, pCatchLoc); |
| 963 | } |
| 964 | |
| 965 | /* don't let the GC discard it */ |
| 966 | Dbg::RegisterObjectId(exceptionId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 969 | CleanupMatchList(match_list, match_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 972 | /* send request and possibly suspend ourselves */ |
| 973 | if (pReq != NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 974 | int old_state = Dbg::ThreadWaiting(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 975 | if (suspend_policy != SP_NONE) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 976 | SetWaitForEventThread(basket.threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 979 | EventFinish(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 980 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 981 | SuspendByPolicy(suspend_policy); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 982 | Dbg::ThreadContinuing(old_state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 985 | return match_count != 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /* |
| 989 | * Announce that a class has been loaded. |
| 990 | * |
| 991 | * Valid mods: |
| 992 | * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude |
| 993 | */ |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 994 | bool JdwpState::PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature, int status) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 995 | ModBasket basket; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 996 | |
| 997 | memset(&basket, 0, sizeof(basket)); |
| 998 | basket.classId = refTypeId; |
| 999 | basket.threadId = Dbg::GetThreadSelfId(); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 1000 | basket.className = Dbg::GetClassName(basket.classId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1001 | |
| 1002 | /* suppress class prep caused by debugger */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1003 | if (InvokeInProgress()) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1004 | VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
| 1007 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1008 | ExpandBuf* pReq = NULL; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1009 | JdwpSuspendPolicy suspend_policy = SP_NONE; |
| 1010 | int match_count = 0; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1011 | { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1012 | MutexLock mu(event_list_lock_); |
| 1013 | JdwpEvent** match_list = AllocMatchList(event_list_size_); |
| 1014 | FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1015 | if (match_count != 0) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1016 | VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) " |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1017 | << StringPrintf("thread=%#llx", basket.threadId) << ") " << signature; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1018 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1019 | suspend_policy = scanSuspendPolicy(match_list, match_count); |
| 1020 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1021 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1022 | if (basket.threadId == debugThreadId) { |
| 1023 | /* |
| 1024 | * JDWP says that, for a class prep in the debugger thread, we |
| 1025 | * should set threadId to null and if any threads were supposed |
| 1026 | * to be suspended then we suspend all other threads. |
| 1027 | */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1028 | VLOG(jdwp) << " NOTE: class prepare in debugger thread!"; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1029 | basket.threadId = 0; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1030 | if (suspend_policy == SP_EVENT_THREAD) { |
| 1031 | suspend_policy = SP_ALL; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | pReq = eventPrep(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1036 | expandBufAdd1(pReq, suspend_policy); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1037 | expandBufAdd4BE(pReq, match_count); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1038 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1039 | for (int i = 0; i < match_count; i++) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1040 | expandBufAdd1(pReq, match_list[i]->eventKind); |
| 1041 | expandBufAdd4BE(pReq, match_list[i]->requestId); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1042 | expandBufAdd8BE(pReq, basket.threadId); |
| 1043 | |
| 1044 | expandBufAdd1(pReq, tag); |
| 1045 | expandBufAdd8BE(pReq, refTypeId); |
| 1046 | expandBufAddUtf8String(pReq, signature); |
| 1047 | expandBufAdd4BE(pReq, status); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1048 | } |
| 1049 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1050 | CleanupMatchList(match_list, match_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1053 | /* send request and possibly suspend ourselves */ |
| 1054 | if (pReq != NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1055 | int old_state = Dbg::ThreadWaiting(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1056 | if (suspend_policy != SP_NONE) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1057 | SetWaitForEventThread(basket.threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1058 | } |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1059 | EventFinish(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1060 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1061 | SuspendByPolicy(suspend_policy); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1062 | Dbg::ThreadContinuing(old_state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1065 | return match_count != 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1068 | /* |
| 1069 | * Send up a chunk of DDM data. |
| 1070 | * |
| 1071 | * While this takes the form of a JDWP "event", it doesn't interact with |
| 1072 | * other debugger traffic, and can't suspend the VM, so we skip all of |
| 1073 | * the fun event token gymnastics. |
| 1074 | */ |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1075 | void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1076 | uint8_t header[kJDWPHeaderLen + 8]; |
| 1077 | size_t dataLen = 0; |
| 1078 | |
| 1079 | CHECK(iov != NULL); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1080 | CHECK_GT(iov_count, 0); |
| 1081 | CHECK_LT(iov_count, 10); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do |
| 1085 | * this by creating a new copy of the vector with space for the header. |
| 1086 | */ |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1087 | iovec wrapiov[iov_count+1]; |
| 1088 | for (int i = 0; i < iov_count; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1089 | wrapiov[i+1].iov_base = iov[i].iov_base; |
| 1090 | wrapiov[i+1].iov_len = iov[i].iov_len; |
| 1091 | dataLen += iov[i].iov_len; |
| 1092 | } |
| 1093 | |
| 1094 | /* form the header (JDWP plus DDMS) */ |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1095 | Set4BE(header, sizeof(header) + dataLen); |
| 1096 | Set4BE(header+4, NextRequestSerial()); |
| 1097 | Set1(header+8, 0); /* flags */ |
| 1098 | Set1(header+9, kJDWPDdmCmdSet); |
| 1099 | Set1(header+10, kJDWPDdmCmd); |
| 1100 | Set4BE(header+11, type); |
| 1101 | Set4BE(header+15, dataLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1102 | |
| 1103 | wrapiov[0].iov_base = header; |
| 1104 | wrapiov[0].iov_len = sizeof(header); |
| 1105 | |
| 1106 | /* |
| 1107 | * Make sure we're in VMWAIT in case the write blocks. |
| 1108 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1109 | int old_state = Dbg::ThreadWaiting(); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1110 | (*transport->sendBufferedRequest)(this, wrapiov, iov_count + 1); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1111 | Dbg::ThreadContinuing(old_state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | } // namespace JDWP |
| 1115 | |
| 1116 | } // namespace art |