blob: b628e6c2148e2d0dacd597ef9080b4f8ed18ed80 [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 Hughes475fc232011-10-25 15:00:35 -070034struct Thread;
35
Elliott Hughes872d4ec2011-10-21 17:07:15 -070036namespace JDWP {
37
Elliott Hughes872d4ec2011-10-21 17:07:15 -070038/*
39 * Fundamental types.
40 *
41 * ObjectId and RefTypeId must be the same size.
42 */
43typedef uint32_t FieldId; /* static or instance field */
44typedef uint32_t MethodId; /* any kind of method, including constructors */
45typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
46typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
47typedef uint64_t FrameId; /* short-lived stack frame ID */
48
49/*
50 * Match these with the type sizes. This way we don't have to pass
51 * a value and a length.
52 */
Elliott Hughesf7c3b662011-10-27 12:04:56 -070053static inline FieldId ReadFieldId(const uint8_t** pBuf) { return Read4BE(pBuf); }
54static inline MethodId ReadMethodId(const uint8_t** pBuf) { return Read4BE(pBuf); }
55static inline ObjectId ReadObjectId(const uint8_t** pBuf) { return Read8BE(pBuf); }
56static inline RefTypeId ReadRefTypeId(const uint8_t** pBuf) { return Read8BE(pBuf); }
57static inline FrameId ReadFrameId(const uint8_t** pBuf) { return Read8BE(pBuf); }
Elliott Hughesaed4be92011-12-02 16:16:23 -080058static inline JdwpTag ReadTag(const uint8_t** pBuf) { return static_cast<JdwpTag>(Read1(pBuf)); }
Elliott Hughesf7c3b662011-10-27 12:04:56 -070059static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); }
60static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); }
61static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); }
62static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); }
63static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070064static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
65static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
66static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
67static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
68static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
69
Elliott Hughes872d4ec2011-10-21 17:07:15 -070070/*
71 * Holds a JDWP "location".
72 */
73struct JdwpLocation {
74 uint8_t typeTag; /* class or interface? */
75 RefTypeId classId; /* method->clazz */
76 MethodId methodId; /* method in which "idx" resides */
77 uint64_t idx; /* relative index into code block */
78};
Elliott Hughes03181a82011-11-17 17:22:21 -080079std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080
81/*
82 * How we talk to the debugger.
83 */
84enum JdwpTransportType {
85 kJdwpTransportUnknown = 0,
86 kJdwpTransportSocket, /* transport=dt_socket */
87 kJdwpTransportAndroidAdb, /* transport=dt_android_adb */
88};
89std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
90
Elliott Hughes376a7a02011-10-24 18:35:55 -070091struct JdwpOptions {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070092 JdwpTransportType transport;
93 bool server;
94 bool suspend;
Elliott Hughesd1cc8362011-10-24 16:58:50 -070095 std::string host;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070096 short port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070097};
98
Elliott Hughes376a7a02011-10-24 18:35:55 -070099struct JdwpEvent;
100struct JdwpNetState;
101struct JdwpReqHeader;
102struct JdwpTransport;
Elliott Hughes761928d2011-11-16 18:33:03 -0800103struct ModBasket;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700104
105/*
Elliott Hughes376a7a02011-10-24 18:35:55 -0700106 * State for JDWP functions.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700107 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700108struct JdwpState {
109 /*
110 * Perform one-time initialization.
111 *
112 * Among other things, this binds to a port to listen for a connection from
113 * the debugger.
114 *
115 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
116 */
117 static JdwpState* Create(const JdwpOptions* options);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700118
Elliott Hughes376a7a02011-10-24 18:35:55 -0700119 ~JdwpState();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120
Elliott Hughes376a7a02011-10-24 18:35:55 -0700121 /*
122 * Returns "true" if a debugger or DDM is connected.
123 */
124 bool IsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125
Elliott Hughes475fc232011-10-25 15:00:35 -0700126 /**
127 * Returns the Thread* for the JDWP daemon thread.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700128 */
Elliott Hughes475fc232011-10-25 15:00:35 -0700129 Thread* GetDebugThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130
Elliott Hughes376a7a02011-10-24 18:35:55 -0700131 /*
132 * Get time, in milliseconds, since the last debugger activity.
133 */
134 int64_t LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135
Elliott Hughes376a7a02011-10-24 18:35:55 -0700136 /*
137 * When we hit a debugger event that requires suspension, it's important
138 * that we wait for the thread to suspend itself before processing any
139 * additional requests. (Otherwise, if the debugger immediately sends a
140 * "resume thread" command, the resume might arrive before the thread has
141 * suspended itself.)
142 *
143 * The thread should call the "set" function before sending the event to
144 * the debugger. The main JDWP handler loop calls "get" before processing
145 * an event, and will wait for thread suspension if it's set. Once the
146 * thread has suspended itself, the JDWP handler calls "clear" and
147 * continues processing the current event. This works in the suspend-all
148 * case because the event thread doesn't suspend itself until everything
149 * else has suspended.
150 *
151 * It's possible that multiple threads could encounter thread-suspending
152 * events at the same time, so we grab a mutex in the "set" call, and
153 * release it in the "clear" call.
154 */
155 //ObjectId GetWaitForEventThread();
156 void SetWaitForEventThread(ObjectId threadId);
157 void ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700158
Elliott Hughes376a7a02011-10-24 18:35:55 -0700159 /*
160 * These notify the debug code that something interesting has happened. This
161 * could be a thread starting or ending, an exception, or an opportunity
162 * for a breakpoint. These calls do not mean that an event the debugger
163 * is interested has happened, just that something has happened that the
164 * debugger *might* be interested in.
165 *
166 * The item of interest may trigger multiple events, some or all of which
167 * are grouped together in a single response.
168 *
169 * The event may cause the current thread or all threads (except the
170 * JDWP support thread) to be suspended.
171 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172
Elliott Hughes376a7a02011-10-24 18:35:55 -0700173 /*
174 * The VM has finished initializing. Only called when the debugger is
175 * connected at the time initialization completes.
176 */
177 bool PostVMStart();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700178
Elliott Hughes376a7a02011-10-24 18:35:55 -0700179 /*
180 * A location of interest has been reached. This is used for breakpoints,
181 * single-stepping, and method entry/exit. (JDWP requires that these four
182 * events are grouped together in a single response.)
183 *
184 * In some cases "*pLoc" will just have a method and class name, e.g. when
185 * issuing a MethodEntry on a native method.
186 *
187 * "eventFlags" indicates the types of events that have occurred.
188 */
189 bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190
Elliott Hughes376a7a02011-10-24 18:35:55 -0700191 /*
192 * An exception has been thrown.
193 *
194 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
195 */
196 bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId, const JdwpLocation* pCatchLoc, ObjectId thisPtr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197
Elliott Hughes376a7a02011-10-24 18:35:55 -0700198 /*
199 * A thread has started or stopped.
200 */
201 bool PostThreadChange(ObjectId threadId, bool start);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
Elliott Hughes376a7a02011-10-24 18:35:55 -0700203 /*
204 * Class has been prepared.
205 */
206 bool PostClassPrepare(int tag, RefTypeId refTypeId, const char* signature, int status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700207
Elliott Hughes376a7a02011-10-24 18:35:55 -0700208 /*
209 * The VM is about to stop.
210 */
211 bool PostVMDeath();
212
213 /*
214 * Send up a chunk of DDM data.
215 */
Elliott Hughescccd84f2011-12-05 16:51:54 -0800216 void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700217
218 /*
219 * Process a request from the debugger.
220 *
221 * "buf" points past the header, to the content of the message. "dataLen"
222 * can therefore be zero.
223 */
224 void ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply);
225
226 /*
227 * Send an event, formatted into "pReq", to the debugger.
228 *
229 * (Messages are sent asynchronously, and do not receive a reply.)
230 */
231 bool SendRequest(ExpandBuf* pReq);
232
233 void ResetState();
234
235 /* atomic ops to get next serial number */
236 uint32_t NextRequestSerial();
237 uint32_t NextEventSerial();
238
239 void Run();
240
Elliott Hughes761928d2011-11-16 18:33:03 -0800241 /*
242 * Register an event by adding it to the event list.
243 *
244 * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller
245 * may discard its pointer after calling this.
246 */
247 JdwpError RegisterEvent(JdwpEvent* pEvent);
248
249 /*
250 * Unregister an event, given the requestId.
251 */
252 void UnregisterEventById(uint32_t requestId);
253
254 /*
255 * Unregister all events.
256 */
257 void UnregisterAll();
258
Elliott Hughes376a7a02011-10-24 18:35:55 -0700259 private:
260 JdwpState(const JdwpOptions* options);
Elliott Hughes761928d2011-11-16 18:33:03 -0800261 bool InvokeInProgress();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700262 bool IsConnected();
Elliott Hughes761928d2011-11-16 18:33:03 -0800263 void SuspendByPolicy(JdwpSuspendPolicy suspendPolicy);
264 void CleanupMatchList(JdwpEvent** matchList, int matchCount);
265 void EventFinish(ExpandBuf* pReq);
266 void FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** matchList, int* pMatchCount);
267 void UnregisterEvent(JdwpEvent* pEvent);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700268
269public: // TODO: fix privacy
270 const JdwpOptions* options_;
271private:
272
273 /* wait for creation of the JDWP thread */
274 Mutex thread_start_lock_;
275 ConditionVariable thread_start_cond_;
276
277 volatile int32_t debug_thread_started_;
Elliott Hughes475fc232011-10-25 15:00:35 -0700278 pthread_t pthread_;
279 Thread* thread_;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700280public: // TODO: fix privacy
281 ObjectId debugThreadId;
282private:
283 bool run;
284
285 const JdwpTransport* transport;
286public: // TODO: fix privacy
287 JdwpNetState* netState;
288private:
289
290 /* for wait-for-debugger */
291 Mutex attach_lock_;
292 ConditionVariable attach_cond_;
293
294 /* time of last debugger activity, in milliseconds */
295 int64_t lastActivityWhen;
296
297 /* global counters and a mutex to protect them */
298 uint32_t requestSerial;
299 uint32_t eventSerial;
300 Mutex serial_lock_;
301
302 /*
303 * Events requested by the debugger (breakpoints, class prep, etc).
304 */
305public: // TODO: fix privacy
306 int numEvents; /* #of elements in eventList */
307 JdwpEvent* eventList; /* linked list of events */
308 Mutex event_lock_; /* guards numEvents/eventList */
309private:
310
311 /*
312 * Synchronize suspension of event thread (to avoid receiving "resume"
313 * events before the thread has finished suspending itself).
314 */
315 Mutex event_thread_lock_;
316 ConditionVariable event_thread_cond_;
317 ObjectId eventThreadId;
318
319 /*
320 * DDM support.
321 */
322public: // TODO: fix privacy
323 bool ddmActive;
324private:
325};
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700326
327} // namespace JDWP
328
329} // namespace art
330
331#endif // ART_JDWP_JDWP_H_