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