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