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" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/time_utils.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 26 | #include "debugger.h" |
| 27 | #include "jdwp/jdwp_priv.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | namespace JDWP { |
| 33 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 34 | static void* StartJdwpThread(void* arg); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * JdwpNetStateBase class implementation |
| 38 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 39 | JdwpNetStateBase::JdwpNetStateBase(JdwpState* state) |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 40 | : state_(state), socket_lock_("JdwpNetStateBase lock", kJdwpSocketLock) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 41 | clientSock = -1; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 42 | wake_pipe_[0] = -1; |
| 43 | wake_pipe_[1] = -1; |
| 44 | input_count_ = 0; |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 45 | awaiting_handshake_ = false; |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 48 | JdwpNetStateBase::~JdwpNetStateBase() { |
| 49 | if (wake_pipe_[0] != -1) { |
| 50 | close(wake_pipe_[0]); |
| 51 | wake_pipe_[0] = -1; |
| 52 | } |
| 53 | if (wake_pipe_[1] != -1) { |
| 54 | close(wake_pipe_[1]); |
| 55 | wake_pipe_[1] = -1; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool JdwpNetStateBase::MakePipe() { |
| 60 | if (pipe(wake_pipe_) == -1) { |
| 61 | PLOG(ERROR) << "pipe failed"; |
| 62 | return false; |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void JdwpNetStateBase::WakePipe() { |
| 68 | // If we might be sitting in select, kick us loose. |
| 69 | if (wake_pipe_[1] != -1) { |
| 70 | VLOG(jdwp) << "+++ writing to wake pipe"; |
| 71 | TEMP_FAILURE_RETRY(write(wake_pipe_[1], "", 1)); |
| 72 | } |
| 73 | } |
| 74 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 75 | void JdwpNetStateBase::ConsumeBytes(size_t count) { |
| 76 | CHECK_GT(count, 0U); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 77 | CHECK_LE(count, input_count_); |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 79 | if (count == input_count_) { |
| 80 | input_count_ = 0; |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 84 | memmove(input_buffer_, input_buffer_ + count, input_count_ - count); |
| 85 | input_count_ -= count; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 88 | bool JdwpNetStateBase::HaveFullPacket() { |
| 89 | if (awaiting_handshake_) { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 90 | return (input_count_ >= kMagicHandshakeLen); |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 91 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 92 | if (input_count_ < 4) { |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 95 | uint32_t length = Get4BE(input_buffer_); |
| 96 | return (input_count_ >= length); |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool JdwpNetStateBase::IsAwaitingHandshake() { |
| 100 | return awaiting_handshake_; |
| 101 | } |
| 102 | |
| 103 | void JdwpNetStateBase::SetAwaitingHandshake(bool new_state) { |
| 104 | awaiting_handshake_ = new_state; |
| 105 | } |
| 106 | |
| 107 | bool JdwpNetStateBase::IsConnected() { |
| 108 | return clientSock >= 0; |
| 109 | } |
| 110 | |
| 111 | // Close a connection from a debugger (which may have already dropped us). |
| 112 | // Resets the state so we're ready to receive a new connection. |
| 113 | // Only called from the JDWP thread. |
| 114 | void JdwpNetStateBase::Close() { |
| 115 | if (clientSock < 0) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | VLOG(jdwp) << "+++ closing JDWP connection on fd " << clientSock; |
| 120 | |
| 121 | close(clientSock); |
| 122 | clientSock = -1; |
| 123 | } |
| 124 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 125 | /* |
Sebastien Hertz | 43c8d72 | 2014-03-18 12:19:52 +0100 | [diff] [blame] | 126 | * Write a packet of "length" bytes. Grabs a mutex to assure atomicity. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 127 | */ |
Sebastien Hertz | 43c8d72 | 2014-03-18 12:19:52 +0100 | [diff] [blame] | 128 | ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply, size_t length) { |
Sebastien Hertz | 43c8d72 | 2014-03-18 12:19:52 +0100 | [diff] [blame] | 129 | DCHECK_LE(length, expandBufGetLength(pReply)); |
Sebastien Hertz | 738c4c9 | 2015-09-02 14:30:13 +0200 | [diff] [blame] | 130 | if (!IsConnected()) { |
| 131 | LOG(WARNING) << "Connection with debugger is closed"; |
| 132 | return -1; |
| 133 | } |
| 134 | MutexLock mu(Thread::Current(), socket_lock_); |
Sebastien Hertz | 43c8d72 | 2014-03-18 12:19:52 +0100 | [diff] [blame] | 135 | return TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply), length)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Write a buffered packet. Grabs a mutex to assure atomicity. |
| 140 | */ |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 141 | ssize_t JdwpNetStateBase::WriteBufferedPacket(const std::vector<iovec>& iov) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 142 | MutexLock mu(Thread::Current(), socket_lock_); |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 143 | return WriteBufferedPacketLocked(iov); |
| 144 | } |
| 145 | |
| 146 | ssize_t JdwpNetStateBase::WriteBufferedPacketLocked(const std::vector<iovec>& iov) { |
| 147 | socket_lock_.AssertHeld(Thread::Current()); |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 148 | DCHECK(IsConnected()) << "Connection with debugger is closed"; |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 149 | return TEMP_FAILURE_RETRY(writev(clientSock, &iov[0], iov.size())); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 152 | bool JdwpState::IsConnected() { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 153 | return netState != nullptr && netState->IsConnected(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 156 | void JdwpState::SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov) { |
Sebastien Hertz | 60ed7da | 2014-08-28 18:50:36 +0200 | [diff] [blame] | 157 | if (!IsConnected()) { |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 158 | // Can happen with some DDMS events. |
| 159 | VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!"; |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | size_t expected = 0; |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 164 | for (size_t i = 0; i < iov.size(); ++i) { |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 165 | expected += iov[i].iov_len; |
| 166 | } |
| 167 | |
| 168 | errno = 0; |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 169 | ssize_t actual = netState->WriteBufferedPacket(iov); |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 170 | if (static_cast<size_t>(actual) != expected) { |
Ian Rogers | d9e4e0c | 2014-01-23 20:11:40 -0800 | [diff] [blame] | 171 | PLOG(ERROR) << StringPrintf("Failed to send JDWP packet %c%c%c%c to debugger (%zd of %zu)", |
| 172 | static_cast<char>(type >> 24), |
| 173 | static_cast<char>(type >> 16), |
| 174 | static_cast<char>(type >> 8), |
| 175 | static_cast<char>(type), |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 176 | actual, expected); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void JdwpState::SendRequest(ExpandBuf* pReq) { |
Sebastien Hertz | 60ed7da | 2014-08-28 18:50:36 +0200 | [diff] [blame] | 181 | if (!IsConnected()) { |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 182 | // Can happen with some DDMS events. |
| 183 | VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!"; |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | errno = 0; |
Sebastien Hertz | 43c8d72 | 2014-03-18 12:19:52 +0100 | [diff] [blame] | 188 | ssize_t actual = netState->WritePacket(pReq, expandBufGetLength(pReq)); |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 189 | if (static_cast<size_t>(actual) != expandBufGetLength(pReq)) { |
Ian Rogers | d9e4e0c | 2014-01-23 20:11:40 -0800 | [diff] [blame] | 190 | PLOG(ERROR) << StringPrintf("Failed to send JDWP packet to debugger (%zd of %zu)", |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 191 | actual, expandBufGetLength(pReq)); |
| 192 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 195 | /* |
| 196 | * Get the next "request" serial number. We use this when sending |
| 197 | * packets to the debugger. |
| 198 | */ |
| 199 | uint32_t JdwpState::NextRequestSerial() { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 200 | return request_serial_++; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 203 | /* |
| 204 | * Get the next "event" serial number. We use this in the response to |
| 205 | * message type EventRequest.Set. |
| 206 | */ |
| 207 | uint32_t JdwpState::NextEventSerial() { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 208 | return event_serial_++; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 211 | JdwpState::JdwpState(const JdwpOptions* options) |
| 212 | : options_(options), |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 213 | thread_start_lock_("JDWP thread start lock", kJdwpStartLock), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 214 | thread_start_cond_("JDWP thread start condition variable", thread_start_lock_), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 215 | pthread_(0), |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 216 | thread_(nullptr), |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 217 | debug_thread_started_(false), |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 218 | debug_thread_id_(0), |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 219 | run(false), |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 220 | netState(nullptr), |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 221 | attach_lock_("JDWP attach lock", kJdwpAttachLock), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 222 | attach_cond_("JDWP attach condition variable", attach_lock_), |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 223 | last_activity_time_ms_(0), |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 224 | request_serial_(0x10000000), |
| 225 | event_serial_(0x20000000), |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 226 | event_list_lock_("JDWP event list lock", kJdwpEventListLock), |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 227 | event_list_(nullptr), |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 228 | event_list_size_(0), |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 229 | jdwp_token_lock_("JDWP token lock"), |
| 230 | jdwp_token_cond_("JDWP token condition variable", jdwp_token_lock_), |
| 231 | jdwp_token_owner_thread_id_(0), |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 232 | ddm_is_active_(false), |
| 233 | should_exit_(false), |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 234 | exit_status_(0), |
| 235 | shutdown_lock_("JDWP shutdown lock", kJdwpShutdownLock), |
| 236 | shutdown_cond_("JDWP shutdown condition variable", shutdown_lock_), |
| 237 | processing_request_(false) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Initialize JDWP. |
| 242 | * |
| 243 | * Does not return until JDWP thread is running, but may return before |
| 244 | * the thread is accepting network connections. |
| 245 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 246 | JdwpState* JdwpState::Create(const JdwpOptions* options) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 247 | Thread* self = Thread::Current(); |
| 248 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 249 | std::unique_ptr<JdwpState> state(new JdwpState(options)); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 250 | switch (options->transport) { |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 251 | case kJdwpTransportSocket: |
| 252 | InitSocketTransport(state.get(), options); |
| 253 | break; |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 254 | #ifdef ART_TARGET_ANDROID |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 255 | case kJdwpTransportAndroidAdb: |
| 256 | InitAdbTransport(state.get(), options); |
| 257 | break; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 258 | #endif |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 259 | default: |
| 260 | LOG(FATAL) << "Unknown transport: " << options->transport; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 261 | } |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 262 | { |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 263 | /* |
| 264 | * Grab a mutex before starting the thread. This ensures they |
| 265 | * won't signal the cond var before we're waiting. |
| 266 | */ |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 267 | state->thread_start_lock_.AssertNotHeld(self); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 268 | MutexLock thread_start_locker(self, state->thread_start_lock_); |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 269 | |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 270 | /* |
| 271 | * We have bound to a port, or are trying to connect outbound to a |
| 272 | * debugger. Create the JDWP thread and let it continue the mission. |
| 273 | */ |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 274 | CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, nullptr, StartJdwpThread, state.get()), |
| 275 | "JDWP thread"); |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * Wait until the thread finishes basic initialization. |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 279 | */ |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 280 | while (!state->debug_thread_started_) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 281 | state->thread_start_cond_.Wait(self); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 282 | } |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 283 | } |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 284 | |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 285 | if (options->suspend) { |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 286 | /* |
| 287 | * For suspend=y, wait for the debugger to connect to us or for us to |
| 288 | * connect to the debugger. |
| 289 | * |
| 290 | * The JDWP thread will signal us when it connects successfully or |
| 291 | * times out (for timeout=xxx), so we have to check to see what happened |
| 292 | * when we wake up. |
| 293 | */ |
| 294 | { |
| 295 | ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach); |
| 296 | MutexLock attach_locker(self, state->attach_lock_); |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 297 | while (state->debug_thread_id_ == 0) { |
| 298 | state->attach_cond_.Wait(self); |
| 299 | } |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 300 | } |
| 301 | if (!state->IsActive()) { |
| 302 | LOG(ERROR) << "JDWP connection failed"; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 303 | return nullptr; |
Jeff Hao | add037d | 2013-07-15 14:24:14 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | LOG(INFO) << "JDWP connected"; |
| 307 | |
| 308 | /* |
| 309 | * Ordinarily we would pause briefly to allow the debugger to set |
| 310 | * breakpoints and so on, but for "suspend=y" the VM init code will |
| 311 | * pause the VM when it sends the VM_START message. |
| 312 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 315 | return state.release(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Reset all session-related state. There should not be an active connection |
| 320 | * to the client at this point. The rest of the VM still thinks there is |
| 321 | * a debugger attached. |
| 322 | * |
| 323 | * This includes freeing up the debugger event list. |
| 324 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 325 | void JdwpState::ResetState() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 326 | /* could reset the serial numbers, but no need to */ |
| 327 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 328 | UnregisterAll(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 329 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 330 | MutexLock mu(Thread::Current(), event_list_lock_); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 331 | CHECK(event_list_ == nullptr); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 332 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * Should not have one of these in progress. If the debugger went away |
| 336 | * mid-request, though, we could see this. |
| 337 | */ |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 338 | if (jdwp_token_owner_thread_id_ != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 339 | LOG(WARNING) << "Resetting state while event in progress"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 340 | DCHECK(false); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Tell the JDWP thread to shut down. Frees "state". |
| 346 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 347 | JdwpState::~JdwpState() { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 348 | if (netState != nullptr) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 349 | /* |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 350 | * Close down the network to inspire the thread to halt. If a request is being processed, |
| 351 | * we need to wait for it to finish first. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 352 | */ |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 353 | { |
| 354 | Thread* self = Thread::Current(); |
| 355 | MutexLock mu(self, shutdown_lock_); |
| 356 | while (processing_request_) { |
| 357 | VLOG(jdwp) << "JDWP command in progress: wait for it to finish ..."; |
| 358 | shutdown_cond_.Wait(self); |
| 359 | } |
| 360 | |
| 361 | VLOG(jdwp) << "JDWP shutting down net..."; |
| 362 | netState->Shutdown(); |
| 363 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 364 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 365 | if (debug_thread_started_) { |
| 366 | run = false; |
| 367 | void* threadReturn; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 368 | if (pthread_join(pthread_, &threadReturn) != 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 369 | LOG(WARNING) << "JDWP thread join failed"; |
| 370 | } |
| 371 | } |
| 372 | |
Elliott Hughes | 0cc1bbd | 2012-01-12 12:27:08 -0800 | [diff] [blame] | 373 | VLOG(jdwp) << "JDWP freeing netstate..."; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 374 | delete netState; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 375 | netState = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 376 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 377 | CHECK(netState == nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 378 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 379 | ResetState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Are we talking to a debugger? |
| 384 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 385 | bool JdwpState::IsActive() { |
| 386 | return IsConnected(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 389 | // Returns "false" if we encounter a connection-fatal error. |
| 390 | bool JdwpState::HandlePacket() { |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 391 | Thread* const self = Thread::Current(); |
| 392 | { |
| 393 | MutexLock mu(self, shutdown_lock_); |
| 394 | processing_request_ = true; |
| 395 | } |
| 396 | JdwpNetStateBase* netStateBase = netState; |
| 397 | CHECK(netStateBase != nullptr) << "Connection has been closed"; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 398 | JDWP::Request request(netStateBase->input_buffer_, netStateBase->input_count_); |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 399 | |
| 400 | ExpandBuf* pReply = expandBufAlloc(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 401 | bool skip_reply = false; |
| 402 | size_t replyLength = ProcessRequest(&request, pReply, &skip_reply); |
| 403 | ssize_t cc = 0; |
| 404 | if (!skip_reply) { |
| 405 | cc = netStateBase->WritePacket(pReply, replyLength); |
| 406 | } else { |
| 407 | DCHECK_EQ(replyLength, 0U); |
| 408 | } |
| 409 | expandBufFree(pReply); |
Sebastien Hertz | 400a3a9 | 2014-02-24 14:56:21 +0100 | [diff] [blame] | 410 | |
| 411 | /* |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 412 | * We processed this request and sent its reply so we can release the JDWP token. |
Sebastien Hertz | 400a3a9 | 2014-02-24 14:56:21 +0100 | [diff] [blame] | 413 | */ |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 414 | ReleaseJdwpTokenForCommand(); |
Sebastien Hertz | 99660e1 | 2014-02-19 15:04:42 +0100 | [diff] [blame] | 415 | |
Sebastien Hertz | 43c8d72 | 2014-03-18 12:19:52 +0100 | [diff] [blame] | 416 | if (cc != static_cast<ssize_t>(replyLength)) { |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 417 | PLOG(ERROR) << "Failed sending reply to debugger"; |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 418 | return false; |
| 419 | } |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 420 | netStateBase->ConsumeBytes(request.GetLength()); |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 421 | { |
| 422 | MutexLock mu(self, shutdown_lock_); |
| 423 | processing_request_ = false; |
| 424 | shutdown_cond_.Broadcast(self); |
| 425 | } |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 429 | /* |
| 430 | * Entry point for JDWP thread. The thread was created through the VM |
| 431 | * mechanisms, so there is a java/lang/Thread associated with us. |
| 432 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 433 | static void* StartJdwpThread(void* arg) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 434 | JdwpState* state = reinterpret_cast<JdwpState*>(arg); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 435 | CHECK(state != nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 436 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 437 | state->Run(); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 438 | return nullptr; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void JdwpState::Run() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 442 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 443 | CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(), |
Mathieu Chartier | 184c9dc | 2015-03-05 13:20:54 -0800 | [diff] [blame] | 444 | !runtime->IsAotCompiler())); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 445 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 446 | VLOG(jdwp) << "JDWP: thread running"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 447 | |
| 448 | /* |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 449 | * Finish initializing, then notify the creating thread that |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 450 | * we're running. |
| 451 | */ |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 452 | thread_ = Thread::Current(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 453 | run = true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 454 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 455 | { |
| 456 | MutexLock locker(thread_, thread_start_lock_); |
| 457 | debug_thread_started_ = true; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 458 | thread_start_cond_.Broadcast(thread_); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 459 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 460 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 461 | /* 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] | 462 | CHECK_EQ(thread_->GetState(), kNative); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 463 | Locks::mutator_lock_->AssertNotHeld(thread_); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 464 | thread_->SetState(kWaitingInMainDebuggerLoop); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 465 | |
| 466 | /* |
| 467 | * Loop forever if we're in server mode, processing connections. In |
| 468 | * non-server mode, we bail out of the thread when the debugger drops |
| 469 | * us. |
| 470 | * |
| 471 | * We broadcast a notification when a debugger attaches, after we |
| 472 | * successfully process the handshake. |
| 473 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 474 | while (run) { |
| 475 | if (options_->server) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 476 | /* |
| 477 | * Block forever, waiting for a connection. To support the |
| 478 | * "timeout=xxx" option we'll need to tweak this. |
| 479 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 480 | if (!netState->Accept()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | } else { |
| 484 | /* |
| 485 | * If we're not acting as a server, we need to connect out to the |
| 486 | * debugger. To support the "timeout=xxx" option we need to |
| 487 | * have a timeout if the handshake reply isn't received in a |
| 488 | * reasonable amount of time. |
| 489 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 490 | if (!netState->Establish(options_)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 491 | /* wake anybody who was waiting for us to succeed */ |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 492 | MutexLock mu(thread_, attach_lock_); |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 493 | debug_thread_id_ = static_cast<ObjectId>(-1); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 494 | attach_cond_.Broadcast(thread_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* prep debug code to handle the new connection */ |
| 500 | Dbg::Connected(); |
| 501 | |
| 502 | /* process requests until the debugger drops */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 503 | bool first = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 504 | while (!Dbg::IsDisposed()) { |
Sebastien Hertz | bb5c355 | 2014-04-14 14:38:24 +0200 | [diff] [blame] | 505 | // sanity check -- shouldn't happen? |
| 506 | CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 507 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 508 | if (!netState->ProcessIncoming()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 509 | /* blocking read */ |
| 510 | break; |
| 511 | } |
| 512 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 513 | if (should_exit_) { |
| 514 | exit(exit_status_); |
| 515 | } |
| 516 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 517 | if (first && !netState->IsAwaitingHandshake()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 518 | /* handshake worked, tell the interpreter that we're active */ |
| 519 | first = false; |
| 520 | |
| 521 | /* set thread ID; requires object registry to be active */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 522 | { |
| 523 | ScopedObjectAccess soa(thread_); |
| 524 | debug_thread_id_ = Dbg::GetThreadSelfId(); |
| 525 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 526 | |
| 527 | /* wake anybody who's waiting for us */ |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 528 | MutexLock mu(thread_, attach_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 529 | attach_cond_.Broadcast(thread_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 533 | netState->Close(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 534 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 535 | if (ddm_is_active_) { |
| 536 | ddm_is_active_ = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 537 | |
| 538 | /* broadcast the disconnect; must be in RUNNING state */ |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 539 | ScopedObjectAccess soa(thread_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 540 | Dbg::DdmDisconnected(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 543 | { |
| 544 | ScopedObjectAccess soa(thread_); |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 545 | |
| 546 | // Release session state, e.g. remove breakpoint instructions. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 547 | ResetState(); |
| 548 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 549 | // Tell the rest of the runtime that the debugger is no longer around. |
| 550 | Dbg::Disconnected(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 551 | |
| 552 | /* if we had threads suspended, resume them now */ |
| 553 | Dbg::UndoDebuggerSuspensions(); |
| 554 | |
| 555 | /* if we connected out, this was a one-shot deal */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 556 | if (!options_->server) { |
| 557 | run = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 561 | /* back to native, for thread shutdown */ |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 562 | CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop); |
| 563 | thread_->SetState(kNative); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 564 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 565 | VLOG(jdwp) << "JDWP: thread detaching and exiting..."; |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 566 | runtime->DetachCurrentThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 569 | void JdwpState::NotifyDdmsActive() { |
| 570 | if (!ddm_is_active_) { |
| 571 | ddm_is_active_ = true; |
| 572 | Dbg::DdmConnected(); |
| 573 | } |
| 574 | } |
| 575 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 576 | Thread* JdwpState::GetDebugThread() { |
| 577 | return thread_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Support routines for waitForDebugger(). |
| 582 | * |
| 583 | * We can't have a trivial "waitForDebugger" function that returns the |
| 584 | * instant the debugger connects, because we run the risk of executing code |
| 585 | * before the debugger has had a chance to configure breakpoints or issue |
| 586 | * suspend calls. It would be nice to just sit in the suspended state, but |
| 587 | * most debuggers don't expect any threads to be suspended when they attach. |
| 588 | * |
| 589 | * There's no JDWP event we can post to tell the debugger, "we've stopped, |
| 590 | * and we like it that way". We could send a fake breakpoint, which should |
| 591 | * cause the debugger to immediately send a resume, but the debugger might |
| 592 | * send the resume immediately or might throw an exception of its own upon |
| 593 | * receiving a breakpoint event that it didn't ask for. |
| 594 | * |
| 595 | * What we really want is a "wait until the debugger is done configuring |
| 596 | * stuff" event. We can approximate this with a "wait until the debugger |
| 597 | * has been idle for a brief period". |
| 598 | */ |
| 599 | |
| 600 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 601 | * Return the time, in milliseconds, since the last debugger activity. |
| 602 | * |
| 603 | * Returns -1 if no debugger is attached, or 0 if we're in the middle of |
| 604 | * processing a debugger request. |
| 605 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 606 | int64_t JdwpState::LastDebuggerActivity() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 607 | if (!Dbg::IsDebuggerActive()) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 608 | LOG(WARNING) << "no active debugger"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 609 | return -1; |
| 610 | } |
| 611 | |
Ian Rogers | 37f3c96 | 2014-07-17 11:25:30 -0700 | [diff] [blame] | 612 | int64_t last = last_activity_time_ms_.LoadSequentiallyConsistent(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 613 | |
| 614 | /* initializing or in the middle of something? */ |
| 615 | if (last == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 616 | VLOG(jdwp) << "+++ last=busy"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | /* now get the current time */ |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 621 | int64_t now = MilliTime(); |
Elliott Hughes | c3b3e75 | 2012-01-27 13:48:50 -0800 | [diff] [blame] | 622 | CHECK_GE(now, last); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 623 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 624 | VLOG(jdwp) << "+++ debugger interval=" << (now - last); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 625 | return now - last; |
| 626 | } |
| 627 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 628 | void JdwpState::ExitAfterReplying(int exit_status) { |
| 629 | LOG(WARNING) << "Debugger told VM to exit with status " << exit_status; |
| 630 | should_exit_ = true; |
| 631 | exit_status_ = exit_status; |
| 632 | } |
| 633 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 634 | std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 635 | os << "JdwpLocation[" |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 636 | << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id) |
Ian Rogers | d9e4e0c | 2014-01-23 20:11:40 -0800 | [diff] [blame] | 637 | << "@" << StringPrintf("%#" PRIx64, rhs.dex_pc) << " " << rhs.type_tag << "]"; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 638 | return os; |
| 639 | } |
| 640 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 641 | bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 642 | return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id && |
| 643 | lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag; |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) { |
| 647 | return !(lhs == rhs); |
| 648 | } |
| 649 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 650 | bool operator==(const JdwpOptions& lhs, const JdwpOptions& rhs) { |
| 651 | if (&lhs == &rhs) { |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | return lhs.transport == rhs.transport && |
| 656 | lhs.server == rhs.server && |
| 657 | lhs.suspend == rhs.suspend && |
| 658 | lhs.host == rhs.host && |
| 659 | lhs.port == rhs.port; |
| 660 | } |
| 661 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 662 | } // namespace JDWP |
| 663 | |
| 664 | } // namespace art |