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