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