Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | struct iovec; |
| 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | namespace JDWP { |
| 34 | |
| 35 | struct JdwpState; /* opaque */ |
| 36 | |
| 37 | /* |
| 38 | * Fundamental types. |
| 39 | * |
| 40 | * ObjectId and RefTypeId must be the same size. |
| 41 | */ |
| 42 | typedef uint32_t FieldId; /* static or instance field */ |
| 43 | typedef uint32_t MethodId; /* any kind of method, including constructors */ |
| 44 | typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */ |
| 45 | typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */ |
| 46 | typedef 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 | */ |
| 52 | static inline FieldId ReadFieldId(const uint8_t** pBuf) { return read4BE(pBuf); } |
| 53 | static inline MethodId ReadMethodId(const uint8_t** pBuf) { return read4BE(pBuf); } |
| 54 | static inline ObjectId ReadObjectId(const uint8_t** pBuf) { return read8BE(pBuf); } |
| 55 | static inline RefTypeId ReadRefTypeId(const uint8_t** pBuf) { return read8BE(pBuf); } |
| 56 | static inline FrameId ReadFrameId(const uint8_t** pBuf) { return read8BE(pBuf); } |
| 57 | static inline void SetFieldId(uint8_t* buf, FieldId val) { return set4BE(buf, val); } |
| 58 | static inline void SetMethodId(uint8_t* buf, MethodId val) { return set4BE(buf, val); } |
| 59 | static inline void SetObjectId(uint8_t* buf, ObjectId val) { return set8BE(buf, val); } |
| 60 | static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return set8BE(buf, val); } |
| 61 | static inline void SetFrameId(uint8_t* buf, FrameId val) { return set8BE(buf, val); } |
| 62 | static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); } |
| 63 | static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); } |
| 64 | static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); } |
| 65 | static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); } |
| 66 | static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); } |
| 67 | |
| 68 | |
| 69 | /* |
| 70 | * Holds a JDWP "location". |
| 71 | */ |
| 72 | struct 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 | */ |
| 82 | enum JdwpTransportType { |
| 83 | kJdwpTransportUnknown = 0, |
| 84 | kJdwpTransportSocket, /* transport=dt_socket */ |
| 85 | kJdwpTransportAndroidAdb, /* transport=dt_android_adb */ |
| 86 | }; |
| 87 | std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs); |
| 88 | |
| 89 | /* |
| 90 | * Holds collection of JDWP initialization parameters. |
| 91 | */ |
| 92 | struct JdwpStartupParams { |
| 93 | JdwpTransportType transport; |
| 94 | bool server; |
| 95 | bool suspend; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame^] | 96 | std::string host; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 97 | short port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 98 | }; |
| 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 | */ |
| 108 | JdwpState* JdwpStartup(const JdwpStartupParams* params); |
| 109 | |
| 110 | /* |
| 111 | * Shut everything down. |
| 112 | */ |
| 113 | void JdwpShutdown(JdwpState* state); |
| 114 | |
| 115 | /* |
| 116 | * Returns "true" if a debugger or DDM is connected. |
| 117 | */ |
| 118 | bool JdwpIsActive(JdwpState* state); |
| 119 | |
| 120 | /* |
| 121 | * Return the debugger thread's handle, or 0 if the debugger thread isn't |
| 122 | * running. |
| 123 | */ |
| 124 | pthread_t GetDebugThread(JdwpState* state); |
| 125 | |
| 126 | /* |
| 127 | * Get time, in milliseconds, since the last debugger activity. |
| 128 | */ |
| 129 | int64_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); |
| 151 | void SetWaitForEventThread(JdwpState* state, ObjectId threadId); |
| 152 | void 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 | */ |
| 172 | bool 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 | */ |
| 184 | bool 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 | */ |
| 191 | bool 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 | */ |
| 198 | bool PostThreadChange(JdwpState* state, ObjectId threadId, bool start); |
| 199 | |
| 200 | /* |
| 201 | * Class has been prepared. |
| 202 | */ |
| 203 | bool PostClassPrepare(JdwpState* state, int tag, RefTypeId refTypeId, |
| 204 | const char* signature, int status); |
| 205 | |
| 206 | /* |
| 207 | * The VM is about to stop. |
| 208 | */ |
| 209 | bool PostVMDeath(JdwpState* state); |
| 210 | |
| 211 | /* |
| 212 | * Send up a chunk of DDM data. |
| 213 | */ |
| 214 | void 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_ |