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