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" |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 23 | #include "../mutex.h" // TODO: fix our include path! |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 24 | |
| 25 | #include <pthread.h> |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | struct iovec; |
| 31 | |
| 32 | namespace art { |
| 33 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame^] | 34 | struct Thread; |
| 35 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 36 | namespace JDWP { |
| 37 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 38 | /* |
| 39 | * Fundamental types. |
| 40 | * |
| 41 | * ObjectId and RefTypeId must be the same size. |
| 42 | */ |
| 43 | typedef uint32_t FieldId; /* static or instance field */ |
| 44 | typedef uint32_t MethodId; /* any kind of method, including constructors */ |
| 45 | typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */ |
| 46 | typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */ |
| 47 | typedef 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 | */ |
| 53 | static inline FieldId ReadFieldId(const uint8_t** pBuf) { return read4BE(pBuf); } |
| 54 | static inline MethodId ReadMethodId(const uint8_t** pBuf) { return read4BE(pBuf); } |
| 55 | static inline ObjectId ReadObjectId(const uint8_t** pBuf) { return read8BE(pBuf); } |
| 56 | static inline RefTypeId ReadRefTypeId(const uint8_t** pBuf) { return read8BE(pBuf); } |
| 57 | static inline FrameId ReadFrameId(const uint8_t** pBuf) { return read8BE(pBuf); } |
| 58 | static inline void SetFieldId(uint8_t* buf, FieldId val) { return set4BE(buf, val); } |
| 59 | static inline void SetMethodId(uint8_t* buf, MethodId val) { return set4BE(buf, val); } |
| 60 | static inline void SetObjectId(uint8_t* buf, ObjectId val) { return set8BE(buf, val); } |
| 61 | static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return set8BE(buf, val); } |
| 62 | static inline void SetFrameId(uint8_t* buf, FrameId val) { return set8BE(buf, val); } |
| 63 | static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); } |
| 64 | static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); } |
| 65 | static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); } |
| 66 | static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); } |
| 67 | static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); } |
| 68 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 89 | struct JdwpOptions { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 90 | JdwpTransportType transport; |
| 91 | bool server; |
| 92 | bool suspend; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 93 | std::string host; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 94 | short port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 97 | struct JdwpEvent; |
| 98 | struct JdwpNetState; |
| 99 | struct JdwpReqHeader; |
| 100 | struct JdwpTransport; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 101 | |
| 102 | /* |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 103 | * State for JDWP functions. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 104 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 105 | struct JdwpState { |
| 106 | /* |
| 107 | * Perform one-time initialization. |
| 108 | * |
| 109 | * Among other things, this binds to a port to listen for a connection from |
| 110 | * the debugger. |
| 111 | * |
| 112 | * Returns a newly-allocated JdwpState struct on success, or NULL on failure. |
| 113 | */ |
| 114 | static JdwpState* Create(const JdwpOptions* options); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 115 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 116 | ~JdwpState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 117 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 118 | /* |
| 119 | * Returns "true" if a debugger or DDM is connected. |
| 120 | */ |
| 121 | bool IsActive(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 122 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame^] | 123 | /** |
| 124 | * Returns the Thread* for the JDWP daemon thread. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 125 | */ |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame^] | 126 | Thread* GetDebugThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Get time, in milliseconds, since the last debugger activity. |
| 130 | */ |
| 131 | int64_t LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 132 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 133 | /* |
| 134 | * When we hit a debugger event that requires suspension, it's important |
| 135 | * that we wait for the thread to suspend itself before processing any |
| 136 | * additional requests. (Otherwise, if the debugger immediately sends a |
| 137 | * "resume thread" command, the resume might arrive before the thread has |
| 138 | * suspended itself.) |
| 139 | * |
| 140 | * The thread should call the "set" function before sending the event to |
| 141 | * the debugger. The main JDWP handler loop calls "get" before processing |
| 142 | * an event, and will wait for thread suspension if it's set. Once the |
| 143 | * thread has suspended itself, the JDWP handler calls "clear" and |
| 144 | * continues processing the current event. This works in the suspend-all |
| 145 | * case because the event thread doesn't suspend itself until everything |
| 146 | * else has suspended. |
| 147 | * |
| 148 | * It's possible that multiple threads could encounter thread-suspending |
| 149 | * events at the same time, so we grab a mutex in the "set" call, and |
| 150 | * release it in the "clear" call. |
| 151 | */ |
| 152 | //ObjectId GetWaitForEventThread(); |
| 153 | void SetWaitForEventThread(ObjectId threadId); |
| 154 | void ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 155 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 156 | /* |
| 157 | * These notify the debug code that something interesting has happened. This |
| 158 | * could be a thread starting or ending, an exception, or an opportunity |
| 159 | * for a breakpoint. These calls do not mean that an event the debugger |
| 160 | * is interested has happened, just that something has happened that the |
| 161 | * debugger *might* be interested in. |
| 162 | * |
| 163 | * The item of interest may trigger multiple events, some or all of which |
| 164 | * are grouped together in a single response. |
| 165 | * |
| 166 | * The event may cause the current thread or all threads (except the |
| 167 | * JDWP support thread) to be suspended. |
| 168 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 169 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 170 | /* |
| 171 | * The VM has finished initializing. Only called when the debugger is |
| 172 | * connected at the time initialization completes. |
| 173 | */ |
| 174 | bool PostVMStart(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 175 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 176 | /* |
| 177 | * A location of interest has been reached. This is used for breakpoints, |
| 178 | * single-stepping, and method entry/exit. (JDWP requires that these four |
| 179 | * events are grouped together in a single response.) |
| 180 | * |
| 181 | * In some cases "*pLoc" will just have a method and class name, e.g. when |
| 182 | * issuing a MethodEntry on a native method. |
| 183 | * |
| 184 | * "eventFlags" indicates the types of events that have occurred. |
| 185 | */ |
| 186 | bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 187 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 188 | /* |
| 189 | * An exception has been thrown. |
| 190 | * |
| 191 | * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught. |
| 192 | */ |
| 193 | bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId, const JdwpLocation* pCatchLoc, ObjectId thisPtr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 194 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 195 | /* |
| 196 | * A thread has started or stopped. |
| 197 | */ |
| 198 | bool PostThreadChange(ObjectId threadId, bool start); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 199 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 200 | /* |
| 201 | * Class has been prepared. |
| 202 | */ |
| 203 | bool PostClassPrepare(int tag, RefTypeId refTypeId, const char* signature, int status); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 204 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 205 | /* |
| 206 | * The VM is about to stop. |
| 207 | */ |
| 208 | bool PostVMDeath(); |
| 209 | |
| 210 | /* |
| 211 | * Send up a chunk of DDM data. |
| 212 | */ |
| 213 | void DdmSendChunkV(int type, const iovec* iov, int iovcnt); |
| 214 | |
| 215 | /* |
| 216 | * Process a request from the debugger. |
| 217 | * |
| 218 | * "buf" points past the header, to the content of the message. "dataLen" |
| 219 | * can therefore be zero. |
| 220 | */ |
| 221 | void ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply); |
| 222 | |
| 223 | /* |
| 224 | * Send an event, formatted into "pReq", to the debugger. |
| 225 | * |
| 226 | * (Messages are sent asynchronously, and do not receive a reply.) |
| 227 | */ |
| 228 | bool SendRequest(ExpandBuf* pReq); |
| 229 | |
| 230 | void ResetState(); |
| 231 | |
| 232 | /* atomic ops to get next serial number */ |
| 233 | uint32_t NextRequestSerial(); |
| 234 | uint32_t NextEventSerial(); |
| 235 | |
| 236 | void Run(); |
| 237 | |
| 238 | private: |
| 239 | JdwpState(const JdwpOptions* options); |
| 240 | bool IsConnected(); |
| 241 | |
| 242 | public: // TODO: fix privacy |
| 243 | const JdwpOptions* options_; |
| 244 | private: |
| 245 | |
| 246 | /* wait for creation of the JDWP thread */ |
| 247 | Mutex thread_start_lock_; |
| 248 | ConditionVariable thread_start_cond_; |
| 249 | |
| 250 | volatile int32_t debug_thread_started_; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame^] | 251 | pthread_t pthread_; |
| 252 | Thread* thread_; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 253 | public: // TODO: fix privacy |
| 254 | ObjectId debugThreadId; |
| 255 | private: |
| 256 | bool run; |
| 257 | |
| 258 | const JdwpTransport* transport; |
| 259 | public: // TODO: fix privacy |
| 260 | JdwpNetState* netState; |
| 261 | private: |
| 262 | |
| 263 | /* for wait-for-debugger */ |
| 264 | Mutex attach_lock_; |
| 265 | ConditionVariable attach_cond_; |
| 266 | |
| 267 | /* time of last debugger activity, in milliseconds */ |
| 268 | int64_t lastActivityWhen; |
| 269 | |
| 270 | /* global counters and a mutex to protect them */ |
| 271 | uint32_t requestSerial; |
| 272 | uint32_t eventSerial; |
| 273 | Mutex serial_lock_; |
| 274 | |
| 275 | /* |
| 276 | * Events requested by the debugger (breakpoints, class prep, etc). |
| 277 | */ |
| 278 | public: // TODO: fix privacy |
| 279 | int numEvents; /* #of elements in eventList */ |
| 280 | JdwpEvent* eventList; /* linked list of events */ |
| 281 | Mutex event_lock_; /* guards numEvents/eventList */ |
| 282 | private: |
| 283 | |
| 284 | /* |
| 285 | * Synchronize suspension of event thread (to avoid receiving "resume" |
| 286 | * events before the thread has finished suspending itself). |
| 287 | */ |
| 288 | Mutex event_thread_lock_; |
| 289 | ConditionVariable event_thread_cond_; |
| 290 | ObjectId eventThreadId; |
| 291 | |
| 292 | /* |
| 293 | * DDM support. |
| 294 | */ |
| 295 | public: // TODO: fix privacy |
| 296 | bool ddmActive; |
| 297 | private: |
| 298 | }; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 299 | |
| 300 | } // namespace JDWP |
| 301 | |
| 302 | } // namespace art |
| 303 | |
| 304 | #endif // ART_JDWP_JDWP_H_ |