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