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