blob: 8534c8e5de427ae0e8ce1cf5aeb1b748639fe69c [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
20#include "jdwp/jdwp_bits.h"
21#include "jdwp/jdwp_constants.h"
22#include "jdwp/jdwp_expand_buf.h"
Elliott Hughes376a7a02011-10-24 18:35:55 -070023#include "../mutex.h" // TODO: fix our include path!
Elliott Hughes872d4ec2011-10-21 17:07:15 -070024
25#include <pthread.h>
26#include <stddef.h>
27#include <stdint.h>
28#include <string.h>
29
30struct iovec;
31
32namespace art {
33
Elliott Hughesd07986f2011-12-06 18:27:45 -080034struct Method;
Elliott Hughes475fc232011-10-25 15:00:35 -070035struct Thread;
36
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
50/*
51 * Match these with the type sizes. This way we don't have to pass
52 * a value and a length.
53 */
Elliott Hughesf7c3b662011-10-27 12:04:56 -070054static inline FieldId ReadFieldId(const uint8_t** pBuf) { return Read4BE(pBuf); }
55static inline MethodId ReadMethodId(const uint8_t** pBuf) { return Read4BE(pBuf); }
56static inline ObjectId ReadObjectId(const uint8_t** pBuf) { return Read8BE(pBuf); }
57static inline RefTypeId ReadRefTypeId(const uint8_t** pBuf) { return Read8BE(pBuf); }
58static inline FrameId ReadFrameId(const uint8_t** pBuf) { return Read8BE(pBuf); }
Elliott Hughesaed4be92011-12-02 16:16:23 -080059static inline JdwpTag ReadTag(const uint8_t** pBuf) { return static_cast<JdwpTag>(Read1(pBuf)); }
Elliott Hughesd07986f2011-12-06 18:27:45 -080060static inline JdwpTypeTag ReadTypeTag(const uint8_t** pBuf) { return static_cast<JdwpTypeTag>(Read1(pBuf)); }
Elliott Hughesf7c3b662011-10-27 12:04:56 -070061static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); }
62static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); }
63static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
64static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
65static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070066static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
67static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
68static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
69static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
70static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
71
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072/*
73 * Holds a JDWP "location".
74 */
75struct JdwpLocation {
Elliott Hughes74847412012-06-20 18:10:21 -070076 JdwpTypeTag type_tag;
77 RefTypeId class_id;
78 MethodId method_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -080079 uint64_t dex_pc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080};
Elliott Hughes03181a82011-11-17 17:22:21 -080081std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs);
Elliott Hughes2aa2e392012-02-17 17:15:43 -080082bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs);
83bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070084
85/*
86 * How we talk to the debugger.
87 */
88enum JdwpTransportType {
89 kJdwpTransportUnknown = 0,
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070090 kJdwpTransportSocket, // transport=dt_socket
91 kJdwpTransportAndroidAdb, // transport=dt_android_adb
Elliott Hughes872d4ec2011-10-21 17:07:15 -070092};
93std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
94
Elliott Hughes376a7a02011-10-24 18:35:55 -070095struct JdwpOptions {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070096 JdwpTransportType transport;
97 bool server;
98 bool suspend;
Elliott Hughesd1cc8362011-10-24 16:58:50 -070099 std::string host;
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800100 uint16_t port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700101};
102
Elliott Hughes376a7a02011-10-24 18:35:55 -0700103struct JdwpEvent;
104struct JdwpNetState;
105struct JdwpReqHeader;
106struct JdwpTransport;
Elliott Hughes761928d2011-11-16 18:33:03 -0800107struct ModBasket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108
109/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700110 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700111 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700112struct JdwpState {
113 /*
114 * Perform one-time initialization.
115 *
116 * Among other things, this binds to a port to listen for a connection from
117 * the debugger.
118 *
119 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
120 */
Elliott Hughesf8349362012-06-18 15:00:06 -0700121 static JdwpState* Create(const JdwpOptions* options) NO_THREAD_SAFETY_ANALYSIS; // TODO: make GCC understand.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122
Elliott Hughes376a7a02011-10-24 18:35:55 -0700123 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124
Elliott Hughes376a7a02011-10-24 18:35:55 -0700125 /*
126 * Returns "true" if a debugger or DDM is connected.
127 */
128 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700129
Elliott Hughes475fc232011-10-25 15:00:35 -0700130 /**
131 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700132 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700133 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700134
Elliott Hughes376a7a02011-10-24 18:35:55 -0700135 /*
136 * Get time, in milliseconds, since the last debugger activity.
137 */
138 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139
Elliott Hughes376a7a02011-10-24 18:35:55 -0700140 /*
141 * When we hit a debugger event that requires suspension, it's important
142 * that we wait for the thread to suspend itself before processing any
143 * additional requests. (Otherwise, if the debugger immediately sends a
144 * "resume thread" command, the resume might arrive before the thread has
145 * suspended itself.)
146 *
147 * The thread should call the "set" function before sending the event to
148 * the debugger. The main JDWP handler loop calls "get" before processing
149 * an event, and will wait for thread suspension if it's set. Once the
150 * thread has suspended itself, the JDWP handler calls "clear" and
151 * continues processing the current event. This works in the suspend-all
152 * case because the event thread doesn't suspend itself until everything
153 * else has suspended.
154 *
155 * It's possible that multiple threads could encounter thread-suspending
156 * events at the same time, so we grab a mutex in the "set" call, and
157 * release it in the "clear" call.
158 */
159 //ObjectId GetWaitForEventThread();
160 void SetWaitForEventThread(ObjectId threadId);
161 void ClearWaitForEventThread();
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 */
181 bool PostVMStart();
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.
192 */
193 bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194
Elliott Hughes376a7a02011-10-24 18:35:55 -0700195 /*
196 * An exception has been thrown.
197 *
198 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
199 */
200 bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId, const JdwpLocation* pCatchLoc, ObjectId thisPtr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201
Elliott Hughes376a7a02011-10-24 18:35:55 -0700202 /*
203 * A thread has started or stopped.
204 */
205 bool PostThreadChange(ObjectId threadId, bool start);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206
Elliott Hughes376a7a02011-10-24 18:35:55 -0700207 /*
208 * Class has been prepared.
209 */
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800210 bool PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature, int status);
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.
218 void NotifyDdmsActive();
219
Elliott Hughes376a7a02011-10-24 18:35:55 -0700220 /*
221 * Send up a chunk of DDM data.
222 */
Elliott Hughescccd84f2011-12-05 16:51:54 -0800223 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count);
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
240 void ResetState();
241
242 /* atomic ops to get next serial number */
243 uint32_t NextRequestSerial();
244 uint32_t NextEventSerial();
245
246 void Run();
247
Elliott Hughes761928d2011-11-16 18:33:03 -0800248 /*
249 * Register an event by adding it to the event list.
250 *
251 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
252 * may discard its pointer after calling this.
253 */
254 JdwpError RegisterEvent(JdwpEvent* pEvent);
255
256 /*
257 * Unregister an event, given the requestId.
258 */
259 void UnregisterEventById(uint32_t requestId);
260
261 /*
262 * Unregister all events.
263 */
264 void UnregisterAll();
265
Elliott Hughes376a7a02011-10-24 18:35:55 -0700266 private:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800267 explicit JdwpState(const JdwpOptions* options);
Elliott Hughes761928d2011-11-16 18:33:03 -0800268 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700269 bool IsConnected();
Elliott Hughesf8349362012-06-18 15:00:06 -0700270 void SuspendByPolicy(JdwpSuspendPolicy suspend_policy);
271 void CleanupMatchList(JdwpEvent** match_list,
272 int match_count) EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_);
Elliott Hughes761928d2011-11-16 18:33:03 -0800273 void EventFinish(ExpandBuf* pReq);
Elliott Hughesf8349362012-06-18 15:00:06 -0700274 void FindMatchingEvents(JdwpEventKind eventKind,
275 ModBasket* basket,
276 JdwpEvent** match_list,
277 int* pMatchCount) EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_);
278 void UnregisterEvent(JdwpEvent* pEvent) EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700279
Elliott Hughes74847412012-06-20 18:10:21 -0700280 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700281 const JdwpOptions* options_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700282
Elliott Hughesa21039c2012-06-21 12:09:25 -0700283 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700284 /* wait for creation of the JDWP thread */
285 Mutex thread_start_lock_;
286 ConditionVariable thread_start_cond_;
287
Elliott Hughes475fc232011-10-25 15:00:35 -0700288 pthread_t pthread_;
289 Thread* thread_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700290
291 volatile int32_t debug_thread_started_;
292 ObjectId debug_thread_id_;
293
Elliott Hughes74847412012-06-20 18:10:21 -0700294 private:
Elliott Hughes376a7a02011-10-24 18:35:55 -0700295 bool run;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700296 const JdwpTransport* transport_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700297
Elliott Hughes74847412012-06-20 18:10:21 -0700298 public: // TODO: fix privacy
Elliott Hughes376a7a02011-10-24 18:35:55 -0700299 JdwpNetState* netState;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700300
Elliott Hughesa21039c2012-06-21 12:09:25 -0700301 private:
302 // For wait-for-debugger.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700303 Mutex attach_lock_;
304 ConditionVariable attach_cond_;
305
Elliott Hughesa21039c2012-06-21 12:09:25 -0700306 // Time of last debugger activity, in milliseconds.
307 int64_t last_activity_time_ms_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700308
Elliott Hughesa21039c2012-06-21 12:09:25 -0700309 // Global counters and a mutex to protect them.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700310 Mutex serial_lock_;
Elliott Hughesf8349362012-06-18 15:00:06 -0700311 uint32_t request_serial_ GUARDED_BY(serial_lock_);
312 uint32_t event_serial_ GUARDED_BY(serial_lock_);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700313
Elliott Hughesa21039c2012-06-21 12:09:25 -0700314 // Linked list of events requested by the debugger (breakpoints, class prep, etc).
Elliott Hughesf8349362012-06-18 15:00:06 -0700315 Mutex event_list_lock_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700316 JdwpEvent* event_list_ GUARDED_BY(event_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700317 int event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700318
Elliott Hughesa21039c2012-06-21 12:09:25 -0700319 // Used to synchronize suspension of the event thread (to avoid receiving "resume"
320 // events before the thread has finished suspending itself).
Elliott Hughes376a7a02011-10-24 18:35:55 -0700321 Mutex event_thread_lock_;
322 ConditionVariable event_thread_cond_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700323 ObjectId event_thread_id_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700324
Elliott Hughesa21039c2012-06-21 12:09:25 -0700325 bool ddm_is_active_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700326};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700327
328} // namespace JDWP
329
330} // namespace art
331
332#endif // ART_JDWP_JDWP_H_