blob: 5796a61920d1821de715a88cd190c6cd91314456 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_JDWP_JDWP_H_
18#define ART_RUNTIME_JDWP_JDWP_H_
Elliott Hughes872d4ec2011-10-21 17:07:15 -070019
David Sehrc431b9d2018-03-02 12:01:51 -080020#include "base/atomic.h"
Andreas Gampe57943812017-12-06 21:39:13 -080021#include "base/logging.h" // For VLOG.
Elliott Hughes76b61672012-12-12 17:47:30 -080022#include "base/mutex.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070023#include "jdwp/jdwp_bits.h"
24#include "jdwp/jdwp_constants.h"
25#include "jdwp/jdwp_expand_buf.h"
Alex Light5643caf2017-02-08 11:39:07 -080026#include "obj_ptr.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070027
28#include <pthread.h>
29#include <stddef.h>
30#include <stdint.h>
31#include <string.h>
Sebastien Hertz7d955652014-10-22 10:57:10 +020032#include <vector>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070033
34struct iovec;
35
36namespace art {
Ian Rogers719d1a32014-03-06 12:13:39 -080037
Mathieu Chartierc7853442015-03-27 14:35:38 -070038class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070039class ArtMethod;
Ian Rogers719d1a32014-03-06 12:13:39 -080040union JValue;
41class Thread;
42
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080044class Class;
45class Object;
46class Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047} // namespace mirror
Sebastien Hertz6995c602014-09-09 12:10:13 +020048class Thread;
Elliott Hughes475fc232011-10-25 15:00:35 -070049
Elliott Hughes872d4ec2011-10-21 17:07:15 -070050namespace JDWP {
51
Elliott Hughes872d4ec2011-10-21 17:07:15 -070052/*
53 * Fundamental types.
54 *
55 * ObjectId and RefTypeId must be the same size.
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070056 * Its OK to change MethodId and FieldId sizes as long as the size is <= 8 bytes.
57 * Note that ArtFields are 64 bit pointers on 64 bit targets. So this one must remain 8 bytes.
Elliott Hughes872d4ec2011-10-21 17:07:15 -070058 */
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070059typedef uint64_t FieldId; /* static or instance field */
60typedef uint64_t MethodId; /* any kind of method, including constructors */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070061typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
62typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
63typedef uint64_t FrameId; /* short-lived stack frame ID */
64
Elliott Hughesa96836a2013-01-17 12:27:49 -080065ObjectId ReadObjectId(const uint8_t** pBuf);
66
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070067static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set8BE(buf, val); }
68static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set8BE(buf, val); }
Elliott Hughesf7c3b662011-10-27 12:04:56 -070069static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
70static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
71static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070072static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd8BE(pReply, id); }
73static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd8BE(pReply, id); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070074static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
75static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
76static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
77
Sebastien Hertz6995c602014-09-09 12:10:13 +020078struct EventLocation {
Mathieu Chartiere401d142015-04-22 13:56:20 -070079 ArtMethod* method;
Sebastien Hertz6995c602014-09-09 12:10:13 +020080 uint32_t dex_pc;
81};
82
Elliott Hughes872d4ec2011-10-21 17:07:15 -070083/*
84 * Holds a JDWP "location".
85 */
86struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070087 JdwpTypeTag type_tag;
88 RefTypeId class_id;
89 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080090 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091};
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080094bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
95bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070096
97/*
98 * How we talk to the debugger.
99 */
100enum JdwpTransportType {
Alex Light40320712017-12-14 11:52:04 -0800101 kJdwpTransportNone = 0,
102 kJdwpTransportUnknown, // Unknown tranpsort
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700103 kJdwpTransportSocket, // transport=dt_socket
104 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700105};
106std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
107
Elliott Hughes376a7a02011-10-24 18:35:55 -0700108struct JdwpOptions {
Alex Light40320712017-12-14 11:52:04 -0800109 JdwpTransportType transport = kJdwpTransportNone;
Sebastien Hertz3be6e9d2015-02-05 16:30:58 +0100110 bool server = false;
111 bool suspend = false;
112 std::string host = "";
113 uint16_t port = static_cast<uint16_t>(-1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114};
115
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800116bool operator==(const JdwpOptions& lhs, const JdwpOptions& rhs);
117
Alex Light40320712017-12-14 11:52:04 -0800118bool ParseJdwpOptions(const std::string& options, JdwpOptions* jdwp_options);
119
Elliott Hughes376a7a02011-10-24 18:35:55 -0700120struct JdwpEvent;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800121class JdwpNetStateBase;
Elliott Hughes761928d2011-11-16 18:33:03 -0800122struct ModBasket;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800123class Request;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124
125/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700126 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700128struct JdwpState {
129 /*
130 * Perform one-time initialization.
131 *
132 * Among other things, this binds to a port to listen for a connection from
133 * the debugger.
134 *
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 * Returns a newly-allocated JdwpState struct on success, or nullptr on failure.
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700136 *
137 * NO_THREAD_SAFETY_ANALYSIS since we can't annotate that we do not have
138 * state->thread_start_lock_ held.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700139 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 static JdwpState* Create(const JdwpOptions* options)
Mathieu Chartier90443472015-07-16 20:32:27 -0700141 REQUIRES(!Locks::mutator_lock_) NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700142
Elliott Hughes376a7a02011-10-24 18:35:55 -0700143 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144
Elliott Hughes376a7a02011-10-24 18:35:55 -0700145 /*
146 * Returns "true" if a debugger or DDM is connected.
147 */
148 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700149
Elliott Hughes475fc232011-10-25 15:00:35 -0700150 /**
151 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700152 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700153 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154
Elliott Hughes376a7a02011-10-24 18:35:55 -0700155 /*
156 * Get time, in milliseconds, since the last debugger activity.
157 */
158 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700159
Elliott Hughes64f574f2013-02-20 14:57:12 -0800160 void ExitAfterReplying(int exit_status);
161
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100162 // Acquires/releases the JDWP synchronization token for the debugger
163 // thread (command handler) so no event thread posts an event while
164 // it processes a command. This must be called only from the debugger
165 // thread.
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 void AcquireJdwpTokenForCommand() REQUIRES(!jdwp_token_lock_);
167 void ReleaseJdwpTokenForCommand() REQUIRES(!jdwp_token_lock_);
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100168
169 // Acquires/releases the JDWP synchronization token for the event thread
170 // so no other thread (debugger thread or event thread) interleaves with
171 // it when posting an event. This must NOT be called from the debugger
172 // thread, only event thread.
Mathieu Chartier90443472015-07-16 20:32:27 -0700173 void AcquireJdwpTokenForEvent(ObjectId threadId) REQUIRES(!jdwp_token_lock_);
174 void ReleaseJdwpTokenForEvent() REQUIRES(!jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700175
Elliott Hughes376a7a02011-10-24 18:35:55 -0700176 /*
177 * These notify the debug code that something interesting has happened. This
178 * could be a thread starting or ending, an exception, or an opportunity
179 * for a breakpoint. These calls do not mean that an event the debugger
180 * is interested has happened, just that something has happened that the
181 * debugger *might* be interested in.
182 *
183 * The item of interest may trigger multiple events, some or all of which
184 * are grouped together in a single response.
185 *
186 * The event may cause the current thread or all threads (except the
187 * JDWP support thread) to be suspended.
188 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189
Elliott Hughes376a7a02011-10-24 18:35:55 -0700190 /*
191 * The VM has finished initializing. Only called when the debugger is
192 * connected at the time initialization completes.
193 */
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700194 void PostVMStart() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700195
Elliott Hughes376a7a02011-10-24 18:35:55 -0700196 /*
197 * A location of interest has been reached. This is used for breakpoints,
198 * single-stepping, and method entry/exit. (JDWP requires that these four
199 * events are grouped together in a single response.)
200 *
201 * In some cases "*pLoc" will just have a method and class name, e.g. when
202 * issuing a MethodEntry on a native method.
203 *
204 * "eventFlags" indicates the types of events that have occurred.
Jeff Hao579b0242013-11-18 13:16:49 -0800205 *
206 * "returnValue" is non-null for MethodExit events only.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700207 */
Vladimir Marko83114892019-04-11 13:05:50 +0100208 void PostLocationEvent(const EventLocation* pLoc,
209 ObjPtr<mirror::Object> thisPtr,
210 int eventFlags,
Jeff Hao579b0242013-11-18 13:16:49 -0800211 const JValue* returnValue)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800212 REQUIRES(!event_list_lock_, !jdwp_token_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213
Elliott Hughes376a7a02011-10-24 18:35:55 -0700214 /*
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200215 * A field of interest has been accessed or modified. This is used for field access and field
216 * modification events.
217 *
218 * "fieldValue" is non-null for field modification events only.
219 * "is_modification" is true for field modification, false for field access.
220 */
Vladimir Marko83114892019-04-11 13:05:50 +0100221 void PostFieldEvent(const EventLocation* pLoc,
222 ArtField* field,
223 ObjPtr<mirror::Object> thisPtr,
224 const JValue* fieldValue,
225 bool is_modification)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800226 REQUIRES(!event_list_lock_, !jdwp_token_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200227
228 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700229 * An exception has been thrown.
230 *
231 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
232 */
Vladimir Marko83114892019-04-11 13:05:50 +0100233 void PostException(const EventLocation* pThrowLoc,
234 ObjPtr<mirror::Throwable> exception_object,
235 const EventLocation* pCatchLoc,
236 ObjPtr<mirror::Object> thisPtr)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800237 REQUIRES(!event_list_lock_, !jdwp_token_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238
Elliott Hughes376a7a02011-10-24 18:35:55 -0700239 /*
240 * A thread has started or stopped.
241 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200242 void PostThreadChange(Thread* thread, bool start)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800243 REQUIRES(!event_list_lock_, !jdwp_token_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244
Elliott Hughes376a7a02011-10-24 18:35:55 -0700245 /*
246 * Class has been prepared.
247 */
Vladimir Marko83114892019-04-11 13:05:50 +0100248 void PostClassPrepare(ObjPtr<mirror::Class> klass)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800249 REQUIRES(!event_list_lock_, !jdwp_token_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700250
Elliott Hughes376a7a02011-10-24 18:35:55 -0700251 /*
252 * The VM is about to stop.
253 */
254 bool PostVMDeath();
255
Elliott Hughesa21039c2012-06-21 12:09:25 -0700256 // Called if/when we realize we're talking to DDMS.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 void NotifyDdmsActive() REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700258
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800259
260 void SetupChunkHeader(uint32_t type, size_t data_len, size_t header_size, uint8_t* out_header);
261
Elliott Hughes376a7a02011-10-24 18:35:55 -0700262 /*
263 * Send up a chunk of DDM data.
264 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700267
Mathieu Chartier90443472015-07-16 20:32:27 -0700268 bool HandlePacket() REQUIRES(!shutdown_lock_, !jdwp_token_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700269
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700270 void SendRequest(ExpandBuf* pReq);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 void ResetState()
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800273 REQUIRES(!event_list_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700275
276 /* atomic ops to get next serial number */
277 uint32_t NextRequestSerial();
278 uint32_t NextEventSerial();
279
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 void Run()
Mathieu Chartier90443472015-07-16 20:32:27 -0700281 REQUIRES(!Locks::mutator_lock_, !Locks::thread_suspend_count_lock_, !thread_start_lock_,
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800282 !attach_lock_, !event_list_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700283
Elliott Hughes761928d2011-11-16 18:33:03 -0800284 /*
285 * Register an event by adding it to the event list.
286 *
287 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
288 * may discard its pointer after calling this.
289 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 JdwpError RegisterEvent(JdwpEvent* pEvent)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800291 REQUIRES(!event_list_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700292 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800293
294 /*
295 * Unregister an event, given the requestId.
296 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 void UnregisterEventById(uint32_t requestId)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800298 REQUIRES(!event_list_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700299 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800300
Alex Light5643caf2017-02-08 11:39:07 -0800301 void UnregisterLocationEventsOnClass(ObjPtr<mirror::Class> klass)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800302 REQUIRES(!event_list_lock_)
Alex Light5643caf2017-02-08 11:39:07 -0800303 REQUIRES_SHARED(Locks::mutator_lock_);
304
Elliott Hughes761928d2011-11-16 18:33:03 -0800305 /*
306 * Unregister all events.
307 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 void UnregisterAll()
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800309 REQUIRES(!event_list_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800311
Elliott Hughes376a7a02011-10-24 18:35:55 -0700312 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800313 explicit JdwpState(const JdwpOptions* options);
Mathieu Chartier90443472015-07-16 20:32:27 -0700314 size_t ProcessRequest(Request* request, ExpandBuf* pReply, bool* skip_reply)
315 REQUIRES(!jdwp_token_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800316 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700317 bool IsConnected();
jeffhaoa77f0f62012-12-05 17:19:31 -0800318 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
Mathieu Chartier90443472015-07-16 20:32:27 -0700319 REQUIRES(!Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
321 ObjectId threadId)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!jdwp_token_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200323 void CleanupMatchList(const std::vector<JdwpEvent*>& match_list)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800324 REQUIRES(event_list_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800325 void EventFinish(ExpandBuf* pReq);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200326 bool FindMatchingEvents(JdwpEventKind eventKind, const ModBasket& basket,
327 std::vector<JdwpEvent*>* match_list)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800328 REQUIRES(!event_list_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200329 void FindMatchingEventsLocked(JdwpEventKind eventKind, const ModBasket& basket,
330 std::vector<JdwpEvent*>* match_list)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800331 REQUIRES(event_list_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700332 void UnregisterEvent(JdwpEvent* pEvent)
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800333 REQUIRES(event_list_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstromf5293522013-07-19 00:24:00 -0700334 void SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700335
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100336 /*
337 * When we hit a debugger event that requires suspension, it's important
338 * that we wait for the thread to suspend itself before processing any
339 * additional requests. Otherwise, if the debugger immediately sends a
340 * "resume thread" command, the resume might arrive before the thread has
341 * suspended itself.
342 *
343 * It's also important no event thread suspends while we process a command
344 * from the debugger. Otherwise we could post an event ("thread death")
345 * before sending the reply of the command being processed ("resume") and
346 * cause bad synchronization with the debugger.
347 *
348 * The thread wanting "exclusive" access to the JDWP world must call the
349 * SetWaitForJdwpToken method before processing a command from the
350 * debugger or sending an event to the debugger.
351 * Once the command is processed or the event thread has posted its event,
352 * it must call the ClearWaitForJdwpToken method to allow another thread
353 * to do JDWP stuff.
354 *
355 * Therefore the main JDWP handler loop will wait for the event thread
356 * suspension before processing the next command. Once the event thread
357 * has suspended itself and cleared the token, the JDWP handler continues
358 * processing commands. This works in the suspend-all case because the
359 * event thread doesn't suspend itself until everything else has suspended.
360 *
361 * It's possible that multiple threads could encounter thread-suspending
362 * events at the same time, so we grab a mutex in the SetWaitForJdwpToken
363 * call, and release it in the ClearWaitForJdwpToken call.
364 */
Mathieu Chartier90443472015-07-16 20:32:27 -0700365 void SetWaitForJdwpToken(ObjectId threadId) REQUIRES(!jdwp_token_lock_);
366 void ClearWaitForJdwpToken() REQUIRES(!jdwp_token_lock_);
Sebastien Hertz99660e12014-02-19 15:04:42 +0100367
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700368 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700369 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700370
Elliott Hughesa21039c2012-06-21 12:09:25 -0700371 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700372 /* wait for creation of the JDWP thread */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700373 Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
374 ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700375
Elliott Hughes475fc232011-10-25 15:00:35 -0700376 pthread_t pthread_;
377 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700378
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700379 volatile bool debug_thread_started_ GUARDED_BY(thread_start_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700380 ObjectId debug_thread_id_;
381
Elliott Hughes74847412012-06-20 18:10:21 -0700382 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700383 bool run;
384
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700385 public: // TODO: fix privacy
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700386 JdwpNetStateBase* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700387
Elliott Hughesa21039c2012-06-21 12:09:25 -0700388 private:
389 // For wait-for-debugger.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_);
391 ConditionVariable attach_cond_ GUARDED_BY(attach_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700392
Elliott Hughesa21039c2012-06-21 12:09:25 -0700393 // Time of last debugger activity, in milliseconds.
Ian Rogers37f3c962014-07-17 11:25:30 -0700394 Atomic<int64_t> last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700395
Elliott Hughesa21039c2012-06-21 12:09:25 -0700396 // Global counters and a mutex to protect them.
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -0700397 AtomicInteger request_serial_;
398 AtomicInteger event_serial_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700399
Elliott Hughesa21039c2012-06-21 12:09:25 -0700400 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800401 Mutex event_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_BEFORE(Locks::breakpoint_lock_);
402 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
403 size_t event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700404
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100405 // Used to synchronize JDWP command handler thread and event threads so only one
406 // thread does JDWP stuff at a time. This prevent from interleaving command handling
407 // and event notification. Otherwise we could receive a "resume" command for an
408 // event thread that is not suspended yet, or post a "thread death" or event "VM death"
409 // event before sending the reply of the "resume" command that caused it.
410 Mutex jdwp_token_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
411 ConditionVariable jdwp_token_cond_ GUARDED_BY(jdwp_token_lock_);
412 ObjectId jdwp_token_owner_thread_id_;
Sebastien Hertz99660e12014-02-19 15:04:42 +0100413
Elliott Hughesa21039c2012-06-21 12:09:25 -0700414 bool ddm_is_active_;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800415
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100416 // Used for VirtualMachine.Exit command handling.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800417 bool should_exit_;
418 int exit_status_;
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100419
420 // Used to synchronize runtime shutdown with JDWP command handler thread.
421 // When the runtime shuts down, it needs to stop JDWP command handler thread by closing the
422 // JDWP connection. However, if the JDWP thread is processing a command, it needs to wait
423 // for the command to finish so we can send its reply before closing the connection.
Hiroshi Yamauchib139b6d2017-02-28 15:01:23 -0800424 Mutex shutdown_lock_ ACQUIRED_AFTER(event_list_lock_);
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100425 ConditionVariable shutdown_cond_ GUARDED_BY(shutdown_lock_);
426 bool processing_request_ GUARDED_BY(shutdown_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700427};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700429std::string DescribeField(const FieldId& field_id) REQUIRES_SHARED(Locks::mutator_lock_);
430std::string DescribeMethod(const MethodId& method_id) REQUIRES_SHARED(Locks::mutator_lock_);
431std::string DescribeRefTypeId(const RefTypeId& ref_type_id) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800432
433class Request {
434 public:
Elliott Hughescb693062013-02-21 09:48:08 -0800435 Request(const uint8_t* bytes, uint32_t available);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800436 ~Request();
437
438 std::string ReadUtf8String();
439
440 // Helper function: read a variable-width value from the input buffer.
441 uint64_t ReadValue(size_t width);
442
443 int32_t ReadSigned32(const char* what);
444
445 uint32_t ReadUnsigned32(const char* what);
446
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700447 FieldId ReadFieldId() REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800448
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700449 MethodId ReadMethodId() REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800450
451 ObjectId ReadObjectId(const char* specific_kind);
452
453 ObjectId ReadArrayId();
454
455 ObjectId ReadObjectId();
456
457 ObjectId ReadThreadId();
458
459 ObjectId ReadThreadGroupId();
460
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700461 RefTypeId ReadRefTypeId() REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800462
463 FrameId ReadFrameId();
464
465 template <typename T> T ReadEnum1(const char* specific_kind) {
Elliott Hughescb693062013-02-21 09:48:08 -0800466 T value = static_cast<T>(Read1());
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800467 VLOG(jdwp) << " " << specific_kind << " " << value;
468 return value;
469 }
470
471 JdwpTag ReadTag();
472
473 JdwpTypeTag ReadTypeTag();
474
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700475 JdwpLocation ReadLocation() REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800476
477 JdwpModKind ReadModKind();
478
Elliott Hughescb693062013-02-21 09:48:08 -0800479 //
480 // Return values from this JDWP packet's header.
481 //
482 size_t GetLength() { return byte_count_; }
483 uint32_t GetId() { return id_; }
484 uint8_t GetCommandSet() { return command_set_; }
485 uint8_t GetCommand() { return command_; }
486
487 // Returns the number of bytes remaining.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800488 size_t size() { return end_ - p_; }
Elliott Hughescb693062013-02-21 09:48:08 -0800489
490 // Returns a pointer to the next byte.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800491 const uint8_t* data() { return p_; }
492
493 void Skip(size_t count) { p_ += count; }
494
495 void CheckConsumed();
496
497 private:
Elliott Hughescb693062013-02-21 09:48:08 -0800498 uint8_t Read1();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800499 uint16_t Read2BE();
Elliott Hughescb693062013-02-21 09:48:08 -0800500 uint32_t Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800501 uint64_t Read8BE();
502
Elliott Hughescb693062013-02-21 09:48:08 -0800503 uint32_t byte_count_;
504 uint32_t id_;
505 uint8_t command_set_;
506 uint8_t command_;
507
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800508 const uint8_t* p_;
509 const uint8_t* end_;
510
511 DISALLOW_COPY_AND_ASSIGN(Request);
512};
513
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514} // namespace JDWP
515
516} // namespace art
517
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700518#endif // ART_RUNTIME_JDWP_JDWP_H_