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