blob: 0c9451ceeb51ed161e7bf525d546d39db4eec2b7 [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 {
Sebastien Hertz6995c602014-09-09 12:10:13 +020039 class ArtField;
Brian Carlstromea46f952013-07-30 01:26:50 -070040 class ArtMethod;
Sebastien Hertz6995c602014-09-09 12:10:13 +020041 class Class;
42 class Object;
43 class Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044} // namespace mirror
Sebastien Hertz6995c602014-09-09 12:10:13 +020045class Thread;
Elliott Hughes475fc232011-10-25 15:00:35 -070046
Elliott Hughes872d4ec2011-10-21 17:07:15 -070047namespace JDWP {
48
Elliott Hughes872d4ec2011-10-21 17:07:15 -070049/*
50 * Fundamental types.
51 *
52 * ObjectId and RefTypeId must be the same size.
53 */
54typedef uint32_t FieldId; /* static or instance field */
55typedef uint32_t MethodId; /* any kind of method, including constructors */
56typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
57typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
58typedef uint64_t FrameId; /* short-lived stack frame ID */
59
Elliott Hughesa96836a2013-01-17 12:27:49 -080060ObjectId ReadObjectId(const uint8_t** pBuf);
61
Elliott Hughesf7c3b662011-10-27 12:04:56 -070062static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); }
63static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); }
64static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
65static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
66static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070067static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
68static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
69static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
70static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
71static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
72
Sebastien Hertz6995c602014-09-09 12:10:13 +020073struct EventLocation {
74 mirror::ArtMethod* method;
75 uint32_t dex_pc;
76};
77
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078/*
79 * Holds a JDWP "location".
80 */
81struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070082 JdwpTypeTag type_tag;
83 RefTypeId class_id;
84 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080085 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070086};
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080089bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
90bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091
92/*
93 * How we talk to the debugger.
94 */
95enum JdwpTransportType {
96 kJdwpTransportUnknown = 0,
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070097 kJdwpTransportSocket, // transport=dt_socket
98 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -070099};
100std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
101
Elliott Hughes376a7a02011-10-24 18:35:55 -0700102struct JdwpOptions {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700103 JdwpTransportType transport;
104 bool server;
105 bool suspend;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700106 std::string host;
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800107 uint16_t port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108};
109
Elliott Hughes376a7a02011-10-24 18:35:55 -0700110struct JdwpEvent;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800111class JdwpNetStateBase;
Elliott Hughes761928d2011-11-16 18:33:03 -0800112struct ModBasket;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800113class Request;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114
115/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700116 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700118struct JdwpState {
119 /*
120 * Perform one-time initialization.
121 *
122 * Among other things, this binds to a port to listen for a connection from
123 * the debugger.
124 *
125 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
126 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 static JdwpState* Create(const JdwpOptions* options)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 LOCKS_EXCLUDED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700129
Elliott Hughes376a7a02011-10-24 18:35:55 -0700130 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131
Elliott Hughes376a7a02011-10-24 18:35:55 -0700132 /*
133 * Returns "true" if a debugger or DDM is connected.
134 */
135 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136
Elliott Hughes475fc232011-10-25 15:00:35 -0700137 /**
138 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700139 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700140 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700141
Elliott Hughes376a7a02011-10-24 18:35:55 -0700142 /*
143 * Get time, in milliseconds, since the last debugger activity.
144 */
145 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146
Elliott Hughes64f574f2013-02-20 14:57:12 -0800147 void ExitAfterReplying(int exit_status);
148
Elliott Hughes376a7a02011-10-24 18:35:55 -0700149 /*
150 * When we hit a debugger event that requires suspension, it's important
151 * that we wait for the thread to suspend itself before processing any
152 * additional requests. (Otherwise, if the debugger immediately sends a
153 * "resume thread" command, the resume might arrive before the thread has
154 * suspended itself.)
155 *
156 * The thread should call the "set" function before sending the event to
157 * the debugger. The main JDWP handler loop calls "get" before processing
158 * an event, and will wait for thread suspension if it's set. Once the
159 * thread has suspended itself, the JDWP handler calls "clear" and
160 * continues processing the current event. This works in the suspend-all
161 * case because the event thread doesn't suspend itself until everything
162 * else has suspended.
163 *
164 * It's possible that multiple threads could encounter thread-suspending
165 * events at the same time, so we grab a mutex in the "set" call, and
166 * release it in the "clear" call.
167 */
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700168 // ObjectId GetWaitForEventThread();
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100169 void SetWaitForEventThread(ObjectId threadId)
170 LOCKS_EXCLUDED(event_thread_lock_, process_request_lock_);
Ian Rogers719d1a32014-03-06 12:13:39 -0800171 void ClearWaitForEventThread() LOCKS_EXCLUDED(event_thread_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172
Elliott Hughes376a7a02011-10-24 18:35:55 -0700173 /*
174 * These notify the debug code that something interesting has happened. This
175 * could be a thread starting or ending, an exception, or an opportunity
176 * for a breakpoint. These calls do not mean that an event the debugger
177 * is interested has happened, just that something has happened that the
178 * debugger *might* be interested in.
179 *
180 * The item of interest may trigger multiple events, some or all of which
181 * are grouped together in a single response.
182 *
183 * The event may cause the current thread or all threads (except the
184 * JDWP support thread) to be suspended.
185 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700186
Elliott Hughes376a7a02011-10-24 18:35:55 -0700187 /*
188 * The VM has finished initializing. Only called when the debugger is
189 * connected at the time initialization completes.
190 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200191 bool PostVMStart() LOCKS_EXCLUDED(event_list_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700192
Elliott Hughes376a7a02011-10-24 18:35:55 -0700193 /*
194 * A location of interest has been reached. This is used for breakpoints,
195 * single-stepping, and method entry/exit. (JDWP requires that these four
196 * events are grouped together in a single response.)
197 *
198 * In some cases "*pLoc" will just have a method and class name, e.g. when
199 * issuing a MethodEntry on a native method.
200 *
201 * "eventFlags" indicates the types of events that have occurred.
Jeff Hao579b0242013-11-18 13:16:49 -0800202 *
203 * "returnValue" is non-null for MethodExit events only.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700204 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200205 bool PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr, int eventFlags,
Jeff Hao579b0242013-11-18 13:16:49 -0800206 const JValue* returnValue)
Sebastien Hertz6995c602014-09-09 12:10:13 +0200207 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209
Elliott Hughes376a7a02011-10-24 18:35:55 -0700210 /*
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200211 * A field of interest has been accessed or modified. This is used for field access and field
212 * modification events.
213 *
214 * "fieldValue" is non-null for field modification events only.
215 * "is_modification" is true for field modification, false for field access.
216 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200217 bool PostFieldEvent(const EventLocation* pLoc, mirror::ArtField* field, mirror::Object* thisPtr,
218 const JValue* fieldValue, bool is_modification)
219 LOCKS_EXCLUDED(event_list_lock_)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
221
222 /*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700223 * An exception has been thrown.
224 *
225 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
226 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200227 bool PostException(const EventLocation* pThrowLoc, mirror::Throwable* exception_object,
228 const EventLocation* pCatchLoc, mirror::Object* thisPtr)
229 LOCKS_EXCLUDED(event_list_lock_)
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 * A thread has started or stopped.
234 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200235 bool PostThreadChange(Thread* thread, bool start)
236 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238
Elliott Hughes376a7a02011-10-24 18:35:55 -0700239 /*
240 * Class has been prepared.
241 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200242 bool PostClassPrepare(mirror::Class* klass)
243 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245
Elliott Hughes376a7a02011-10-24 18:35:55 -0700246 /*
247 * The VM is about to stop.
248 */
249 bool PostVMDeath();
250
Elliott Hughesa21039c2012-06-21 12:09:25 -0700251 // Called if/when we realize we're talking to DDMS.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700252 void NotifyDdmsActive() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700253
Elliott Hughes376a7a02011-10-24 18:35:55 -0700254 /*
255 * Send up a chunk of DDM data.
256 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700259
Elliott Hughescb693062013-02-21 09:48:08 -0800260 bool HandlePacket();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700261
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700262 void SendRequest(ExpandBuf* pReq);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700263
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 void ResetState()
265 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700267
268 /* atomic ops to get next serial number */
269 uint32_t NextRequestSerial();
270 uint32_t NextEventSerial();
271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 void Run()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 LOCKS_EXCLUDED(Locks::mutator_lock_,
274 Locks::thread_suspend_count_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700275
Elliott Hughes761928d2011-11-16 18:33:03 -0800276 /*
277 * Register an event by adding it to the event list.
278 *
279 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
280 * may discard its pointer after calling this.
281 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 JdwpError RegisterEvent(JdwpEvent* pEvent)
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
286 /*
287 * Unregister an event, given the requestId.
288 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 void UnregisterEventById(uint32_t requestId)
290 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800292
293 /*
294 * Unregister all events.
295 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 void UnregisterAll()
297 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700298 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800299
Elliott Hughes376a7a02011-10-24 18:35:55 -0700300 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800301 explicit JdwpState(const JdwpOptions* options);
Ian Rogersc0542af2014-09-03 16:16:56 -0700302 size_t ProcessRequest(Request* request, ExpandBuf* pReply);
Elliott Hughes761928d2011-11-16 18:33:03 -0800303 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700304 bool IsConnected();
jeffhaoa77f0f62012-12-05 17:19:31 -0800305 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700306 LOCKS_EXCLUDED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
308 ObjectId threadId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700310 void CleanupMatchList(JdwpEvent** match_list,
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200311 size_t match_count)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800314 void EventFinish(ExpandBuf* pReq);
Elliott Hughesf8349362012-06-18 15:00:06 -0700315 void FindMatchingEvents(JdwpEventKind eventKind,
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200316 const ModBasket& basket,
Elliott Hughesf8349362012-06-18 15:00:06 -0700317 JdwpEvent** match_list,
Sebastien Hertzbca0d3d2014-04-11 16:01:17 +0200318 size_t* pMatchCount)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 void UnregisterEvent(JdwpEvent* pEvent)
322 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf5293522013-07-19 00:24:00 -0700324 void SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700325
Sebastien Hertz99660e12014-02-19 15:04:42 +0100326 void StartProcessingRequest() LOCKS_EXCLUDED(process_request_lock_);
327 void EndProcessingRequest() LOCKS_EXCLUDED(process_request_lock_);
328 void WaitForProcessingRequest() LOCKS_EXCLUDED(process_request_lock_);
329
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700330 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700331 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700332
Elliott Hughesa21039c2012-06-21 12:09:25 -0700333 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700334 /* wait for creation of the JDWP thread */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
336 ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700337
Elliott Hughes475fc232011-10-25 15:00:35 -0700338 pthread_t pthread_;
339 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 volatile int32_t debug_thread_started_ GUARDED_BY(thread_start_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700342 ObjectId debug_thread_id_;
343
Elliott Hughes74847412012-06-20 18:10:21 -0700344 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700345 bool run;
346
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700347 public: // TODO: fix privacy
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700348 JdwpNetStateBase* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700349
Elliott Hughesa21039c2012-06-21 12:09:25 -0700350 private:
351 // For wait-for-debugger.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_);
353 ConditionVariable attach_cond_ GUARDED_BY(attach_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700354
Elliott Hughesa21039c2012-06-21 12:09:25 -0700355 // Time of last debugger activity, in milliseconds.
Ian Rogers37f3c962014-07-17 11:25:30 -0700356 Atomic<int64_t> last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700357
Elliott Hughesa21039c2012-06-21 12:09:25 -0700358 // Global counters and a mutex to protect them.
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -0700359 AtomicInteger request_serial_;
360 AtomicInteger event_serial_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700361
Elliott Hughesa21039c2012-06-21 12:09:25 -0700362 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100363 Mutex event_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_BEFORE(Locks::breakpoint_lock_);
Ian Rogers719d1a32014-03-06 12:13:39 -0800364
Elliott Hughesa21039c2012-06-21 12:09:25 -0700365 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100366 size_t event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700367
Elliott Hughesa21039c2012-06-21 12:09:25 -0700368 // Used to synchronize suspension of the event thread (to avoid receiving "resume"
369 // events before the thread has finished suspending itself).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 Mutex event_thread_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
371 ConditionVariable event_thread_cond_ GUARDED_BY(event_thread_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700372 ObjectId event_thread_id_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700373
Sebastien Hertz99660e12014-02-19 15:04:42 +0100374 // Used to synchronize request processing and event sending (to avoid sending an event before
375 // sending the reply of a command being processed).
Sebastien Hertz400a3a92014-02-24 14:56:21 +0100376 Mutex process_request_lock_ ACQUIRED_AFTER(event_thread_lock_);
Sebastien Hertz99660e12014-02-19 15:04:42 +0100377 ConditionVariable process_request_cond_ GUARDED_BY(process_request_lock_);
378 bool processing_request_ GUARDED_BY(process_request_lock_);
379
Elliott Hughesa21039c2012-06-21 12:09:25 -0700380 bool ddm_is_active_;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800381
382 bool should_exit_;
383 int exit_status_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700384};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700385
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800386std::string DescribeField(const FieldId& field_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
387std::string DescribeMethod(const MethodId& method_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
388std::string DescribeRefTypeId(const RefTypeId& ref_type_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
389
390class Request {
391 public:
Elliott Hughescb693062013-02-21 09:48:08 -0800392 Request(const uint8_t* bytes, uint32_t available);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800393 ~Request();
394
395 std::string ReadUtf8String();
396
397 // Helper function: read a variable-width value from the input buffer.
398 uint64_t ReadValue(size_t width);
399
400 int32_t ReadSigned32(const char* what);
401
402 uint32_t ReadUnsigned32(const char* what);
403
404 FieldId ReadFieldId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
405
406 MethodId ReadMethodId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
407
408 ObjectId ReadObjectId(const char* specific_kind);
409
410 ObjectId ReadArrayId();
411
412 ObjectId ReadObjectId();
413
414 ObjectId ReadThreadId();
415
416 ObjectId ReadThreadGroupId();
417
418 RefTypeId ReadRefTypeId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
419
420 FrameId ReadFrameId();
421
422 template <typename T> T ReadEnum1(const char* specific_kind) {
Elliott Hughescb693062013-02-21 09:48:08 -0800423 T value = static_cast<T>(Read1());
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800424 VLOG(jdwp) << " " << specific_kind << " " << value;
425 return value;
426 }
427
428 JdwpTag ReadTag();
429
430 JdwpTypeTag ReadTypeTag();
431
432 JdwpLocation ReadLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
433
434 JdwpModKind ReadModKind();
435
Elliott Hughescb693062013-02-21 09:48:08 -0800436 //
437 // Return values from this JDWP packet's header.
438 //
439 size_t GetLength() { return byte_count_; }
440 uint32_t GetId() { return id_; }
441 uint8_t GetCommandSet() { return command_set_; }
442 uint8_t GetCommand() { return command_; }
443
444 // Returns the number of bytes remaining.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800445 size_t size() { return end_ - p_; }
Elliott Hughescb693062013-02-21 09:48:08 -0800446
447 // Returns a pointer to the next byte.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800448 const uint8_t* data() { return p_; }
449
450 void Skip(size_t count) { p_ += count; }
451
452 void CheckConsumed();
453
454 private:
Elliott Hughescb693062013-02-21 09:48:08 -0800455 uint8_t Read1();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800456 uint16_t Read2BE();
Elliott Hughescb693062013-02-21 09:48:08 -0800457 uint32_t Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800458 uint64_t Read8BE();
459
Elliott Hughescb693062013-02-21 09:48:08 -0800460 uint32_t byte_count_;
461 uint32_t id_;
462 uint8_t command_set_;
463 uint8_t command_;
464
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800465 const uint8_t* p_;
466 const uint8_t* end_;
467
468 DISALLOW_COPY_AND_ASSIGN(Request);
469};
470
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700471} // namespace JDWP
472
473} // namespace art
474
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700475#endif // ART_RUNTIME_JDWP_JDWP_H_