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) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 128 | if (!Dbg::RequiresDeoptimization()) { |
| 129 | // We don't need deoptimization for debugging. |
| 130 | return false; |
| 131 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 132 | switch (eventKind) { |
| 133 | case EK_METHOD_ENTRY: |
| 134 | case EK_METHOD_EXIT: |
| 135 | case EK_METHOD_EXIT_WITH_RETURN_VALUE: |
| 136 | case EK_SINGLE_STEP: |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 137 | case EK_FIELD_ACCESS: |
| 138 | case EK_FIELD_MODIFICATION: |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 139 | return true; |
| 140 | default: |
| 141 | return false; |
| 142 | } |
| 143 | } |
| 144 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 145 | static uint32_t GetInstrumentationEventFor(JdwpEventKind eventKind) { |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 146 | switch (eventKind) { |
| 147 | case EK_BREAKPOINT: |
| 148 | case EK_SINGLE_STEP: |
| 149 | return instrumentation::Instrumentation::kDexPcMoved; |
| 150 | case EK_EXCEPTION: |
| 151 | case EK_EXCEPTION_CATCH: |
| 152 | return instrumentation::Instrumentation::kExceptionCaught; |
| 153 | case EK_METHOD_ENTRY: |
| 154 | return instrumentation::Instrumentation::kMethodEntered; |
| 155 | case EK_METHOD_EXIT: |
| 156 | case EK_METHOD_EXIT_WITH_RETURN_VALUE: |
| 157 | return instrumentation::Instrumentation::kMethodExited; |
| 158 | case EK_FIELD_ACCESS: |
| 159 | return instrumentation::Instrumentation::kFieldRead; |
| 160 | case EK_FIELD_MODIFICATION: |
| 161 | return instrumentation::Instrumentation::kFieldWritten; |
| 162 | default: |
| 163 | return 0; |
| 164 | } |
| 165 | } |
| 166 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 167 | /* |
| 168 | * Add an event to the list. Ordering is not important. |
| 169 | * |
| 170 | * If something prevents the event from being registered, e.g. it's a |
| 171 | * single-step request on a thread that doesn't exist, the event will |
| 172 | * not be added to the list, and an appropriate error will be returned. |
| 173 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 174 | JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 175 | CHECK(pEvent != nullptr); |
| 176 | CHECK(pEvent->prev == nullptr); |
| 177 | CHECK(pEvent->next == nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 178 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 179 | { |
| 180 | /* |
| 181 | * If one or more "break"-type mods are used, register them with |
| 182 | * the interpreter. |
| 183 | */ |
| 184 | DeoptimizationRequest req; |
| 185 | for (int i = 0; i < pEvent->modCount; i++) { |
| 186 | const JdwpEventMod* pMod = &pEvent->mods[i]; |
| 187 | if (pMod->modKind == MK_LOCATION_ONLY) { |
Sebastien Hertz | 033aabf | 2014-10-08 13:54:55 +0200 | [diff] [blame] | 188 | // Should only concern breakpoint, field access, field modification, step, and exception |
| 189 | // events. |
| 190 | // However breakpoint requires specific handling. Field access, field modification and step |
| 191 | // events need full deoptimization to be reported while exception event is reported during |
| 192 | // exception handling. |
| 193 | if (pEvent->eventKind == EK_BREAKPOINT) { |
| 194 | Dbg::WatchLocation(&pMod->locationOnly.loc, &req); |
| 195 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 196 | } else if (pMod->modKind == MK_STEP) { |
| 197 | /* should only be for EK_SINGLE_STEP; should only be one */ |
| 198 | JdwpStepSize size = static_cast<JdwpStepSize>(pMod->step.size); |
| 199 | JdwpStepDepth depth = static_cast<JdwpStepDepth>(pMod->step.depth); |
| 200 | JdwpError status = Dbg::ConfigureStep(pMod->step.threadId, size, depth); |
| 201 | if (status != ERR_NONE) { |
| 202 | return status; |
| 203 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 204 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 205 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 206 | if (NeedsFullDeoptimization(pEvent->eventKind)) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 207 | CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing); |
| 208 | CHECK(req.Method() == nullptr); |
| 209 | req.SetKind(DeoptimizationRequest::kFullDeoptimization); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 210 | } |
| 211 | Dbg::RequestDeoptimization(req); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 212 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 213 | uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind); |
| 214 | if (instrumentation_event != 0) { |
| 215 | DeoptimizationRequest req; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 216 | req.SetKind(DeoptimizationRequest::kRegisterForEvent); |
| 217 | req.SetInstrumentationEvent(instrumentation_event); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 218 | Dbg::RequestDeoptimization(req); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 219 | } |
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 | /* |
| 223 | * Add to list. |
| 224 | */ |
| 225 | MutexLock mu(Thread::Current(), event_list_lock_); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 226 | if (event_list_ != nullptr) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 227 | pEvent->next = event_list_; |
| 228 | event_list_->prev = pEvent; |
| 229 | } |
| 230 | event_list_ = pEvent; |
| 231 | ++event_list_size_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 232 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 233 | |
| 234 | Dbg::ManageDeoptimization(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 235 | |
| 236 | return ERR_NONE; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Remove an event from the list. This will also remove the event from |
| 241 | * any optimization tables, e.g. breakpoints. |
| 242 | * |
| 243 | * Does not free the JdwpEvent. |
| 244 | * |
| 245 | * Grab the eventLock before calling here. |
| 246 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 247 | void JdwpState::UnregisterEvent(JdwpEvent* pEvent) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 248 | if (pEvent->prev == nullptr) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 249 | /* head of the list */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 250 | CHECK(event_list_ == pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 251 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 252 | event_list_ = pEvent->next; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 253 | } else { |
| 254 | pEvent->prev->next = pEvent->next; |
| 255 | } |
| 256 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 257 | if (pEvent->next != nullptr) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 258 | pEvent->next->prev = pEvent->prev; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 259 | pEvent->next = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 260 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 261 | pEvent->prev = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 262 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 263 | { |
| 264 | /* |
| 265 | * Unhook us from the interpreter, if necessary. |
| 266 | */ |
| 267 | DeoptimizationRequest req; |
| 268 | for (int i = 0; i < pEvent->modCount; i++) { |
| 269 | JdwpEventMod* pMod = &pEvent->mods[i]; |
| 270 | if (pMod->modKind == MK_LOCATION_ONLY) { |
Sebastien Hertz | 033aabf | 2014-10-08 13:54:55 +0200 | [diff] [blame] | 271 | // Like in RegisterEvent, we need specific handling for breakpoint only. |
| 272 | if (pEvent->eventKind == EK_BREAKPOINT) { |
| 273 | Dbg::UnwatchLocation(&pMod->locationOnly.loc, &req); |
| 274 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 275 | } |
| 276 | if (pMod->modKind == MK_STEP) { |
| 277 | /* should only be for EK_SINGLE_STEP; should only be one */ |
| 278 | Dbg::UnconfigureStep(pMod->step.threadId); |
| 279 | } |
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 | if (pEvent->eventKind == EK_SINGLE_STEP) { |
| 282 | // Special case for single-steps where we want to avoid the slow pattern deoptimize/undeoptimize |
| 283 | // loop between each single-step. In a IDE, this would happens each time the user click on the |
| 284 | // "single-step" button. Here we delay the full undeoptimization to the next resume |
| 285 | // (VM.Resume or ThreadReference.Resume) or the end of the debugging session (VM.Dispose or |
| 286 | // runtime shutdown). |
| 287 | // Therefore, in a singles-stepping sequence, only the first single-step will trigger a full |
| 288 | // deoptimization and only the last single-step will trigger a full undeoptimization. |
| 289 | Dbg::DelayFullUndeoptimization(); |
| 290 | } else if (NeedsFullDeoptimization(pEvent->eventKind)) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 291 | CHECK_EQ(req.GetKind(), DeoptimizationRequest::kNothing); |
| 292 | CHECK(req.Method() == nullptr); |
| 293 | req.SetKind(DeoptimizationRequest::kFullUndeoptimization); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 294 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 295 | Dbg::RequestDeoptimization(req); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 296 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 297 | uint32_t instrumentation_event = GetInstrumentationEventFor(pEvent->eventKind); |
| 298 | if (instrumentation_event != 0) { |
| 299 | DeoptimizationRequest req; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 300 | req.SetKind(DeoptimizationRequest::kUnregisterForEvent); |
| 301 | req.SetInstrumentationEvent(instrumentation_event); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 302 | Dbg::RequestDeoptimization(req); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 303 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 304 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 305 | --event_list_size_; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 306 | CHECK(event_list_size_ != 0 || event_list_ == nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Remove the event with the given ID from the list. |
| 311 | * |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 312 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 313 | void JdwpState::UnregisterEventById(uint32_t requestId) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 314 | bool found = false; |
| 315 | { |
| 316 | MutexLock mu(Thread::Current(), event_list_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 317 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 318 | for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) { |
| 319 | if (pEvent->requestId == requestId) { |
| 320 | found = true; |
| 321 | UnregisterEvent(pEvent); |
| 322 | EventFree(pEvent); |
| 323 | break; /* there can be only one with a given ID */ |
| 324 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 325 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 328 | if (found) { |
| 329 | Dbg::ManageDeoptimization(); |
| 330 | } else { |
Sebastien Hertz | f272af4 | 2014-09-18 10:20:42 +0200 | [diff] [blame] | 331 | // Failure to find the event isn't really an error. For instance, it looks like Eclipse will |
| 332 | // try to be extra careful and will explicitly remove one-off single-step events (using a |
| 333 | // 'count' event modifier of 1). So the event may have already been removed as part of the |
| 334 | // event notification (see JdwpState::CleanupMatchList). |
| 335 | VLOG(jdwp) << StringPrintf("No match when removing event reqId=0x%04x", requestId); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 336 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Remove all entries from the event list. |
| 341 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 342 | void JdwpState::UnregisterAll() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 343 | MutexLock mu(Thread::Current(), event_list_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 344 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 345 | JdwpEvent* pEvent = event_list_; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 346 | while (pEvent != nullptr) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 347 | JdwpEvent* pNextEvent = pEvent->next; |
| 348 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 349 | UnregisterEvent(pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 350 | EventFree(pEvent); |
| 351 | pEvent = pNextEvent; |
| 352 | } |
| 353 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 354 | event_list_ = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Allocate a JdwpEvent struct with enough space to hold the specified |
| 359 | * number of mod records. |
| 360 | */ |
| 361 | JdwpEvent* EventAlloc(int numMods) { |
| 362 | JdwpEvent* newEvent; |
| 363 | int allocSize = offsetof(JdwpEvent, mods) + numMods * sizeof(newEvent->mods[0]); |
| 364 | newEvent = reinterpret_cast<JdwpEvent*>(malloc(allocSize)); |
| 365 | memset(newEvent, 0, allocSize); |
| 366 | return newEvent; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Free a JdwpEvent. |
| 371 | * |
| 372 | * Do not call this until the event has been removed from the list. |
| 373 | */ |
| 374 | void EventFree(JdwpEvent* pEvent) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 375 | if (pEvent == nullptr) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 376 | return; |
| 377 | } |
| 378 | |
| 379 | /* make sure it was removed from the list */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 380 | CHECK(pEvent->prev == nullptr); |
| 381 | CHECK(pEvent->next == nullptr); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 382 | /* want to check state->event_list_ != pEvent */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 383 | |
| 384 | /* |
| 385 | * Free any hairy bits in the mods. |
| 386 | */ |
| 387 | for (int i = 0; i < pEvent->modCount; i++) { |
| 388 | if (pEvent->mods[i].modKind == MK_CLASS_MATCH) { |
| 389 | free(pEvent->mods[i].classMatch.classPattern); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 390 | pEvent->mods[i].classMatch.classPattern = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 391 | } |
| 392 | if (pEvent->mods[i].modKind == MK_CLASS_EXCLUDE) { |
| 393 | free(pEvent->mods[i].classExclude.classPattern); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 394 | pEvent->mods[i].classExclude.classPattern = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | free(pEvent); |
| 399 | } |
| 400 | |
| 401 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 402 | * Run through the list and remove any entries with an expired "count" mod |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 403 | * from the event list. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 404 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 405 | void JdwpState::CleanupMatchList(const std::vector<JdwpEvent*>& match_list) { |
| 406 | for (JdwpEvent* pEvent : match_list) { |
| 407 | for (int i = 0; i < pEvent->modCount; ++i) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 408 | 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] | 409 | VLOG(jdwp) << StringPrintf("##### Removing expired event (requestId=%#" PRIx32 ")", |
| 410 | pEvent->requestId); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 411 | UnregisterEvent(pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 412 | EventFree(pEvent); |
| 413 | break; |
| 414 | } |
| 415 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 416 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Match a string against a "restricted regular expression", which is just |
| 421 | * a string that may start or end with '*' (e.g. "*.Foo" or "java.*"). |
| 422 | * |
| 423 | * ("Restricted name globbing" might have been a better term.) |
| 424 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 425 | static bool PatternMatch(const char* pattern, const std::string& target) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 426 | size_t patLen = strlen(pattern); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 427 | if (pattern[0] == '*') { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 428 | patLen--; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 429 | if (target.size() < patLen) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 430 | return false; |
| 431 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 432 | return strcmp(pattern+1, target.c_str() + (target.size()-patLen)) == 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 433 | } else if (pattern[patLen-1] == '*') { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 434 | return strncmp(pattern, target.c_str(), patLen-1) == 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 435 | } else { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 436 | return strcmp(pattern, target.c_str()) == 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 441 | * See if the event's mods match up with the contents of "basket". |
| 442 | * |
| 443 | * If we find a Count mod before rejecting an event, we decrement it. We |
| 444 | * need to do this even if later mods cause us to ignore the event. |
| 445 | */ |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 446 | static bool ModsMatch(JdwpEvent* pEvent, const ModBasket& basket) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 447 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 448 | JdwpEventMod* pMod = pEvent->mods; |
| 449 | |
| 450 | for (int i = pEvent->modCount; i > 0; i--, pMod++) { |
| 451 | switch (pMod->modKind) { |
| 452 | case MK_COUNT: |
| 453 | CHECK_GT(pMod->count.count, 0); |
| 454 | pMod->count.count--; |
Sebastien Hertz | 4320779 | 2014-04-15 16:03:27 +0200 | [diff] [blame] | 455 | if (pMod->count.count > 0) { |
| 456 | return false; |
| 457 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 458 | break; |
| 459 | case MK_CONDITIONAL: |
| 460 | CHECK(false); // should not be getting these |
| 461 | break; |
| 462 | case MK_THREAD_ONLY: |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 463 | if (!Dbg::MatchThread(pMod->threadOnly.threadId, basket.thread)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | break; |
| 467 | case MK_CLASS_ONLY: |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 468 | if (!Dbg::MatchType(basket.locationClass, pMod->classOnly.refTypeId)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | break; |
| 472 | case MK_CLASS_MATCH: |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 473 | if (!PatternMatch(pMod->classMatch.classPattern, basket.className)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 474 | return false; |
| 475 | } |
| 476 | break; |
| 477 | case MK_CLASS_EXCLUDE: |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 478 | if (PatternMatch(pMod->classMatch.classPattern, basket.className)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | break; |
| 482 | case MK_LOCATION_ONLY: |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 483 | if (!Dbg::MatchLocation(pMod->locationOnly.loc, *basket.pLoc)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | break; |
| 487 | case MK_EXCEPTION_ONLY: |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 488 | if (pMod->exceptionOnly.refTypeId != 0 && |
| 489 | !Dbg::MatchType(basket.exceptionClass, pMod->exceptionOnly.refTypeId)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 490 | return false; |
| 491 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 492 | if ((basket.caught && !pMod->exceptionOnly.caught) || |
| 493 | (!basket.caught && !pMod->exceptionOnly.uncaught)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | break; |
| 497 | case MK_FIELD_ONLY: |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 498 | if (!Dbg::MatchField(pMod->fieldOnly.refTypeId, pMod->fieldOnly.fieldId, basket.field)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 499 | return false; |
| 500 | } |
| 501 | break; |
| 502 | case MK_STEP: |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 503 | if (!Dbg::MatchThread(pMod->step.threadId, basket.thread)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 504 | return false; |
| 505 | } |
| 506 | break; |
| 507 | case MK_INSTANCE_ONLY: |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 508 | if (!Dbg::MatchInstance(pMod->instanceOnly.objectId, basket.thisPtr)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | break; |
| 512 | default: |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 513 | LOG(FATAL) << "unknown mod kind " << pMod->modKind; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | } |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | /* |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 521 | * Find all events of type "event_kind" with mods that match up with the |
| 522 | * rest of the arguments while holding the event list lock. This method |
| 523 | * is used by FindMatchingEvents below. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 524 | * |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 525 | * Found events are appended to "match_list" so this may be called multiple times for grouped |
| 526 | * events. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 527 | * |
| 528 | * DO NOT call this multiple times for the same eventKind, as Count mods are |
| 529 | * decremented during the scan. |
| 530 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 531 | void JdwpState::FindMatchingEventsLocked(JdwpEventKind event_kind, const ModBasket& basket, |
| 532 | std::vector<JdwpEvent*>* match_list) { |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 533 | for (JdwpEvent* pEvent = event_list_; pEvent != nullptr; pEvent = pEvent->next) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 534 | if (pEvent->eventKind == event_kind && ModsMatch(pEvent, basket)) { |
| 535 | match_list->push_back(pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 536 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | /* |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 541 | * Find all events of type "event_kind" with mods that match up with the |
| 542 | * rest of the arguments and return true if at least one event matches, |
| 543 | * false otherwise. |
| 544 | * |
| 545 | * Found events are appended to "match_list" so this may be called multiple |
| 546 | * times for grouped events. |
| 547 | * |
| 548 | * DO NOT call this multiple times for the same eventKind, as Count mods are |
| 549 | * decremented during the scan. |
| 550 | */ |
| 551 | bool JdwpState::FindMatchingEvents(JdwpEventKind event_kind, const ModBasket& basket, |
| 552 | std::vector<JdwpEvent*>* match_list) { |
| 553 | MutexLock mu(Thread::Current(), event_list_lock_); |
| 554 | match_list->reserve(event_list_size_); |
| 555 | FindMatchingEventsLocked(event_kind, basket, match_list); |
| 556 | return !match_list->empty(); |
| 557 | } |
| 558 | |
| 559 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 560 | * Scan through the list of matches and determine the most severe |
| 561 | * suspension policy. |
| 562 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 563 | static JdwpSuspendPolicy ScanSuspendPolicy(const std::vector<JdwpEvent*>& match_list) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 564 | JdwpSuspendPolicy policy = SP_NONE; |
| 565 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 566 | for (JdwpEvent* pEvent : match_list) { |
| 567 | if (pEvent->suspend_policy > policy) { |
| 568 | policy = pEvent->suspend_policy; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 569 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return policy; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Three possibilities: |
| 577 | * SP_NONE - do nothing |
| 578 | * SP_EVENT_THREAD - suspend ourselves |
| 579 | * SP_ALL - suspend everybody except JDWP support thread |
| 580 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 581 | void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 582 | VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")"; |
| 583 | if (suspend_policy == SP_NONE) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 587 | if (suspend_policy == SP_ALL) { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 588 | Dbg::SuspendVM(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 589 | } else { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 590 | CHECK_EQ(suspend_policy, SP_EVENT_THREAD); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* this is rare but possible -- see CLASS_PREPARE handling */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 594 | if (thread_self_id == debug_thread_id_) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 595 | LOG(INFO) << "NOTE: SuspendByPolicy not suspending JDWP thread"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
| 599 | DebugInvokeReq* pReq = Dbg::GetInvokeReq(); |
| 600 | while (true) { |
| 601 | pReq->ready = true; |
| 602 | Dbg::SuspendSelf(); |
| 603 | pReq->ready = false; |
| 604 | |
| 605 | /* |
| 606 | * The JDWP thread has told us (and possibly all other threads) to |
| 607 | * resume. See if it has left anything in our DebugInvokeReq mailbox. |
| 608 | */ |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 609 | if (!pReq->invoke_needed) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 610 | /*LOGD("SuspendByPolicy: no invoke needed");*/ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 611 | break; |
| 612 | } |
| 613 | |
| 614 | /* grab this before posting/suspending again */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 615 | SetWaitForEventThread(thread_self_id); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 616 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 617 | /* leave pReq->invoke_needed_ raised so we can check reentrancy */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 618 | Dbg::ExecuteMethod(pReq); |
| 619 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 620 | pReq->error = ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 624 | void JdwpState::SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy, |
| 625 | ObjectId threadId) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 626 | Thread* const self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 627 | self->AssertThreadSuspensionIsAllowable(); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 628 | CHECK(pReq != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 629 | /* send request and possibly suspend ourselves */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 630 | JDWP::ObjectId thread_self_id = Dbg::GetThreadSelfId(); |
| 631 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend); |
| 632 | if (suspend_policy != SP_NONE) { |
| 633 | SetWaitForEventThread(threadId); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 634 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 635 | EventFinish(pReq); |
| 636 | SuspendByPolicy(suspend_policy, thread_self_id); |
| 637 | self->TransitionFromSuspendedToRunnable(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 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)); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 728 | Set4BE(buf + 4, NextRequestSerial()); |
| 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 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 752 | void JdwpState::PostVMStart() { |
| 753 | JdwpSuspendPolicy suspend_policy = (options_->suspend) ? SP_ALL : SP_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 754 | ObjectId threadId = Dbg::GetThreadSelfId(); |
| 755 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 756 | VLOG(jdwp) << "EVENT: " << EK_VM_START; |
| 757 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 758 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 759 | ExpandBuf* pReq = eventPrep(); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 760 | expandBufAdd1(pReq, suspend_policy); |
| 761 | expandBufAdd4BE(pReq, 1); |
| 762 | expandBufAdd1(pReq, EK_VM_START); |
| 763 | expandBufAdd4BE(pReq, 0); /* requestId */ |
| 764 | expandBufAddObjectId(pReq, threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 765 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 766 | Dbg::ManageDeoptimization(); |
| 767 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 768 | /* send request and possibly suspend ourselves */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 769 | SendRequestAndPossiblySuspend(pReq, suspend_policy, threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 772 | static void LogMatchingEventsAndThread(const std::vector<JdwpEvent*> match_list, |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 773 | ObjectId thread_id) |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 774 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 775 | for (size_t i = 0, e = match_list.size(); i < e; ++i) { |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 776 | JdwpEvent* pEvent = match_list[i]; |
| 777 | VLOG(jdwp) << "EVENT #" << i << ": " << pEvent->eventKind |
| 778 | << StringPrintf(" (requestId=%#" PRIx32 ")", pEvent->requestId); |
| 779 | } |
| 780 | std::string thread_name; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 781 | JdwpError error = Dbg::GetThreadName(thread_id, &thread_name); |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 782 | if (error != JDWP::ERR_NONE) { |
| 783 | thread_name = "<unknown>"; |
| 784 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 785 | VLOG(jdwp) << StringPrintf(" thread=%#" PRIx64, thread_id) << " " << thread_name; |
| 786 | } |
| 787 | |
| 788 | static void SetJdwpLocationFromEventLocation(const JDWP::EventLocation* event_location, |
| 789 | JDWP::JdwpLocation* jdwp_location) |
| 790 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 791 | DCHECK(event_location != nullptr); |
| 792 | DCHECK(jdwp_location != nullptr); |
| 793 | Dbg::SetJdwpLocation(jdwp_location, event_location->method, event_location->dex_pc); |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 796 | /* |
| 797 | * A location of interest has been reached. This handles: |
| 798 | * Breakpoint |
| 799 | * SingleStep |
| 800 | * MethodEntry |
| 801 | * MethodExit |
| 802 | * These four types must be grouped together in a single response. The |
| 803 | * "eventFlags" indicates the type of event(s) that have happened. |
| 804 | * |
| 805 | * Valid mods: |
| 806 | * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, InstanceOnly |
| 807 | * LocationOnly (for breakpoint/step only) |
| 808 | * Step (for step only) |
| 809 | * |
| 810 | * Interesting test cases: |
| 811 | * - Put a breakpoint on a native method. Eclipse creates METHOD_ENTRY |
| 812 | * and METHOD_EXIT events with a ClassOnly mod on the method's class. |
| 813 | * - Use "run to line". Eclipse creates a BREAKPOINT with Count=1. |
| 814 | * - Single-step to a line with a breakpoint. Should get a single |
| 815 | * event message with both events in it. |
| 816 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 817 | void JdwpState::PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr, |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 818 | int eventFlags, const JValue* returnValue) { |
| 819 | DCHECK(pLoc != nullptr); |
| 820 | DCHECK(pLoc->method != nullptr); |
| 821 | DCHECK_EQ(pLoc->method->IsStatic(), thisPtr == nullptr); |
| 822 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 823 | ModBasket basket; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 824 | basket.pLoc = pLoc; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 825 | basket.locationClass = pLoc->method->GetDeclaringClass(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 826 | basket.thisPtr = thisPtr; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 827 | basket.thread = Thread::Current(); |
| 828 | basket.className = Dbg::GetClassName(basket.locationClass); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * On rare occasions we may need to execute interpreted code in the VM |
| 832 | * while handling a request from the debugger. Don't fire breakpoints |
| 833 | * while doing so. (I don't think we currently do this at all, so |
| 834 | * this is mostly paranoia.) |
| 835 | */ |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 836 | if (basket.thread == GetDebugThread()) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 837 | VLOG(jdwp) << "Ignoring location event in JDWP thread"; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 838 | return; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* |
| 842 | * The debugger variable display tab may invoke the interpreter to format |
| 843 | * complex objects. We want to ignore breakpoints and method entry/exit |
| 844 | * traps while working on behalf of the debugger. |
| 845 | * |
| 846 | * If we don't ignore them, the VM will get hung up, because we'll |
| 847 | * suspend on a breakpoint while the debugger is still waiting for its |
| 848 | * method invocation to complete. |
| 849 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 850 | if (InvokeInProgress()) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 851 | VLOG(jdwp) << "Not checking breakpoints during invoke (" << basket.className << ")"; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 852 | return; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 855 | std::vector<JdwpEvent*> match_list; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 856 | { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 857 | // We use the locked version because we have multiple possible match events. |
| 858 | MutexLock mu(Thread::Current(), event_list_lock_); |
| 859 | match_list.reserve(event_list_size_); |
| 860 | if ((eventFlags & Dbg::kBreakpoint) != 0) { |
| 861 | FindMatchingEventsLocked(EK_BREAKPOINT, basket, &match_list); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 862 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 863 | if ((eventFlags & Dbg::kSingleStep) != 0) { |
| 864 | FindMatchingEventsLocked(EK_SINGLE_STEP, basket, &match_list); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 865 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 866 | if ((eventFlags & Dbg::kMethodEntry) != 0) { |
| 867 | FindMatchingEventsLocked(EK_METHOD_ENTRY, basket, &match_list); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 868 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 869 | if ((eventFlags & Dbg::kMethodExit) != 0) { |
| 870 | FindMatchingEventsLocked(EK_METHOD_EXIT, basket, &match_list); |
| 871 | FindMatchingEventsLocked(EK_METHOD_EXIT_WITH_RETURN_VALUE, basket, &match_list); |
| 872 | } |
| 873 | } |
| 874 | if (match_list.empty()) { |
| 875 | // No matching event. |
| 876 | return; |
| 877 | } |
| 878 | JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list); |
| 879 | |
| 880 | ObjectId thread_id = Dbg::GetThreadId(basket.thread); |
| 881 | JDWP::JdwpLocation jdwp_location; |
| 882 | SetJdwpLocationFromEventLocation(pLoc, &jdwp_location); |
| 883 | |
| 884 | if (VLOG_IS_ON(jdwp)) { |
| 885 | LogMatchingEventsAndThread(match_list, thread_id); |
| 886 | VLOG(jdwp) << " location=" << jdwp_location; |
| 887 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
| 888 | } |
| 889 | |
| 890 | ExpandBuf* pReq = eventPrep(); |
| 891 | expandBufAdd1(pReq, suspend_policy); |
| 892 | expandBufAdd4BE(pReq, match_list.size()); |
| 893 | |
| 894 | for (const JdwpEvent* pEvent : match_list) { |
| 895 | expandBufAdd1(pReq, pEvent->eventKind); |
| 896 | expandBufAdd4BE(pReq, pEvent->requestId); |
| 897 | expandBufAddObjectId(pReq, thread_id); |
| 898 | expandBufAddLocation(pReq, jdwp_location); |
| 899 | if (pEvent->eventKind == EK_METHOD_EXIT_WITH_RETURN_VALUE) { |
| 900 | Dbg::OutputMethodReturnValue(jdwp_location.method_id, returnValue, pReq); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | { |
| 905 | MutexLock mu(Thread::Current(), event_list_lock_); |
| 906 | CleanupMatchList(match_list); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 907 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 908 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 909 | Dbg::ManageDeoptimization(); |
| 910 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 911 | SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 914 | void JdwpState::PostFieldEvent(const EventLocation* pLoc, mirror::ArtField* field, |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 915 | mirror::Object* this_object, const JValue* fieldValue, |
| 916 | bool is_modification) { |
| 917 | DCHECK(pLoc != nullptr); |
| 918 | DCHECK(field != nullptr); |
| 919 | DCHECK_EQ(fieldValue != nullptr, is_modification); |
| 920 | DCHECK_EQ(field->IsStatic(), this_object == nullptr); |
| 921 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 922 | ModBasket basket; |
| 923 | basket.pLoc = pLoc; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 924 | basket.locationClass = pLoc->method->GetDeclaringClass(); |
| 925 | basket.thisPtr = this_object; |
| 926 | basket.thread = Thread::Current(); |
| 927 | basket.className = Dbg::GetClassName(basket.locationClass); |
| 928 | basket.field = field; |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 929 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 930 | if (InvokeInProgress()) { |
| 931 | VLOG(jdwp) << "Not posting field event during invoke"; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 932 | return; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 933 | } |
| 934 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 935 | std::vector<JdwpEvent*> match_list; |
| 936 | const JdwpEventKind match_kind = (is_modification) ? EK_FIELD_MODIFICATION : EK_FIELD_ACCESS; |
| 937 | if (!FindMatchingEvents(match_kind, basket, &match_list)) { |
| 938 | // No matching event. |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list); |
| 943 | ObjectId thread_id = Dbg::GetThreadId(basket.thread); |
| 944 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
| 945 | ObjectId instance_id = registry->Add(basket.thisPtr); |
| 946 | RefTypeId field_type_id = registry->AddRefType(field->GetDeclaringClass()); |
| 947 | FieldId field_id = Dbg::ToFieldId(field); |
| 948 | JDWP::JdwpLocation jdwp_location; |
| 949 | SetJdwpLocationFromEventLocation(pLoc, &jdwp_location); |
| 950 | |
| 951 | if (VLOG_IS_ON(jdwp)) { |
| 952 | LogMatchingEventsAndThread(match_list, thread_id); |
| 953 | VLOG(jdwp) << " location=" << jdwp_location; |
| 954 | VLOG(jdwp) << StringPrintf(" this=%#" PRIx64, instance_id); |
| 955 | VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, field_type_id) << " " |
| 956 | << Dbg::GetClassName(field_id); |
| 957 | VLOG(jdwp) << StringPrintf(" field=%#" PRIx32, field_id) << " " |
| 958 | << Dbg::GetFieldName(field_id); |
| 959 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
| 960 | } |
| 961 | |
| 962 | ExpandBuf* pReq = eventPrep(); |
| 963 | expandBufAdd1(pReq, suspend_policy); |
| 964 | expandBufAdd4BE(pReq, match_list.size()); |
| 965 | |
| 966 | // Get field's reference type tag. |
| 967 | JDWP::JdwpTypeTag type_tag = Dbg::GetTypeTag(field->GetDeclaringClass()); |
| 968 | |
| 969 | // Get instance type tag. |
| 970 | uint8_t tag; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 971 | { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 972 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 973 | tag = Dbg::TagFromObject(soa, basket.thisPtr); |
| 974 | } |
| 975 | |
| 976 | for (const JdwpEvent* pEvent : match_list) { |
| 977 | expandBufAdd1(pReq, pEvent->eventKind); |
| 978 | expandBufAdd4BE(pReq, pEvent->requestId); |
| 979 | expandBufAddObjectId(pReq, thread_id); |
| 980 | expandBufAddLocation(pReq, jdwp_location); |
| 981 | expandBufAdd1(pReq, type_tag); |
| 982 | expandBufAddRefTypeId(pReq, field_type_id); |
| 983 | expandBufAddFieldId(pReq, field_id); |
| 984 | expandBufAdd1(pReq, tag); |
| 985 | expandBufAddObjectId(pReq, instance_id); |
| 986 | if (is_modification) { |
| 987 | Dbg::OutputFieldValue(field_id, fieldValue, pReq); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 988 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 989 | } |
Sebastien Hertz | bca0d3d | 2014-04-11 16:01:17 +0200 | [diff] [blame] | 990 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 991 | { |
| 992 | MutexLock mu(Thread::Current(), event_list_lock_); |
| 993 | CleanupMatchList(match_list); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | Dbg::ManageDeoptimization(); |
| 997 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 998 | SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1001 | /* |
| 1002 | * A thread is starting or stopping. |
| 1003 | * |
| 1004 | * Valid mods: |
| 1005 | * Count, ThreadOnly |
| 1006 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1007 | void JdwpState::PostThreadChange(Thread* thread, bool start) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1008 | CHECK_EQ(thread, Thread::Current()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1009 | |
| 1010 | /* |
| 1011 | * I don't think this can happen. |
| 1012 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1013 | if (InvokeInProgress()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1014 | LOG(WARNING) << "Not posting thread change during invoke"; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1015 | return; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Sebastien Hertz | 107e757 | 2014-12-18 11:13:15 +0100 | [diff] [blame] | 1018 | // We need the java.lang.Thread object associated to the starting/ending |
| 1019 | // thread to get its JDWP id. Therefore we can't report event if there |
| 1020 | // is no Java peer. This happens when the runtime shuts down and re-attaches |
| 1021 | // the current thread without creating a Java peer. |
| 1022 | if (thread->GetPeer() == nullptr) { |
| 1023 | return; |
| 1024 | } |
| 1025 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1026 | ModBasket basket; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1027 | basket.thread = thread; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1028 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1029 | std::vector<JdwpEvent*> match_list; |
| 1030 | const JdwpEventKind match_kind = (start) ? EK_THREAD_START : EK_THREAD_DEATH; |
| 1031 | if (!FindMatchingEvents(match_kind, basket, &match_list)) { |
| 1032 | // No matching event. |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list); |
| 1037 | ObjectId thread_id = Dbg::GetThreadId(basket.thread); |
| 1038 | |
| 1039 | if (VLOG_IS_ON(jdwp)) { |
| 1040 | LogMatchingEventsAndThread(match_list, thread_id); |
| 1041 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
| 1042 | } |
| 1043 | |
| 1044 | ExpandBuf* pReq = eventPrep(); |
| 1045 | expandBufAdd1(pReq, suspend_policy); |
| 1046 | expandBufAdd4BE(pReq, match_list.size()); |
| 1047 | |
| 1048 | for (const JdwpEvent* pEvent : match_list) { |
| 1049 | expandBufAdd1(pReq, pEvent->eventKind); |
| 1050 | expandBufAdd4BE(pReq, pEvent->requestId); |
| 1051 | expandBufAdd8BE(pReq, thread_id); |
| 1052 | } |
| 1053 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 1054 | { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1055 | MutexLock mu(Thread::Current(), event_list_lock_); |
| 1056 | CleanupMatchList(match_list); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 1057 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1058 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1059 | Dbg::ManageDeoptimization(); |
| 1060 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1061 | SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * Send a polite "VM is dying" message to the debugger. |
| 1066 | * |
| 1067 | * Skips the usual "event token" stuff. |
| 1068 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1069 | bool JdwpState::PostVMDeath() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1070 | VLOG(jdwp) << "EVENT: " << EK_VM_DEATH; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1071 | |
| 1072 | ExpandBuf* pReq = eventPrep(); |
| 1073 | expandBufAdd1(pReq, SP_NONE); |
| 1074 | expandBufAdd4BE(pReq, 1); |
| 1075 | |
| 1076 | expandBufAdd1(pReq, EK_VM_DEATH); |
| 1077 | expandBufAdd4BE(pReq, 0); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1078 | EventFinish(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1079 | return true; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
| 1083 | * An exception has been thrown. It may or may not have been caught. |
| 1084 | * |
| 1085 | * Valid mods: |
| 1086 | * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude, LocationOnly, |
| 1087 | * ExceptionOnly, InstanceOnly |
| 1088 | * |
| 1089 | * The "exceptionId" has not been added to the GC-visible object registry, |
| 1090 | * because there's a pretty good chance that we're not going to send it |
| 1091 | * up the debugger. |
| 1092 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1093 | void JdwpState::PostException(const EventLocation* pThrowLoc, mirror::Throwable* exception_object, |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1094 | const EventLocation* pCatchLoc, mirror::Object* thisPtr) { |
| 1095 | DCHECK(exception_object != nullptr); |
| 1096 | DCHECK(pThrowLoc != nullptr); |
| 1097 | DCHECK(pCatchLoc != nullptr); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1098 | if (pThrowLoc->method != nullptr) { |
| 1099 | DCHECK_EQ(pThrowLoc->method->IsStatic(), thisPtr == nullptr); |
| 1100 | } else { |
| 1101 | VLOG(jdwp) << "Unexpected: exception event with empty throw location"; |
| 1102 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1103 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1104 | ModBasket basket; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1105 | basket.pLoc = pThrowLoc; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1106 | if (pThrowLoc->method != nullptr) { |
| 1107 | basket.locationClass = pThrowLoc->method->GetDeclaringClass(); |
| 1108 | } else { |
| 1109 | basket.locationClass = nullptr; |
| 1110 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1111 | basket.thread = Thread::Current(); |
| 1112 | basket.className = Dbg::GetClassName(basket.locationClass); |
| 1113 | basket.exceptionClass = exception_object->GetClass(); |
| 1114 | basket.caught = (pCatchLoc->method != 0); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1115 | basket.thisPtr = thisPtr; |
| 1116 | |
| 1117 | /* don't try to post an exception caused by the debugger */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1118 | if (InvokeInProgress()) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1119 | VLOG(jdwp) << "Not posting exception hit during invoke (" << basket.className << ")"; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1120 | return; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1123 | std::vector<JdwpEvent*> match_list; |
| 1124 | if (!FindMatchingEvents(EK_EXCEPTION, basket, &match_list)) { |
| 1125 | // No matching event. |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list); |
| 1130 | ObjectId thread_id = Dbg::GetThreadId(basket.thread); |
| 1131 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
| 1132 | ObjectId exceptionId = registry->Add(exception_object); |
| 1133 | JDWP::JdwpLocation jdwp_throw_location; |
| 1134 | JDWP::JdwpLocation jdwp_catch_location; |
| 1135 | SetJdwpLocationFromEventLocation(pThrowLoc, &jdwp_throw_location); |
| 1136 | SetJdwpLocationFromEventLocation(pCatchLoc, &jdwp_catch_location); |
| 1137 | |
| 1138 | if (VLOG_IS_ON(jdwp)) { |
| 1139 | std::string exceptionClassName(PrettyDescriptor(exception_object->GetClass())); |
| 1140 | |
| 1141 | LogMatchingEventsAndThread(match_list, thread_id); |
| 1142 | VLOG(jdwp) << " throwLocation=" << jdwp_throw_location; |
| 1143 | if (jdwp_catch_location.class_id == 0) { |
| 1144 | VLOG(jdwp) << " catchLocation=uncaught"; |
| 1145 | } else { |
| 1146 | VLOG(jdwp) << " catchLocation=" << jdwp_catch_location; |
| 1147 | } |
| 1148 | VLOG(jdwp) << StringPrintf(" exception=%#" PRIx64, exceptionId) << " " |
| 1149 | << exceptionClassName; |
| 1150 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
| 1151 | } |
| 1152 | |
| 1153 | ExpandBuf* pReq = eventPrep(); |
| 1154 | expandBufAdd1(pReq, suspend_policy); |
| 1155 | expandBufAdd4BE(pReq, match_list.size()); |
| 1156 | |
| 1157 | for (const JdwpEvent* pEvent : match_list) { |
| 1158 | expandBufAdd1(pReq, pEvent->eventKind); |
| 1159 | expandBufAdd4BE(pReq, pEvent->requestId); |
| 1160 | expandBufAddObjectId(pReq, thread_id); |
| 1161 | expandBufAddLocation(pReq, jdwp_throw_location); |
| 1162 | expandBufAdd1(pReq, JT_OBJECT); |
| 1163 | expandBufAddObjectId(pReq, exceptionId); |
| 1164 | expandBufAddLocation(pReq, jdwp_catch_location); |
| 1165 | } |
| 1166 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1167 | { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1168 | MutexLock mu(Thread::Current(), event_list_lock_); |
| 1169 | CleanupMatchList(match_list); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1172 | Dbg::ManageDeoptimization(); |
| 1173 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1174 | SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Announce that a class has been loaded. |
| 1179 | * |
| 1180 | * Valid mods: |
| 1181 | * Count, ThreadOnly, ClassOnly, ClassMatch, ClassExclude |
| 1182 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1183 | void JdwpState::PostClassPrepare(mirror::Class* klass) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1184 | DCHECK(klass != nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1185 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1186 | ModBasket basket; |
| 1187 | basket.locationClass = klass; |
| 1188 | basket.thread = Thread::Current(); |
| 1189 | basket.className = Dbg::GetClassName(basket.locationClass); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1190 | |
| 1191 | /* suppress class prep caused by debugger */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1192 | if (InvokeInProgress()) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1193 | VLOG(jdwp) << "Not posting class prep caused by invoke (" << basket.className << ")"; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1194 | return; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1197 | std::vector<JdwpEvent*> match_list; |
| 1198 | if (!FindMatchingEvents(EK_CLASS_PREPARE, basket, &match_list)) { |
| 1199 | // No matching event. |
| 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | JdwpSuspendPolicy suspend_policy = ScanSuspendPolicy(match_list); |
| 1204 | ObjectId thread_id = Dbg::GetThreadId(basket.thread); |
| 1205 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
| 1206 | RefTypeId class_id = registry->AddRefType(basket.locationClass); |
| 1207 | |
| 1208 | // OLD-TODO - we currently always send both "verified" and "prepared" since |
| 1209 | // debuggers seem to like that. There might be some advantage to honesty, |
| 1210 | // since the class may not yet be verified. |
| 1211 | int status = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1212 | JDWP::JdwpTypeTag tag = Dbg::GetTypeTag(basket.locationClass); |
| 1213 | std::string temp; |
| 1214 | std::string signature(basket.locationClass->GetDescriptor(&temp)); |
| 1215 | |
| 1216 | if (VLOG_IS_ON(jdwp)) { |
| 1217 | LogMatchingEventsAndThread(match_list, thread_id); |
| 1218 | VLOG(jdwp) << StringPrintf(" type=%#" PRIx64, class_id) << " " << signature; |
| 1219 | VLOG(jdwp) << " suspend_policy=" << suspend_policy; |
| 1220 | } |
| 1221 | |
| 1222 | if (thread_id == debug_thread_id_) { |
| 1223 | /* |
| 1224 | * JDWP says that, for a class prep in the debugger thread, we |
| 1225 | * should set thread to null and if any threads were supposed |
| 1226 | * to be suspended then we suspend all other threads. |
| 1227 | */ |
| 1228 | VLOG(jdwp) << " NOTE: class prepare in debugger thread!"; |
| 1229 | thread_id = 0; |
| 1230 | if (suspend_policy == SP_EVENT_THREAD) { |
| 1231 | suspend_policy = SP_ALL; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | ExpandBuf* pReq = eventPrep(); |
| 1236 | expandBufAdd1(pReq, suspend_policy); |
| 1237 | expandBufAdd4BE(pReq, match_list.size()); |
| 1238 | |
| 1239 | for (const JdwpEvent* pEvent : match_list) { |
| 1240 | expandBufAdd1(pReq, pEvent->eventKind); |
| 1241 | expandBufAdd4BE(pReq, pEvent->requestId); |
| 1242 | expandBufAddObjectId(pReq, thread_id); |
| 1243 | expandBufAdd1(pReq, tag); |
| 1244 | expandBufAddRefTypeId(pReq, class_id); |
| 1245 | expandBufAddUtf8String(pReq, signature); |
| 1246 | expandBufAdd4BE(pReq, status); |
| 1247 | } |
| 1248 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1249 | { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1250 | MutexLock mu(Thread::Current(), event_list_lock_); |
| 1251 | CleanupMatchList(match_list); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1254 | Dbg::ManageDeoptimization(); |
| 1255 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1256 | SendRequestAndPossiblySuspend(pReq, suspend_policy, thread_id); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1259 | /* |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 1260 | * Setup the header for a chunk of DDM data. |
| 1261 | */ |
| 1262 | void JdwpState::SetupChunkHeader(uint32_t type, size_t data_len, size_t header_size, |
| 1263 | uint8_t* out_header) { |
| 1264 | CHECK_EQ(header_size, static_cast<size_t>(kJDWPHeaderLen + 8)); |
| 1265 | /* form the header (JDWP plus DDMS) */ |
| 1266 | Set4BE(out_header, header_size + data_len); |
| 1267 | Set4BE(out_header + 4, NextRequestSerial()); |
| 1268 | Set1(out_header + 8, 0); /* flags */ |
| 1269 | Set1(out_header + 9, kJDWPDdmCmdSet); |
| 1270 | Set1(out_header + 10, kJDWPDdmCmd); |
| 1271 | Set4BE(out_header + 11, type); |
| 1272 | Set4BE(out_header + 15, data_len); |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1276 | * Send up a chunk of DDM data. |
| 1277 | * |
| 1278 | * While this takes the form of a JDWP "event", it doesn't interact with |
| 1279 | * other debugger traffic, and can't suspend the VM, so we skip all of |
| 1280 | * the fun event token gymnastics. |
| 1281 | */ |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1282 | void JdwpState::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 1283 | uint8_t header[kJDWPHeaderLen + 8] = { 0 }; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1284 | size_t dataLen = 0; |
| 1285 | |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1286 | CHECK(iov != nullptr); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1287 | CHECK_GT(iov_count, 0); |
| 1288 | CHECK_LT(iov_count, 10); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * "Wrap" the contents of the iovec with a JDWP/DDMS header. We do |
| 1292 | * this by creating a new copy of the vector with space for the header. |
| 1293 | */ |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 1294 | std::vector<iovec> wrapiov; |
| 1295 | wrapiov.push_back(iovec()); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1296 | for (int i = 0; i < iov_count; i++) { |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 1297 | wrapiov.push_back(iov[i]); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1298 | dataLen += iov[i].iov_len; |
| 1299 | } |
| 1300 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 1301 | SetupChunkHeader(type, dataLen, sizeof(header), header); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1302 | |
| 1303 | wrapiov[0].iov_base = header; |
| 1304 | wrapiov[0].iov_len = sizeof(header); |
| 1305 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1306 | // Try to avoid blocking GC during a send, but only safe when not using mutexes at a lower-level |
| 1307 | // than mutator for lock ordering reasons. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1308 | Thread* self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1309 | bool safe_to_release_mutator_lock_over_send = !Locks::mutator_lock_->IsExclusiveHeld(self); |
| 1310 | if (safe_to_release_mutator_lock_over_send) { |
Brian Carlstrom | 38f85e4 | 2013-07-18 14:45:22 -0700 | [diff] [blame] | 1311 | for (size_t i = 0; i < kMutatorLock; ++i) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 1312 | if (self->GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1313 | safe_to_release_mutator_lock_over_send = false; |
| 1314 | break; |
| 1315 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | if (safe_to_release_mutator_lock_over_send) { |
| 1319 | // Change state to waiting to allow GC, ... while we're sending. |
| 1320 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend); |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 1321 | SendBufferedRequest(type, wrapiov); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1322 | self->TransitionFromSuspendedToRunnable(); |
| 1323 | } else { |
| 1324 | // Send and possibly block GC... |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 1325 | SendBufferedRequest(type, wrapiov); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1326 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | } // namespace JDWP |
| 1330 | |
| 1331 | } // namespace art |