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