blob: 71bae087feb9405adcd3de593104068a9e747900 [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
17#ifndef ART_JDWP_JDWP_H_
18#define ART_JDWP_JDWP_H_
19
Elliott Hughes76b61672012-12-12 17:47:30 -080020#include "base/mutex.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070021#include "jdwp/jdwp_bits.h"
22#include "jdwp/jdwp_constants.h"
23#include "jdwp/jdwp_expand_buf.h"
24
25#include <pthread.h>
26#include <stddef.h>
27#include <stdint.h>
28#include <string.h>
29
30struct iovec;
31
32namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace mirror {
Mathieu Chartier66f19252012-09-18 08:57:04 -070034class AbstractMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035} // namespace mirror
Ian Rogers1b09b092012-08-20 15:35:52 -070036class Thread;
Elliott Hughes475fc232011-10-25 15:00:35 -070037
Elliott Hughes872d4ec2011-10-21 17:07:15 -070038namespace JDWP {
39
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040/*
41 * Fundamental types.
42 *
43 * ObjectId and RefTypeId must be the same size.
44 */
45typedef uint32_t FieldId; /* static or instance field */
46typedef uint32_t MethodId; /* any kind of method, including constructors */
47typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
48typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
49typedef uint64_t FrameId; /* short-lived stack frame ID */
50
Elliott Hughesa96836a2013-01-17 12:27:49 -080051ObjectId ReadObjectId(const uint8_t** pBuf);
52
Elliott Hughesf7c3b662011-10-27 12:04:56 -070053static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); }
54static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); }
55static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
56static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
57static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070058static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
59static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
60static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
61static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
62static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
63
Elliott Hughes872d4ec2011-10-21 17:07:15 -070064/*
65 * Holds a JDWP "location".
66 */
67struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070068 JdwpTypeTag type_tag;
69 RefTypeId class_id;
70 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080071 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072};
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -070074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080075bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
76bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077
78/*
79 * How we talk to the debugger.
80 */
81enum JdwpTransportType {
82 kJdwpTransportUnknown = 0,
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070083 kJdwpTransportSocket, // transport=dt_socket
84 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -070085};
86std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
87
Elliott Hughes376a7a02011-10-24 18:35:55 -070088struct JdwpOptions {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089 JdwpTransportType transport;
90 bool server;
91 bool suspend;
Elliott Hughesd1cc8362011-10-24 16:58:50 -070092 std::string host;
Elliott Hughes6d8dd472012-01-17 18:27:41 -080093 uint16_t port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070094};
95
Elliott Hughes376a7a02011-10-24 18:35:55 -070096struct JdwpEvent;
97struct JdwpNetState;
98struct JdwpReqHeader;
99struct JdwpTransport;
Elliott Hughes761928d2011-11-16 18:33:03 -0800100struct ModBasket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700101
102/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700103 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700104 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700105struct JdwpState {
106 /*
107 * Perform one-time initialization.
108 *
109 * Among other things, this binds to a port to listen for a connection from
110 * the debugger.
111 *
112 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
113 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 static JdwpState* Create(const JdwpOptions* options)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 LOCKS_EXCLUDED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700116
Elliott Hughes376a7a02011-10-24 18:35:55 -0700117 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700118
Elliott Hughes376a7a02011-10-24 18:35:55 -0700119 /*
120 * Returns "true" if a debugger or DDM is connected.
121 */
122 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123
Elliott Hughes475fc232011-10-25 15:00:35 -0700124 /**
125 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700126 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700127 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128
Elliott Hughes376a7a02011-10-24 18:35:55 -0700129 /*
130 * Get time, in milliseconds, since the last debugger activity.
131 */
132 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133
Elliott Hughes376a7a02011-10-24 18:35:55 -0700134 /*
135 * When we hit a debugger event that requires suspension, it's important
136 * that we wait for the thread to suspend itself before processing any
137 * additional requests. (Otherwise, if the debugger immediately sends a
138 * "resume thread" command, the resume might arrive before the thread has
139 * suspended itself.)
140 *
141 * The thread should call the "set" function before sending the event to
142 * the debugger. The main JDWP handler loop calls "get" before processing
143 * an event, and will wait for thread suspension if it's set. Once the
144 * thread has suspended itself, the JDWP handler calls "clear" and
145 * continues processing the current event. This works in the suspend-all
146 * case because the event thread doesn't suspend itself until everything
147 * else has suspended.
148 *
149 * It's possible that multiple threads could encounter thread-suspending
150 * events at the same time, so we grab a mutex in the "set" call, and
151 * release it in the "clear" call.
152 */
153 //ObjectId GetWaitForEventThread();
154 void SetWaitForEventThread(ObjectId threadId);
155 void ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156
Elliott Hughes376a7a02011-10-24 18:35:55 -0700157 /*
158 * These notify the debug code that something interesting has happened. This
159 * could be a thread starting or ending, an exception, or an opportunity
160 * for a breakpoint. These calls do not mean that an event the debugger
161 * is interested has happened, just that something has happened that the
162 * debugger *might* be interested in.
163 *
164 * The item of interest may trigger multiple events, some or all of which
165 * are grouped together in a single response.
166 *
167 * The event may cause the current thread or all threads (except the
168 * JDWP support thread) to be suspended.
169 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170
Elliott Hughes376a7a02011-10-24 18:35:55 -0700171 /*
172 * The VM has finished initializing. Only called when the debugger is
173 * connected at the time initialization completes.
174 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 bool PostVMStart() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176
Elliott Hughes376a7a02011-10-24 18:35:55 -0700177 /*
178 * A location of interest has been reached. This is used for breakpoints,
179 * single-stepping, and method entry/exit. (JDWP requires that these four
180 * events are grouped together in a single response.)
181 *
182 * In some cases "*pLoc" will just have a method and class name, e.g. when
183 * issuing a MethodEntry on a native method.
184 *
185 * "eventFlags" indicates the types of events that have occurred.
186 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187 bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189
Elliott Hughes376a7a02011-10-24 18:35:55 -0700190 /*
191 * An exception has been thrown.
192 *
193 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
194 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195 bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId,
196 const JdwpLocation* pCatchLoc, ObjectId thisPtr)
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 /*
200 * A thread has started or stopped.
201 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 bool PostThreadChange(ObjectId threadId, bool start)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700204
Elliott Hughes376a7a02011-10-24 18:35:55 -0700205 /*
206 * Class has been prepared.
207 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 bool PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
209 int status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211
Elliott Hughes376a7a02011-10-24 18:35:55 -0700212 /*
213 * The VM is about to stop.
214 */
215 bool PostVMDeath();
216
Elliott Hughesa21039c2012-06-21 12:09:25 -0700217 // Called if/when we realize we're talking to DDMS.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 void NotifyDdmsActive() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700219
Elliott Hughes376a7a02011-10-24 18:35:55 -0700220 /*
221 * Send up a chunk of DDM data.
222 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700225
226 /*
227 * Process a request from the debugger.
228 *
229 * "buf" points past the header, to the content of the message. "dataLen"
230 * can therefore be zero.
231 */
232 void ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply);
233
234 /*
235 * Send an event, formatted into "pReq", to the debugger.
236 *
237 * (Messages are sent asynchronously, and do not receive a reply.)
238 */
239 bool SendRequest(ExpandBuf* pReq);
240
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 void ResetState()
242 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700244
245 /* atomic ops to get next serial number */
246 uint32_t NextRequestSerial();
247 uint32_t NextEventSerial();
248
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 void Run()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 LOCKS_EXCLUDED(Locks::mutator_lock_,
251 Locks::thread_suspend_count_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700252
Elliott Hughes761928d2011-11-16 18:33:03 -0800253 /*
254 * Register an event by adding it to the event list.
255 *
256 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
257 * may discard its pointer after calling this.
258 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 JdwpError RegisterEvent(JdwpEvent* pEvent)
260 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800262
263 /*
264 * Unregister an event, given the requestId.
265 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 void UnregisterEventById(uint32_t requestId)
267 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800269
270 /*
271 * Unregister all events.
272 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 void UnregisterAll()
274 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800276
Elliott Hughes376a7a02011-10-24 18:35:55 -0700277 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800278 explicit JdwpState(const JdwpOptions* options);
Elliott Hughes761928d2011-11-16 18:33:03 -0800279 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700280 bool IsConnected();
jeffhaoa77f0f62012-12-05 17:19:31 -0800281 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700282 LOCKS_EXCLUDED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
284 ObjectId threadId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700286 void CleanupMatchList(JdwpEvent** match_list,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 int match_count)
288 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700289 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800290 void EventFinish(ExpandBuf* pReq);
Elliott Hughesf8349362012-06-18 15:00:06 -0700291 void FindMatchingEvents(JdwpEventKind eventKind,
292 ModBasket* basket,
293 JdwpEvent** match_list,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 int* pMatchCount)
295 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 void UnregisterEvent(JdwpEvent* pEvent)
298 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700300
Elliott Hughes74847412012-06-20 18:10:21 -0700301 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700302 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700303
Elliott Hughesa21039c2012-06-21 12:09:25 -0700304 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700305 /* wait for creation of the JDWP thread */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306 Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
307 ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700308
Elliott Hughes475fc232011-10-25 15:00:35 -0700309 pthread_t pthread_;
310 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700311
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 volatile int32_t debug_thread_started_ GUARDED_BY(thread_start_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700313 ObjectId debug_thread_id_;
314
Elliott Hughes74847412012-06-20 18:10:21 -0700315 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700316 bool run;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700317 const JdwpTransport* transport_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700318
Elliott Hughes74847412012-06-20 18:10:21 -0700319 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700320 JdwpNetState* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700321
Elliott Hughesa21039c2012-06-21 12:09:25 -0700322 private:
323 // For wait-for-debugger.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_);
325 ConditionVariable attach_cond_ GUARDED_BY(attach_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700326
Elliott Hughesa21039c2012-06-21 12:09:25 -0700327 // Time of last debugger activity, in milliseconds.
328 int64_t last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700329
Elliott Hughesa21039c2012-06-21 12:09:25 -0700330 // Global counters and a mutex to protect them.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 Mutex serial_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700332 uint32_t request_serial_ GUARDED_BY(serial_lock_);
333 uint32_t event_serial_ GUARDED_BY(serial_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700334
Elliott Hughesa21039c2012-06-21 12:09:25 -0700335 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Elliott Hughesf8349362012-06-18 15:00:06 -0700336 Mutex event_list_lock_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700337 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700338 int event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700339
Elliott Hughesa21039c2012-06-21 12:09:25 -0700340 // Used to synchronize suspension of the event thread (to avoid receiving "resume"
341 // events before the thread has finished suspending itself).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 Mutex event_thread_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
343 ConditionVariable event_thread_cond_ GUARDED_BY(event_thread_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700344 ObjectId event_thread_id_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700345
Elliott Hughesa21039c2012-06-21 12:09:25 -0700346 bool ddm_is_active_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700347};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700348
349} // namespace JDWP
350
351} // namespace art
352
353#endif // ART_JDWP_JDWP_H_