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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_JDWP_JDWP_H_ |
| 18 | #define ART_RUNTIME_JDWP_JDWP_H_ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 19 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 20 | #include "atomic.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 21 | #include "base/mutex.h" |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 22 | #include "jdwp/jdwp_bits.h" |
| 23 | #include "jdwp/jdwp_constants.h" |
| 24 | #include "jdwp/jdwp_expand_buf.h" |
| 25 | |
| 26 | #include <pthread.h> |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | struct iovec; |
| 32 | |
| 33 | namespace art { |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 34 | |
| 35 | union JValue; |
| 36 | class Thread; |
| 37 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | namespace mirror { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 39 | class ArtMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | } // namespace mirror |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 42 | namespace JDWP { |
| 43 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Fundamental types. |
| 46 | * |
| 47 | * ObjectId and RefTypeId must be the same size. |
| 48 | */ |
| 49 | typedef uint32_t FieldId; /* static or instance field */ |
| 50 | typedef uint32_t MethodId; /* any kind of method, including constructors */ |
| 51 | typedef uint64_t ObjectId; /* any object (threadID, stringID, arrayID, etc) */ |
| 52 | typedef uint64_t RefTypeId; /* like ObjectID, but unique for Class objects */ |
| 53 | typedef uint64_t FrameId; /* short-lived stack frame ID */ |
| 54 | |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 55 | ObjectId ReadObjectId(const uint8_t** pBuf); |
| 56 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 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); } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 68 | /* |
| 69 | * Holds a JDWP "location". |
| 70 | */ |
| 71 | struct JdwpLocation { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 72 | JdwpTypeTag type_tag; |
| 73 | RefTypeId class_id; |
| 74 | MethodId method_id; |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 75 | uint64_t dex_pc; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 76 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 77 | std::ostream& operator<<(std::ostream& os, const JdwpLocation& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 78 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 79 | bool operator==(const JdwpLocation& lhs, const JdwpLocation& rhs); |
| 80 | bool operator!=(const JdwpLocation& lhs, const JdwpLocation& rhs); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * How we talk to the debugger. |
| 84 | */ |
| 85 | enum JdwpTransportType { |
| 86 | kJdwpTransportUnknown = 0, |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 87 | kJdwpTransportSocket, // transport=dt_socket |
| 88 | kJdwpTransportAndroidAdb, // transport=dt_android_adb |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 89 | }; |
| 90 | std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs); |
| 91 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 92 | struct JdwpOptions { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 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 | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 97 | uint16_t port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 100 | struct JdwpEvent; |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 101 | class JdwpNetStateBase; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 102 | struct ModBasket; |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 103 | class Request; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 104 | |
| 105 | /* |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 106 | * State for JDWP functions. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 107 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 108 | struct JdwpState { |
| 109 | /* |
| 110 | * Perform one-time initialization. |
| 111 | * |
| 112 | * Among other things, this binds to a port to listen for a connection from |
| 113 | * the debugger. |
| 114 | * |
| 115 | * Returns a newly-allocated JdwpState struct on success, or NULL on failure. |
| 116 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 117 | static JdwpState* Create(const JdwpOptions* options) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 118 | LOCKS_EXCLUDED(Locks::mutator_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 119 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 120 | ~JdwpState(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 121 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Returns "true" if a debugger or DDM is connected. |
| 124 | */ |
| 125 | bool IsActive(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 126 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Returns the Thread* for the JDWP daemon thread. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 129 | */ |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 130 | Thread* GetDebugThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 131 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 132 | /* |
| 133 | * Get time, in milliseconds, since the last debugger activity. |
| 134 | */ |
| 135 | int64_t LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 136 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 137 | void ExitAfterReplying(int exit_status); |
| 138 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 139 | /* |
| 140 | * When we hit a debugger event that requires suspension, it's important |
| 141 | * that we wait for the thread to suspend itself before processing any |
| 142 | * additional requests. (Otherwise, if the debugger immediately sends a |
| 143 | * "resume thread" command, the resume might arrive before the thread has |
| 144 | * suspended itself.) |
| 145 | * |
| 146 | * The thread should call the "set" function before sending the event to |
| 147 | * the debugger. The main JDWP handler loop calls "get" before processing |
| 148 | * an event, and will wait for thread suspension if it's set. Once the |
| 149 | * thread has suspended itself, the JDWP handler calls "clear" and |
| 150 | * continues processing the current event. This works in the suspend-all |
| 151 | * case because the event thread doesn't suspend itself until everything |
| 152 | * else has suspended. |
| 153 | * |
| 154 | * It's possible that multiple threads could encounter thread-suspending |
| 155 | * events at the same time, so we grab a mutex in the "set" call, and |
| 156 | * release it in the "clear" call. |
| 157 | */ |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 158 | // ObjectId GetWaitForEventThread(); |
Sebastien Hertz | 400a3a9 | 2014-02-24 14:56:21 +0100 | [diff] [blame] | 159 | void SetWaitForEventThread(ObjectId threadId) |
| 160 | LOCKS_EXCLUDED(event_thread_lock_, process_request_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 161 | void ClearWaitForEventThread() LOCKS_EXCLUDED(event_thread_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 162 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 163 | /* |
| 164 | * These notify the debug code that something interesting has happened. This |
| 165 | * could be a thread starting or ending, an exception, or an opportunity |
| 166 | * for a breakpoint. These calls do not mean that an event the debugger |
| 167 | * is interested has happened, just that something has happened that the |
| 168 | * debugger *might* be interested in. |
| 169 | * |
| 170 | * The item of interest may trigger multiple events, some or all of which |
| 171 | * are grouped together in a single response. |
| 172 | * |
| 173 | * The event may cause the current thread or all threads (except the |
| 174 | * JDWP support thread) to be suspended. |
| 175 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 176 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 177 | /* |
| 178 | * The VM has finished initializing. Only called when the debugger is |
| 179 | * connected at the time initialization completes. |
| 180 | */ |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 181 | bool PostVMStart() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 182 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 183 | /* |
| 184 | * A location of interest has been reached. This is used for breakpoints, |
| 185 | * single-stepping, and method entry/exit. (JDWP requires that these four |
| 186 | * events are grouped together in a single response.) |
| 187 | * |
| 188 | * In some cases "*pLoc" will just have a method and class name, e.g. when |
| 189 | * issuing a MethodEntry on a native method. |
| 190 | * |
| 191 | * "eventFlags" indicates the types of events that have occurred. |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 192 | * |
| 193 | * "returnValue" is non-null for MethodExit events only. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 194 | */ |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 195 | bool PostLocationEvent(const JdwpLocation* pLoc, ObjectId thisPtr, int eventFlags, |
| 196 | const JValue* returnValue) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 197 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 198 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 199 | /* |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 200 | * A field of interest has been accessed or modified. This is used for field access and field |
| 201 | * modification events. |
| 202 | * |
| 203 | * "fieldValue" is non-null for field modification events only. |
| 204 | * "is_modification" is true for field modification, false for field access. |
| 205 | */ |
| 206 | bool PostFieldEvent(const JdwpLocation* pLoc, RefTypeId typeId, FieldId fieldId, |
| 207 | ObjectId thisPtr, const JValue* fieldValue, bool is_modification) |
| 208 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 209 | |
| 210 | /* |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 211 | * An exception has been thrown. |
| 212 | * |
| 213 | * Pass in a zeroed-out "*pCatchLoc" if the exception wasn't caught. |
| 214 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 215 | bool PostException(const JdwpLocation* pThrowLoc, ObjectId excepId, RefTypeId excepClassId, |
| 216 | const JdwpLocation* pCatchLoc, ObjectId thisPtr) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 217 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 218 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 219 | /* |
| 220 | * A thread has started or stopped. |
| 221 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 222 | bool PostThreadChange(ObjectId threadId, bool start) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 223 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 224 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 225 | /* |
| 226 | * Class has been prepared. |
| 227 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 228 | bool PostClassPrepare(JdwpTypeTag tag, RefTypeId refTypeId, const std::string& signature, |
| 229 | int status) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 230 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 231 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 232 | /* |
| 233 | * The VM is about to stop. |
| 234 | */ |
| 235 | bool PostVMDeath(); |
| 236 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 237 | // Called if/when we realize we're talking to DDMS. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 238 | void NotifyDdmsActive() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 239 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 240 | /* |
| 241 | * Send up a chunk of DDM data. |
| 242 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 243 | void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 244 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 245 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 246 | bool HandlePacket(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 247 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 248 | void SendRequest(ExpandBuf* pReq); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 249 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 250 | void ResetState() |
| 251 | LOCKS_EXCLUDED(event_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 252 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 253 | |
| 254 | /* atomic ops to get next serial number */ |
| 255 | uint32_t NextRequestSerial(); |
| 256 | uint32_t NextEventSerial(); |
| 257 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 258 | void Run() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 259 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 260 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 261 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 262 | /* |
| 263 | * Register an event by adding it to the event list. |
| 264 | * |
| 265 | * "*pEvent" must be storage allocated with jdwpEventAlloc(). The caller |
| 266 | * may discard its pointer after calling this. |
| 267 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 268 | JdwpError RegisterEvent(JdwpEvent* pEvent) |
| 269 | LOCKS_EXCLUDED(event_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 270 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * Unregister an event, given the requestId. |
| 274 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 275 | void UnregisterEventById(uint32_t requestId) |
| 276 | LOCKS_EXCLUDED(event_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 277 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * Unregister all events. |
| 281 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 282 | void UnregisterAll() |
| 283 | LOCKS_EXCLUDED(event_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 284 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 285 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 286 | private: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 287 | explicit JdwpState(const JdwpOptions* options); |
Sebastien Hertz | 43c8d72 | 2014-03-18 12:19:52 +0100 | [diff] [blame] | 288 | size_t ProcessRequest(Request& request, ExpandBuf* pReply); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 289 | bool InvokeInProgress(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 290 | bool IsConnected(); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 291 | void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 292 | LOCKS_EXCLUDED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 293 | void SendRequestAndPossiblySuspend(ExpandBuf* pReq, JdwpSuspendPolicy suspend_policy, |
| 294 | ObjectId threadId) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 295 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 296 | void CleanupMatchList(JdwpEvent** match_list, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 297 | int match_count) |
| 298 | EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 299 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 300 | void EventFinish(ExpandBuf* pReq); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 301 | void FindMatchingEvents(JdwpEventKind eventKind, |
| 302 | ModBasket* basket, |
| 303 | JdwpEvent** match_list, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 304 | int* pMatchCount) |
| 305 | EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 306 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 307 | void UnregisterEvent(JdwpEvent* pEvent) |
| 308 | EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 309 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 310 | void SendBufferedRequest(uint32_t type, const std::vector<iovec>& iov); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 311 | |
Sebastien Hertz | 99660e1 | 2014-02-19 15:04:42 +0100 | [diff] [blame] | 312 | void StartProcessingRequest() LOCKS_EXCLUDED(process_request_lock_); |
| 313 | void EndProcessingRequest() LOCKS_EXCLUDED(process_request_lock_); |
| 314 | void WaitForProcessingRequest() LOCKS_EXCLUDED(process_request_lock_); |
| 315 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 316 | public: // TODO: fix privacy |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 317 | const JdwpOptions* options_; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 318 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 319 | private: |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 320 | /* wait for creation of the JDWP thread */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 321 | Mutex thread_start_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 322 | ConditionVariable thread_start_cond_ GUARDED_BY(thread_start_lock_); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 323 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 324 | pthread_t pthread_; |
| 325 | Thread* thread_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 326 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 327 | volatile int32_t debug_thread_started_ GUARDED_BY(thread_start_lock_); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 328 | ObjectId debug_thread_id_; |
| 329 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 330 | private: |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 331 | bool run; |
| 332 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 333 | public: // TODO: fix privacy |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 334 | JdwpNetStateBase* netState; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 335 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 336 | private: |
| 337 | // For wait-for-debugger. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 338 | Mutex attach_lock_ ACQUIRED_AFTER(thread_start_lock_); |
| 339 | ConditionVariable attach_cond_ GUARDED_BY(attach_lock_); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 340 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 341 | // Time of last debugger activity, in milliseconds. |
| 342 | int64_t last_activity_time_ms_; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 343 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 344 | // Global counters and a mutex to protect them. |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 345 | AtomicInteger request_serial_; |
| 346 | AtomicInteger event_serial_; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 347 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 348 | // Linked list of events requested by the debugger (breakpoints, class prep, etc). |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 349 | Mutex event_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_BEFORE(Locks::breakpoint_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 350 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 351 | JdwpEvent* event_list_ GUARDED_BY(event_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 352 | size_t event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 353 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 354 | // Used to synchronize suspension of the event thread (to avoid receiving "resume" |
| 355 | // events before the thread has finished suspending itself). |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 356 | Mutex event_thread_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 357 | ConditionVariable event_thread_cond_ GUARDED_BY(event_thread_lock_); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 358 | ObjectId event_thread_id_; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 359 | |
Sebastien Hertz | 99660e1 | 2014-02-19 15:04:42 +0100 | [diff] [blame] | 360 | // Used to synchronize request processing and event sending (to avoid sending an event before |
| 361 | // sending the reply of a command being processed). |
Sebastien Hertz | 400a3a9 | 2014-02-24 14:56:21 +0100 | [diff] [blame] | 362 | Mutex process_request_lock_ ACQUIRED_AFTER(event_thread_lock_); |
Sebastien Hertz | 99660e1 | 2014-02-19 15:04:42 +0100 | [diff] [blame] | 363 | ConditionVariable process_request_cond_ GUARDED_BY(process_request_lock_); |
| 364 | bool processing_request_ GUARDED_BY(process_request_lock_); |
| 365 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 366 | bool ddm_is_active_; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 367 | |
| 368 | bool should_exit_; |
| 369 | int exit_status_; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 370 | }; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 371 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 372 | std::string DescribeField(const FieldId& field_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 373 | std::string DescribeMethod(const MethodId& method_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 374 | std::string DescribeRefTypeId(const RefTypeId& ref_type_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 375 | |
| 376 | class Request { |
| 377 | public: |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 378 | Request(const uint8_t* bytes, uint32_t available); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 379 | ~Request(); |
| 380 | |
| 381 | std::string ReadUtf8String(); |
| 382 | |
| 383 | // Helper function: read a variable-width value from the input buffer. |
| 384 | uint64_t ReadValue(size_t width); |
| 385 | |
| 386 | int32_t ReadSigned32(const char* what); |
| 387 | |
| 388 | uint32_t ReadUnsigned32(const char* what); |
| 389 | |
| 390 | FieldId ReadFieldId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 391 | |
| 392 | MethodId ReadMethodId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 393 | |
| 394 | ObjectId ReadObjectId(const char* specific_kind); |
| 395 | |
| 396 | ObjectId ReadArrayId(); |
| 397 | |
| 398 | ObjectId ReadObjectId(); |
| 399 | |
| 400 | ObjectId ReadThreadId(); |
| 401 | |
| 402 | ObjectId ReadThreadGroupId(); |
| 403 | |
| 404 | RefTypeId ReadRefTypeId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 405 | |
| 406 | FrameId ReadFrameId(); |
| 407 | |
| 408 | template <typename T> T ReadEnum1(const char* specific_kind) { |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 409 | T value = static_cast<T>(Read1()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 410 | VLOG(jdwp) << " " << specific_kind << " " << value; |
| 411 | return value; |
| 412 | } |
| 413 | |
| 414 | JdwpTag ReadTag(); |
| 415 | |
| 416 | JdwpTypeTag ReadTypeTag(); |
| 417 | |
| 418 | JdwpLocation ReadLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 419 | |
| 420 | JdwpModKind ReadModKind(); |
| 421 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 422 | // |
| 423 | // Return values from this JDWP packet's header. |
| 424 | // |
| 425 | size_t GetLength() { return byte_count_; } |
| 426 | uint32_t GetId() { return id_; } |
| 427 | uint8_t GetCommandSet() { return command_set_; } |
| 428 | uint8_t GetCommand() { return command_; } |
| 429 | |
| 430 | // Returns the number of bytes remaining. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 431 | size_t size() { return end_ - p_; } |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 432 | |
| 433 | // Returns a pointer to the next byte. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 434 | const uint8_t* data() { return p_; } |
| 435 | |
| 436 | void Skip(size_t count) { p_ += count; } |
| 437 | |
| 438 | void CheckConsumed(); |
| 439 | |
| 440 | private: |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 441 | uint8_t Read1(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 442 | uint16_t Read2BE(); |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 443 | uint32_t Read4BE(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 444 | uint64_t Read8BE(); |
| 445 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 446 | uint32_t byte_count_; |
| 447 | uint32_t id_; |
| 448 | uint8_t command_set_; |
| 449 | uint8_t command_; |
| 450 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 451 | const uint8_t* p_; |
| 452 | const uint8_t* end_; |
| 453 | |
| 454 | DISALLOW_COPY_AND_ASSIGN(Request); |
| 455 | }; |
| 456 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 457 | } // namespace JDWP |
| 458 | |
| 459 | } // namespace art |
| 460 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 461 | #endif // ART_RUNTIME_JDWP_JDWP_H_ |