blob: 1477324ca0a4659d712f6a6f28db15ec621a94d0 [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>
30
31struct iovec;
32
33namespace art {
Ian Rogers719d1a32014-03-06 12:13:39 -080034
35union JValue;
36class Thread;
37
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070039 class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040} // namespace mirror
Elliott Hughes475fc232011-10-25 15:00:35 -070041
Elliott Hughes872d4ec2011-10-21 17:07:15 -070042namespace JDWP {
43
Elliott Hughes872d4ec2011-10-21 17:07:15 -070044/*
45 * Fundamental types.
46 *
47 * ObjectId and RefTypeId must be the same size.
48 */
49typedef uint32_t FieldId; /* static or instance field */
50typedef uint32_t MethodId; /* any kind of method, including constructors */
51typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
52typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
53typedef uint64_t FrameId; /* short-lived stack frame ID */
54
Elliott Hughesa96836a2013-01-17 12:27:49 -080055ObjectId ReadObjectId(const uint8_t** pBuf);
56
Elliott Hughesf7c3b662011-10-27 12:04:56 -070057static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); }
58static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); }
59static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
60static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
61static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070062static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
63static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
64static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
65static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
66static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
67
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068/*
69 * Holds a JDWP "location".
70 */
71struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070072 JdwpTypeTag type_tag;
73 RefTypeId class_id;
74 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080075 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070076};
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080079bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
80bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070081
82/*
83 * How we talk to the debugger.
84 */
85enum JdwpTransportType {
86 kJdwpTransportUnknown = 0,
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070087 kJdwpTransportSocket, // transport=dt_socket
88 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089};
90std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
91
Elliott Hughes376a7a02011-10-24 18:35:55 -070092struct JdwpOptions {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070093 JdwpTransportType transport;
94 bool server;
95 bool suspend;
Elliott Hughesd1cc8362011-10-24 16:58:50 -070096 std::string host;
Elliott Hughes6d8dd472012-01-17 18:27:41 -080097 uint16_t port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098};
99
Elliott Hughes376a7a02011-10-24 18:35:55 -0700100struct JdwpEvent;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800101class JdwpNetStateBase;
Elliott Hughes761928d2011-11-16 18:33:03 -0800102struct ModBasket;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800103class Request;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700104
105/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700106 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700107 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700108struct JdwpState {
109 /*
110 * Perform one-time initialization.
111 *
112 * Among other things, this binds to a port to listen for a connection from
113 * the debugger.
114 *
115 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
116 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 static JdwpState* Create(const JdwpOptions* options)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 LOCKS_EXCLUDED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119
Elliott Hughes376a7a02011-10-24 18:35:55 -0700120 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121
Elliott Hughes376a7a02011-10-24 18:35:55 -0700122 /*
123 * Returns "true" if a debugger or DDM is connected.
124 */
125 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126
Elliott Hughes475fc232011-10-25 15:00:35 -0700127 /**
128 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700129 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700130 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131
Elliott Hughes376a7a02011-10-24 18:35:55 -0700132 /*
133 * Get time, in milliseconds, since the last debugger activity.
134 */
135 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136
Elliott Hughes64f574f2013-02-20 14:57:12 -0800137 void ExitAfterReplying(int exit_status);
138
Elliott Hughes376a7a02011-10-24 18:35:55 -0700139 /*
140 * When we hit a debugger event that requires suspension, it's important
141 * that we wait for the thread to suspend itself before processing any
142 * additional requests. (Otherwise, if the debugger immediately sends a
143 * "resume thread" command, the resume might arrive before the thread has
144 * suspended itself.)
145 *
146 * The thread should call the "set" function before sending the event to
147 * the debugger. The main JDWP handler loop calls "get" before processing
148 * an event, and will wait for thread suspension if it's set. Once the
149 * thread has suspended itself, the JDWP handler calls "clear" and
150 * continues processing the current event. This works in the suspend-all
151 * case because the event thread doesn't suspend itself until everything
152 * else has suspended.
153 *
154 * It's possible that multiple threads could encounter thread-suspending
155 * events at the same time, so we grab a mutex in the "set" call, and
156 * release it in the "clear" call.
157 */
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700158 // ObjectId GetWaitForEventThread();
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100159 void SetWaitForEventThread(ObjectId threadId)
160 LOCKS_EXCLUDED(event_thread_lock_, process_request_lock_);
Ian Rogers719d1a32014-03-06 12:13:39 -0800161 void ClearWaitForEventThread() LOCKS_EXCLUDED(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700162
Elliott Hughes376a7a02011-10-24 18:35:55 -0700163 /*
164 * These notify the debug code that something interesting has happened. This
165 * could be a thread starting or ending, an exception, or an opportunity
166 * for a breakpoint. These calls do not mean that an event the debugger
167 * is interested has happened, just that something has happened that the
168 * debugger *might* be interested in.
169 *
170 * The item of interest may trigger multiple events, some or all of which
171 * are grouped together in a single response.
172 *
173 * The event may cause the current thread or all threads (except the
174 * JDWP support thread) to be suspended.
175 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176
Elliott Hughes376a7a02011-10-24 18:35:55 -0700177 /*
178 * The VM has finished initializing. Only called when the debugger is
179 * connected at the time initialization completes.
180 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 bool PostVMStart() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182
Elliott Hughes376a7a02011-10-24 18:35:55 -0700183 /*
184 * A location of interest has been reached. This is used for breakpoints,
185 * single-stepping, and method entry/exit. (JDWP requires that these four
186 * events are grouped together in a single response.)
187 *
188 * In some cases "*pLoc" will just have a method and class name, e.g. when
189 * issuing a MethodEntry on a native method.
190 *
191 * "eventFlags" indicates the types of events that have occurred.
Jeff Hao579b0242013-11-18 13:16:49 -0800192 *
193 * "returnValue" is non-null for MethodExit events only.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700194 */
Jeff Hao579b0242013-11-18 13:16:49 -0800195 bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags,
196 const JValue* returnValue)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198
Elliott Hughes376a7a02011-10-24 18:35:55 -0700199 /*
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200200 * A field of interest has been accessed or modified. This is used for field access and field
201 * modification events.
202 *
203 * "fieldValue" is non-null for field modification events only.
204 * "is_modification" is true for field modification, false for field access.
205 */
206 bool PostFieldEvent(const JdwpLocation* pLoc, RefTypeId typeId, FieldId fieldId,
207 ObjectId thisPtr, const JValue* fieldValue, bool is_modification)
208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
209
210 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700211 * An exception has been thrown.
212 *
213 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
214 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId,
216 const JdwpLocation* pCatchLoc, ObjectId thisPtr)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218
Elliott Hughes376a7a02011-10-24 18:35:55 -0700219 /*
220 * A thread has started or stopped.
221 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 bool PostThreadChange(ObjectId threadId, bool start)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224
Elliott Hughes376a7a02011-10-24 18:35:55 -0700225 /*
226 * Class has been prepared.
227 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 bool PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
229 int status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700231
Elliott Hughes376a7a02011-10-24 18:35:55 -0700232 /*
233 * The VM is about to stop.
234 */
235 bool PostVMDeath();
236
Elliott Hughesa21039c2012-06-21 12:09:25 -0700237 // Called if/when we realize we're talking to DDMS.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700238 void NotifyDdmsActive() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700239
Elliott Hughes376a7a02011-10-24 18:35:55 -0700240 /*
241 * Send up a chunk of DDM data.
242 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700245
Elliott Hughescb693062013-02-21 09:48:08 -0800246 bool HandlePacket();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700247
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700248 void SendRequest(ExpandBuf* pReq);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700249
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 void ResetState()
251 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700253
254 /* atomic ops to get next serial number */
255 uint32_t NextRequestSerial();
256 uint32_t NextEventSerial();
257
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 void Run()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 LOCKS_EXCLUDED(Locks::mutator_lock_,
260 Locks::thread_suspend_count_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700261
Elliott Hughes761928d2011-11-16 18:33:03 -0800262 /*
263 * Register an event by adding it to the event list.
264 *
265 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
266 * may discard its pointer after calling this.
267 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 JdwpError RegisterEvent(JdwpEvent* pEvent)
269 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800271
272 /*
273 * Unregister an event, given the requestId.
274 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 void UnregisterEventById(uint32_t requestId)
276 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700277 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800278
279 /*
280 * Unregister all events.
281 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 void UnregisterAll()
283 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800285
Elliott Hughes376a7a02011-10-24 18:35:55 -0700286 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800287 explicit JdwpState(const JdwpOptions* options);
Sebastien Hertz43c8d722014-03-18 12:19:52 +0100288 size_t ProcessRequest(Request& request, ExpandBuf* pReply);
Elliott Hughes761928d2011-11-16 18:33:03 -0800289 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700290 bool IsConnected();
jeffhaoa77f0f62012-12-05 17:19:31 -0800291 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 LOCKS_EXCLUDED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
294 ObjectId threadId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700296 void CleanupMatchList(JdwpEvent** match_list,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 int match_count)
298 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800300 void EventFinish(ExpandBuf* pReq);
Elliott Hughesf8349362012-06-18 15:00:06 -0700301 void FindMatchingEvents(JdwpEventKind eventKind,
302 ModBasket* basket,
303 JdwpEvent** match_list,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 int* pMatchCount)
305 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 void UnregisterEvent(JdwpEvent* pEvent)
308 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf5293522013-07-19 00:24:00 -0700310 void SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700311
Sebastien Hertz99660e12014-02-19 15:04:42 +0100312 void StartProcessingRequest() LOCKS_EXCLUDED(process_request_lock_);
313 void EndProcessingRequest() LOCKS_EXCLUDED(process_request_lock_);
314 void WaitForProcessingRequest() LOCKS_EXCLUDED(process_request_lock_);
315
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700316 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700317 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700318
Elliott Hughesa21039c2012-06-21 12:09:25 -0700319 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700320 /* wait for creation of the JDWP thread */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
322 ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700323
Elliott Hughes475fc232011-10-25 15:00:35 -0700324 pthread_t pthread_;
325 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700326
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327 volatile int32_t debug_thread_started_ GUARDED_BY(thread_start_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700328 ObjectId debug_thread_id_;
329
Elliott Hughes74847412012-06-20 18:10:21 -0700330 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700331 bool run;
332
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700333 public: // TODO: fix privacy
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700334 JdwpNetStateBase* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700335
Elliott Hughesa21039c2012-06-21 12:09:25 -0700336 private:
337 // For wait-for-debugger.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338 Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_);
339 ConditionVariable attach_cond_ GUARDED_BY(attach_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700340
Elliott Hughesa21039c2012-06-21 12:09:25 -0700341 // Time of last debugger activity, in milliseconds.
342 int64_t last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700343
Elliott Hughesa21039c2012-06-21 12:09:25 -0700344 // Global counters and a mutex to protect them.
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -0700345 AtomicInteger request_serial_;
346 AtomicInteger event_serial_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700347
Elliott Hughesa21039c2012-06-21 12:09:25 -0700348 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100349 Mutex event_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_BEFORE(Locks::breakpoint_lock_);
Ian Rogers719d1a32014-03-06 12:13:39 -0800350
Elliott Hughesa21039c2012-06-21 12:09:25 -0700351 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100352 size_t event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700353
Elliott Hughesa21039c2012-06-21 12:09:25 -0700354 // Used to synchronize suspension of the event thread (to avoid receiving "resume"
355 // events before the thread has finished suspending itself).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 Mutex event_thread_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
357 ConditionVariable event_thread_cond_ GUARDED_BY(event_thread_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700358 ObjectId event_thread_id_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700359
Sebastien Hertz99660e12014-02-19 15:04:42 +0100360 // Used to synchronize request processing and event sending (to avoid sending an event before
361 // sending the reply of a command being processed).
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100362 Mutex process_request_lock_ ACQUIRED_AFTER(event_thread_lock_);
Sebastien Hertz99660e12014-02-19 15:04:42 +0100363 ConditionVariable process_request_cond_ GUARDED_BY(process_request_lock_);
364 bool processing_request_ GUARDED_BY(process_request_lock_);
365
Elliott Hughesa21039c2012-06-21 12:09:25 -0700366 bool ddm_is_active_;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800367
368 bool should_exit_;
369 int exit_status_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700370};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800372std::string DescribeField(const FieldId& field_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
373std::string DescribeMethod(const MethodId& method_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
374std::string DescribeRefTypeId(const RefTypeId& ref_type_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
375
376class Request {
377 public:
Elliott Hughescb693062013-02-21 09:48:08 -0800378 Request(const uint8_t* bytes, uint32_t available);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800379 ~Request();
380
381 std::string ReadUtf8String();
382
383 // Helper function: read a variable-width value from the input buffer.
384 uint64_t ReadValue(size_t width);
385
386 int32_t ReadSigned32(const char* what);
387
388 uint32_t ReadUnsigned32(const char* what);
389
390 FieldId ReadFieldId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
391
392 MethodId ReadMethodId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
393
394 ObjectId ReadObjectId(const char* specific_kind);
395
396 ObjectId ReadArrayId();
397
398 ObjectId ReadObjectId();
399
400 ObjectId ReadThreadId();
401
402 ObjectId ReadThreadGroupId();
403
404 RefTypeId ReadRefTypeId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
405
406 FrameId ReadFrameId();
407
408 template <typename T> T ReadEnum1(const char* specific_kind) {
Elliott Hughescb693062013-02-21 09:48:08 -0800409 T value = static_cast<T>(Read1());
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800410 VLOG(jdwp) << " " << specific_kind << " " << value;
411 return value;
412 }
413
414 JdwpTag ReadTag();
415
416 JdwpTypeTag ReadTypeTag();
417
418 JdwpLocation ReadLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
419
420 JdwpModKind ReadModKind();
421
Elliott Hughescb693062013-02-21 09:48:08 -0800422 //
423 // Return values from this JDWP packet's header.
424 //
425 size_t GetLength() { return byte_count_; }
426 uint32_t GetId() { return id_; }
427 uint8_t GetCommandSet() { return command_set_; }
428 uint8_t GetCommand() { return command_; }
429
430 // Returns the number of bytes remaining.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800431 size_t size() { return end_ - p_; }
Elliott Hughescb693062013-02-21 09:48:08 -0800432
433 // Returns a pointer to the next byte.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800434 const uint8_t* data() { return p_; }
435
436 void Skip(size_t count) { p_ += count; }
437
438 void CheckConsumed();
439
440 private:
Elliott Hughescb693062013-02-21 09:48:08 -0800441 uint8_t Read1();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800442 uint16_t Read2BE();
Elliott Hughescb693062013-02-21 09:48:08 -0800443 uint32_t Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800444 uint64_t Read8BE();
445
Elliott Hughescb693062013-02-21 09:48:08 -0800446 uint32_t byte_count_;
447 uint32_t id_;
448 uint8_t command_set_;
449 uint8_t command_;
450
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800451 const uint8_t* p_;
452 const uint8_t* end_;
453
454 DISALLOW_COPY_AND_ASSIGN(Request);
455};
456
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457} // namespace JDWP
458
459} // namespace art
460
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700461#endif // ART_RUNTIME_JDWP_JDWP_H_