blob: cd2bfa77a8ea42c08e79449d46f6ca1311ad1178 [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
34namespace JDWP {
35
Elliott Hughes872d4ec2011-10-21 17:07:15 -070036/*
37 * Fundamental types.
38 *
39 * ObjectId and RefTypeId must be the same size.
40 */
41typedef uint32_t FieldId; /* static or instance field */
42typedef uint32_t MethodId; /* any kind of method, including constructors */
43typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
44typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
45typedef uint64_t FrameId; /* short-lived stack frame ID */
46
47/*
48 * Match these with the type sizes. This way we don't have to pass
49 * a value and a length.
50 */
51static inline FieldId ReadFieldId(const uint8_t** pBuf) { return read4BE(pBuf); }
52static inline MethodId ReadMethodId(const uint8_t** pBuf) { return read4BE(pBuf); }
53static inline ObjectId ReadObjectId(const uint8_t** pBuf) { return read8BE(pBuf); }
54static inline RefTypeId ReadRefTypeId(const uint8_t** pBuf) { return read8BE(pBuf); }
55static inline FrameId ReadFrameId(const uint8_t** pBuf) { return read8BE(pBuf); }
56static inline void SetFieldId(uint8_t* buf, FieldId val) { return set4BE(buf, val); }
57static inline void SetMethodId(uint8_t* buf, MethodId val) { return set4BE(buf, val); }
58static inline void SetObjectId(uint8_t* buf, ObjectId val) { return set8BE(buf, val); }
59static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return set8BE(buf, val); }
60static inline void SetFrameId(uint8_t* buf, FrameId val) { return set8BE(buf, val); }
61static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
62static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
63static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
64static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
65static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
66
Elliott Hughes872d4ec2011-10-21 17:07:15 -070067/*
68 * Holds a JDWP "location".
69 */
70struct JdwpLocation {
71 uint8_t typeTag; /* class or interface? */
72 RefTypeId classId; /* method->clazz */
73 MethodId methodId; /* method in which "idx" resides */
74 uint64_t idx; /* relative index into code block */
75};
76
77/*
78 * How we talk to the debugger.
79 */
80enum JdwpTransportType {
81 kJdwpTransportUnknown = 0,
82 kJdwpTransportSocket, /* transport=dt_socket */
83 kJdwpTransportAndroidAdb, /* transport=dt_android_adb */
84};
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 Hughes872d4ec2011-10-21 17:07:15 -070092 short 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 Hughes872d4ec2011-10-21 17:07:15 -070099
100/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700101 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700103struct JdwpState {
104 /*
105 * Perform one-time initialization.
106 *
107 * Among other things, this binds to a port to listen for a connection from
108 * the debugger.
109 *
110 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
111 */
112 static JdwpState* Create(const JdwpOptions* options);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113
Elliott Hughes376a7a02011-10-24 18:35:55 -0700114 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700115
Elliott Hughes376a7a02011-10-24 18:35:55 -0700116 /*
117 * Returns "true" if a debugger or DDM is connected.
118 */
119 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120
Elliott Hughes376a7a02011-10-24 18:35:55 -0700121 /*
122 * Return the debugger thread's handle, or 0 if the debugger thread isn't
123 * running.
124 */
125 pthread_t GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126
Elliott Hughes376a7a02011-10-24 18:35:55 -0700127 /*
128 * Get time, in milliseconds, since the last debugger activity.
129 */
130 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131
Elliott Hughes376a7a02011-10-24 18:35:55 -0700132 /*
133 * When we hit a debugger event that requires suspension, it's important
134 * that we wait for the thread to suspend itself before processing any
135 * additional requests. (Otherwise, if the debugger immediately sends a
136 * "resume thread" command, the resume might arrive before the thread has
137 * suspended itself.)
138 *
139 * The thread should call the "set" function before sending the event to
140 * the debugger. The main JDWP handler loop calls "get" before processing
141 * an event, and will wait for thread suspension if it's set. Once the
142 * thread has suspended itself, the JDWP handler calls "clear" and
143 * continues processing the current event. This works in the suspend-all
144 * case because the event thread doesn't suspend itself until everything
145 * else has suspended.
146 *
147 * It's possible that multiple threads could encounter thread-suspending
148 * events at the same time, so we grab a mutex in the "set" call, and
149 * release it in the "clear" call.
150 */
151 //ObjectId GetWaitForEventThread();
152 void SetWaitForEventThread(ObjectId threadId);
153 void ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154
Elliott Hughes376a7a02011-10-24 18:35:55 -0700155 /*
156 * These notify the debug code that something interesting has happened. This
157 * could be a thread starting or ending, an exception, or an opportunity
158 * for a breakpoint. These calls do not mean that an event the debugger
159 * is interested has happened, just that something has happened that the
160 * debugger *might* be interested in.
161 *
162 * The item of interest may trigger multiple events, some or all of which
163 * are grouped together in a single response.
164 *
165 * The event may cause the current thread or all threads (except the
166 * JDWP support thread) to be suspended.
167 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168
Elliott Hughes376a7a02011-10-24 18:35:55 -0700169 /*
170 * The VM has finished initializing. Only called when the debugger is
171 * connected at the time initialization completes.
172 */
173 bool PostVMStart();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174
Elliott Hughes376a7a02011-10-24 18:35:55 -0700175 /*
176 * A location of interest has been reached. This is used for breakpoints,
177 * single-stepping, and method entry/exit. (JDWP requires that these four
178 * events are grouped together in a single response.)
179 *
180 * In some cases "*pLoc" will just have a method and class name, e.g. when
181 * issuing a MethodEntry on a native method.
182 *
183 * "eventFlags" indicates the types of events that have occurred.
184 */
185 bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700186
Elliott Hughes376a7a02011-10-24 18:35:55 -0700187 /*
188 * An exception has been thrown.
189 *
190 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
191 */
192 bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId, const JdwpLocation* pCatchLoc, ObjectId thisPtr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
Elliott Hughes376a7a02011-10-24 18:35:55 -0700194 /*
195 * A thread has started or stopped.
196 */
197 bool PostThreadChange(ObjectId threadId, bool start);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198
Elliott Hughes376a7a02011-10-24 18:35:55 -0700199 /*
200 * Class has been prepared.
201 */
202 bool PostClassPrepare(int tag, RefTypeId refTypeId, const char* signature, int status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203
Elliott Hughes376a7a02011-10-24 18:35:55 -0700204 /*
205 * The VM is about to stop.
206 */
207 bool PostVMDeath();
208
209 /*
210 * Send up a chunk of DDM data.
211 */
212 void DdmSendChunkV(int type, const iovec* iov, int iovcnt);
213
214 /*
215 * Process a request from the debugger.
216 *
217 * "buf" points past the header, to the content of the message. "dataLen"
218 * can therefore be zero.
219 */
220 void ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply);
221
222 /*
223 * Send an event, formatted into "pReq", to the debugger.
224 *
225 * (Messages are sent asynchronously, and do not receive a reply.)
226 */
227 bool SendRequest(ExpandBuf* pReq);
228
229 void ResetState();
230
231 /* atomic ops to get next serial number */
232 uint32_t NextRequestSerial();
233 uint32_t NextEventSerial();
234
235 void Run();
236
237 private:
238 JdwpState(const JdwpOptions* options);
239 bool IsConnected();
240
241public: // TODO: fix privacy
242 const JdwpOptions* options_;
243private:
244
245 /* wait for creation of the JDWP thread */
246 Mutex thread_start_lock_;
247 ConditionVariable thread_start_cond_;
248
249 volatile int32_t debug_thread_started_;
250 pthread_t debugThreadHandle;
251public: // TODO: fix privacy
252 ObjectId debugThreadId;
253private:
254 bool run;
255
256 const JdwpTransport* transport;
257public: // TODO: fix privacy
258 JdwpNetState* netState;
259private:
260
261 /* for wait-for-debugger */
262 Mutex attach_lock_;
263 ConditionVariable attach_cond_;
264
265 /* time of last debugger activity, in milliseconds */
266 int64_t lastActivityWhen;
267
268 /* global counters and a mutex to protect them */
269 uint32_t requestSerial;
270 uint32_t eventSerial;
271 Mutex serial_lock_;
272
273 /*
274 * Events requested by the debugger (breakpoints, class prep, etc).
275 */
276public: // TODO: fix privacy
277 int numEvents; /* #of elements in eventList */
278 JdwpEvent* eventList; /* linked list of events */
279 Mutex event_lock_; /* guards numEvents/eventList */
280private:
281
282 /*
283 * Synchronize suspension of event thread (to avoid receiving "resume"
284 * events before the thread has finished suspending itself).
285 */
286 Mutex event_thread_lock_;
287 ConditionVariable event_thread_cond_;
288 ObjectId eventThreadId;
289
290 /*
291 * DDM support.
292 */
293public: // TODO: fix privacy
294 bool ddmActive;
295private:
296};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297
298} // namespace JDWP
299
300} // namespace art
301
302#endif // ART_JDWP_JDWP_H_