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