blob: 4155c8269988121d6b74cb314fbc1ac87781d184 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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 Hughes07ed66b2012-12-12 18:34:25 -080017#include <errno.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070018#include <stdlib.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070019#include <sys/time.h>
20#include <time.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#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 Hughes872d4ec2011-10-21 17:07:15 -070028
29namespace art {
30
31namespace JDWP {
32
Elliott Hughes376a7a02011-10-24 18:35:55 -070033static void* StartJdwpThread(void* arg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070034
35/*
36 * JdwpNetStateBase class implementation
37 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070038JdwpNetStateBase::JdwpNetStateBase(JdwpState* state)
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070039 : state_(state), socket_lock_("JdwpNetStateBase lock", kJdwpSocketLock) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040 clientSock = -1;
Elliott Hughes5d10a872013-04-17 19:26:43 -070041 wake_pipe_[0] = -1;
42 wake_pipe_[1] = -1;
43 input_count_ = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070044 awaiting_handshake_ = false;
Elliott Hughescb693062013-02-21 09:48:08 -080045}
46
Elliott Hughes5d10a872013-04-17 19:26:43 -070047JdwpNetStateBase::~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
58bool JdwpNetStateBase::MakePipe() {
59 if (pipe(wake_pipe_) == -1) {
60 PLOG(ERROR) << "pipe failed";
61 return false;
62 }
63 return true;
64}
65
66void 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 Hughescb693062013-02-21 09:48:08 -080074void JdwpNetStateBase::ConsumeBytes(size_t count) {
75 CHECK_GT(count, 0U);
Elliott Hughes5d10a872013-04-17 19:26:43 -070076 CHECK_LE(count, input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -080077
Elliott Hughes5d10a872013-04-17 19:26:43 -070078 if (count == input_count_) {
79 input_count_ = 0;
Elliott Hughescb693062013-02-21 09:48:08 -080080 return;
81 }
82
Elliott Hughes5d10a872013-04-17 19:26:43 -070083 memmove(input_buffer_, input_buffer_ + count, input_count_ - count);
84 input_count_ -= count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070085}
86
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070087bool JdwpNetStateBase::HaveFullPacket() {
88 if (awaiting_handshake_) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070089 return (input_count_ >= kMagicHandshakeLen);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070090 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070091 if (input_count_ < 4) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070092 return false;
93 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070094 uint32_t length = Get4BE(input_buffer_);
95 return (input_count_ >= length);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070096}
97
98bool JdwpNetStateBase::IsAwaitingHandshake() {
99 return awaiting_handshake_;
100}
101
102void JdwpNetStateBase::SetAwaitingHandshake(bool new_state) {
103 awaiting_handshake_ = new_state;
104}
105
106bool 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.
113void 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 Hughes872d4ec2011-10-21 17:07:15 -0700124/*
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100125 * Write a packet of "length" bytes. Grabs a mutex to assure atomicity.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 */
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100127ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply, size_t length) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700128 MutexLock mu(Thread::Current(), socket_lock_);
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100129 DCHECK_LE(length, expandBufGetLength(pReply));
130 return TEMP_FAILURE_RETRY(write(clientSock, expandBufGetBuffer(pReply), length));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131}
132
133/*
134 * Write a buffered packet. Grabs a mutex to assure atomicity.
135 */
Brian Carlstromf5293522013-07-19 00:24:00 -0700136ssize_t JdwpNetStateBase::WriteBufferedPacket(const std::vector<iovec>& iov) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700137 MutexLock mu(Thread::Current(), socket_lock_);
Brian Carlstromf5293522013-07-19 00:24:00 -0700138 return TEMP_FAILURE_RETRY(writev(clientSock, &iov[0], iov.size()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139}
140
Elliott Hughes376a7a02011-10-24 18:35:55 -0700141bool JdwpState::IsConnected() {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700142 return netState != NULL && netState->IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700143}
144
Brian Carlstromf5293522013-07-19 00:24:00 -0700145void JdwpState::SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700146 if (netState->clientSock < 0) {
147 // Can happen with some DDMS events.
148 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
149 return;
150 }
151
152 size_t expected = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700153 for (size_t i = 0; i < iov.size(); ++i) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700154 expected += iov[i].iov_len;
155 }
156
157 errno = 0;
Brian Carlstromf5293522013-07-19 00:24:00 -0700158 ssize_t actual = netState->WriteBufferedPacket(iov);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700159 if (static_cast<size_t>(actual) != expected) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800160 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet %c%c%c%c to debugger (%zd of %zu)",
161 static_cast<char>(type >> 24),
162 static_cast<char>(type >> 16),
163 static_cast<char>(type >> 8),
164 static_cast<char>(type),
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700165 actual, expected);
166 }
167}
168
169void JdwpState::SendRequest(ExpandBuf* pReq) {
170 if (netState->clientSock < 0) {
171 // Can happen with some DDMS events.
172 VLOG(jdwp) << "Not sending JDWP packet: no debugger attached!";
173 return;
174 }
175
176 errno = 0;
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100177 ssize_t actual = netState->WritePacket(pReq, expandBufGetLength(pReq));
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700178 if (static_cast<size_t>(actual) != expandBufGetLength(pReq)) {
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800179 PLOG(ERROR) << StringPrintf("Failed to send JDWP packet to debugger (%zd of %zu)",
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700180 actual, expandBufGetLength(pReq));
181 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182}
183
Elliott Hughes376a7a02011-10-24 18:35:55 -0700184/*
185 * Get the next "request" serial number. We use this when sending
186 * packets to the debugger.
187 */
188uint32_t JdwpState::NextRequestSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700189 return request_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190}
191
Elliott Hughes376a7a02011-10-24 18:35:55 -0700192/*
193 * Get the next "event" serial number. We use this in the response to
194 * message type EventRequest.Set.
195 */
196uint32_t JdwpState::NextEventSerial() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700197 return event_serial_++;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198}
199
Elliott Hughes376a7a02011-10-24 18:35:55 -0700200JdwpState::JdwpState(const JdwpOptions* options)
201 : options_(options),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800202 thread_start_lock_("JDWP thread start lock", kJdwpStartLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700203 thread_start_cond_("JDWP thread start condition variable", thread_start_lock_),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 pthread_(0),
205 thread_(NULL),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206 debug_thread_started_(false),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700207 debug_thread_id_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208 run(false),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 netState(NULL),
jeffhao0dfbb7e2012-11-28 15:26:03 -0800210 attach_lock_("JDWP attach lock", kJdwpAttachLock),
Ian Rogersc604d732012-10-14 16:09:54 -0700211 attach_cond_("JDWP attach condition variable", attach_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700212 last_activity_time_ms_(0),
Elliott Hughesf8349362012-06-18 15:00:06 -0700213 request_serial_(0x10000000),
214 event_serial_(0x20000000),
jeffhaoa77f0f62012-12-05 17:19:31 -0800215 event_list_lock_("JDWP event list lock", kJdwpEventListLock),
Elliott Hughesf8349362012-06-18 15:00:06 -0700216 event_list_(NULL),
217 event_list_size_(0),
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218 event_thread_lock_("JDWP event thread lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700219 event_thread_cond_("JDWP event thread condition variable", event_thread_lock_),
Elliott Hughesa21039c2012-06-21 12:09:25 -0700220 event_thread_id_(0),
Sebastien Hertz99660e12014-02-19 15:04:42 +0100221 process_request_lock_("JDWP process request lock"),
222 process_request_cond_("JDWP process request condition variable", process_request_lock_),
223 processing_request_(false),
Elliott Hughes64f574f2013-02-20 14:57:12 -0800224 ddm_is_active_(false),
225 should_exit_(false),
226 exit_status_(0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227}
228
229/*
230 * Initialize JDWP.
231 *
232 * Does not return until JDWP thread is running, but may return before
233 * the thread is accepting network connections.
234 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700235JdwpState* JdwpState::Create(const JdwpOptions* options) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700236 Thread* self = Thread::Current();
237 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700238 std::unique_ptr<JdwpState> state(new JdwpState(options));
Elliott Hughes376a7a02011-10-24 18:35:55 -0700239 switch (options->transport) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200240 case kJdwpTransportSocket:
241 InitSocketTransport(state.get(), options);
242 break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243#ifdef HAVE_ANDROID_OS
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200244 case kJdwpTransportAndroidAdb:
245 InitAdbTransport(state.get(), options);
246 break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247#endif
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200248 default:
249 LOG(FATAL) << "Unknown transport: " << options->transport;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700250 }
251
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200252 {
Jeff Haoadd037d2013-07-15 14:24:14 -0700253 /*
254 * Grab a mutex before starting the thread. This ensures they
255 * won't signal the cond var before we're waiting.
256 */
Ian Rogers50b35e22012-10-04 10:09:15 -0700257 MutexLock thread_start_locker(self, state->thread_start_lock_);
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200258
Jeff Haoadd037d2013-07-15 14:24:14 -0700259 /*
260 * We have bound to a port, or are trying to connect outbound to a
261 * debugger. Create the JDWP thread and let it continue the mission.
262 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200263 CHECK_PTHREAD_CALL(pthread_create, (&state->pthread_, nullptr, StartJdwpThread, state.get()),
264 "JDWP thread");
Jeff Haoadd037d2013-07-15 14:24:14 -0700265
266 /*
267 * Wait until the thread finishes basic initialization.
Jeff Haoadd037d2013-07-15 14:24:14 -0700268 */
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200269 while (!state->debug_thread_started_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700270 state->thread_start_cond_.Wait(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700271 }
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200272 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700273
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200274 if (options->suspend) {
Jeff Haoadd037d2013-07-15 14:24:14 -0700275 /*
276 * For suspend=y, wait for the debugger to connect to us or for us to
277 * connect to the debugger.
278 *
279 * The JDWP thread will signal us when it connects successfully or
280 * times out (for timeout=xxx), so we have to check to see what happened
281 * when we wake up.
282 */
283 {
284 ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
285 MutexLock attach_locker(self, state->attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200286 while (state->debug_thread_id_ == 0) {
287 state->attach_cond_.Wait(self);
288 }
Jeff Haoadd037d2013-07-15 14:24:14 -0700289 }
290 if (!state->IsActive()) {
291 LOG(ERROR) << "JDWP connection failed";
292 return NULL;
293 }
294
295 LOG(INFO) << "JDWP connected";
296
297 /*
298 * Ordinarily we would pause briefly to allow the debugger to set
299 * breakpoints and so on, but for "suspend=y" the VM init code will
300 * pause the VM when it sends the VM_START message.
301 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700302 }
303
Elliott Hughes761928d2011-11-16 18:33:03 -0800304 return state.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700305}
306
307/*
308 * Reset all session-related state. There should not be an active connection
309 * to the client at this point. The rest of the VM still thinks there is
310 * a debugger attached.
311 *
312 * This includes freeing up the debugger event list.
313 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700314void JdwpState::ResetState() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700315 /* could reset the serial numbers, but no need to */
316
Elliott Hughes761928d2011-11-16 18:33:03 -0800317 UnregisterAll();
Elliott Hughesf8349362012-06-18 15:00:06 -0700318 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700319 MutexLock mu(Thread::Current(), event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700320 CHECK(event_list_ == NULL);
321 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700322
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100323 Dbg::ProcessDelayedFullUndeoptimizations();
324
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325 /*
326 * Should not have one of these in progress. If the debugger went away
327 * mid-request, though, we could see this.
328 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700329 if (event_thread_id_ != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800330 LOG(WARNING) << "Resetting state while event in progress";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331 DCHECK(false);
332 }
333}
334
335/*
336 * Tell the JDWP thread to shut down. Frees "state".
337 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700338JdwpState::~JdwpState() {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700339 if (netState != NULL) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700340 /*
341 * Close down the network to inspire the thread to halt.
342 */
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800343 VLOG(jdwp) << "JDWP shutting down net...";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700344 netState->Shutdown();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700345
Elliott Hughes376a7a02011-10-24 18:35:55 -0700346 if (debug_thread_started_) {
347 run = false;
348 void* threadReturn;
Elliott Hughes475fc232011-10-25 15:00:35 -0700349 if (pthread_join(pthread_, &threadReturn) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700350 LOG(WARNING) << "JDWP thread join failed";
351 }
352 }
353
Elliott Hughes0cc1bbd2012-01-12 12:27:08 -0800354 VLOG(jdwp) << "JDWP freeing netstate...";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700355 delete netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700356 netState = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700357 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700358 CHECK(netState == NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359
Elliott Hughes376a7a02011-10-24 18:35:55 -0700360 ResetState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361}
362
363/*
364 * Are we talking to a debugger?
365 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700366bool JdwpState::IsActive() {
367 return IsConnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368}
369
Elliott Hughescb693062013-02-21 09:48:08 -0800370// Returns "false" if we encounter a connection-fatal error.
371bool JdwpState::HandlePacket() {
372 JdwpNetStateBase* netStateBase = reinterpret_cast<JdwpNetStateBase*>(netState);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700373 JDWP::Request request(netStateBase->input_buffer_, netStateBase->input_count_);
Elliott Hughescb693062013-02-21 09:48:08 -0800374
375 ExpandBuf* pReply = expandBufAlloc();
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100376 size_t replyLength = ProcessRequest(request, pReply);
377 ssize_t cc = netStateBase->WritePacket(pReply, replyLength);
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100378
379 /*
380 * We processed this request and sent its reply. Notify other threads waiting for us they can now
381 * send events.
382 */
Sebastien Hertz99660e12014-02-19 15:04:42 +0100383 EndProcessingRequest();
384
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100385 if (cc != static_cast<ssize_t>(replyLength)) {
Elliott Hughescb693062013-02-21 09:48:08 -0800386 PLOG(ERROR) << "Failed sending reply to debugger";
387 expandBufFree(pReply);
388 return false;
389 }
390 expandBufFree(pReply);
391 netStateBase->ConsumeBytes(request.GetLength());
392 return true;
393}
394
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700395/*
396 * Entry point for JDWP thread. The thread was created through the VM
397 * mechanisms, so there is a java/lang/Thread associated with us.
398 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700399static void* StartJdwpThread(void* arg) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700400 JdwpState* state = reinterpret_cast<JdwpState*>(arg);
401 CHECK(state != NULL);
402
Elliott Hughes376a7a02011-10-24 18:35:55 -0700403 state->Run();
404 return NULL;
405}
406
407void JdwpState::Run() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800409 CHECK(runtime->AttachCurrentThread("JDWP", true, runtime->GetSystemThreadGroup(),
410 !runtime->IsCompiler()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700411
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800412 VLOG(jdwp) << "JDWP: thread running";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700413
414 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700415 * Finish initializing, then notify the creating thread that
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416 * we're running.
417 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700418 thread_ = Thread::Current();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700419 run = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420
Ian Rogers81d425b2012-09-27 16:03:43 -0700421 {
422 MutexLock locker(thread_, thread_start_lock_);
423 debug_thread_started_ = true;
Ian Rogersc604d732012-10-14 16:09:54 -0700424 thread_start_cond_.Broadcast(thread_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700425 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700427 /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
Ian Rogers50b35e22012-10-04 10:09:15 -0700428 CHECK_EQ(thread_->GetState(), kNative);
Ian Rogers62d6c772013-02-27 08:32:07 -0800429 Locks::mutator_lock_->AssertNotHeld(thread_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700430 thread_->SetState(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700431
432 /*
433 * Loop forever if we're in server mode, processing connections. In
434 * non-server mode, we bail out of the thread when the debugger drops
435 * us.
436 *
437 * We broadcast a notification when a debugger attaches, after we
438 * successfully process the handshake.
439 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700440 while (run) {
441 if (options_->server) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442 /*
443 * Block forever, waiting for a connection. To support the
444 * "timeout=xxx" option we'll need to tweak this.
445 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700446 if (!netState->Accept()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447 break;
448 }
449 } else {
450 /*
451 * If we're not acting as a server, we need to connect out to the
452 * debugger. To support the "timeout=xxx" option we need to
453 * have a timeout if the handshake reply isn't received in a
454 * reasonable amount of time.
455 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700456 if (!netState->Establish(options_)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457 /* wake anybody who was waiting for us to succeed */
Ian Rogers50b35e22012-10-04 10:09:15 -0700458 MutexLock mu(thread_, attach_lock_);
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200459 debug_thread_id_ = static_cast<ObjectId>(-1);
Ian Rogersc604d732012-10-14 16:09:54 -0700460 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700461 break;
462 }
463 }
464
465 /* prep debug code to handle the new connection */
466 Dbg::Connected();
467
468 /* process requests until the debugger drops */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700469 bool first = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800470 while (!Dbg::IsDisposed()) {
Sebastien Hertzbb5c3552014-04-14 14:38:24 +0200471 // sanity check -- shouldn't happen?
472 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700473
Elliott Hughes5d10a872013-04-17 19:26:43 -0700474 if (!netState->ProcessIncoming()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475 /* blocking read */
476 break;
477 }
478
Elliott Hughes64f574f2013-02-20 14:57:12 -0800479 if (should_exit_) {
480 exit(exit_status_);
481 }
482
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700483 if (first && !netState->IsAwaitingHandshake()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484 /* handshake worked, tell the interpreter that we're active */
485 first = false;
486
487 /* set thread ID; requires object registry to be active */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 {
489 ScopedObjectAccess soa(thread_);
490 debug_thread_id_ = Dbg::GetThreadSelfId();
491 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492
493 /* wake anybody who's waiting for us */
Ian Rogers81d425b2012-09-27 16:03:43 -0700494 MutexLock mu(thread_, attach_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700495 attach_cond_.Broadcast(thread_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700496 }
497 }
498
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700499 netState->Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500
Elliott Hughesa21039c2012-06-21 12:09:25 -0700501 if (ddm_is_active_) {
502 ddm_is_active_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700503
504 /* broadcast the disconnect; must be in RUNNING state */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700505 thread_->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 Dbg::DdmDisconnected();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700507 thread_->TransitionFromRunnableToSuspended(kWaitingInMainDebuggerLoop);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508 }
509
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700510 {
511 ScopedObjectAccess soa(thread_);
Elliott Hughes0f827162013-02-26 12:12:58 -0800512
513 // Release session state, e.g. remove breakpoint instructions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700514 ResetState();
515 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800516 // Tell the rest of the runtime that the debugger is no longer around.
517 Dbg::Disconnected();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700518
519 /* if we had threads suspended, resume them now */
520 Dbg::UndoDebuggerSuspensions();
521
522 /* if we connected out, this was a one-shot deal */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700523 if (!options_->server) {
524 run = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525 }
526 }
527
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700528 /* back to native, for thread shutdown */
Ian Rogers81d425b2012-09-27 16:03:43 -0700529 CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
530 thread_->SetState(kNative);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800532 VLOG(jdwp) << "JDWP: thread detaching and exiting...";
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700533 runtime->DetachCurrentThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700534}
535
Elliott Hughesa21039c2012-06-21 12:09:25 -0700536void JdwpState::NotifyDdmsActive() {
537 if (!ddm_is_active_) {
538 ddm_is_active_ = true;
539 Dbg::DdmConnected();
540 }
541}
542
Elliott Hughes475fc232011-10-25 15:00:35 -0700543Thread* JdwpState::GetDebugThread() {
544 return thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545}
546
547/*
548 * Support routines for waitForDebugger().
549 *
550 * We can't have a trivial "waitForDebugger" function that returns the
551 * instant the debugger connects, because we run the risk of executing code
552 * before the debugger has had a chance to configure breakpoints or issue
553 * suspend calls. It would be nice to just sit in the suspended state, but
554 * most debuggers don't expect any threads to be suspended when they attach.
555 *
556 * There's no JDWP event we can post to tell the debugger, "we've stopped,
557 * and we like it that way". We could send a fake breakpoint, which should
558 * cause the debugger to immediately send a resume, but the debugger might
559 * send the resume immediately or might throw an exception of its own upon
560 * receiving a breakpoint event that it didn't ask for.
561 *
562 * What we really want is a "wait until the debugger is done configuring
563 * stuff" event. We can approximate this with a "wait until the debugger
564 * has been idle for a brief period".
565 */
566
567/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700568 * Return the time, in milliseconds, since the last debugger activity.
569 *
570 * Returns -1 if no debugger is attached, or 0 if we're in the middle of
571 * processing a debugger request.
572 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700573int64_t JdwpState::LastDebuggerActivity() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700574 if (!Dbg::IsDebuggerActive()) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700575 LOG(WARNING) << "no active debugger";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700576 return -1;
577 }
578
Ian Rogers37f3c962014-07-17 11:25:30 -0700579 int64_t last = last_activity_time_ms_.LoadSequentiallyConsistent();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580
581 /* initializing or in the middle of something? */
582 if (last == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800583 VLOG(jdwp) << "+++ last=busy";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700584 return 0;
585 }
586
587 /* now get the current time */
Elliott Hughes7162ad92011-10-27 14:08:42 -0700588 int64_t now = MilliTime();
Elliott Hughesc3b3e752012-01-27 13:48:50 -0800589 CHECK_GE(now, last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700590
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800591 VLOG(jdwp) << "+++ debugger interval=" << (now - last);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700592 return now - last;
593}
594
Elliott Hughes64f574f2013-02-20 14:57:12 -0800595void JdwpState::ExitAfterReplying(int exit_status) {
596 LOG(WARNING) << "Debugger told VM to exit with status " << exit_status;
597 should_exit_ = true;
598 exit_status_ = exit_status;
599}
600
Elliott Hughes03181a82011-11-17 17:22:21 -0800601std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) {
Elliott Hughesd07986f2011-12-06 18:27:45 -0800602 os << "JdwpLocation["
Elliott Hughesa96836a2013-01-17 12:27:49 -0800603 << Dbg::GetClassName(rhs.class_id) << "." << Dbg::GetMethodName(rhs.method_id)
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800604 << "@" << StringPrintf("%#" PRIx64, rhs.dex_pc) << " " << rhs.type_tag << "]";
Elliott Hughes03181a82011-11-17 17:22:21 -0800605 return os;
606}
607
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800608bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs) {
Elliott Hughes74847412012-06-20 18:10:21 -0700609 return lhs.dex_pc == rhs.dex_pc && lhs.method_id == rhs.method_id &&
610 lhs.class_id == rhs.class_id && lhs.type_tag == rhs.type_tag;
Elliott Hughes2aa2e392012-02-17 17:15:43 -0800611}
612
613bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs) {
614 return !(lhs == rhs);
615}
616
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617} // namespace JDWP
618
619} // namespace art