blob: 6cac0f678d6cdf0d51b9918f2d2269cea2e1b281 [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 {
33
Mathieu Chartier66f19252012-09-18 08:57:04 -070034class AbstractMethod;
Ian Rogers1b09b092012-08-20 15:35:52 -070035class Thread;
Elliott Hughes475fc232011-10-25 15:00:35 -070036
Elliott Hughes872d4ec2011-10-21 17:07:15 -070037namespace JDWP {
38
Elliott Hughes872d4ec2011-10-21 17:07:15 -070039/*
40 * Fundamental types.
41 *
42 * ObjectId and RefTypeId must be the same size.
43 */
44typedef uint32_t FieldId; /* static or instance field */
45typedef uint32_t MethodId; /* any kind of method, including constructors */
46typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
47typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
48typedef uint64_t FrameId; /* short-lived stack frame ID */
49
Elliott Hughesa96836a2013-01-17 12:27:49 -080050ObjectId ReadObjectId(const uint8_t** pBuf);
51
Elliott Hughesf7c3b662011-10-27 12:04:56 -070052static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); }
53static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); }
54static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
55static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
56static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
58static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
59static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
60static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
61static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
62
Elliott Hughes872d4ec2011-10-21 17:07:15 -070063/*
64 * Holds a JDWP "location".
65 */
66struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070067 JdwpTypeTag type_tag;
68 RefTypeId class_id;
69 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080070 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070071};
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080074bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
75bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070076
77/*
78 * How we talk to the debugger.
79 */
80enum JdwpTransportType {
81 kJdwpTransportUnknown = 0,
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070082 kJdwpTransportSocket, // transport=dt_socket
83 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -070084};
85std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
86
Elliott Hughes376a7a02011-10-24 18:35:55 -070087struct JdwpOptions {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070088 JdwpTransportType transport;
89 bool server;
90 bool suspend;
Elliott Hughesd1cc8362011-10-24 16:58:50 -070091 std::string host;
Elliott Hughes6d8dd472012-01-17 18:27:41 -080092 uint16_t port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070093};
94
Elliott Hughes376a7a02011-10-24 18:35:55 -070095struct JdwpEvent;
96struct JdwpNetState;
97struct JdwpReqHeader;
98struct JdwpTransport;
Elliott Hughes761928d2011-11-16 18:33:03 -080099struct ModBasket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100
101/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700102 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700103 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700104struct JdwpState {
105 /*
106 * Perform one-time initialization.
107 *
108 * Among other things, this binds to a port to listen for a connection from
109 * the debugger.
110 *
111 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
112 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113 static JdwpState* Create(const JdwpOptions* options)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700114 LOCKS_EXCLUDED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700115
Elliott Hughes376a7a02011-10-24 18:35:55 -0700116 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117
Elliott Hughes376a7a02011-10-24 18:35:55 -0700118 /*
119 * Returns "true" if a debugger or DDM is connected.
120 */
121 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122
Elliott Hughes475fc232011-10-25 15:00:35 -0700123 /**
124 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700125 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700126 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127
Elliott Hughes376a7a02011-10-24 18:35:55 -0700128 /*
129 * Get time, in milliseconds, since the last debugger activity.
130 */
131 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132
Elliott Hughes376a7a02011-10-24 18:35:55 -0700133 /*
134 * When we hit a debugger event that requires suspension, it's important
135 * that we wait for the thread to suspend itself before processing any
136 * additional requests. (Otherwise, if the debugger immediately sends a
137 * "resume thread" command, the resume might arrive before the thread has
138 * suspended itself.)
139 *
140 * The thread should call the "set" function before sending the event to
141 * the debugger. The main JDWP handler loop calls "get" before processing
142 * an event, and will wait for thread suspension if it's set. Once the
143 * thread has suspended itself, the JDWP handler calls "clear" and
144 * continues processing the current event. This works in the suspend-all
145 * case because the event thread doesn't suspend itself until everything
146 * else has suspended.
147 *
148 * It's possible that multiple threads could encounter thread-suspending
149 * events at the same time, so we grab a mutex in the "set" call, and
150 * release it in the "clear" call.
151 */
152 //ObjectId GetWaitForEventThread();
153 void SetWaitForEventThread(ObjectId threadId);
154 void ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700155
Elliott Hughes376a7a02011-10-24 18:35:55 -0700156 /*
157 * These notify the debug code that something interesting has happened. This
158 * could be a thread starting or ending, an exception, or an opportunity
159 * for a breakpoint. These calls do not mean that an event the debugger
160 * is interested has happened, just that something has happened that the
161 * debugger *might* be interested in.
162 *
163 * The item of interest may trigger multiple events, some or all of which
164 * are grouped together in a single response.
165 *
166 * The event may cause the current thread or all threads (except the
167 * JDWP support thread) to be suspended.
168 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169
Elliott Hughes376a7a02011-10-24 18:35:55 -0700170 /*
171 * The VM has finished initializing. Only called when the debugger is
172 * connected at the time initialization completes.
173 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700174 bool PostVMStart() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700175
Elliott Hughes376a7a02011-10-24 18:35:55 -0700176 /*
177 * A location of interest has been reached. This is used for breakpoints,
178 * single-stepping, and method entry/exit. (JDWP requires that these four
179 * events are grouped together in a single response.)
180 *
181 * In some cases "*pLoc" will just have a method and class name, e.g. when
182 * issuing a MethodEntry on a native method.
183 *
184 * "eventFlags" indicates the types of events that have occurred.
185 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700186 bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700188
Elliott Hughes376a7a02011-10-24 18:35:55 -0700189 /*
190 * An exception has been thrown.
191 *
192 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
193 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194 bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId,
195 const JdwpLocation* pCatchLoc, ObjectId thisPtr)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197
Elliott Hughes376a7a02011-10-24 18:35:55 -0700198 /*
199 * A thread has started or stopped.
200 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 bool PostThreadChange(ObjectId threadId, bool start)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203
Elliott Hughes376a7a02011-10-24 18:35:55 -0700204 /*
205 * Class has been prepared.
206 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207 bool PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature,
208 int status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210
Elliott Hughes376a7a02011-10-24 18:35:55 -0700211 /*
212 * The VM is about to stop.
213 */
214 bool PostVMDeath();
215
Elliott Hughesa21039c2012-06-21 12:09:25 -0700216 // Called if/when we realize we're talking to DDMS.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700217 void NotifyDdmsActive() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700218
Elliott Hughes376a7a02011-10-24 18:35:55 -0700219 /*
220 * Send up a chunk of DDM data.
221 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700224
225 /*
226 * Process a request from the debugger.
227 *
228 * "buf" points past the header, to the content of the message. "dataLen"
229 * can therefore be zero.
230 */
231 void ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply);
232
233 /*
234 * Send an event, formatted into "pReq", to the debugger.
235 *
236 * (Messages are sent asynchronously, and do not receive a reply.)
237 */
238 bool SendRequest(ExpandBuf* pReq);
239
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 void ResetState()
241 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700243
244 /* atomic ops to get next serial number */
245 uint32_t NextRequestSerial();
246 uint32_t NextEventSerial();
247
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 void Run()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 LOCKS_EXCLUDED(Locks::mutator_lock_,
250 Locks::thread_suspend_count_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700251
Elliott Hughes761928d2011-11-16 18:33:03 -0800252 /*
253 * Register an event by adding it to the event list.
254 *
255 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
256 * may discard its pointer after calling this.
257 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 JdwpError RegisterEvent(JdwpEvent* pEvent)
259 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700260 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800261
262 /*
263 * Unregister an event, given the requestId.
264 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 void UnregisterEventById(uint32_t requestId)
266 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800268
269 /*
270 * Unregister all events.
271 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 void UnregisterAll()
273 LOCKS_EXCLUDED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800275
Elliott Hughes376a7a02011-10-24 18:35:55 -0700276 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800277 explicit JdwpState(const JdwpOptions* options);
Elliott Hughes761928d2011-11-16 18:33:03 -0800278 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700279 bool IsConnected();
jeffhaoa77f0f62012-12-05 17:19:31 -0800280 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700281 LOCKS_EXCLUDED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy,
283 ObjectId threadId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700285 void CleanupMatchList(JdwpEvent** match_list,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 int match_count)
287 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800289 void EventFinish(ExpandBuf* pReq);
Elliott Hughesf8349362012-06-18 15:00:06 -0700290 void FindMatchingEvents(JdwpEventKind eventKind,
291 ModBasket* basket,
292 JdwpEvent** match_list,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 int* pMatchCount)
294 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 void UnregisterEvent(JdwpEvent* pEvent)
297 EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700298 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700299
Elliott Hughes74847412012-06-20 18:10:21 -0700300 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700301 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700302
Elliott Hughesa21039c2012-06-21 12:09:25 -0700303 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700304 /* wait for creation of the JDWP thread */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
306 ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700307
Elliott Hughes475fc232011-10-25 15:00:35 -0700308 pthread_t pthread_;
309 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700310
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 volatile int32_t debug_thread_started_ GUARDED_BY(thread_start_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700312 ObjectId debug_thread_id_;
313
Elliott Hughes74847412012-06-20 18:10:21 -0700314 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700315 bool run;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700316 const JdwpTransport* transport_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700317
Elliott Hughes74847412012-06-20 18:10:21 -0700318 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700319 JdwpNetState* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700320
Elliott Hughesa21039c2012-06-21 12:09:25 -0700321 private:
322 // For wait-for-debugger.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323 Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_);
324 ConditionVariable attach_cond_ GUARDED_BY(attach_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700325
Elliott Hughesa21039c2012-06-21 12:09:25 -0700326 // Time of last debugger activity, in milliseconds.
327 int64_t last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700328
Elliott Hughesa21039c2012-06-21 12:09:25 -0700329 // Global counters and a mutex to protect them.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700330 Mutex serial_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700331 uint32_t request_serial_ GUARDED_BY(serial_lock_);
332 uint32_t event_serial_ GUARDED_BY(serial_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700333
Elliott Hughesa21039c2012-06-21 12:09:25 -0700334 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Elliott Hughesf8349362012-06-18 15:00:06 -0700335 Mutex event_list_lock_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700336 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700337 int event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700338
Elliott Hughesa21039c2012-06-21 12:09:25 -0700339 // Used to synchronize suspension of the event thread (to avoid receiving "resume"
340 // events before the thread has finished suspending itself).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 Mutex event_thread_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
342 ConditionVariable event_thread_cond_ GUARDED_BY(event_thread_lock_);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700343 ObjectId event_thread_id_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700344
Elliott Hughesa21039c2012-06-21 12:09:25 -0700345 bool ddm_is_active_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700346};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347
348} // namespace JDWP
349
350} // namespace art
351
352#endif // ART_JDWP_JDWP_H_