blob: f5ac9d02418ec544a35b00ffdcde69d7e1165ed5 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080020#include "atomic.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080021#include "base/mutex.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070022#include "jdwp/jdwp_bits.h"
23#include "jdwp/jdwp_constants.h"
24#include "jdwp/jdwp_expand_buf.h"
25
26#include <pthread.h>
27#include <stddef.h>
28#include <stdint.h>
29#include <string.h>
Sebastien Hertz7d955652014-10-22 10:57:10 +020030#include <vector>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070031
32struct iovec;
33
34namespace art {
Ian Rogers719d1a32014-03-06 12:13:39 -080035
Mathieu Chartierc7853442015-03-27 14:35:38 -070036class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070037class ArtMethod;
Ian Rogers719d1a32014-03-06 12:13:39 -080038union JValue;
39class Thread;
40
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041namespace mirror {
Sebastien Hertz6995c602014-09-09 12:10:13 +020042 class Class;
43 class Object;
44 class Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045} // namespace mirror
Sebastien Hertz6995c602014-09-09 12:10:13 +020046class Thread;
Elliott Hughes475fc232011-10-25 15:00:35 -070047
Elliott Hughes872d4ec2011-10-21 17:07:15 -070048namespace JDWP {
49
Elliott Hughes872d4ec2011-10-21 17:07:15 -070050/*
51 * Fundamental types.
52 *
53 * ObjectId and RefTypeId must be the same size.
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070054 * Its OK to change MethodId and FieldId sizes as long as the size is <= 8 bytes.
55 * 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 -070056 */
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070057typedef uint64_t FieldId; /* static or instance field */
58typedef uint64_t MethodId; /* any kind of method, including constructors */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
60typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
61typedef uint64_t FrameId; /* short-lived stack frame ID */
62
Elliott Hughesa96836a2013-01-17 12:27:49 -080063ObjectId ReadObjectId(const uint8_t** pBuf);
64
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070065static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set8BE(buf, val); }
66static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set8BE(buf, val); }
Elliott Hughesf7c3b662011-10-27 12:04:56 -070067static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
68static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
69static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070070static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd8BE(pReply, id); }
71static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd8BE(pReply, id); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
73static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
74static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
75
Sebastien Hertz6995c602014-09-09 12:10:13 +020076struct EventLocation {
Mathieu Chartiere401d142015-04-22 13:56:20 -070077 ArtMethod* method;
Sebastien Hertz6995c602014-09-09 12:10:13 +020078 uint32_t dex_pc;
79};
80
Elliott Hughes872d4ec2011-10-21 17:07:15 -070081/*
82 * Holds a JDWP "location".
83 */
84struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070085 JdwpTypeTag type_tag;
86 RefTypeId class_id;
87 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080088 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089};
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs)
Mathieu Chartier90443472015-07-16 20:32:27 -070091 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080092bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
93bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070094
95/*
96 * How we talk to the debugger.
97 */
98enum JdwpTransportType {
99 kJdwpTransportUnknown = 0,
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700100 kJdwpTransportSocket, // transport=dt_socket
101 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102};
103std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
104
Elliott Hughes376a7a02011-10-24 18:35:55 -0700105struct JdwpOptions {
Sebastien Hertz3be6e9d2015-02-05 16:30:58 +0100106 JdwpTransportType transport = kJdwpTransportUnknown;
107 bool server = false;
108 bool suspend = false;
109 std::string host = "";
110 uint16_t port = static_cast<uint16_t>(-1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700111};
112
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800113bool operator==(const JdwpOptions& lhs, const JdwpOptions& rhs);
114
Elliott Hughes376a7a02011-10-24 18:35:55 -0700115struct JdwpEvent;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800116class JdwpNetStateBase;
Elliott Hughes761928d2011-11-16 18:33:03 -0800117struct ModBasket;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800118class Request;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119
120/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700121 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700123struct JdwpState {
124 /*
125 * Perform one-time initialization.
126 *
127 * Among other things, this binds to a port to listen for a connection from
128 * the debugger.
129 *
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 * Returns a newly-allocated JdwpState struct on success, or nullptr on failure.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700131 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 static JdwpState* Create(const JdwpOptions* options)
Mathieu Chartier90443472015-07-16 20:32:27 -0700133 REQUIRES(!Locks::mutator_lock_) NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700134
Elliott Hughes376a7a02011-10-24 18:35:55 -0700135 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136
Elliott Hughes376a7a02011-10-24 18:35:55 -0700137 /*
138 * Returns "true" if a debugger or DDM is connected.
139 */
140 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700141
Elliott Hughes475fc232011-10-25 15:00:35 -0700142 /**
143 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700144 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700145 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146
Elliott Hughes376a7a02011-10-24 18:35:55 -0700147 /*
148 * Get time, in milliseconds, since the last debugger activity.
149 */
150 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151
Elliott Hughes64f574f2013-02-20 14:57:12 -0800152 void ExitAfterReplying(int exit_status);
153
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100154 // Acquires/releases the JDWP synchronization token for the debugger
155 // thread (command handler) so no event thread posts an event while
156 // it processes a command. This must be called only from the debugger
157 // thread.
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 void AcquireJdwpTokenForCommand() REQUIRES(!jdwp_token_lock_);
159 void ReleaseJdwpTokenForCommand() REQUIRES(!jdwp_token_lock_);
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100160
161 // Acquires/releases the JDWP synchronization token for the event thread
162 // so no other thread (debugger thread or event thread) interleaves with
163 // it when posting an event. This must NOT be called from the debugger
164 // thread, only event thread.
Mathieu Chartier90443472015-07-16 20:32:27 -0700165 void AcquireJdwpTokenForEvent(ObjectId threadId) REQUIRES(!jdwp_token_lock_);
166 void ReleaseJdwpTokenForEvent() REQUIRES(!jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167
Elliott Hughes376a7a02011-10-24 18:35:55 -0700168 /*
169 * These notify the debug code that something interesting has happened. This
170 * could be a thread starting or ending, an exception, or an opportunity
171 * for a breakpoint. These calls do not mean that an event the debugger
172 * is interested has happened, just that something has happened that the
173 * debugger *might* be interested in.
174 *
175 * The item of interest may trigger multiple events, some or all of which
176 * are grouped together in a single response.
177 *
178 * The event may cause the current thread or all threads (except the
179 * JDWP support thread) to be suspended.
180 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181
Elliott Hughes376a7a02011-10-24 18:35:55 -0700182 /*
183 * The VM has finished initializing. Only called when the debugger is
184 * connected at the time initialization completes.
185 */
Mathieu Chartier90443472015-07-16 20:32:27 -0700186 void PostVMStart() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187
Elliott Hughes376a7a02011-10-24 18:35:55 -0700188 /*
189 * A location of interest has been reached. This is used for breakpoints,
190 * single-stepping, and method entry/exit. (JDWP requires that these four
191 * events are grouped together in a single response.)
192 *
193 * In some cases "*pLoc" will just have a method and class name, e.g. when
194 * issuing a MethodEntry on a native method.
195 *
196 * "eventFlags" indicates the types of events that have occurred.
Jeff Hao579b0242013-11-18 13:16:49 -0800197 *
198 * "returnValue" is non-null for MethodExit events only.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700199 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200200 void PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr, int eventFlags,
Jeff Hao579b0242013-11-18 13:16:49 -0800201 const JValue* returnValue)
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 REQUIRES(!event_list_lock_, !jdwp_token_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203
Elliott Hughes376a7a02011-10-24 18:35:55 -0700204 /*
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200205 * A field of interest has been accessed or modified. This is used for field access and field
206 * modification events.
207 *
208 * "fieldValue" is non-null for field modification events only.
209 * "is_modification" is true for field modification, false for field access.
210 */
Mathieu Chartierc7853442015-03-27 14:35:38 -0700211 void PostFieldEvent(const EventLocation* pLoc, ArtField* field, mirror::Object* thisPtr,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200212 const JValue* fieldValue, bool is_modification)
Mathieu Chartier90443472015-07-16 20:32:27 -0700213 REQUIRES(!event_list_lock_, !jdwp_token_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200214
215 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700216 * An exception has been thrown.
217 *
218 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
219 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200220 void PostException(const EventLocation* pThrowLoc, mirror::Throwable* exception_object,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200221 const EventLocation* pCatchLoc, mirror::Object* thisPtr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700222 REQUIRES(!event_list_lock_, !jdwp_token_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700223
Elliott Hughes376a7a02011-10-24 18:35:55 -0700224 /*
225 * A thread has started or stopped.
226 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200227 void PostThreadChange(Thread* thread, bool start)
Mathieu Chartier90443472015-07-16 20:32:27 -0700228 REQUIRES(!event_list_lock_, !jdwp_token_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700229
Elliott Hughes376a7a02011-10-24 18:35:55 -0700230 /*
231 * Class has been prepared.
232 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200233 void PostClassPrepare(mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700234 REQUIRES(!event_list_lock_, !jdwp_token_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235
Elliott Hughes376a7a02011-10-24 18:35:55 -0700236 /*
237 * The VM is about to stop.
238 */
239 bool PostVMDeath();
240
Elliott Hughesa21039c2012-06-21 12:09:25 -0700241 // Called if/when we realize we're talking to DDMS.
Mathieu Chartier90443472015-07-16 20:32:27 -0700242 void NotifyDdmsActive() SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700243
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800244
245 void SetupChunkHeader(uint32_t type, size_t data_len, size_t header_size, uint8_t* out_header);
246
Elliott Hughes376a7a02011-10-24 18:35:55 -0700247 /*
248 * Send up a chunk of DDM data.
249 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700252
Mathieu Chartier90443472015-07-16 20:32:27 -0700253 bool HandlePacket() REQUIRES(!shutdown_lock_, !jdwp_token_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700254
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700255 void SendRequest(ExpandBuf* pReq);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700256
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 void ResetState()
Mathieu Chartier90443472015-07-16 20:32:27 -0700258 REQUIRES(!event_list_lock_)
259 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700260
261 /* atomic ops to get next serial number */
262 uint32_t NextRequestSerial();
263 uint32_t NextEventSerial();
264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 void Run()
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 REQUIRES(!Locks::mutator_lock_, !Locks::thread_suspend_count_lock_, !thread_start_lock_,
267 !attach_lock_, !event_list_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700268
Elliott Hughes761928d2011-11-16 18:33:03 -0800269 /*
270 * Register an event by adding it to the event list.
271 *
272 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
273 * may discard its pointer after calling this.
274 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 JdwpError RegisterEvent(JdwpEvent* pEvent)
Mathieu Chartier90443472015-07-16 20:32:27 -0700276 REQUIRES(!event_list_lock_)
277 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800278
279 /*
280 * Unregister an event, given the requestId.
281 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 void UnregisterEventById(uint32_t requestId)
Mathieu Chartier90443472015-07-16 20:32:27 -0700283 REQUIRES(!event_list_lock_)
284 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800285
286 /*
287 * Unregister all events.
288 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 void UnregisterAll()
Mathieu Chartier90443472015-07-16 20:32:27 -0700290 REQUIRES(!event_list_lock_)
291 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800292
Elliott Hughes376a7a02011-10-24 18:35:55 -0700293 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800294 explicit JdwpState(const JdwpOptions* options);
Mathieu Chartier90443472015-07-16 20:32:27 -0700295 size_t ProcessRequest(Request* request, ExpandBuf* pReply, bool* skip_reply)
296 REQUIRES(!jdwp_token_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800297 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700298 bool IsConnected();
jeffhaoa77f0f62012-12-05 17:19:31 -0800299 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
Mathieu Chartier90443472015-07-16 20:32:27 -0700300 REQUIRES(!Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
302 ObjectId threadId)
Mathieu Chartier90443472015-07-16 20:32:27 -0700303 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!jdwp_token_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200304 void CleanupMatchList(const std::vector<JdwpEvent*>& match_list)
Mathieu Chartier90443472015-07-16 20:32:27 -0700305 REQUIRES(event_list_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800306 void EventFinish(ExpandBuf* pReq);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200307 bool FindMatchingEvents(JdwpEventKind eventKind, const ModBasket& basket,
308 std::vector<JdwpEvent*>* match_list)
Mathieu Chartier90443472015-07-16 20:32:27 -0700309 REQUIRES(!event_list_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200310 void FindMatchingEventsLocked(JdwpEventKind eventKind, const ModBasket& basket,
311 std::vector<JdwpEvent*>* match_list)
Mathieu Chartier90443472015-07-16 20:32:27 -0700312 REQUIRES(event_list_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 void UnregisterEvent(JdwpEvent* pEvent)
Mathieu Chartier90443472015-07-16 20:32:27 -0700314 REQUIRES(event_list_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromf5293522013-07-19 00:24:00 -0700315 void SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700316
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100317 /*
318 * When we hit a debugger event that requires suspension, it's important
319 * that we wait for the thread to suspend itself before processing any
320 * additional requests. Otherwise, if the debugger immediately sends a
321 * "resume thread" command, the resume might arrive before the thread has
322 * suspended itself.
323 *
324 * It's also important no event thread suspends while we process a command
325 * from the debugger. Otherwise we could post an event ("thread death")
326 * before sending the reply of the command being processed ("resume") and
327 * cause bad synchronization with the debugger.
328 *
329 * The thread wanting "exclusive" access to the JDWP world must call the
330 * SetWaitForJdwpToken method before processing a command from the
331 * debugger or sending an event to the debugger.
332 * Once the command is processed or the event thread has posted its event,
333 * it must call the ClearWaitForJdwpToken method to allow another thread
334 * to do JDWP stuff.
335 *
336 * Therefore the main JDWP handler loop will wait for the event thread
337 * suspension before processing the next command. Once the event thread
338 * has suspended itself and cleared the token, the JDWP handler continues
339 * processing commands. This works in the suspend-all case because the
340 * event thread doesn't suspend itself until everything else has suspended.
341 *
342 * It's possible that multiple threads could encounter thread-suspending
343 * events at the same time, so we grab a mutex in the SetWaitForJdwpToken
344 * call, and release it in the ClearWaitForJdwpToken call.
345 */
Mathieu Chartier90443472015-07-16 20:32:27 -0700346 void SetWaitForJdwpToken(ObjectId threadId) REQUIRES(!jdwp_token_lock_);
347 void ClearWaitForJdwpToken() REQUIRES(!jdwp_token_lock_);
Sebastien Hertz99660e12014-02-19 15:04:42 +0100348
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700349 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700350 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700351
Elliott Hughesa21039c2012-06-21 12:09:25 -0700352 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700353 /* wait for creation of the JDWP thread */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
355 ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700356
Elliott Hughes475fc232011-10-25 15:00:35 -0700357 pthread_t pthread_;
358 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700359
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 volatile int32_t debug_thread_started_ GUARDED_BY(thread_start_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700361 ObjectId debug_thread_id_;
362
Elliott Hughes74847412012-06-20 18:10:21 -0700363 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700364 bool run;
365
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700366 public: // TODO: fix privacy
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700367 JdwpNetStateBase* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700368
Elliott Hughesa21039c2012-06-21 12:09:25 -0700369 private:
370 // For wait-for-debugger.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_);
372 ConditionVariable attach_cond_ GUARDED_BY(attach_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700373
Elliott Hughesa21039c2012-06-21 12:09:25 -0700374 // Time of last debugger activity, in milliseconds.
Ian Rogers37f3c962014-07-17 11:25:30 -0700375 Atomic<int64_t> last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700376
Elliott Hughesa21039c2012-06-21 12:09:25 -0700377 // Global counters and a mutex to protect them.
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -0700378 AtomicInteger request_serial_;
379 AtomicInteger event_serial_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700380
Elliott Hughesa21039c2012-06-21 12:09:25 -0700381 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100382 Mutex event_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_BEFORE(Locks::breakpoint_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700383 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100384 size_t event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700385
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100386 // Used to synchronize JDWP command handler thread and event threads so only one
387 // thread does JDWP stuff at a time. This prevent from interleaving command handling
388 // and event notification. Otherwise we could receive a "resume" command for an
389 // event thread that is not suspended yet, or post a "thread death" or event "VM death"
390 // event before sending the reply of the "resume" command that caused it.
391 Mutex jdwp_token_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
392 ConditionVariable jdwp_token_cond_ GUARDED_BY(jdwp_token_lock_);
393 ObjectId jdwp_token_owner_thread_id_;
Sebastien Hertz99660e12014-02-19 15:04:42 +0100394
Elliott Hughesa21039c2012-06-21 12:09:25 -0700395 bool ddm_is_active_;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800396
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100397 // Used for VirtualMachine.Exit command handling.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800398 bool should_exit_;
399 int exit_status_;
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100400
401 // Used to synchronize runtime shutdown with JDWP command handler thread.
402 // When the runtime shuts down, it needs to stop JDWP command handler thread by closing the
403 // JDWP connection. However, if the JDWP thread is processing a command, it needs to wait
404 // for the command to finish so we can send its reply before closing the connection.
405 Mutex shutdown_lock_ ACQUIRED_AFTER(event_list_lock_);
406 ConditionVariable shutdown_cond_ GUARDED_BY(shutdown_lock_);
407 bool processing_request_ GUARDED_BY(shutdown_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700408};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700409
Mathieu Chartier90443472015-07-16 20:32:27 -0700410std::string DescribeField(const FieldId& field_id) SHARED_REQUIRES(Locks::mutator_lock_);
411std::string DescribeMethod(const MethodId& method_id) SHARED_REQUIRES(Locks::mutator_lock_);
412std::string DescribeRefTypeId(const RefTypeId& ref_type_id) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800413
414class Request {
415 public:
Elliott Hughescb693062013-02-21 09:48:08 -0800416 Request(const uint8_t* bytes, uint32_t available);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800417 ~Request();
418
419 std::string ReadUtf8String();
420
421 // Helper function: read a variable-width value from the input buffer.
422 uint64_t ReadValue(size_t width);
423
424 int32_t ReadSigned32(const char* what);
425
426 uint32_t ReadUnsigned32(const char* what);
427
Mathieu Chartier90443472015-07-16 20:32:27 -0700428 FieldId ReadFieldId() SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800429
Mathieu Chartier90443472015-07-16 20:32:27 -0700430 MethodId ReadMethodId() SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800431
432 ObjectId ReadObjectId(const char* specific_kind);
433
434 ObjectId ReadArrayId();
435
436 ObjectId ReadObjectId();
437
438 ObjectId ReadThreadId();
439
440 ObjectId ReadThreadGroupId();
441
Mathieu Chartier90443472015-07-16 20:32:27 -0700442 RefTypeId ReadRefTypeId() SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800443
444 FrameId ReadFrameId();
445
446 template <typename T> T ReadEnum1(const char* specific_kind) {
Elliott Hughescb693062013-02-21 09:48:08 -0800447 T value = static_cast<T>(Read1());
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800448 VLOG(jdwp) << " " << specific_kind << " " << value;
449 return value;
450 }
451
452 JdwpTag ReadTag();
453
454 JdwpTypeTag ReadTypeTag();
455
Mathieu Chartier90443472015-07-16 20:32:27 -0700456 JdwpLocation ReadLocation() SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800457
458 JdwpModKind ReadModKind();
459
Elliott Hughescb693062013-02-21 09:48:08 -0800460 //
461 // Return values from this JDWP packet's header.
462 //
463 size_t GetLength() { return byte_count_; }
464 uint32_t GetId() { return id_; }
465 uint8_t GetCommandSet() { return command_set_; }
466 uint8_t GetCommand() { return command_; }
467
468 // Returns the number of bytes remaining.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800469 size_t size() { return end_ - p_; }
Elliott Hughescb693062013-02-21 09:48:08 -0800470
471 // Returns a pointer to the next byte.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800472 const uint8_t* data() { return p_; }
473
474 void Skip(size_t count) { p_ += count; }
475
476 void CheckConsumed();
477
478 private:
Elliott Hughescb693062013-02-21 09:48:08 -0800479 uint8_t Read1();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800480 uint16_t Read2BE();
Elliott Hughescb693062013-02-21 09:48:08 -0800481 uint32_t Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800482 uint64_t Read8BE();
483
Elliott Hughescb693062013-02-21 09:48:08 -0800484 uint32_t byte_count_;
485 uint32_t id_;
486 uint8_t command_set_;
487 uint8_t command_;
488
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800489 const uint8_t* p_;
490 const uint8_t* end_;
491
492 DISALLOW_COPY_AND_ASSIGN(Request);
493};
494
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495} // namespace JDWP
496
497} // namespace art
498
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700499#endif // ART_RUNTIME_JDWP_JDWP_H_