blob: e16221c69ad9ab2da27297693c285a0ffd0c52b7 [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
36union JValue;
37class Thread;
38
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039namespace mirror {
Sebastien Hertz6995c602014-09-09 12:10:13 +020040 class ArtField;
Brian Carlstromea46f952013-07-30 01:26:50 -070041 class ArtMethod;
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.
54 */
55typedef uint32_t FieldId; /* static or instance field */
56typedef uint32_t MethodId; /* any kind of method, including constructors */
57typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
58typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
59typedef uint64_t FrameId; /* short-lived stack frame ID */
60
Elliott Hughesa96836a2013-01-17 12:27:49 -080061ObjectId ReadObjectId(const uint8_t** pBuf);
62
Elliott Hughesf7c3b662011-10-27 12:04:56 -070063static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); }
64static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); }
65static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
66static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
67static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
69static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
70static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
71static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
72static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
73
Sebastien Hertz6995c602014-09-09 12:10:13 +020074struct EventLocation {
75 mirror::ArtMethod* method;
76 uint32_t dex_pc;
77};
78
Elliott Hughes872d4ec2011-10-21 17:07:15 -070079/*
80 * Holds a JDWP "location".
81 */
82struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070083 JdwpTypeTag type_tag;
84 RefTypeId class_id;
85 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080086 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070087};
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080090bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
91bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070092
93/*
94 * How we talk to the debugger.
95 */
96enum JdwpTransportType {
97 kJdwpTransportUnknown = 0,
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070098 kJdwpTransportSocket, // transport=dt_socket
99 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100};
101std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
102
Elliott Hughes376a7a02011-10-24 18:35:55 -0700103struct JdwpOptions {
Sebastien Hertz3be6e9d2015-02-05 16:30:58 +0100104 JdwpTransportType transport = kJdwpTransportUnknown;
105 bool server = false;
106 bool suspend = false;
107 std::string host = "";
108 uint16_t port = static_cast<uint16_t>(-1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700109};
110
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800111bool operator==(const JdwpOptions& lhs, const JdwpOptions& rhs);
112
Elliott Hughes376a7a02011-10-24 18:35:55 -0700113struct JdwpEvent;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800114class JdwpNetStateBase;
Elliott Hughes761928d2011-11-16 18:33:03 -0800115struct ModBasket;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800116class Request;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117
118/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700119 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700121struct JdwpState {
122 /*
123 * Perform one-time initialization.
124 *
125 * Among other things, this binds to a port to listen for a connection from
126 * the debugger.
127 *
128 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
129 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 static JdwpState* Create(const JdwpOptions* options)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700131 LOCKS_EXCLUDED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132
Elliott Hughes376a7a02011-10-24 18:35:55 -0700133 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700134
Elliott Hughes376a7a02011-10-24 18:35:55 -0700135 /*
136 * Returns "true" if a debugger or DDM is connected.
137 */
138 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139
Elliott Hughes475fc232011-10-25 15:00:35 -0700140 /**
141 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700142 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700143 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144
Elliott Hughes376a7a02011-10-24 18:35:55 -0700145 /*
146 * Get time, in milliseconds, since the last debugger activity.
147 */
148 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700149
Elliott Hughes64f574f2013-02-20 14:57:12 -0800150 void ExitAfterReplying(int exit_status);
151
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100152 // Acquires/releases the JDWP synchronization token for the debugger
153 // thread (command handler) so no event thread posts an event while
154 // it processes a command. This must be called only from the debugger
155 // thread.
156 void AcquireJdwpTokenForCommand() LOCKS_EXCLUDED(jdwp_token_lock_);
157 void ReleaseJdwpTokenForCommand() LOCKS_EXCLUDED(jdwp_token_lock_);
158
159 // Acquires/releases the JDWP synchronization token for the event thread
160 // so no other thread (debugger thread or event thread) interleaves with
161 // it when posting an event. This must NOT be called from the debugger
162 // thread, only event thread.
163 void AcquireJdwpTokenForEvent(ObjectId threadId) LOCKS_EXCLUDED(jdwp_token_lock_);
164 void ReleaseJdwpTokenForEvent() LOCKS_EXCLUDED(jdwp_token_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165
Elliott Hughes376a7a02011-10-24 18:35:55 -0700166 /*
167 * These notify the debug code that something interesting has happened. This
168 * could be a thread starting or ending, an exception, or an opportunity
169 * for a breakpoint. These calls do not mean that an event the debugger
170 * is interested has happened, just that something has happened that the
171 * debugger *might* be interested in.
172 *
173 * The item of interest may trigger multiple events, some or all of which
174 * are grouped together in a single response.
175 *
176 * The event may cause the current thread or all threads (except the
177 * JDWP support thread) to be suspended.
178 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179
Elliott Hughes376a7a02011-10-24 18:35:55 -0700180 /*
181 * The VM has finished initializing. Only called when the debugger is
182 * connected at the time initialization completes.
183 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200184 void PostVMStart() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185
Elliott Hughes376a7a02011-10-24 18:35:55 -0700186 /*
187 * A location of interest has been reached. This is used for breakpoints,
188 * single-stepping, and method entry/exit. (JDWP requires that these four
189 * events are grouped together in a single response.)
190 *
191 * In some cases "*pLoc" will just have a method and class name, e.g. when
192 * issuing a MethodEntry on a native method.
193 *
194 * "eventFlags" indicates the types of events that have occurred.
Jeff Hao579b0242013-11-18 13:16:49 -0800195 *
196 * "returnValue" is non-null for MethodExit events only.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700197 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200198 void PostLocationEvent(const EventLocation* pLoc, mirror::Object* thisPtr, int eventFlags,
Jeff Hao579b0242013-11-18 13:16:49 -0800199 const JValue* returnValue)
Sebastien Hertz6995c602014-09-09 12:10:13 +0200200 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
Elliott Hughes376a7a02011-10-24 18:35:55 -0700203 /*
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200204 * A field of interest has been accessed or modified. This is used for field access and field
205 * modification events.
206 *
207 * "fieldValue" is non-null for field modification events only.
208 * "is_modification" is true for field modification, false for field access.
209 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200210 void PostFieldEvent(const EventLocation* pLoc, mirror::ArtField* field, mirror::Object* thisPtr,
Sebastien Hertz6995c602014-09-09 12:10:13 +0200211 const JValue* fieldValue, bool is_modification)
212 LOCKS_EXCLUDED(event_list_lock_)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200213 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
214
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)
222 LOCKS_EXCLUDED(event_list_lock_)
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 * A thread has started or stopped.
227 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200228 void PostThreadChange(Thread* thread, bool start)
Sebastien Hertz6995c602014-09-09 12:10:13 +0200229 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 * Class has been prepared.
234 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200235 void PostClassPrepare(mirror::Class* klass)
Sebastien Hertz6995c602014-09-09 12:10:13 +0200236 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 * The VM is about to stop.
241 */
242 bool PostVMDeath();
243
Elliott Hughesa21039c2012-06-21 12:09:25 -0700244 // Called if/when we realize we're talking to DDMS.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 void NotifyDdmsActive() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700246
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800247
248 void SetupChunkHeader(uint32_t type, size_t data_len, size_t header_size, uint8_t* out_header);
249
Elliott Hughes376a7a02011-10-24 18:35:55 -0700250 /*
251 * Send up a chunk of DDM data.
252 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700255
Elliott Hughescb693062013-02-21 09:48:08 -0800256 bool HandlePacket();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700257
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700258 void SendRequest(ExpandBuf* pReq);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700259
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 void ResetState()
261 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700263
264 /* atomic ops to get next serial number */
265 uint32_t NextRequestSerial();
266 uint32_t NextEventSerial();
267
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 void Run()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700269 LOCKS_EXCLUDED(Locks::mutator_lock_,
270 Locks::thread_suspend_count_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700271
Elliott Hughes761928d2011-11-16 18:33:03 -0800272 /*
273 * Register an event by adding it to the event list.
274 *
275 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
276 * may discard its pointer after calling this.
277 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 JdwpError RegisterEvent(JdwpEvent* pEvent)
279 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800281
282 /*
283 * Unregister an event, given the requestId.
284 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 void UnregisterEventById(uint32_t requestId)
286 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800288
289 /*
290 * Unregister all events.
291 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 void UnregisterAll()
293 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700294 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800295
Elliott Hughes376a7a02011-10-24 18:35:55 -0700296 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800297 explicit JdwpState(const JdwpOptions* options);
Ian Rogersc0542af2014-09-03 16:16:56 -0700298 size_t ProcessRequest(Request* request, ExpandBuf* pReply);
Elliott Hughes761928d2011-11-16 18:33:03 -0800299 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700300 bool IsConnected();
jeffhaoa77f0f62012-12-05 17:19:31 -0800301 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700302 LOCKS_EXCLUDED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
304 ObjectId threadId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200306 void CleanupMatchList(const std::vector<JdwpEvent*>& match_list)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800309 void EventFinish(ExpandBuf* pReq);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200310 bool FindMatchingEvents(JdwpEventKind eventKind, const ModBasket& basket,
311 std::vector<JdwpEvent*>* match_list)
312 LOCKS_EXCLUDED(event_list_lock_)
313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
314 void FindMatchingEventsLocked(JdwpEventKind eventKind, const ModBasket& basket,
315 std::vector<JdwpEvent*>* match_list)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 void UnregisterEvent(JdwpEvent* pEvent)
319 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf5293522013-07-19 00:24:00 -0700321 void SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700322
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100323 /*
324 * When we hit a debugger event that requires suspension, it's important
325 * that we wait for the thread to suspend itself before processing any
326 * additional requests. Otherwise, if the debugger immediately sends a
327 * "resume thread" command, the resume might arrive before the thread has
328 * suspended itself.
329 *
330 * It's also important no event thread suspends while we process a command
331 * from the debugger. Otherwise we could post an event ("thread death")
332 * before sending the reply of the command being processed ("resume") and
333 * cause bad synchronization with the debugger.
334 *
335 * The thread wanting "exclusive" access to the JDWP world must call the
336 * SetWaitForJdwpToken method before processing a command from the
337 * debugger or sending an event to the debugger.
338 * Once the command is processed or the event thread has posted its event,
339 * it must call the ClearWaitForJdwpToken method to allow another thread
340 * to do JDWP stuff.
341 *
342 * Therefore the main JDWP handler loop will wait for the event thread
343 * suspension before processing the next command. Once the event thread
344 * has suspended itself and cleared the token, the JDWP handler continues
345 * processing commands. This works in the suspend-all case because the
346 * event thread doesn't suspend itself until everything else has suspended.
347 *
348 * It's possible that multiple threads could encounter thread-suspending
349 * events at the same time, so we grab a mutex in the SetWaitForJdwpToken
350 * call, and release it in the ClearWaitForJdwpToken call.
351 */
352 void SetWaitForJdwpToken(ObjectId threadId) LOCKS_EXCLUDED(jdwp_token_lock_);
353 void ClearWaitForJdwpToken() LOCKS_EXCLUDED(jdwp_token_lock_);
Sebastien Hertz99660e12014-02-19 15:04:42 +0100354
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700355 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700356 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700357
Elliott Hughesa21039c2012-06-21 12:09:25 -0700358 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700359 /* wait for creation of the JDWP thread */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
361 ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700362
Elliott Hughes475fc232011-10-25 15:00:35 -0700363 pthread_t pthread_;
364 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700365
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 volatile int32_t debug_thread_started_ GUARDED_BY(thread_start_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700367 ObjectId debug_thread_id_;
368
Elliott Hughes74847412012-06-20 18:10:21 -0700369 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700370 bool run;
371
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700372 public: // TODO: fix privacy
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700373 JdwpNetStateBase* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700374
Elliott Hughesa21039c2012-06-21 12:09:25 -0700375 private:
376 // For wait-for-debugger.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_);
378 ConditionVariable attach_cond_ GUARDED_BY(attach_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700379
Elliott Hughesa21039c2012-06-21 12:09:25 -0700380 // Time of last debugger activity, in milliseconds.
Ian Rogers37f3c962014-07-17 11:25:30 -0700381 Atomic<int64_t> last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700382
Elliott Hughesa21039c2012-06-21 12:09:25 -0700383 // Global counters and a mutex to protect them.
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -0700384 AtomicInteger request_serial_;
385 AtomicInteger event_serial_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700386
Elliott Hughesa21039c2012-06-21 12:09:25 -0700387 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100388 Mutex event_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_BEFORE(Locks::breakpoint_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700389 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100390 size_t event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700391
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100392 // Used to synchronize JDWP command handler thread and event threads so only one
393 // thread does JDWP stuff at a time. This prevent from interleaving command handling
394 // and event notification. Otherwise we could receive a "resume" command for an
395 // event thread that is not suspended yet, or post a "thread death" or event "VM death"
396 // event before sending the reply of the "resume" command that caused it.
397 Mutex jdwp_token_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
398 ConditionVariable jdwp_token_cond_ GUARDED_BY(jdwp_token_lock_);
399 ObjectId jdwp_token_owner_thread_id_;
Sebastien Hertz99660e12014-02-19 15:04:42 +0100400
Elliott Hughesa21039c2012-06-21 12:09:25 -0700401 bool ddm_is_active_;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800402
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100403 // Used for VirtualMachine.Exit command handling.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800404 bool should_exit_;
405 int exit_status_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700406};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800408std::string DescribeField(const FieldId& field_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
409std::string DescribeMethod(const MethodId& method_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
410std::string DescribeRefTypeId(const RefTypeId& ref_type_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
411
412class Request {
413 public:
Elliott Hughescb693062013-02-21 09:48:08 -0800414 Request(const uint8_t* bytes, uint32_t available);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800415 ~Request();
416
417 std::string ReadUtf8String();
418
419 // Helper function: read a variable-width value from the input buffer.
420 uint64_t ReadValue(size_t width);
421
422 int32_t ReadSigned32(const char* what);
423
424 uint32_t ReadUnsigned32(const char* what);
425
426 FieldId ReadFieldId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
427
428 MethodId ReadMethodId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
429
430 ObjectId ReadObjectId(const char* specific_kind);
431
432 ObjectId ReadArrayId();
433
434 ObjectId ReadObjectId();
435
436 ObjectId ReadThreadId();
437
438 ObjectId ReadThreadGroupId();
439
440 RefTypeId ReadRefTypeId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
441
442 FrameId ReadFrameId();
443
444 template <typename T> T ReadEnum1(const char* specific_kind) {
Elliott Hughescb693062013-02-21 09:48:08 -0800445 T value = static_cast<T>(Read1());
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800446 VLOG(jdwp) << " " << specific_kind << " " << value;
447 return value;
448 }
449
450 JdwpTag ReadTag();
451
452 JdwpTypeTag ReadTypeTag();
453
454 JdwpLocation ReadLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
455
456 JdwpModKind ReadModKind();
457
Elliott Hughescb693062013-02-21 09:48:08 -0800458 //
459 // Return values from this JDWP packet's header.
460 //
461 size_t GetLength() { return byte_count_; }
462 uint32_t GetId() { return id_; }
463 uint8_t GetCommandSet() { return command_set_; }
464 uint8_t GetCommand() { return command_; }
465
466 // Returns the number of bytes remaining.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800467 size_t size() { return end_ - p_; }
Elliott Hughescb693062013-02-21 09:48:08 -0800468
469 // Returns a pointer to the next byte.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800470 const uint8_t* data() { return p_; }
471
472 void Skip(size_t count) { p_ += count; }
473
474 void CheckConsumed();
475
476 private:
Elliott Hughescb693062013-02-21 09:48:08 -0800477 uint8_t Read1();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800478 uint16_t Read2BE();
Elliott Hughescb693062013-02-21 09:48:08 -0800479 uint32_t Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800480 uint64_t Read8BE();
481
Elliott Hughescb693062013-02-21 09:48:08 -0800482 uint32_t byte_count_;
483 uint32_t id_;
484 uint8_t command_set_;
485 uint8_t command_;
486
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800487 const uint8_t* p_;
488 const uint8_t* end_;
489
490 DISALLOW_COPY_AND_ASSIGN(Request);
491};
492
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493} // namespace JDWP
494
495} // namespace art
496
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700497#endif // ART_RUNTIME_JDWP_JDWP_H_