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