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