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 | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 34 | struct Method; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 35 | struct Thread; |
| 36 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 37 | namespace JDWP { |
| 38 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Fundamental types. |
| 41 | * |
| 42 | * ObjectId and RefTypeId must be the same size. |
| 43 | */ |
| 44 | typedef uint32_t FieldId; /* static or instance field */ |
| 45 | typedef uint32_t MethodId; /* any kind of method, including constructors */ |
| 46 | typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */ |
| 47 | typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */ |
| 48 | typedef uint64_t FrameId; /* short-lived stack frame ID */ |
| 49 | |
| 50 | /* |
| 51 | * Match these with the type sizes. This way we don't have to pass |
| 52 | * a value and a length. |
| 53 | */ |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 54 | static inline FieldId ReadFieldId(const uint8_t** pBuf) { return Read4BE(pBuf); } |
| 55 | static inline MethodId ReadMethodId(const uint8_t** pBuf) { return Read4BE(pBuf); } |
| 56 | static inline ObjectId ReadObjectId(const uint8_t** pBuf) { return Read8BE(pBuf); } |
| 57 | static inline RefTypeId ReadRefTypeId(const uint8_t** pBuf) { return Read8BE(pBuf); } |
| 58 | static inline FrameId ReadFrameId(const uint8_t** pBuf) { return Read8BE(pBuf); } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 59 | static inline JdwpTag ReadTag(const uint8_t** pBuf) { return static_cast<JdwpTag>(Read1(pBuf)); } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 60 | static inline JdwpTypeTag ReadTypeTag(const uint8_t** pBuf) { return static_cast<JdwpTypeTag>(Read1(pBuf)); } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 61 | static inline void SetFieldId(uint8_t* buf, FieldId val) { return Set4BE(buf, val); } |
| 62 | static inline void SetMethodId(uint8_t* buf, MethodId val) { return Set4BE(buf, val); } |
| 63 | static inline void SetObjectId(uint8_t* buf, ObjectId val) { return Set8BE(buf, val); } |
| 64 | static inline void SetRefTypeId(uint8_t* buf, RefTypeId val) { return Set8BE(buf, val); } |
| 65 | static inline void SetFrameId(uint8_t* buf, FrameId val) { return Set8BE(buf, val); } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 66 | static inline void expandBufAddFieldId(ExpandBuf* pReply, FieldId id) { expandBufAdd4BE(pReply, id); } |
| 67 | static inline void expandBufAddMethodId(ExpandBuf* pReply, MethodId id) { expandBufAdd4BE(pReply, id); } |
| 68 | static inline void expandBufAddObjectId(ExpandBuf* pReply, ObjectId id) { expandBufAdd8BE(pReply, id); } |
| 69 | static inline void expandBufAddRefTypeId(ExpandBuf* pReply, RefTypeId id) { expandBufAdd8BE(pReply, id); } |
| 70 | static inline void expandBufAddFrameId(ExpandBuf* pReply, FrameId id) { expandBufAdd8BE(pReply, id); } |
| 71 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Holds a JDWP "location". |
| 74 | */ |
| 75 | struct JdwpLocation { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 76 | JdwpTypeTag typeTag; |
| 77 | RefTypeId classId; |
| 78 | MethodId methodId; |
| 79 | uint64_t idx; // A Dex PC. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 80 | }; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 81 | std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * How we talk to the debugger. |
| 85 | */ |
| 86 | enum JdwpTransportType { |
| 87 | kJdwpTransportUnknown = 0, |
| 88 | kJdwpTransportSocket, /* transport=dt_socket */ |
| 89 | kJdwpTransportAndroidAdb, /* transport=dt_android_adb */ |
| 90 | }; |
| 91 | std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs); |
| 92 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 93 | struct JdwpOptions { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 94 | JdwpTransportType transport; |
| 95 | bool server; |
| 96 | bool suspend; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 97 | std::string host; |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 98 | uint16_t port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 101 | struct JdwpEvent; |
| 102 | struct JdwpNetState; |
| 103 | struct JdwpReqHeader; |
| 104 | struct JdwpTransport; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 105 | struct ModBasket; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 106 | |
| 107 | /* |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 108 | * State for JDWP functions. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 109 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 110 | struct JdwpState { |
| 111 | /* |
| 112 | * Perform one-time initialization. |
| 113 | * |
| 114 | * Among other things, this binds to a port to listen for a connection from |
| 115 | * the debugger. |
| 116 | * |
| 117 | * Returns a newly-allocated JdwpState struct on success, or NULL on failure. |
| 118 | */ |
| 119 | static JdwpState* Create(const JdwpOptions* options); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 120 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 121 | ~JdwpState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 122 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Returns "true" if a debugger or DDM is connected. |
| 125 | */ |
| 126 | bool IsActive(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Returns the Thread* for the JDWP daemon thread. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 130 | */ |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 131 | Thread* GetDebugThread(); |
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 | * Get time, in milliseconds, since the last debugger activity. |
| 135 | */ |
| 136 | int64_t LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 137 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 138 | /* |
| 139 | * When we hit a debugger event that requires suspension, it's important |
| 140 | * that we wait for the thread to suspend itself before processing any |
| 141 | * additional requests. (Otherwise, if the debugger immediately sends a |
| 142 | * "resume thread" command, the resume might arrive before the thread has |
| 143 | * suspended itself.) |
| 144 | * |
| 145 | * The thread should call the "set" function before sending the event to |
| 146 | * the debugger. The main JDWP handler loop calls "get" before processing |
| 147 | * an event, and will wait for thread suspension if it's set. Once the |
| 148 | * thread has suspended itself, the JDWP handler calls "clear" and |
| 149 | * continues processing the current event. This works in the suspend-all |
| 150 | * case because the event thread doesn't suspend itself until everything |
| 151 | * else has suspended. |
| 152 | * |
| 153 | * It's possible that multiple threads could encounter thread-suspending |
| 154 | * events at the same time, so we grab a mutex in the "set" call, and |
| 155 | * release it in the "clear" call. |
| 156 | */ |
| 157 | //ObjectId GetWaitForEventThread(); |
| 158 | void SetWaitForEventThread(ObjectId threadId); |
| 159 | void ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 161 | /* |
| 162 | * These notify the debug code that something interesting has happened. This |
| 163 | * could be a thread starting or ending, an exception, or an opportunity |
| 164 | * for a breakpoint. These calls do not mean that an event the debugger |
| 165 | * is interested has happened, just that something has happened that the |
| 166 | * debugger *might* be interested in. |
| 167 | * |
| 168 | * The item of interest may trigger multiple events, some or all of which |
| 169 | * are grouped together in a single response. |
| 170 | * |
| 171 | * The event may cause the current thread or all threads (except the |
| 172 | * JDWP support thread) to be suspended. |
| 173 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 174 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 175 | /* |
| 176 | * The VM has finished initializing. Only called when the debugger is |
| 177 | * connected at the time initialization completes. |
| 178 | */ |
| 179 | bool PostVMStart(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 180 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 181 | /* |
| 182 | * A location of interest has been reached. This is used for breakpoints, |
| 183 | * single-stepping, and method entry/exit. (JDWP requires that these four |
| 184 | * events are grouped together in a single response.) |
| 185 | * |
| 186 | * In some cases "*pLoc" will just have a method and class name, e.g. when |
| 187 | * issuing a MethodEntry on a native method. |
| 188 | * |
| 189 | * "eventFlags" indicates the types of events that have occurred. |
| 190 | */ |
| 191 | bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 192 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 193 | /* |
| 194 | * An exception has been thrown. |
| 195 | * |
| 196 | * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught. |
| 197 | */ |
| 198 | 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] | 199 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 200 | /* |
| 201 | * A thread has started or stopped. |
| 202 | */ |
| 203 | bool PostThreadChange(ObjectId threadId, bool start); |
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 | * Class has been prepared. |
| 207 | */ |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 208 | bool PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature, int status); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 209 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 210 | /* |
| 211 | * The VM is about to stop. |
| 212 | */ |
| 213 | bool PostVMDeath(); |
| 214 | |
| 215 | /* |
| 216 | * Send up a chunk of DDM data. |
| 217 | */ |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 218 | void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Process a request from the debugger. |
| 222 | * |
| 223 | * "buf" points past the header, to the content of the message. "dataLen" |
| 224 | * can therefore be zero. |
| 225 | */ |
| 226 | void ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply); |
| 227 | |
| 228 | /* |
| 229 | * Send an event, formatted into "pReq", to the debugger. |
| 230 | * |
| 231 | * (Messages are sent asynchronously, and do not receive a reply.) |
| 232 | */ |
| 233 | bool SendRequest(ExpandBuf* pReq); |
| 234 | |
| 235 | void ResetState(); |
| 236 | |
| 237 | /* atomic ops to get next serial number */ |
| 238 | uint32_t NextRequestSerial(); |
| 239 | uint32_t NextEventSerial(); |
| 240 | |
| 241 | void Run(); |
| 242 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 243 | /* |
| 244 | * Register an event by adding it to the event list. |
| 245 | * |
| 246 | * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller |
| 247 | * may discard its pointer after calling this. |
| 248 | */ |
| 249 | JdwpError RegisterEvent(JdwpEvent* pEvent); |
| 250 | |
| 251 | /* |
| 252 | * Unregister an event, given the requestId. |
| 253 | */ |
| 254 | void UnregisterEventById(uint32_t requestId); |
| 255 | |
| 256 | /* |
| 257 | * Unregister all events. |
| 258 | */ |
| 259 | void UnregisterAll(); |
| 260 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 261 | private: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 262 | explicit JdwpState(const JdwpOptions* options); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 263 | bool InvokeInProgress(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 264 | bool IsConnected(); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 265 | void SuspendByPolicy(JdwpSuspendPolicy suspendPolicy); |
| 266 | void CleanupMatchList(JdwpEvent** matchList, int matchCount); |
| 267 | void EventFinish(ExpandBuf* pReq); |
| 268 | void FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** matchList, int* pMatchCount); |
| 269 | void UnregisterEvent(JdwpEvent* pEvent); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 270 | |
| 271 | public: // TODO: fix privacy |
| 272 | const JdwpOptions* options_; |
| 273 | private: |
| 274 | |
| 275 | /* wait for creation of the JDWP thread */ |
| 276 | Mutex thread_start_lock_; |
| 277 | ConditionVariable thread_start_cond_; |
| 278 | |
| 279 | volatile int32_t debug_thread_started_; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 280 | pthread_t pthread_; |
| 281 | Thread* thread_; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 282 | public: // TODO: fix privacy |
| 283 | ObjectId debugThreadId; |
| 284 | private: |
| 285 | bool run; |
| 286 | |
| 287 | const JdwpTransport* transport; |
| 288 | public: // TODO: fix privacy |
| 289 | JdwpNetState* netState; |
| 290 | private: |
| 291 | |
| 292 | /* for wait-for-debugger */ |
| 293 | Mutex attach_lock_; |
| 294 | ConditionVariable attach_cond_; |
| 295 | |
| 296 | /* time of last debugger activity, in milliseconds */ |
| 297 | int64_t lastActivityWhen; |
| 298 | |
| 299 | /* global counters and a mutex to protect them */ |
| 300 | uint32_t requestSerial; |
| 301 | uint32_t eventSerial; |
| 302 | Mutex serial_lock_; |
| 303 | |
| 304 | /* |
| 305 | * Events requested by the debugger (breakpoints, class prep, etc). |
| 306 | */ |
| 307 | public: // TODO: fix privacy |
| 308 | int numEvents; /* #of elements in eventList */ |
| 309 | JdwpEvent* eventList; /* linked list of events */ |
| 310 | Mutex event_lock_; /* guards numEvents/eventList */ |
| 311 | private: |
| 312 | |
| 313 | /* |
| 314 | * Synchronize suspension of event thread (to avoid receiving "resume" |
| 315 | * events before the thread has finished suspending itself). |
| 316 | */ |
| 317 | Mutex event_thread_lock_; |
| 318 | ConditionVariable event_thread_cond_; |
| 319 | ObjectId eventThreadId; |
| 320 | |
| 321 | /* |
| 322 | * DDM support. |
| 323 | */ |
| 324 | public: // TODO: fix privacy |
| 325 | bool ddmActive; |
| 326 | private: |
| 327 | }; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 328 | |
| 329 | } // namespace JDWP |
| 330 | |
| 331 | } // namespace art |
| 332 | |
| 333 | #endif // ART_JDWP_JDWP_H_ |