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