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() { |
| 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) { |
| 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_); |
| 75 | return requestSerial++; |
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_); |
| 84 | return eventSerial++; |
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), |
| 92 | debugThreadId(0), |
| 93 | run(false), |
| 94 | transport(NULL), |
| 95 | netState(NULL), |
| 96 | attach_lock_("JDWP attach lock"), |
| 97 | attach_cond_("JDWP attach condition variable"), |
| 98 | lastActivityWhen(0), |
| 99 | requestSerial(0x10000000), |
| 100 | eventSerial(0x20000000), |
| 101 | serial_lock_("JDWP serial lock"), |
| 102 | numEvents(0), |
| 103 | eventList(NULL), |
| 104 | event_lock_("JDWP event lock"), |
| 105 | event_thread_lock_("JDWP event thread lock"), |
| 106 | event_thread_cond_("JDWP event thread condition variable"), |
| 107 | eventThreadId(0), |
| 108 | ddmActive(false) { |
| 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 | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 118 | /* comment this out when debugging JDWP itself */ |
| 119 | //android_setMinPriority(LOG_TAG, ANDROID_LOG_DEBUG); |
| 120 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 121 | UniquePtr<JdwpState> state(new JdwpState(options)); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 122 | switch (options->transport) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 123 | case kJdwpTransportSocket: |
| 124 | // LOGD("prepping for JDWP over TCP"); |
| 125 | state->transport = SocketTransport(); |
| 126 | break; |
| 127 | #ifdef HAVE_ANDROID_OS |
| 128 | case kJdwpTransportAndroidAdb: |
| 129 | // LOGD("prepping for JDWP over ADB"); |
| 130 | state->transport = AndroidAdbTransport(); |
| 131 | break; |
| 132 | #endif |
| 133 | default: |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 134 | LOG(FATAL) << "Unknown transport: " << options->transport; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 137 | if (!(*state->transport->startup)(state.get(), options)) { |
| 138 | return NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Grab a mutex or two before starting the thread. This ensures they |
| 143 | * won't signal the cond var before we're waiting. |
| 144 | */ |
| 145 | state->thread_start_lock_.Lock(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 146 | if (options->suspend) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 147 | state->attach_lock_.Lock(); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * We have bound to a port, or are trying to connect outbound to a |
| 152 | * debugger. Create the JDWP thread and let it continue the mission. |
| 153 | */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 154 | 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] | 155 | |
| 156 | /* |
| 157 | * Wait until the thread finishes basic initialization. |
| 158 | * TODO: cond vars should be waited upon in a loop |
| 159 | */ |
| 160 | state->thread_start_cond_.Wait(state->thread_start_lock_); |
| 161 | state->thread_start_lock_.Unlock(); |
| 162 | |
| 163 | /* |
| 164 | * For suspend=y, wait for the debugger to connect to us or for us to |
| 165 | * connect to the debugger. |
| 166 | * |
| 167 | * The JDWP thread will signal us when it connects successfully or |
| 168 | * times out (for timeout=xxx), so we have to check to see what happened |
| 169 | * when we wake up. |
| 170 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 171 | if (options->suspend) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 172 | { |
| 173 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 174 | |
| 175 | state->attach_cond_.Wait(state->attach_lock_); |
| 176 | state->attach_lock_.Unlock(); |
| 177 | } |
| 178 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 179 | if (!state->IsActive()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 180 | LOG(ERROR) << "JDWP connection failed"; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 181 | return NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | LOG(INFO) << "JDWP connected"; |
| 185 | |
| 186 | /* |
| 187 | * Ordinarily we would pause briefly to allow the debugger to set |
| 188 | * breakpoints and so on, but for "suspend=y" the VM init code will |
| 189 | * pause the VM when it sends the VM_START message. |
| 190 | */ |
| 191 | } |
| 192 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 193 | return state.release(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Reset all session-related state. There should not be an active connection |
| 198 | * to the client at this point. The rest of the VM still thinks there is |
| 199 | * a debugger attached. |
| 200 | * |
| 201 | * This includes freeing up the debugger event list. |
| 202 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 203 | void JdwpState::ResetState() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 204 | /* could reset the serial numbers, but no need to */ |
| 205 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 206 | UnregisterAll(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 207 | CHECK(eventList == NULL); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Should not have one of these in progress. If the debugger went away |
| 211 | * mid-request, though, we could see this. |
| 212 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 213 | if (eventThreadId != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 214 | LOG(WARNING) << "Resetting state while event in progress"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 215 | DCHECK(false); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Tell the JDWP thread to shut down. Frees "state". |
| 221 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 222 | JdwpState::~JdwpState() { |
| 223 | if (transport != NULL) { |
| 224 | if (IsConnected()) { |
| 225 | PostVMDeath(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Close down the network to inspire the thread to halt. |
| 230 | */ |
Elliott Hughes | 0cc1bbd | 2012-01-12 12:27:08 -0800 | [diff] [blame] | 231 | VLOG(jdwp) << "JDWP shutting down net..."; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 232 | (*transport->shutdown)(this); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 233 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 234 | if (debug_thread_started_) { |
| 235 | run = false; |
| 236 | void* threadReturn; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 237 | if (pthread_join(pthread_, &threadReturn) != 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 238 | LOG(WARNING) << "JDWP thread join failed"; |
| 239 | } |
| 240 | } |
| 241 | |
Elliott Hughes | 0cc1bbd | 2012-01-12 12:27:08 -0800 | [diff] [blame] | 242 | VLOG(jdwp) << "JDWP freeing netstate..."; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 243 | (*transport->free)(this); |
| 244 | netState = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 245 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 246 | CHECK(netState == NULL); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 247 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 248 | ResetState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Are we talking to a debugger? |
| 253 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 254 | bool JdwpState::IsActive() { |
| 255 | return IsConnected(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Entry point for JDWP thread. The thread was created through the VM |
| 260 | * mechanisms, so there is a java/lang/Thread associated with us. |
| 261 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 262 | static void* StartJdwpThread(void* arg) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 263 | JdwpState* state = reinterpret_cast<JdwpState*>(arg); |
| 264 | CHECK(state != NULL); |
| 265 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 266 | state->Run(); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | void JdwpState::Run() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 271 | Runtime* runtime = Runtime::Current(); |
| 272 | runtime->AttachCurrentThread("JDWP", true); |
| 273 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 274 | VLOG(jdwp) << "JDWP: thread running"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 275 | |
| 276 | /* |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 277 | * Finish initializing, then notify the creating thread that |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 278 | * we're running. |
| 279 | */ |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 280 | thread_ = Thread::Current(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 281 | run = true; |
| 282 | android_atomic_release_store(true, &debug_thread_started_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 283 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 284 | thread_start_lock_.Lock(); |
| 285 | thread_start_cond_.Broadcast(); |
| 286 | thread_start_lock_.Unlock(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 287 | |
| 288 | /* set the thread state to VMWAIT so GCs don't wait for us */ |
| 289 | Dbg::ThreadWaiting(); |
| 290 | |
| 291 | /* |
| 292 | * Loop forever if we're in server mode, processing connections. In |
| 293 | * non-server mode, we bail out of the thread when the debugger drops |
| 294 | * us. |
| 295 | * |
| 296 | * We broadcast a notification when a debugger attaches, after we |
| 297 | * successfully process the handshake. |
| 298 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 299 | while (run) { |
| 300 | if (options_->server) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 301 | /* |
| 302 | * Block forever, waiting for a connection. To support the |
| 303 | * "timeout=xxx" option we'll need to tweak this. |
| 304 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 305 | if (!(*transport->accept)(this)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | } else { |
| 309 | /* |
| 310 | * If we're not acting as a server, we need to connect out to the |
| 311 | * debugger. To support the "timeout=xxx" option we need to |
| 312 | * have a timeout if the handshake reply isn't received in a |
| 313 | * reasonable amount of time. |
| 314 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 315 | if (!(*transport->establish)(this)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 316 | /* wake anybody who was waiting for us to succeed */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 317 | MutexLock mu(attach_lock_); |
| 318 | attach_cond_.Broadcast(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* prep debug code to handle the new connection */ |
| 324 | Dbg::Connected(); |
| 325 | |
| 326 | /* process requests until the debugger drops */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 327 | bool first = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 328 | while (!Dbg::IsDisposed()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 329 | // sanity check -- shouldn't happen? |
| 330 | if (Thread::Current()->GetState() != Thread::kVmWait) { |
| 331 | LOG(ERROR) << "JDWP thread no longer in VMWAIT (now " << Thread::Current()->GetState() << "); resetting"; |
| 332 | Dbg::ThreadWaiting(); |
| 333 | } |
| 334 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 335 | if (!(*transport->processIncoming)(this)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 336 | /* blocking read */ |
| 337 | break; |
| 338 | } |
| 339 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 340 | if (first && !(*transport->awaitingHandshake)(this)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 341 | /* handshake worked, tell the interpreter that we're active */ |
| 342 | first = false; |
| 343 | |
| 344 | /* set thread ID; requires object registry to be active */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 345 | debugThreadId = Dbg::GetThreadSelfId(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 346 | |
| 347 | /* wake anybody who's waiting for us */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 348 | MutexLock mu(attach_lock_); |
| 349 | attach_cond_.Broadcast(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 353 | (*transport->close)(this); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 354 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 355 | if (ddmActive) { |
| 356 | ddmActive = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 357 | |
| 358 | /* broadcast the disconnect; must be in RUNNING state */ |
| 359 | Dbg::ThreadRunning(); |
| 360 | Dbg::DdmDisconnected(); |
| 361 | Dbg::ThreadWaiting(); |
| 362 | } |
| 363 | |
| 364 | /* release session state, e.g. remove breakpoint instructions */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 365 | ResetState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 366 | |
| 367 | /* tell the interpreter that the debugger is no longer around */ |
| 368 | Dbg::Disconnected(); |
| 369 | |
| 370 | /* if we had threads suspended, resume them now */ |
| 371 | Dbg::UndoDebuggerSuspensions(); |
| 372 | |
| 373 | /* if we connected out, this was a one-shot deal */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 374 | if (!options_->server) { |
| 375 | run = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | /* back to running, for thread shutdown */ |
| 380 | Dbg::ThreadRunning(); |
| 381 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 382 | VLOG(jdwp) << "JDWP: thread detaching and exiting..."; |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 383 | runtime->DetachCurrentThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 386 | Thread* JdwpState::GetDebugThread() { |
| 387 | return thread_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Support routines for waitForDebugger(). |
| 392 | * |
| 393 | * We can't have a trivial "waitForDebugger" function that returns the |
| 394 | * instant the debugger connects, because we run the risk of executing code |
| 395 | * before the debugger has had a chance to configure breakpoints or issue |
| 396 | * suspend calls. It would be nice to just sit in the suspended state, but |
| 397 | * most debuggers don't expect any threads to be suspended when they attach. |
| 398 | * |
| 399 | * There's no JDWP event we can post to tell the debugger, "we've stopped, |
| 400 | * and we like it that way". We could send a fake breakpoint, which should |
| 401 | * cause the debugger to immediately send a resume, but the debugger might |
| 402 | * send the resume immediately or might throw an exception of its own upon |
| 403 | * receiving a breakpoint event that it didn't ask for. |
| 404 | * |
| 405 | * What we really want is a "wait until the debugger is done configuring |
| 406 | * stuff" event. We can approximate this with a "wait until the debugger |
| 407 | * has been idle for a brief period". |
| 408 | */ |
| 409 | |
| 410 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 411 | * Return the time, in milliseconds, since the last debugger activity. |
| 412 | * |
| 413 | * Returns -1 if no debugger is attached, or 0 if we're in the middle of |
| 414 | * processing a debugger request. |
| 415 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 416 | int64_t JdwpState::LastDebuggerActivity() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 417 | if (!Dbg::IsDebuggerConnected()) { |
| 418 | LOG(DEBUG) << "no active debugger"; |
| 419 | return -1; |
| 420 | } |
| 421 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 422 | int64_t last = QuasiAtomicRead64(&lastActivityWhen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 423 | |
| 424 | /* initializing or in the middle of something? */ |
| 425 | if (last == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 426 | VLOG(jdwp) << "+++ last=busy"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* now get the current time */ |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 431 | int64_t now = MilliTime(); |
Elliott Hughes | c3b3e75 | 2012-01-27 13:48:50 -0800 | [diff] [blame] | 432 | CHECK_GE(now, last); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 433 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 434 | VLOG(jdwp) << "+++ debugger interval=" << (now - last); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 435 | return now - last; |
| 436 | } |
| 437 | |
| 438 | static const char* kTransportNames[] = { |
| 439 | "Unknown", |
| 440 | "Socket", |
| 441 | "AndroidAdb", |
| 442 | }; |
| 443 | std::ostream& operator<<(std::ostream& os, const JdwpTransportType& value) { |
| 444 | int32_t int_value = static_cast<int32_t>(value); |
| 445 | if (value >= kJdwpTransportUnknown && value <= kJdwpTransportAndroidAdb) { |
| 446 | os << kTransportNames[int_value]; |
| 447 | } else { |
| 448 | os << "JdwpTransportType[" << int_value << "]"; |
| 449 | } |
| 450 | return os; |
| 451 | } |
| 452 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 453 | std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 454 | os << "JdwpLocation[" |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 455 | << Dbg::GetClassName(rhs.classId) << "." << Dbg::GetMethodName(rhs.classId, rhs.methodId) |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 456 | << "@" << rhs.idx << " " << rhs.typeTag << "]"; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 457 | return os; |
| 458 | } |
| 459 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 460 | std::ostream& operator<<(std::ostream& os, const JdwpTag& value) { |
| 461 | switch (value) { |
| 462 | case JT_ARRAY: os << "JT_ARRAY"; break; |
| 463 | case JT_BYTE: os << "JT_BYTE"; break; |
| 464 | case JT_CHAR: os << "JT_CHAR"; break; |
| 465 | case JT_OBJECT: os << "JT_OBJECT"; break; |
| 466 | case JT_FLOAT: os << "JT_FLOAT"; break; |
| 467 | case JT_DOUBLE: os << "JT_DOUBLE"; break; |
| 468 | case JT_INT: os << "JT_INT"; break; |
| 469 | case JT_LONG: os << "JT_LONG"; break; |
| 470 | case JT_SHORT: os << "JT_SHORT"; break; |
| 471 | case JT_VOID: os << "JT_VOID"; break; |
| 472 | case JT_BOOLEAN: os << "JT_BOOLEAN"; break; |
| 473 | case JT_STRING: os << "JT_STRING"; break; |
| 474 | case JT_THREAD: os << "JT_THREAD"; break; |
| 475 | case JT_THREAD_GROUP: os << "JT_THREAD_GROUP"; break; |
| 476 | case JT_CLASS_LOADER: os << "JT_CLASS_LOADER"; break; |
| 477 | case JT_CLASS_OBJECT: os << "JT_CLASS_OBJECT"; break; |
| 478 | default: |
| 479 | os << "JdwpTag[" << static_cast<int32_t>(value) << "]"; |
| 480 | } |
| 481 | return os; |
| 482 | } |
| 483 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 484 | std::ostream& operator<<(std::ostream& os, const JdwpTypeTag& value) { |
| 485 | switch (value) { |
| 486 | case TT_CLASS: os << "TT_CLASS"; break; |
| 487 | case TT_INTERFACE: os << "TT_INTERFACE"; break; |
| 488 | case TT_ARRAY: os << "TT_ARRAY"; break; |
| 489 | default: |
| 490 | os << "JdwpTypeTag[" << static_cast<int32_t>(value) << "]"; |
| 491 | } |
| 492 | return os; |
| 493 | } |
| 494 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 495 | } // namespace JDWP |
| 496 | |
| 497 | } // namespace art |