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 | */ |
| 16 | |
| 17 | /* |
| 18 | * JDWP initialization. |
| 19 | */ |
| 20 | |
| 21 | #include "atomic.h" |
| 22 | #include "debugger.h" |
| 23 | #include "jdwp/jdwp_priv.h" |
| 24 | #include "logging.h" |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <time.h> |
| 30 | #include <errno.h> |
| 31 | |
| 32 | namespace art { |
| 33 | |
| 34 | namespace JDWP { |
| 35 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 36 | static void* StartJdwpThread(void* arg); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * JdwpNetStateBase class implementation |
| 40 | */ |
| 41 | JdwpNetStateBase::JdwpNetStateBase() : socket_lock_("JdwpNetStateBase lock") { |
| 42 | clientSock = -1; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Write a packet. Grabs a mutex to assure atomicity. |
| 47 | */ |
| 48 | ssize_t JdwpNetStateBase::writePacket(ExpandBuf* pReply) { |
| 49 | MutexLock mu(socket_lock_); |
| 50 | return write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply)); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Write a buffered packet. Grabs a mutex to assure atomicity. |
| 55 | */ |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 56 | ssize_t JdwpNetStateBase::writeBufferedPacket(const iovec* iov, int iov_count) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 57 | MutexLock mu(socket_lock_); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 58 | return writev(clientSock, iov, iov_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 61 | bool JdwpState::IsConnected() { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 62 | return (*transport_->isConnected)(this); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 65 | bool JdwpState::SendRequest(ExpandBuf* pReq) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 66 | return (*transport_->sendRequest)(this, pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 69 | /* |
| 70 | * Get the next "request" serial number. We use this when sending |
| 71 | * packets to the debugger. |
| 72 | */ |
| 73 | uint32_t JdwpState::NextRequestSerial() { |
| 74 | MutexLock mu(serial_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 75 | return request_serial_++; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Get the next "event" serial number. We use this in the response to |
| 80 | * message type EventRequest.Set. |
| 81 | */ |
| 82 | uint32_t JdwpState::NextEventSerial() { |
| 83 | MutexLock mu(serial_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 84 | return event_serial_++; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 87 | JdwpState::JdwpState(const JdwpOptions* options) |
| 88 | : options_(options), |
| 89 | thread_start_lock_("JDWP thread start lock"), |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 90 | thread_start_cond_("JDWP thread start condition variable"), |
| 91 | debug_thread_started_(false), |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 92 | debug_thread_id_(0), |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 93 | run(false), |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 94 | transport_(NULL), |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 95 | netState(NULL), |
| 96 | attach_lock_("JDWP attach lock"), |
| 97 | attach_cond_("JDWP attach condition variable"), |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 98 | last_activity_time_ms_(0), |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 99 | serial_lock_("JDWP serial lock"), |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 100 | request_serial_(0x10000000), |
| 101 | event_serial_(0x20000000), |
| 102 | event_list_lock_("JDWP event list lock"), |
| 103 | event_list_(NULL), |
| 104 | event_list_size_(0), |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 105 | event_thread_lock_("JDWP event thread lock"), |
| 106 | event_thread_cond_("JDWP event thread condition variable"), |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 107 | event_thread_id_(0), |
| 108 | ddm_is_active_(false) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Initialize JDWP. |
| 113 | * |
| 114 | * Does not return until JDWP thread is running, but may return before |
| 115 | * the thread is accepting network connections. |
| 116 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 117 | JdwpState* JdwpState::Create(const JdwpOptions* options) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 118 | UniquePtr<JdwpState> state(new JdwpState(options)); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 119 | switch (options->transport) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 120 | case kJdwpTransportSocket: |
| 121 | // LOGD("prepping for JDWP over TCP"); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 122 | state->transport_ = SocketTransport(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 123 | break; |
| 124 | #ifdef HAVE_ANDROID_OS |
| 125 | case kJdwpTransportAndroidAdb: |
| 126 | // LOGD("prepping for JDWP over ADB"); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 127 | state->transport_ = AndroidAdbTransport(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 128 | break; |
| 129 | #endif |
| 130 | default: |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 131 | LOG(FATAL) << "Unknown transport: " << options->transport; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 134 | if (!(*state->transport_->startup)(state.get(), options)) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 135 | return NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Grab a mutex or two before starting the thread. This ensures they |
| 140 | * won't signal the cond var before we're waiting. |
| 141 | */ |
| 142 | state->thread_start_lock_.Lock(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 143 | const bool should_suspend = options->suspend; |
| 144 | if (should_suspend) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 145 | state->attach_lock_.Lock(); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * We have bound to a port, or are trying to connect outbound to a |
| 150 | * debugger. Create the JDWP thread and let it continue the mission. |
| 151 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 152 | CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, NULL, StartJdwpThread, state.get()), "JDWP thread"); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Wait until the thread finishes basic initialization. |
| 156 | * TODO: cond vars should be waited upon in a loop |
| 157 | */ |
| 158 | state->thread_start_cond_.Wait(state->thread_start_lock_); |
| 159 | state->thread_start_lock_.Unlock(); |
| 160 | |
| 161 | /* |
| 162 | * For suspend=y, wait for the debugger to connect to us or for us to |
| 163 | * connect to the debugger. |
| 164 | * |
| 165 | * The JDWP thread will signal us when it connects successfully or |
| 166 | * times out (for timeout=xxx), so we have to check to see what happened |
| 167 | * when we wake up. |
| 168 | */ |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 169 | if (should_suspend) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 170 | { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 171 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 172 | |
| 173 | state->attach_cond_.Wait(state->attach_lock_); |
| 174 | state->attach_lock_.Unlock(); |
| 175 | } |
| 176 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 177 | if (!state->IsActive()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 178 | LOG(ERROR) << "JDWP connection failed"; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 179 | return NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | LOG(INFO) << "JDWP connected"; |
| 183 | |
| 184 | /* |
| 185 | * Ordinarily we would pause briefly to allow the debugger to set |
| 186 | * breakpoints and so on, but for "suspend=y" the VM init code will |
| 187 | * pause the VM when it sends the VM_START message. |
| 188 | */ |
| 189 | } |
| 190 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 191 | return state.release(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Reset all session-related state. There should not be an active connection |
| 196 | * to the client at this point. The rest of the VM still thinks there is |
| 197 | * a debugger attached. |
| 198 | * |
| 199 | * This includes freeing up the debugger event list. |
| 200 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 201 | void JdwpState::ResetState() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 202 | /* could reset the serial numbers, but no need to */ |
| 203 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 204 | UnregisterAll(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 205 | { |
| 206 | MutexLock mu(event_list_lock_); |
| 207 | CHECK(event_list_ == NULL); |
| 208 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Should not have one of these in progress. If the debugger went away |
| 212 | * mid-request, though, we could see this. |
| 213 | */ |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 214 | if (event_thread_id_ != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 215 | LOG(WARNING) << "Resetting state while event in progress"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 216 | DCHECK(false); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Tell the JDWP thread to shut down. Frees "state". |
| 222 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 223 | JdwpState::~JdwpState() { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 224 | if (transport_ != NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 225 | if (IsConnected()) { |
| 226 | PostVMDeath(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Close down the network to inspire the thread to halt. |
| 231 | */ |
Elliott Hughes | 0cc1bbd | 2012-01-12 12:27:08 -0800 | [diff] [blame] | 232 | VLOG(jdwp) << "JDWP shutting down net..."; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 233 | (*transport_->shutdown)(this); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 234 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 235 | if (debug_thread_started_) { |
| 236 | run = false; |
| 237 | void* threadReturn; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 238 | if (pthread_join(pthread_, &threadReturn) != 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 239 | LOG(WARNING) << "JDWP thread join failed"; |
| 240 | } |
| 241 | } |
| 242 | |
Elliott Hughes | 0cc1bbd | 2012-01-12 12:27:08 -0800 | [diff] [blame] | 243 | VLOG(jdwp) << "JDWP freeing netstate..."; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 244 | (*transport_->free)(this); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 245 | netState = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 246 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 247 | CHECK(netState == NULL); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 248 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 249 | ResetState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Are we talking to a debugger? |
| 254 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 255 | bool JdwpState::IsActive() { |
| 256 | return IsConnected(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Entry point for JDWP thread. The thread was created through the VM |
| 261 | * mechanisms, so there is a java/lang/Thread associated with us. |
| 262 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 263 | static void* StartJdwpThread(void* arg) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 264 | JdwpState* state = reinterpret_cast<JdwpState*>(arg); |
| 265 | CHECK(state != NULL); |
| 266 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 267 | state->Run(); |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | void JdwpState::Run() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 272 | Runtime* runtime = Runtime::Current(); |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 273 | runtime->AttachCurrentThread("JDWP", true, Thread::GetSystemThreadGroup()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 274 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 275 | VLOG(jdwp) << "JDWP: thread running"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 276 | |
| 277 | /* |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 278 | * Finish initializing, then notify the creating thread that |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 279 | * we're running. |
| 280 | */ |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 281 | thread_ = Thread::Current(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 282 | run = true; |
| 283 | android_atomic_release_store(true, &debug_thread_started_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 284 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 285 | thread_start_lock_.Lock(); |
| 286 | thread_start_cond_.Broadcast(); |
| 287 | thread_start_lock_.Unlock(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 288 | |
| 289 | /* set the thread state to VMWAIT so GCs don't wait for us */ |
| 290 | Dbg::ThreadWaiting(); |
| 291 | |
| 292 | /* |
| 293 | * Loop forever if we're in server mode, processing connections. In |
| 294 | * non-server mode, we bail out of the thread when the debugger drops |
| 295 | * us. |
| 296 | * |
| 297 | * We broadcast a notification when a debugger attaches, after we |
| 298 | * successfully process the handshake. |
| 299 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 300 | while (run) { |
| 301 | if (options_->server) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 302 | /* |
| 303 | * Block forever, waiting for a connection. To support the |
| 304 | * "timeout=xxx" option we'll need to tweak this. |
| 305 | */ |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 306 | if (!(*transport_->accept)(this)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | } else { |
| 310 | /* |
| 311 | * If we're not acting as a server, we need to connect out to the |
| 312 | * debugger. To support the "timeout=xxx" option we need to |
| 313 | * have a timeout if the handshake reply isn't received in a |
| 314 | * reasonable amount of time. |
| 315 | */ |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 316 | if (!(*transport_->establish)(this, options_)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 317 | /* wake anybody who was waiting for us to succeed */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 318 | MutexLock mu(attach_lock_); |
| 319 | attach_cond_.Broadcast(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /* prep debug code to handle the new connection */ |
| 325 | Dbg::Connected(); |
| 326 | |
| 327 | /* process requests until the debugger drops */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 328 | bool first = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 329 | while (!Dbg::IsDisposed()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 330 | // sanity check -- shouldn't happen? |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 331 | if (Thread::Current()->GetState() != kVmWait) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 332 | LOG(ERROR) << "JDWP thread no longer in VMWAIT (now " << Thread::Current()->GetState() << "); resetting"; |
| 333 | Dbg::ThreadWaiting(); |
| 334 | } |
| 335 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 336 | if (!(*transport_->processIncoming)(this)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 337 | /* blocking read */ |
| 338 | break; |
| 339 | } |
| 340 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 341 | if (first && !(*transport_->awaitingHandshake)(this)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 342 | /* handshake worked, tell the interpreter that we're active */ |
| 343 | first = false; |
| 344 | |
| 345 | /* set thread ID; requires object registry to be active */ |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 346 | debug_thread_id_ = Dbg::GetThreadSelfId(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 347 | |
| 348 | /* wake anybody who's waiting for us */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 349 | MutexLock mu(attach_lock_); |
| 350 | attach_cond_.Broadcast(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 354 | (*transport_->close)(this); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 355 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 356 | if (ddm_is_active_) { |
| 357 | ddm_is_active_ = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 358 | |
| 359 | /* broadcast the disconnect; must be in RUNNING state */ |
| 360 | Dbg::ThreadRunning(); |
| 361 | Dbg::DdmDisconnected(); |
| 362 | Dbg::ThreadWaiting(); |
| 363 | } |
| 364 | |
| 365 | /* release session state, e.g. remove breakpoint instructions */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 366 | ResetState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 367 | |
| 368 | /* tell the interpreter that the debugger is no longer around */ |
| 369 | Dbg::Disconnected(); |
| 370 | |
| 371 | /* if we had threads suspended, resume them now */ |
| 372 | Dbg::UndoDebuggerSuspensions(); |
| 373 | |
| 374 | /* if we connected out, this was a one-shot deal */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 375 | if (!options_->server) { |
| 376 | run = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | /* back to running, for thread shutdown */ |
| 381 | Dbg::ThreadRunning(); |
| 382 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 383 | VLOG(jdwp) << "JDWP: thread detaching and exiting..."; |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 384 | runtime->DetachCurrentThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 387 | void JdwpState::NotifyDdmsActive() { |
| 388 | if (!ddm_is_active_) { |
| 389 | ddm_is_active_ = true; |
| 390 | Dbg::DdmConnected(); |
| 391 | } |
| 392 | } |
| 393 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 394 | Thread* JdwpState::GetDebugThread() { |
| 395 | return thread_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Support routines for waitForDebugger(). |
| 400 | * |
| 401 | * We can't have a trivial "waitForDebugger" function that returns the |
| 402 | * instant the debugger connects, because we run the risk of executing code |
| 403 | * before the debugger has had a chance to configure breakpoints or issue |
| 404 | * suspend calls. It would be nice to just sit in the suspended state, but |
| 405 | * most debuggers don't expect any threads to be suspended when they attach. |
| 406 | * |
| 407 | * There's no JDWP event we can post to tell the debugger, "we've stopped, |
| 408 | * and we like it that way". We could send a fake breakpoint, which should |
| 409 | * cause the debugger to immediately send a resume, but the debugger might |
| 410 | * send the resume immediately or might throw an exception of its own upon |
| 411 | * receiving a breakpoint event that it didn't ask for. |
| 412 | * |
| 413 | * What we really want is a "wait until the debugger is done configuring |
| 414 | * stuff" event. We can approximate this with a "wait until the debugger |
| 415 | * has been idle for a brief period". |
| 416 | */ |
| 417 | |
| 418 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 419 | * Return the time, in milliseconds, since the last debugger activity. |
| 420 | * |
| 421 | * Returns -1 if no debugger is attached, or 0 if we're in the middle of |
| 422 | * processing a debugger request. |
| 423 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 424 | int64_t JdwpState::LastDebuggerActivity() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 425 | if (!Dbg::IsDebuggerActive()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 426 | LOG(DEBUG) << "no active debugger"; |
| 427 | return -1; |
| 428 | } |
| 429 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame^] | 430 | int64_t last = QuasiAtomic::Read64(&last_activity_time_ms_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 431 | |
| 432 | /* initializing or in the middle of something? */ |
| 433 | if (last == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 434 | VLOG(jdwp) << "+++ last=busy"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | /* now get the current time */ |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 439 | int64_t now = MilliTime(); |
Elliott Hughes | c3b3e75 | 2012-01-27 13:48:50 -0800 | [diff] [blame] | 440 | CHECK_GE(now, last); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 441 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 442 | VLOG(jdwp) << "+++ debugger interval=" << (now - last); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 443 | return now - last; |
| 444 | } |
| 445 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 446 | std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 447 | os << "JdwpLocation[" |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 448 | << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.class_id, rhs.method_id) |
| 449 | << "@" << StringPrintf("%#llx", rhs.dex_pc) << " " << rhs.type_tag << "]"; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 450 | return os; |
| 451 | } |
| 452 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 453 | bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 454 | return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id && |
| 455 | lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag; |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) { |
| 459 | return !(lhs == rhs); |
| 460 | } |
| 461 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 462 | } // namespace JDWP |
| 463 | |
| 464 | } // namespace art |