blob: 8455ba5c52de66856ffdd1f0777d64374a3c5705 [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"
23
24#include <pthread.h>
25#include <stddef.h>
26#include <stdint.h>
27#include <string.h>
28
29struct iovec;
30
31namespace art {
32
33namespace JDWP {
34
35struct JdwpState; /* opaque */
36
37/*
38 * Fundamental types.
39 *
40 * ObjectId and RefTypeId must be the same size.
41 */
42typedef uint32_t FieldId; /* static or instance field */
43typedef uint32_t MethodId; /* any kind of method, including constructors */
44typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */
45typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */
46typedef uint64_t FrameId; /* short-lived stack frame ID */
47
48/*
49 * Match these with the type sizes. This way we don't have to pass
50 * a value and a length.
51 */
52static inline FieldId ReadFieldId(const uint8_t** pBuf) { return read4BE(pBuf); }
53static inline MethodId ReadMethodId(const uint8_t** pBuf) { return read4BE(pBuf); }
54static inline ObjectId ReadObjectId(const uint8_t** pBuf) { return read8BE(pBuf); }
55static inline RefTypeId ReadRefTypeId(const uint8_t** pBuf) { return read8BE(pBuf); }
56static inline FrameId ReadFrameId(const uint8_t** pBuf) { return read8BE(pBuf); }
57static inline void SetFieldId(uint8_t* buf, FieldId val) { return set4BE(buf, val); }
58static inline void SetMethodId(uint8_t* buf, MethodId val) { return set4BE(buf, val); }
59static inline void SetObjectId(uint8_t* buf, ObjectId val) { return set8BE(buf, val); }
60static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return set8BE(buf, val); }
61static inline void SetFrameId(uint8_t* buf, FrameId val) { return set8BE(buf, val); }
62static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); }
63static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); }
64static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); }
65static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); }
66static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); }
67
68
69/*
70 * Holds a JDWP "location".
71 */
72struct JdwpLocation {
73 uint8_t typeTag; /* class or interface? */
74 RefTypeId classId; /* method->clazz */
75 MethodId methodId; /* method in which "idx" resides */
76 uint64_t idx; /* relative index into code block */
77};
78
79/*
80 * How we talk to the debugger.
81 */
82enum JdwpTransportType {
83 kJdwpTransportUnknown = 0,
84 kJdwpTransportSocket, /* transport=dt_socket */
85 kJdwpTransportAndroidAdb, /* transport=dt_android_adb */
86};
87std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
88
89/*
90 * Holds collection of JDWP initialization parameters.
91 */
92struct JdwpStartupParams {
93 JdwpTransportType transport;
94 bool server;
95 bool suspend;
Elliott Hughesd1cc8362011-10-24 16:58:50 -070096 std::string host;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070097 short port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098};
99
100/*
101 * Perform one-time initialization.
102 *
103 * Among other things, this binds to a port to listen for a connection from
104 * the debugger.
105 *
106 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
107 */
108JdwpState* JdwpStartup(const JdwpStartupParams* params);
109
110/*
111 * Shut everything down.
112 */
113void JdwpShutdown(JdwpState* state);
114
115/*
116 * Returns "true" if a debugger or DDM is connected.
117 */
118bool JdwpIsActive(JdwpState* state);
119
120/*
121 * Return the debugger thread's handle, or 0 if the debugger thread isn't
122 * running.
123 */
124pthread_t GetDebugThread(JdwpState* state);
125
126/*
127 * Get time, in milliseconds, since the last debugger activity.
128 */
129int64_t LastDebuggerActivity(JdwpState* state);
130
131/*
132 * When we hit a debugger event that requires suspension, it's important
133 * that we wait for the thread to suspend itself before processing any
134 * additional requests. (Otherwise, if the debugger immediately sends a
135 * "resume thread" command, the resume might arrive before the thread has
136 * suspended itself.)
137 *
138 * The thread should call the "set" function before sending the event to
139 * the debugger. The main JDWP handler loop calls "get" before processing
140 * an event, and will wait for thread suspension if it's set. Once the
141 * thread has suspended itself, the JDWP handler calls "clear" and
142 * continues processing the current event. This works in the suspend-all
143 * case because the event thread doesn't suspend itself until everything
144 * else has suspended.
145 *
146 * It's possible that multiple threads could encounter thread-suspending
147 * events at the same time, so we grab a mutex in the "set" call, and
148 * release it in the "clear" call.
149 */
150//ObjectId GetWaitForEventThread(JdwpState* state);
151void SetWaitForEventThread(JdwpState* state, ObjectId threadId);
152void ClearWaitForEventThread(JdwpState* state);
153
154/*
155 * These notify the debug code that something interesting has happened. This
156 * could be a thread starting or ending, an exception, or an opportunity
157 * for a breakpoint. These calls do not mean that an event the debugger
158 * is interested has happened, just that something has happened that the
159 * debugger *might* be interested in.
160 *
161 * The item of interest may trigger multiple events, some or all of which
162 * are grouped together in a single response.
163 *
164 * The event may cause the current thread or all threads (except the
165 * JDWP support thread) to be suspended.
166 */
167
168/*
169 * The VM has finished initializing. Only called when the debugger is
170 * connected at the time initialization completes.
171 */
172bool PostVMStart(JdwpState* state, bool suspend);
173
174/*
175 * A location of interest has been reached. This is used for breakpoints,
176 * single-stepping, and method entry/exit. (JDWP requires that these four
177 * events are grouped together in a single response.)
178 *
179 * In some cases "*pLoc" will just have a method and class name, e.g. when
180 * issuing a MethodEntry on a native method.
181 *
182 * "eventFlags" indicates the types of events that have occurred.
183 */
184bool PostLocationEvent(JdwpState* state, const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags);
185
186/*
187 * An exception has been thrown.
188 *
189 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
190 */
191bool PostException(JdwpState* state, const JdwpLocation* pThrowLoc,
192 ObjectId excepId, RefTypeId excepClassId, const JdwpLocation* pCatchLoc,
193 ObjectId thisPtr);
194
195/*
196 * A thread has started or stopped.
197 */
198bool PostThreadChange(JdwpState* state, ObjectId threadId, bool start);
199
200/*
201 * Class has been prepared.
202 */
203bool PostClassPrepare(JdwpState* state, int tag, RefTypeId refTypeId,
204 const char* signature, int status);
205
206/*
207 * The VM is about to stop.
208 */
209bool PostVMDeath(JdwpState* state);
210
211/*
212 * Send up a chunk of DDM data.
213 */
214void DdmSendChunkV(JdwpState* state, int type, const iovec* iov, int iovcnt);
215
216} // namespace JDWP
217
218} // namespace art
219
220#endif // ART_JDWP_JDWP_H_