blob: 1c205bc4d7b952b62c0e9f17fb133549dea6252a [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;
96 char host[64];
97 short port;
98 /* more will be here someday */
99};
100
101/*
102 * Perform one-time initialization.
103 *
104 * Among other things, this binds to a port to listen for a connection from
105 * the debugger.
106 *
107 * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
108 */
109JdwpState* JdwpStartup(const JdwpStartupParams* params);
110
111/*
112 * Shut everything down.
113 */
114void JdwpShutdown(JdwpState* state);
115
116/*
117 * Returns "true" if a debugger or DDM is connected.
118 */
119bool JdwpIsActive(JdwpState* state);
120
121/*
122 * Return the debugger thread's handle, or 0 if the debugger thread isn't
123 * running.
124 */
125pthread_t GetDebugThread(JdwpState* state);
126
127/*
128 * Get time, in milliseconds, since the last debugger activity.
129 */
130int64_t LastDebuggerActivity(JdwpState* state);
131
132/*
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(JdwpState* state);
152void SetWaitForEventThread(JdwpState* state, ObjectId threadId);
153void ClearWaitForEventThread(JdwpState* state);
154
155/*
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 */
168
169/*
170 * The VM has finished initializing. Only called when the debugger is
171 * connected at the time initialization completes.
172 */
173bool PostVMStart(JdwpState* state, bool suspend);
174
175/*
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 */
185bool PostLocationEvent(JdwpState* state, const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags);
186
187/*
188 * An exception has been thrown.
189 *
190 * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught.
191 */
192bool PostException(JdwpState* state, const JdwpLocation* pThrowLoc,
193 ObjectId excepId, RefTypeId excepClassId, const JdwpLocation* pCatchLoc,
194 ObjectId thisPtr);
195
196/*
197 * A thread has started or stopped.
198 */
199bool PostThreadChange(JdwpState* state, ObjectId threadId, bool start);
200
201/*
202 * Class has been prepared.
203 */
204bool PostClassPrepare(JdwpState* state, int tag, RefTypeId refTypeId,
205 const char* signature, int status);
206
207/*
208 * The VM is about to stop.
209 */
210bool PostVMDeath(JdwpState* state);
211
212/*
213 * Send up a chunk of DDM data.
214 */
215void DdmSendChunkV(JdwpState* state, int type, const iovec* iov, int iovcnt);
216
217} // namespace JDWP
218
219} // namespace art
220
221#endif // ART_JDWP_JDWP_H_