Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "debugger.h" |
| 18 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 19 | #include <sys/uio.h> |
| 20 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | |
| 23 | #include "class_linker.h" |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 24 | #include "class_loader.h" |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 25 | #include "dex_verifier.h" // For Instruction. |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 26 | #include "context.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 27 | #include "object_utils.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 28 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 29 | #include "ScopedPrimitiveArray.h" |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 30 | #include "space.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 31 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 32 | #include "thread_list.h" |
| 33 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 34 | extern "C" void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*); |
| 35 | #ifndef HAVE_ANDROID_OS |
| 36 | void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*) { |
| 37 | // No-op for glibc. |
| 38 | } |
| 39 | #endif |
| 40 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 41 | namespace art { |
| 42 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 43 | static const size_t kMaxAllocRecordStackDepth = 16; // Max 255. |
| 44 | static const size_t kNumAllocRecords = 512; // Must be power of 2. |
| 45 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 46 | static const uintptr_t kInvalidId = 1; |
| 47 | static const Object* kInvalidObject = reinterpret_cast<Object*>(kInvalidId); |
| 48 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 49 | class ObjectRegistry { |
| 50 | public: |
| 51 | ObjectRegistry() : lock_("ObjectRegistry lock") { |
| 52 | } |
| 53 | |
| 54 | JDWP::ObjectId Add(Object* o) { |
| 55 | if (o == NULL) { |
| 56 | return 0; |
| 57 | } |
| 58 | JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o)); |
| 59 | MutexLock mu(lock_); |
| 60 | map_[id] = o; |
| 61 | return id; |
| 62 | } |
| 63 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 64 | void Clear() { |
| 65 | MutexLock mu(lock_); |
| 66 | LOG(DEBUG) << "Debugger has detached; object registry had " << map_.size() << " entries"; |
| 67 | map_.clear(); |
| 68 | } |
| 69 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 70 | bool Contains(JDWP::ObjectId id) { |
| 71 | MutexLock mu(lock_); |
| 72 | return map_.find(id) != map_.end(); |
| 73 | } |
| 74 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 75 | template<typename T> T Get(JDWP::ObjectId id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 76 | if (id == 0) { |
| 77 | return NULL; |
| 78 | } |
| 79 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 80 | MutexLock mu(lock_); |
| 81 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 82 | It it = map_.find(id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 83 | return (it != map_.end()) ? reinterpret_cast<T>(it->second) : reinterpret_cast<T>(kInvalidId); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 86 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 87 | MutexLock mu(lock_); |
| 88 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 89 | for (It it = map_.begin(); it != map_.end(); ++it) { |
| 90 | visitor(it->second, arg); |
| 91 | } |
| 92 | } |
| 93 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 94 | private: |
| 95 | Mutex lock_; |
| 96 | std::map<JDWP::ObjectId, Object*> map_; |
| 97 | }; |
| 98 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 99 | struct AllocRecordStackTraceElement { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 100 | Method* method; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 101 | uintptr_t raw_pc; |
| 102 | |
| 103 | int32_t LineNumber() const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 104 | return MethodHelper(method).GetLineNumFromNativePC(raw_pc); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
| 108 | struct AllocRecord { |
| 109 | Class* type; |
| 110 | size_t byte_count; |
| 111 | uint16_t thin_lock_id; |
| 112 | AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method. |
| 113 | |
| 114 | size_t GetDepth() { |
| 115 | size_t depth = 0; |
| 116 | while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) { |
| 117 | ++depth; |
| 118 | } |
| 119 | return depth; |
| 120 | } |
| 121 | }; |
| 122 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 123 | struct Breakpoint { |
| 124 | Method* method; |
| 125 | uint32_t pc; |
| 126 | Breakpoint(Method* method, uint32_t pc) : method(method), pc(pc) {} |
| 127 | }; |
| 128 | |
| 129 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) { |
| 130 | os << "Breakpoint[" << PrettyMethod(rhs.method) << " @" << rhs.pc << "]"; |
| 131 | return os; |
| 132 | } |
| 133 | |
| 134 | struct SingleStepControl { |
| 135 | // Are we single-stepping right now? |
| 136 | bool is_active; |
| 137 | Thread* thread; |
| 138 | |
| 139 | JDWP::JdwpStepSize step_size; |
| 140 | JDWP::JdwpStepDepth step_depth; |
| 141 | |
| 142 | const Method* method; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 143 | int32_t line_number; // Or -1 for native methods. |
| 144 | std::set<uint32_t> dex_pcs; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 145 | int stack_depth; |
| 146 | }; |
| 147 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 148 | // JDWP is allowed unless the Zygote forbids it. |
| 149 | static bool gJdwpAllowed = true; |
| 150 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 151 | // Was there a -Xrunjdwp or -agent argument on the command-line? |
| 152 | static bool gJdwpConfigured = false; |
| 153 | |
| 154 | // Broken-down JDWP options. (Only valid if gJdwpConfigured is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 155 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 156 | |
| 157 | // Runtime JDWP state. |
| 158 | static JDWP::JdwpState* gJdwpState = NULL; |
| 159 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 160 | static bool gDebuggerActive; // debugger is making requests. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 161 | static bool gDisposed; // debugger called VirtualMachine.Dispose, so we should drop the connection. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 162 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 163 | static bool gDdmThreadNotification = false; |
| 164 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 165 | // DDMS GC-related settings. |
| 166 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 167 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 168 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 169 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 170 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 171 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 172 | static ObjectRegistry* gRegistry = NULL; |
| 173 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 174 | // Recent allocation tracking. |
| 175 | static Mutex gAllocTrackerLock("AllocTracker lock"); |
| 176 | AllocRecord* Dbg::recent_allocation_records_ = NULL; // TODO: CircularBuffer<AllocRecord> |
| 177 | static size_t gAllocRecordHead = 0; |
| 178 | static size_t gAllocRecordCount = 0; |
| 179 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 180 | // Breakpoints and single-stepping. |
| 181 | static Mutex gBreakpointsLock("breakpoints lock"); |
| 182 | static std::vector<Breakpoint> gBreakpoints; |
| 183 | static SingleStepControl gSingleStepControl; |
| 184 | |
| 185 | static bool IsBreakpoint(Method* m, uint32_t dex_pc) { |
| 186 | MutexLock mu(gBreakpointsLock); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 187 | uint32_t pc = dex_pc / 2; // dex bytecodes are twice the size JDWP expects. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 188 | for (size_t i = 0; i < gBreakpoints.size(); ++i) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 189 | if (gBreakpoints[i].method == m && gBreakpoints[i].pc == pc) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 190 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 197 | static Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status) { |
| 198 | Object* o = gRegistry->Get<Object*>(id); |
| 199 | if (o == NULL || o == kInvalidObject) { |
| 200 | status = JDWP::ERR_INVALID_OBJECT; |
| 201 | return NULL; |
| 202 | } |
| 203 | if (!o->IsArrayInstance()) { |
| 204 | status = JDWP::ERR_INVALID_ARRAY; |
| 205 | return NULL; |
| 206 | } |
| 207 | status = JDWP::ERR_NONE; |
| 208 | return o->AsArray(); |
| 209 | } |
| 210 | |
| 211 | static Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status) { |
| 212 | Object* o = gRegistry->Get<Object*>(id); |
| 213 | if (o == NULL || o == kInvalidObject) { |
| 214 | status = JDWP::ERR_INVALID_OBJECT; |
| 215 | return NULL; |
| 216 | } |
| 217 | if (!o->IsClass()) { |
| 218 | status = JDWP::ERR_INVALID_CLASS; |
| 219 | return NULL; |
| 220 | } |
| 221 | status = JDWP::ERR_NONE; |
| 222 | return o->AsClass(); |
| 223 | } |
| 224 | |
| 225 | static Thread* DecodeThread(JDWP::ObjectId threadId) { |
| 226 | Object* thread_peer = gRegistry->Get<Object*>(threadId); |
| 227 | if (thread_peer == NULL || thread_peer == kInvalidObject) { |
| 228 | return NULL; |
| 229 | } |
| 230 | return Thread::FromManagedThread(thread_peer); |
| 231 | } |
| 232 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 233 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 234 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 235 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 236 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 237 | } |
| 238 | |
| 239 | static JDWP::JdwpTag TagFromClass(Class* c) { |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 240 | CHECK(c != NULL); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 241 | if (c->IsArrayClass()) { |
| 242 | return JDWP::JT_ARRAY; |
| 243 | } |
| 244 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 245 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 246 | if (c->IsStringClass()) { |
| 247 | return JDWP::JT_STRING; |
| 248 | } else if (c->IsClassClass()) { |
| 249 | return JDWP::JT_CLASS_OBJECT; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 250 | } else if (class_linker->FindSystemClass("Ljava/lang/Thread;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 251 | return JDWP::JT_THREAD; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 252 | } else if (class_linker->FindSystemClass("Ljava/lang/ThreadGroup;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 253 | return JDWP::JT_THREAD_GROUP; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 254 | } else if (class_linker->FindSystemClass("Ljava/lang/ClassLoader;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 255 | return JDWP::JT_CLASS_LOADER; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 256 | } else { |
| 257 | return JDWP::JT_OBJECT; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Objects declared to hold Object might actually hold a more specific |
| 263 | * type. The debugger may take a special interest in these (e.g. it |
| 264 | * wants to display the contents of Strings), so we want to return an |
| 265 | * appropriate tag. |
| 266 | * |
| 267 | * Null objects are tagged JT_OBJECT. |
| 268 | */ |
| 269 | static JDWP::JdwpTag TagFromObject(const Object* o) { |
| 270 | return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass()); |
| 271 | } |
| 272 | |
| 273 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 274 | switch (tag) { |
| 275 | case JDWP::JT_BOOLEAN: |
| 276 | case JDWP::JT_BYTE: |
| 277 | case JDWP::JT_CHAR: |
| 278 | case JDWP::JT_FLOAT: |
| 279 | case JDWP::JT_DOUBLE: |
| 280 | case JDWP::JT_INT: |
| 281 | case JDWP::JT_LONG: |
| 282 | case JDWP::JT_SHORT: |
| 283 | case JDWP::JT_VOID: |
| 284 | return true; |
| 285 | default: |
| 286 | return false; |
| 287 | } |
| 288 | } |
| 289 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 290 | /* |
| 291 | * Handle one of the JDWP name/value pairs. |
| 292 | * |
| 293 | * JDWP options are: |
| 294 | * help: if specified, show help message and bail |
| 295 | * transport: may be dt_socket or dt_shmem |
| 296 | * address: for dt_socket, "host:port", or just "port" when listening |
| 297 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 298 | * timeout: how long to wait for debugger to connect / listen |
| 299 | * |
| 300 | * Useful with server=n (these aren't supported yet): |
| 301 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 302 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 303 | * launch=<command-line>: launch the debugger itself |
| 304 | * |
| 305 | * The "transport" option is required, as is "address" if server=n. |
| 306 | */ |
| 307 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 308 | if (name == "transport") { |
| 309 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 310 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 311 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 312 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 313 | } else { |
| 314 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 315 | return false; |
| 316 | } |
| 317 | } else if (name == "server") { |
| 318 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 319 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 320 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 321 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 322 | } else { |
| 323 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 324 | return false; |
| 325 | } |
| 326 | } else if (name == "suspend") { |
| 327 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 328 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 329 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 330 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 331 | } else { |
| 332 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 333 | return false; |
| 334 | } |
| 335 | } else if (name == "address") { |
| 336 | /* this is either <port> or <host>:<port> */ |
| 337 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 338 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 339 | std::string::size_type colon = value.find(':'); |
| 340 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 341 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 342 | port_string = value.substr(colon + 1); |
| 343 | } else { |
| 344 | port_string = value; |
| 345 | } |
| 346 | if (port_string.empty()) { |
| 347 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 348 | return false; |
| 349 | } |
| 350 | char* end; |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 351 | uint64_t port = strtoul(port_string.c_str(), &end, 10); |
| 352 | if (*end != '\0' || port > 0xffff) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 353 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 354 | return false; |
| 355 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 356 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 357 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 358 | /* valid but unsupported */ |
| 359 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 360 | } else { |
| 361 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 362 | } |
| 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 369 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 370 | */ |
| 371 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 372 | VLOG(jdwp) << "ParseJdwpOptions: " << options; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 373 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 374 | std::vector<std::string> pairs; |
| 375 | Split(options, ',', pairs); |
| 376 | |
| 377 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 378 | std::string::size_type equals = pairs[i].find('='); |
| 379 | if (equals == std::string::npos) { |
| 380 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 381 | return false; |
| 382 | } |
| 383 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 384 | } |
| 385 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 386 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 387 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 388 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 389 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 390 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | gJdwpConfigured = true; |
| 395 | return true; |
| 396 | } |
| 397 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 398 | void Dbg::StartJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 399 | if (!gJdwpAllowed || !gJdwpConfigured) { |
| 400 | // No JDWP for you! |
| 401 | return; |
| 402 | } |
| 403 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 404 | CHECK(gRegistry == NULL); |
| 405 | gRegistry = new ObjectRegistry; |
| 406 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 407 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 408 | // debugger, passively listen for a debugger, or block waiting for a |
| 409 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 410 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 411 | if (gJdwpState == NULL) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 412 | // We probably failed because some other process has the port already, which means that |
| 413 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 414 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 415 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // If a debugger has already attached, send the "welcome" message. |
| 419 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 420 | if (gJdwpState->IsActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 421 | //ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 422 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 423 | LOG(WARNING) << "Failed to post 'start' message to debugger"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 428 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 429 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 430 | delete gRegistry; |
| 431 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 434 | void Dbg::GcDidFinish() { |
| 435 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
| 436 | LOG(DEBUG) << "Sending VM heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 437 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 438 | } |
| 439 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
| 440 | LOG(DEBUG) << "Dumping VM heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 441 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 442 | } |
| 443 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
| 444 | LOG(DEBUG) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 445 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 449 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 450 | gJdwpAllowed = allowed; |
| 451 | } |
| 452 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 453 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 454 | return Thread::Current()->GetInvokeReq(); |
| 455 | } |
| 456 | |
| 457 | Thread* Dbg::GetDebugThread() { |
| 458 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 459 | } |
| 460 | |
| 461 | void Dbg::ClearWaitForEventThread() { |
| 462 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 466 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 467 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 468 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 469 | gDisposed = false; |
| 470 | } |
| 471 | |
| 472 | void Dbg::Disposed() { |
| 473 | gDisposed = true; |
| 474 | } |
| 475 | |
| 476 | bool Dbg::IsDisposed() { |
| 477 | return gDisposed; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 480 | void Dbg::GoActive() { |
| 481 | // Enable all debugging features, including scans for breakpoints. |
| 482 | // This is a no-op if we're already active. |
| 483 | // Only called from the JDWP handler thread. |
| 484 | if (gDebuggerActive) { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | LOG(INFO) << "Debugger is active"; |
| 489 | |
| 490 | // TODO: CHECK we don't have any outstanding breakpoints. |
| 491 | |
| 492 | gDebuggerActive = true; |
| 493 | |
| 494 | //dvmEnableAllSubMode(kSubModeDebuggerActive); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 498 | CHECK(gDebuggerConnected); |
| 499 | |
| 500 | gDebuggerActive = false; |
| 501 | |
| 502 | //dvmDisableAllSubMode(kSubModeDebuggerActive); |
| 503 | |
| 504 | gRegistry->Clear(); |
| 505 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | bool Dbg::IsDebuggerConnected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 509 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | bool Dbg::IsDebuggingEnabled() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 513 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 517 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | int Dbg::ThreadRunning() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 521 | return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | int Dbg::ThreadWaiting() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 525 | return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 528 | int Dbg::ThreadContinuing(int new_state) { |
| 529 | return static_cast<int>(Thread::Current()->SetState(static_cast<Thread::State>(new_state))); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 533 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void Dbg::Exit(int status) { |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 537 | exit(status); // This is all dalvik did. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 540 | void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 541 | if (gRegistry != NULL) { |
| 542 | gRegistry->VisitRoots(visitor, arg); |
| 543 | } |
| 544 | } |
| 545 | |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 546 | std::string Dbg::GetClassName(JDWP::RefTypeId classId) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 547 | Object* o = gRegistry->Get<Object*>(classId); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 548 | if (o == NULL) { |
| 549 | return "NULL"; |
| 550 | } |
| 551 | if (o == kInvalidObject) { |
| 552 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(classId)); |
| 553 | } |
| 554 | if (!o->IsClass()) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 555 | return StringPrintf("non-class %p", o); // This is only used for debugging output anyway. |
| 556 | } |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 557 | return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 560 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& classObjectId) { |
| 561 | JDWP::JdwpError status; |
| 562 | Class* c = DecodeClass(id, status); |
| 563 | if (c == NULL) { |
| 564 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 565 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 566 | classObjectId = gRegistry->Add(c); |
| 567 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 570 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclassId) { |
| 571 | JDWP::JdwpError status; |
| 572 | Class* c = DecodeClass(id, status); |
| 573 | if (c == NULL) { |
| 574 | return status; |
| 575 | } |
| 576 | if (c->IsInterface()) { |
| 577 | // http://code.google.com/p/android/issues/detail?id=20856 |
| 578 | superclassId = NULL; |
| 579 | } else { |
| 580 | superclassId = gRegistry->Add(c->GetSuperClass()); |
| 581 | } |
| 582 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 585 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 586 | Object* o = gRegistry->Get<Object*>(id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 587 | if (o == NULL || o == kInvalidObject) { |
| 588 | return JDWP::ERR_INVALID_OBJECT; |
| 589 | } |
| 590 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 591 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 594 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
| 595 | JDWP::JdwpError status; |
| 596 | Class* c = DecodeClass(id, status); |
| 597 | if (c == NULL) { |
| 598 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 599 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 600 | |
| 601 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 602 | |
| 603 | // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set. |
| 604 | // Class.getModifiers doesn't return it, but JDWP does, so we set it here. |
| 605 | access_flags |= kAccSuper; |
| 606 | |
| 607 | expandBufAdd4BE(pReply, access_flags); |
| 608 | |
| 609 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 612 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId classId, JDWP::ExpandBuf* pReply) { |
| 613 | JDWP::JdwpError status; |
| 614 | Class* c = DecodeClass(classId, status); |
| 615 | if (c == NULL) { |
| 616 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 617 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 618 | |
| 619 | expandBufAdd1(pReply, c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS); |
| 620 | expandBufAddRefTypeId(pReply, classId); |
| 621 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 624 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 625 | // Get the complete list of reference classes (i.e. all classes except |
| 626 | // the primitive types). |
| 627 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 628 | struct ClassListCreator { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 629 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 632 | static bool Visit(Class* c, void* arg) { |
| 633 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 634 | } |
| 635 | |
| 636 | bool Visit(Class* c) { |
| 637 | if (!c->IsPrimitive()) { |
| 638 | classes.push_back(static_cast<JDWP::RefTypeId>(gRegistry->Add(c))); |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 643 | std::vector<JDWP::RefTypeId>& classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 644 | }; |
| 645 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 646 | ClassListCreator clc(classes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 647 | Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 650 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId classId, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) { |
| 651 | JDWP::JdwpError status; |
| 652 | Class* c = DecodeClass(classId, status); |
| 653 | if (c == NULL) { |
| 654 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 657 | if (c->IsArrayClass()) { |
| 658 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 659 | *pTypeTag = JDWP::TT_ARRAY; |
| 660 | } else { |
| 661 | if (c->IsErroneous()) { |
| 662 | *pStatus = JDWP::CS_ERROR; |
| 663 | } else { |
| 664 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 665 | } |
| 666 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 667 | } |
| 668 | |
| 669 | if (pDescriptor != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 670 | *pDescriptor = ClassHelper(c).GetDescriptor(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 671 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 672 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 675 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) { |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 676 | std::vector<Class*> classes; |
| 677 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
| 678 | ids.clear(); |
| 679 | for (size_t i = 0; i < classes.size(); ++i) { |
| 680 | ids.push_back(gRegistry->Add(classes[i])); |
| 681 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 684 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId objectId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 685 | Object* o = gRegistry->Get<Object*>(objectId); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 686 | if (o == NULL || o == kInvalidObject) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 687 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 688 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 689 | |
| 690 | JDWP::JdwpTypeTag type_tag; |
| 691 | if (o->GetClass()->IsArrayClass()) { |
| 692 | type_tag = JDWP::TT_ARRAY; |
| 693 | } else if (o->GetClass()->IsInterface()) { |
| 694 | type_tag = JDWP::TT_INTERFACE; |
| 695 | } else { |
| 696 | type_tag = JDWP::TT_CLASS; |
| 697 | } |
| 698 | JDWP::RefTypeId type_id = gRegistry->Add(o->GetClass()); |
| 699 | |
| 700 | expandBufAdd1(pReply, type_tag); |
| 701 | expandBufAddRefTypeId(pReply, type_id); |
| 702 | |
| 703 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 706 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId classId, std::string& signature) { |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 707 | JDWP::JdwpError status; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 708 | Class* c = DecodeClass(classId, status); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 709 | if (c == NULL) { |
| 710 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 711 | } |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 712 | signature = ClassHelper(c).GetDescriptor(); |
| 713 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 716 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId classId, std::string& result) { |
| 717 | JDWP::JdwpError status; |
| 718 | Class* c = DecodeClass(classId, status); |
| 719 | if (c == NULL) { |
| 720 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 721 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 722 | result = ClassHelper(c).GetSourceFile(); |
| 723 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 726 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 727 | Object* o = gRegistry->Get<Object*>(objectId); |
| 728 | return TagFromObject(o); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 731 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 732 | switch (tag) { |
| 733 | case JDWP::JT_VOID: |
| 734 | return 0; |
| 735 | case JDWP::JT_BYTE: |
| 736 | case JDWP::JT_BOOLEAN: |
| 737 | return 1; |
| 738 | case JDWP::JT_CHAR: |
| 739 | case JDWP::JT_SHORT: |
| 740 | return 2; |
| 741 | case JDWP::JT_FLOAT: |
| 742 | case JDWP::JT_INT: |
| 743 | return 4; |
| 744 | case JDWP::JT_ARRAY: |
| 745 | case JDWP::JT_OBJECT: |
| 746 | case JDWP::JT_STRING: |
| 747 | case JDWP::JT_THREAD: |
| 748 | case JDWP::JT_THREAD_GROUP: |
| 749 | case JDWP::JT_CLASS_LOADER: |
| 750 | case JDWP::JT_CLASS_OBJECT: |
| 751 | return sizeof(JDWP::ObjectId); |
| 752 | case JDWP::JT_DOUBLE: |
| 753 | case JDWP::JT_LONG: |
| 754 | return 8; |
| 755 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 756 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 757 | return -1; |
| 758 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 761 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId arrayId, int& length) { |
| 762 | JDWP::JdwpError status; |
| 763 | Array* a = DecodeArray(arrayId, status); |
| 764 | if (a == NULL) { |
| 765 | return status; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 766 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 767 | length = a->GetLength(); |
| 768 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 771 | JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId arrayId, int offset, int count, JDWP::ExpandBuf* pReply) { |
| 772 | JDWP::JdwpError status; |
| 773 | Array* a = DecodeArray(arrayId, status); |
| 774 | if (a == NULL) { |
| 775 | return status; |
| 776 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 777 | |
| 778 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 779 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 780 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 781 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 782 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 783 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 784 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 785 | expandBufAdd1(pReply, tag); |
| 786 | expandBufAdd4BE(pReply, count); |
| 787 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 788 | if (IsPrimitiveTag(tag)) { |
| 789 | size_t width = GetTagWidth(tag); |
| 790 | const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData()); |
| 791 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 792 | if (width == 8) { |
| 793 | const uint64_t* src8 = reinterpret_cast<const uint64_t*>(src); |
| 794 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 795 | } else if (width == 4) { |
| 796 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 797 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 798 | } else if (width == 2) { |
| 799 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 800 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 801 | } else { |
| 802 | memcpy(dst, &src[offset * width], count * width); |
| 803 | } |
| 804 | } else { |
| 805 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 806 | for (int i = 0; i < count; ++i) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 807 | Object* element = oa->Get(offset + i); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 808 | JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag; |
| 809 | expandBufAdd1(pReply, specific_tag); |
| 810 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 811 | } |
| 812 | } |
| 813 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 814 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 817 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId arrayId, int offset, int count, const uint8_t* src) { |
| 818 | JDWP::JdwpError status; |
| 819 | Array* a = DecodeArray(arrayId, status); |
| 820 | if (a == NULL) { |
| 821 | return status; |
| 822 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 823 | |
| 824 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 825 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 826 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 827 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 828 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 829 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 830 | |
| 831 | if (IsPrimitiveTag(tag)) { |
| 832 | size_t width = GetTagWidth(tag); |
| 833 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData())[offset * width]); |
| 834 | if (width == 8) { |
| 835 | for (int i = 0; i < count; ++i) { |
| 836 | // Handle potentially non-aligned memory access one byte at a time for ARM's benefit. |
| 837 | uint64_t value; |
| 838 | for (size_t j = 0; j < sizeof(uint64_t); ++j) reinterpret_cast<uint8_t*>(&value)[j] = src[j]; |
| 839 | src += sizeof(uint64_t); |
| 840 | JDWP::Write8BE(&dst, value); |
| 841 | } |
| 842 | } else if (width == 4) { |
| 843 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 844 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[i]); |
| 845 | } else if (width == 2) { |
| 846 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 847 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[i]); |
| 848 | } else { |
| 849 | memcpy(&dst[offset * width], src, count * width); |
| 850 | } |
| 851 | } else { |
| 852 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 853 | for (int i = 0; i < count; ++i) { |
| 854 | JDWP::ObjectId id = JDWP::ReadObjectId(&src); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 855 | Object* o = gRegistry->Get<Object*>(id); |
| 856 | if (o == kInvalidObject) { |
| 857 | return JDWP::ERR_INVALID_OBJECT; |
| 858 | } |
| 859 | oa->Set(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 863 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 866 | JDWP::ObjectId Dbg::CreateString(const std::string& str) { |
| 867 | return gRegistry->Add(String::AllocFromModifiedUtf8(str.c_str())); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 870 | JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId classId, JDWP::ObjectId& new_object) { |
| 871 | JDWP::JdwpError status; |
| 872 | Class* c = DecodeClass(classId, status); |
| 873 | if (c == NULL) { |
| 874 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 875 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 876 | new_object = gRegistry->Add(c->AllocObject()); |
| 877 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 880 | /* |
| 881 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 882 | */ |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 883 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId arrayClassId, uint32_t length, JDWP::ObjectId& new_array) { |
| 884 | JDWP::JdwpError status; |
| 885 | Class* c = DecodeClass(arrayClassId, status); |
| 886 | if (c == NULL) { |
| 887 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 888 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 889 | new_array = gRegistry->Add(Array::Alloc(c, length)); |
| 890 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 894 | JDWP::JdwpError status; |
| 895 | Class* c1 = DecodeClass(instClassId, status); |
| 896 | Class* c2 = DecodeClass(classId, status); |
| 897 | if (c1 == NULL || c2 == NULL) { |
| 898 | // TODO: it doesn't seem like we can do any better here? |
| 899 | return false; |
| 900 | } |
| 901 | return c1->InstanceOf(c2); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 904 | static JDWP::FieldId ToFieldId(const Field* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 905 | #ifdef MOVING_GARBAGE_COLLECTOR |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 906 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 907 | #else |
| 908 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| 909 | #endif |
| 910 | } |
| 911 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 912 | static JDWP::MethodId ToMethodId(const Method* m) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 913 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 914 | UNIMPLEMENTED(FATAL); |
| 915 | #else |
| 916 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
| 917 | #endif |
| 918 | } |
| 919 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 920 | static Field* FromFieldId(JDWP::FieldId fid) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 921 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 922 | UNIMPLEMENTED(FATAL); |
| 923 | #else |
| 924 | return reinterpret_cast<Field*>(static_cast<uintptr_t>(fid)); |
| 925 | #endif |
| 926 | } |
| 927 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 928 | static Method* FromMethodId(JDWP::MethodId mid) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 929 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 930 | UNIMPLEMENTED(FATAL); |
| 931 | #else |
| 932 | return reinterpret_cast<Method*>(static_cast<uintptr_t>(mid)); |
| 933 | #endif |
| 934 | } |
| 935 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 936 | static void SetLocation(JDWP::JdwpLocation& location, Method* m, uintptr_t native_pc) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 937 | if (m == NULL) { |
| 938 | memset(&location, 0, sizeof(location)); |
| 939 | } else { |
| 940 | Class* c = m->GetDeclaringClass(); |
| 941 | location.typeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 942 | location.classId = gRegistry->Add(c); |
| 943 | location.methodId = ToMethodId(m); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 944 | location.idx = m->IsNative() ? -1 : m->ToDexPC(native_pc) / 2; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 945 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 948 | std::string Dbg::GetMethodName(JDWP::RefTypeId, JDWP::MethodId methodId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 949 | Method* m = FromMethodId(methodId); |
| 950 | return MethodHelper(m).GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 953 | /* |
| 954 | * Augment the access flags for synthetic methods and fields by setting |
| 955 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 956 | * flags not specified by the Java programming language. |
| 957 | */ |
| 958 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 959 | accessFlags &= kAccJavaFlagsMask; |
| 960 | if ((accessFlags & kAccSynthetic) != 0) { |
| 961 | accessFlags |= 0xf0000000; |
| 962 | } |
| 963 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 966 | static const uint16_t kEclipseWorkaroundSlot = 1000; |
| 967 | |
| 968 | /* |
| 969 | * Eclipse appears to expect that the "this" reference is in slot zero. |
| 970 | * If it's not, the "variables" display will show two copies of "this", |
| 971 | * possibly because it gets "this" from SF.ThisObject and then displays |
| 972 | * all locals with nonzero slot numbers. |
| 973 | * |
| 974 | * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On |
| 975 | * SF.GetValues / SF.SetValues we map them back. |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 976 | * |
| 977 | * TODO: jdb uses the value to determine whether a variable is a local or an argument, |
| 978 | * by checking whether it's less than the number of arguments. To make that work, we'd |
| 979 | * have to "mangle" all the arguments to come first, not just the implicit argument 'this'. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 980 | */ |
| 981 | static uint16_t MangleSlot(uint16_t slot, const char* name) { |
| 982 | uint16_t newSlot = slot; |
| 983 | if (strcmp(name, "this") == 0) { |
| 984 | newSlot = 0; |
| 985 | } else if (slot == 0) { |
| 986 | newSlot = kEclipseWorkaroundSlot; |
| 987 | } |
| 988 | return newSlot; |
| 989 | } |
| 990 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 991 | static uint16_t DemangleSlot(uint16_t slot, Method* m) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 992 | if (slot == kEclipseWorkaroundSlot) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 993 | return 0; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 994 | } else if (slot == 0) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 995 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 996 | CHECK(code_item != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 997 | return code_item->registers_size_ - code_item->ins_size_; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 998 | } |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 999 | return slot; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1002 | JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId classId, bool with_generic, JDWP::ExpandBuf* pReply) { |
| 1003 | JDWP::JdwpError status; |
| 1004 | Class* c = DecodeClass(classId, status); |
| 1005 | if (c == NULL) { |
| 1006 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1007 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1008 | |
| 1009 | size_t instance_field_count = c->NumInstanceFields(); |
| 1010 | size_t static_field_count = c->NumStaticFields(); |
| 1011 | |
| 1012 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1013 | |
| 1014 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
| 1015 | Field* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1016 | FieldHelper fh(f); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1017 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1018 | expandBufAddUtf8String(pReply, fh.GetName()); |
| 1019 | expandBufAddUtf8String(pReply, fh.GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1020 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1021 | static const char genericSignature[1] = ""; |
| 1022 | expandBufAddUtf8String(pReply, genericSignature); |
| 1023 | } |
| 1024 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1025 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1026 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1029 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId classId, bool with_generic, JDWP::ExpandBuf* pReply) { |
| 1030 | JDWP::JdwpError status; |
| 1031 | Class* c = DecodeClass(classId, status); |
| 1032 | if (c == NULL) { |
| 1033 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1034 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1035 | |
| 1036 | size_t direct_method_count = c->NumDirectMethods(); |
| 1037 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1038 | |
| 1039 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1040 | |
| 1041 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
| 1042 | Method* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1043 | MethodHelper mh(m); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1044 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1045 | expandBufAddUtf8String(pReply, mh.GetName()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1046 | expandBufAddUtf8String(pReply, mh.GetSignature()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1047 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1048 | static const char genericSignature[1] = ""; |
| 1049 | expandBufAddUtf8String(pReply, genericSignature); |
| 1050 | } |
| 1051 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1052 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1053 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1056 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId classId, JDWP::ExpandBuf* pReply) { |
| 1057 | JDWP::JdwpError status; |
| 1058 | Class* c = DecodeClass(classId, status); |
| 1059 | if (c == NULL) { |
| 1060 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1061 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1062 | |
| 1063 | ClassHelper kh(c); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1064 | size_t interface_count = kh.NumInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1065 | expandBufAdd4BE(pReply, interface_count); |
| 1066 | for (size_t i = 0; i < interface_count; ++i) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1067 | expandBufAddRefTypeId(pReply, gRegistry->Add(kh.GetInterface(i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1068 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1069 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1072 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1073 | struct DebugCallbackContext { |
| 1074 | int numItems; |
| 1075 | JDWP::ExpandBuf* pReply; |
| 1076 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1077 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1078 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1079 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1080 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1081 | pContext->numItems++; |
| 1082 | return true; |
| 1083 | } |
| 1084 | }; |
| 1085 | |
| 1086 | Method* m = FromMethodId(methodId); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1087 | MethodHelper mh(m); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1088 | uint64_t start, end; |
| 1089 | if (m->IsNative()) { |
| 1090 | start = -1; |
| 1091 | end = -1; |
| 1092 | } else { |
| 1093 | start = 0; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1094 | // TODO: what are the units supposed to be? *2? |
| 1095 | end = mh.GetCodeItem()->insns_size_in_code_units_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | expandBufAdd8BE(pReply, start); |
| 1099 | expandBufAdd8BE(pReply, end); |
| 1100 | |
| 1101 | // Add numLines later |
| 1102 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1103 | expandBufAdd4BE(pReply, 0); |
| 1104 | |
| 1105 | DebugCallbackContext context; |
| 1106 | context.numItems = 0; |
| 1107 | context.pReply = pReply; |
| 1108 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1109 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 1110 | DebugCallbackContext::Callback, NULL, &context); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1111 | |
| 1112 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1115 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId methodId, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1116 | struct DebugCallbackContext { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1117 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1118 | size_t variable_count; |
| 1119 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1120 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1121 | static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1122 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1123 | |
Elliott Hughes | aa6e1cd | 2012-01-18 19:26:06 -0800 | [diff] [blame] | 1124 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' slot=%d", pContext->variable_count, startAddress, endAddress - startAddress, name, descriptor, signature, slot); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1125 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1126 | slot = MangleSlot(slot, name); |
| 1127 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1128 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1129 | expandBufAddUtf8String(pContext->pReply, name); |
| 1130 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1131 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1132 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1133 | } |
| 1134 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1135 | expandBufAdd4BE(pContext->pReply, slot); |
| 1136 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1137 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1138 | } |
| 1139 | }; |
| 1140 | |
| 1141 | Method* m = FromMethodId(methodId); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1142 | MethodHelper mh(m); |
| 1143 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1144 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1145 | // arg_count considers doubles and longs to take 2 units. |
| 1146 | // variable_count considers everything to take 1 unit. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1147 | std::string shorty(mh.GetShorty()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1148 | expandBufAdd4BE(pReply, m->NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1149 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1150 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1151 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1152 | expandBufAdd4BE(pReply, 0); |
| 1153 | |
| 1154 | DebugCallbackContext context; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1155 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1156 | context.variable_count = 0; |
| 1157 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1158 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1159 | mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, |
| 1160 | DebugCallbackContext::Callback, &context); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1161 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1162 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1165 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId fieldId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1166 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(fieldId)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1169 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId fieldId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1170 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(fieldId)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1174 | Object* o = gRegistry->Get<Object*>(objectId); |
| 1175 | Field* f = FromFieldId(fieldId); |
| 1176 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1177 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1178 | |
| 1179 | if (IsPrimitiveTag(tag)) { |
| 1180 | expandBufAdd1(pReply, tag); |
| 1181 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1182 | expandBufAdd1(pReply, f->Get32(o)); |
| 1183 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1184 | expandBufAdd2BE(pReply, f->Get32(o)); |
| 1185 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1186 | expandBufAdd4BE(pReply, f->Get32(o)); |
| 1187 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1188 | expandBufAdd8BE(pReply, f->Get64(o)); |
| 1189 | } else { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1190 | LOG(FATAL) << "Unknown tag: " << tag; |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1191 | } |
| 1192 | } else { |
| 1193 | Object* value = f->GetObject(o); |
| 1194 | expandBufAdd1(pReply, TagFromObject(value)); |
| 1195 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1196 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1199 | JDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1200 | Object* o = gRegistry->Get<Object*>(objectId); |
| 1201 | Field* f = FromFieldId(fieldId); |
| 1202 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1203 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1204 | |
| 1205 | if (IsPrimitiveTag(tag)) { |
| 1206 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1207 | f->Set64(o, value); |
| 1208 | } else { |
| 1209 | f->Set32(o, value); |
| 1210 | } |
| 1211 | } else { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1212 | Object* v = gRegistry->Get<Object*>(value); |
| 1213 | Class* field_type = FieldHelper(f).GetType(); |
| 1214 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1215 | return JDWP::ERR_INVALID_OBJECT; |
| 1216 | } |
| 1217 | f->SetObject(o, v); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1218 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1219 | |
| 1220 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1223 | void Dbg::GetStaticFieldValue(JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 1224 | GetFieldValue(0, fieldId, pReply); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1227 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId fieldId, uint64_t value, int width) { |
| 1228 | return SetFieldValue(0, fieldId, value, width); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1231 | std::string Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 1232 | String* s = gRegistry->Get<String*>(strId); |
| 1233 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1236 | bool Dbg::GetThreadName(JDWP::ObjectId threadId, std::string& name) { |
| 1237 | ScopedThreadListLock thread_list_lock; |
| 1238 | Thread* thread = DecodeThread(threadId); |
| 1239 | if (thread == NULL) { |
| 1240 | return false; |
| 1241 | } |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 1242 | StringAppendF(&name, "<%d> %s", thread->GetThinLockId(), thread->GetThreadName()->ToModifiedUtf8().c_str()); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1243 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1246 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId threadId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1247 | Object* thread = gRegistry->Get<Object*>(threadId); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1248 | if (thread == kInvalidObject) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1249 | return JDWP::ERR_INVALID_OBJECT; |
| 1250 | } |
| 1251 | |
| 1252 | // Okay, so it's an object, but is it actually a thread? |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame^] | 1253 | if (DecodeThread(threadId) == NULL) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1254 | return JDWP::ERR_INVALID_THREAD; |
| 1255 | } |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1256 | |
| 1257 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;"); |
| 1258 | CHECK(c != NULL); |
| 1259 | Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;"); |
| 1260 | CHECK(f != NULL); |
| 1261 | Object* group = f->GetObject(thread); |
| 1262 | CHECK(group != NULL); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1263 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1264 | |
| 1265 | expandBufAddObjectId(pReply, thread_group_id); |
| 1266 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1269 | std::string Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 1270 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1271 | CHECK(thread_group != NULL); |
| 1272 | |
| 1273 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1274 | CHECK(c != NULL); |
| 1275 | Field* f = c->FindInstanceField("name", "Ljava/lang/String;"); |
| 1276 | CHECK(f != NULL); |
| 1277 | String* s = reinterpret_cast<String*>(f->GetObject(thread_group)); |
| 1278 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1282 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1283 | CHECK(thread_group != NULL); |
| 1284 | |
| 1285 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1286 | CHECK(c != NULL); |
| 1287 | Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;"); |
| 1288 | CHECK(f != NULL); |
| 1289 | Object* parent = f->GetObject(thread_group); |
| 1290 | return gRegistry->Add(parent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1293 | static Object* GetStaticThreadGroup(const char* field_name) { |
| 1294 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1295 | CHECK(c != NULL); |
| 1296 | Field* f = c->FindStaticField(field_name, "Ljava/lang/ThreadGroup;"); |
| 1297 | CHECK(f != NULL); |
| 1298 | Object* group = f->GetObject(NULL); |
| 1299 | CHECK(group != NULL); |
| 1300 | return group; |
| 1301 | } |
| 1302 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1303 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1304 | return gRegistry->Add(GetStaticThreadGroup("mSystem")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1308 | return gRegistry->Add(GetStaticThreadGroup("mMain")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1311 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1312 | ScopedThreadListLock thread_list_lock; |
| 1313 | |
| 1314 | Thread* thread = DecodeThread(threadId); |
| 1315 | if (thread == NULL) { |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
| 1319 | switch (thread->GetState()) { |
| 1320 | case Thread::kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1321 | case Thread::kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1322 | case Thread::kTimedWaiting: *pThreadStatus = JDWP::TS_SLEEPING; break; |
| 1323 | case Thread::kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break; |
| 1324 | case Thread::kWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1325 | case Thread::kInitializing: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1326 | case Thread::kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1327 | case Thread::kNative: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1328 | case Thread::kVmWait: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1329 | case Thread::kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1330 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1331 | LOG(FATAL) << "Unknown thread state " << thread->GetState(); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1334 | *pSuspendStatus = (thread->IsSuspended() ? JDWP::SUSPEND_STATUS_SUSPENDED : JDWP::SUSPEND_STATUS_NOT_SUSPENDED); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1335 | |
| 1336 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1339 | JDWP::JdwpError Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId, JDWP::ExpandBuf* pReply) { |
| 1340 | Thread* thread = DecodeThread(threadId); |
| 1341 | if (thread == NULL) { |
| 1342 | return JDWP::ERR_INVALID_THREAD; |
| 1343 | } |
| 1344 | expandBufAdd4BE(pReply, thread->GetSuspendCount()); |
| 1345 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1349 | return DecodeThread(threadId) != NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1353 | return DecodeThread(threadId)->IsSuspended(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1356 | void Dbg::GetThreadGroupThreadsImpl(Object* thread_group, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 1357 | struct ThreadListVisitor { |
| 1358 | static void Visit(Thread* t, void* arg) { |
| 1359 | reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t); |
| 1360 | } |
| 1361 | |
| 1362 | void Visit(Thread* t) { |
| 1363 | if (t == Dbg::GetDebugThread()) { |
| 1364 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 1365 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 1366 | return; |
| 1367 | } |
| 1368 | if (thread_group == NULL || t->GetThreadGroup() == thread_group) { |
| 1369 | threads.push_back(gRegistry->Add(t->GetPeer())); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | Object* thread_group; |
| 1374 | std::vector<JDWP::ObjectId> threads; |
| 1375 | }; |
| 1376 | |
| 1377 | ThreadListVisitor tlv; |
| 1378 | tlv.thread_group = thread_group; |
| 1379 | |
| 1380 | { |
| 1381 | ScopedThreadListLock thread_list_lock; |
| 1382 | Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv); |
| 1383 | } |
| 1384 | |
| 1385 | *pThreadCount = tlv.threads.size(); |
| 1386 | if (*pThreadCount == 0) { |
| 1387 | *ppThreadIds = NULL; |
| 1388 | } else { |
| 1389 | *ppThreadIds = new JDWP::ObjectId[*pThreadCount]; |
| 1390 | for (size_t i = 0; i < *pThreadCount; ++i) { |
| 1391 | (*ppThreadIds)[i] = tlv.threads[i]; |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1396 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1397 | GetThreadGroupThreadsImpl(gRegistry->Get<Object*>(threadGroupId), ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1401 | GetThreadGroupThreadsImpl(NULL, ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1404 | static int GetStackDepth(Thread* thread) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1405 | struct CountStackDepthVisitor : public Thread::StackVisitor { |
| 1406 | CountStackDepthVisitor() : depth(0) {} |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1407 | virtual void VisitFrame(const Frame& f, uintptr_t) { |
| 1408 | // TODO: we'll need to skip callee-save frames too. |
| 1409 | if (f.HasMethod()) { |
| 1410 | ++depth; |
| 1411 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1412 | } |
| 1413 | size_t depth; |
| 1414 | }; |
| 1415 | CountStackDepthVisitor visitor; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1416 | thread->WalkStack(&visitor); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1417 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1420 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
| 1421 | ScopedThreadListLock thread_list_lock; |
| 1422 | return GetStackDepth(DecodeThread(threadId)); |
| 1423 | } |
| 1424 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1425 | bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
| 1426 | ScopedThreadListLock thread_list_lock; |
| 1427 | struct GetFrameVisitor : public Thread::StackVisitor { |
| 1428 | GetFrameVisitor(int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1429 | : found(false), depth(0), desired_frame_number(desired_frame_number), pFrameId(pFrameId), pLoc(pLoc) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1430 | } |
| 1431 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1432 | // TODO: we'll need to skip callee-save frames too. |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1433 | if (!f.HasMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1434 | return; // The debugger can't do anything useful with a frame that has no Method*. |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | if (depth == desired_frame_number) { |
| 1438 | *pFrameId = reinterpret_cast<JDWP::FrameId>(f.GetSP()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1439 | SetLocation(*pLoc, f.GetMethod(), pc); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1440 | found = true; |
| 1441 | } |
| 1442 | ++depth; |
| 1443 | } |
| 1444 | bool found; |
| 1445 | int depth; |
| 1446 | int desired_frame_number; |
| 1447 | JDWP::FrameId* pFrameId; |
| 1448 | JDWP::JdwpLocation* pLoc; |
| 1449 | }; |
| 1450 | GetFrameVisitor visitor(desired_frame_number, pFrameId, pLoc); |
| 1451 | visitor.desired_frame_number = desired_frame_number; |
| 1452 | DecodeThread(threadId)->WalkStack(&visitor); |
| 1453 | return visitor.found; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1457 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1460 | void Dbg::SuspendVM() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1461 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); // TODO: do we really want to change back? should the JDWP thread be Runnable usually? |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1462 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1466 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1470 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1471 | ScopedThreadListLock thread_list_lock; |
| 1472 | Thread* thread = Thread::FromManagedThread(peer); |
| 1473 | if (thread == NULL) { |
| 1474 | LOG(WARNING) << "No such thread for suspend: " << peer; |
| 1475 | return; |
| 1476 | } |
| 1477 | Runtime::Current()->GetThreadList()->Suspend(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1481 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1482 | ScopedThreadListLock thread_list_lock; |
| 1483 | Thread* thread = Thread::FromManagedThread(peer); |
| 1484 | if (thread == NULL) { |
| 1485 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 1486 | return; |
| 1487 | } |
| 1488 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1492 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1495 | static Object* GetThis(Frame& f) { |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1496 | Method* m = f.GetMethod(); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1497 | Object* o = NULL; |
| 1498 | if (!m->IsNative() && !m->IsStatic()) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1499 | uint16_t reg = DemangleSlot(0, m); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1500 | o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
| 1501 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1502 | return o; |
| 1503 | } |
| 1504 | |
| 1505 | void Dbg::GetThisObject(JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
| 1506 | Method** sp = reinterpret_cast<Method**>(frameId); |
| 1507 | Frame f(sp); |
| 1508 | Object* o = GetThis(f); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1509 | *pThisId = gRegistry->Add(o); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1512 | void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1513 | Method** sp = reinterpret_cast<Method**>(frameId); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1514 | Frame f(sp); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1515 | Method* m = f.GetMethod(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1516 | uint16_t reg = DemangleSlot(slot, m); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1517 | |
| 1518 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1519 | uint32_t vmap_offset; |
| 1520 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1521 | UNIMPLEMENTED(FATAL) << "Don't know how to pull locals from callee save frames: " << vmap_offset; |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1522 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1523 | |
| 1524 | switch (tag) { |
| 1525 | case JDWP::JT_BOOLEAN: |
| 1526 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1527 | CHECK_EQ(width, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1528 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1529 | VLOG(jdwp) << "get boolean local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1530 | JDWP::Set1(buf+1, intVal != 0); |
| 1531 | } |
| 1532 | break; |
| 1533 | case JDWP::JT_BYTE: |
| 1534 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1535 | CHECK_EQ(width, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1536 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1537 | VLOG(jdwp) << "get byte local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1538 | JDWP::Set1(buf+1, intVal); |
| 1539 | } |
| 1540 | break; |
| 1541 | case JDWP::JT_SHORT: |
| 1542 | case JDWP::JT_CHAR: |
| 1543 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1544 | CHECK_EQ(width, 2U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1545 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1546 | VLOG(jdwp) << "get short/char local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1547 | JDWP::Set2BE(buf+1, intVal); |
| 1548 | } |
| 1549 | break; |
| 1550 | case JDWP::JT_INT: |
| 1551 | case JDWP::JT_FLOAT: |
| 1552 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1553 | CHECK_EQ(width, 4U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1554 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1555 | VLOG(jdwp) << "get int/float local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1556 | JDWP::Set4BE(buf+1, intVal); |
| 1557 | } |
| 1558 | break; |
| 1559 | case JDWP::JT_ARRAY: |
| 1560 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1561 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1562 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1563 | VLOG(jdwp) << "get array local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1564 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1565 | LOG(FATAL) << "Register " << reg << " expected to hold array: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1566 | } |
| 1567 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1568 | } |
| 1569 | break; |
| 1570 | case JDWP::JT_OBJECT: |
| 1571 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1572 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1573 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1574 | VLOG(jdwp) << "get object local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1575 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1576 | LOG(FATAL) << "Register " << reg << " expected to hold object: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1577 | } |
| 1578 | tag = TagFromObject(o); |
| 1579 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1580 | } |
| 1581 | break; |
| 1582 | case JDWP::JT_DOUBLE: |
| 1583 | case JDWP::JT_LONG: |
| 1584 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1585 | CHECK_EQ(width, 8U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1586 | uint32_t lo = f.GetVReg(m, reg); |
| 1587 | uint64_t hi = f.GetVReg(m, reg + 1); |
| 1588 | uint64_t longVal = (hi << 32) | lo; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1589 | VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1590 | JDWP::Set8BE(buf+1, longVal); |
| 1591 | } |
| 1592 | break; |
| 1593 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1594 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1595 | break; |
| 1596 | } |
| 1597 | |
| 1598 | // Prepend tag, which may have been updated. |
| 1599 | JDWP::Set1(buf, tag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1602 | void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint64_t value, size_t width) { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1603 | Method** sp = reinterpret_cast<Method**>(frameId); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1604 | Frame f(sp); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1605 | Method* m = f.GetMethod(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1606 | uint16_t reg = DemangleSlot(slot, m); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1607 | |
| 1608 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1609 | uint32_t vmap_offset; |
| 1610 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1611 | UNIMPLEMENTED(FATAL) << "Don't know how to pull locals from callee save frames: " << vmap_offset; |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | switch (tag) { |
| 1615 | case JDWP::JT_BOOLEAN: |
| 1616 | case JDWP::JT_BYTE: |
| 1617 | CHECK_EQ(width, 1U); |
| 1618 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1619 | break; |
| 1620 | case JDWP::JT_SHORT: |
| 1621 | case JDWP::JT_CHAR: |
| 1622 | CHECK_EQ(width, 2U); |
| 1623 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1624 | break; |
| 1625 | case JDWP::JT_INT: |
| 1626 | case JDWP::JT_FLOAT: |
| 1627 | CHECK_EQ(width, 4U); |
| 1628 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1629 | break; |
| 1630 | case JDWP::JT_ARRAY: |
| 1631 | case JDWP::JT_OBJECT: |
| 1632 | case JDWP::JT_STRING: |
| 1633 | { |
| 1634 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 1635 | Object* o = gRegistry->Get<Object*>(static_cast<JDWP::ObjectId>(value)); |
| 1636 | f.SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o))); |
| 1637 | } |
| 1638 | break; |
| 1639 | case JDWP::JT_DOUBLE: |
| 1640 | case JDWP::JT_LONG: |
| 1641 | CHECK_EQ(width, 8U); |
| 1642 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1643 | f.SetVReg(m, reg + 1, static_cast<uint32_t>(value >> 32)); |
| 1644 | break; |
| 1645 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1646 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1647 | break; |
| 1648 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1651 | void Dbg::PostLocationEvent(const Method* m, int dex_pc, Object* this_object, int event_flags) { |
| 1652 | Class* c = m->GetDeclaringClass(); |
| 1653 | |
| 1654 | JDWP::JdwpLocation location; |
| 1655 | location.typeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1656 | location.classId = gRegistry->Add(c); |
| 1657 | location.methodId = ToMethodId(m); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1658 | location.idx = m->IsNative() ? -1 : dex_pc / 2; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1659 | |
| 1660 | // Note we use "NoReg" so we don't keep track of references that are |
| 1661 | // never actually sent to the debugger. 'this_id' is only used to |
| 1662 | // compare against registered events... |
| 1663 | JDWP::ObjectId this_id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(this_object)); |
| 1664 | if (gJdwpState->PostLocationEvent(&location, this_id, event_flags)) { |
| 1665 | // ...unless there's a registered event, in which case we |
| 1666 | // need to really track the class and 'this'. |
| 1667 | gRegistry->Add(c); |
| 1668 | gRegistry->Add(this_object); |
| 1669 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1672 | void Dbg::PostException(Method** sp, Method* throwMethod, uintptr_t throwNativePc, Method* catchMethod, uintptr_t catchNativePc, Object* exception) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 1673 | if (!gDebuggerActive) { |
| 1674 | return; |
| 1675 | } |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1676 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1677 | JDWP::JdwpLocation throw_location; |
| 1678 | SetLocation(throw_location, throwMethod, throwNativePc); |
| 1679 | JDWP::JdwpLocation catch_location; |
| 1680 | SetLocation(catch_location, catchMethod, catchNativePc); |
| 1681 | |
| 1682 | // We need 'this' for InstanceOnly filters. |
| 1683 | JDWP::ObjectId this_id; |
| 1684 | GetThisObject(reinterpret_cast<JDWP::FrameId>(sp), &this_id); |
| 1685 | |
| 1686 | /* |
| 1687 | * Hand the event to the JDWP exception handler. Note we're using the |
| 1688 | * "NoReg" objectID on the exception, which is not strictly correct -- |
| 1689 | * the exception object WILL be passed up to the debugger if the |
| 1690 | * debugger is interested in the event. We do this because the current |
| 1691 | * implementation of the debugger object registry never throws anything |
| 1692 | * away, and some people were experiencing a fatal build up of exception |
| 1693 | * objects when dealing with certain libraries. |
| 1694 | */ |
| 1695 | JDWP::ObjectId exception_id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(exception)); |
| 1696 | JDWP::RefTypeId exception_class_id = gRegistry->Add(exception->GetClass()); |
| 1697 | |
| 1698 | gJdwpState->PostException(&throw_location, exception_id, exception_class_id, &catch_location, this_id); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1701 | void Dbg::PostClassPrepare(Class* c) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1702 | if (!gDebuggerActive) { |
| 1703 | return; |
| 1704 | } |
| 1705 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1706 | // OLD-TODO - we currently always send both "verified" and "prepared" since |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1707 | // debuggers seem to like that. There might be some advantage to honesty, |
| 1708 | // since the class may not yet be verified. |
| 1709 | int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1710 | JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1711 | gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1714 | void Dbg::UpdateDebugger(int32_t dex_pc, Thread* self, Method** sp) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1715 | if (!gDebuggerActive || dex_pc == -2 /* fake method exit */) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1716 | return; |
| 1717 | } |
| 1718 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1719 | Frame f(sp); |
| 1720 | f.Next(); // Skip callee save frame. |
| 1721 | Method* m = f.GetMethod(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1722 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1723 | if (dex_pc == -1) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1724 | // We use a pc of -1 to represent method entry, since we might branch back to pc 0 later. |
| 1725 | // This means that for this special notification, there can't be anything else interesting |
| 1726 | // going on, so we're done already. |
| 1727 | Dbg::PostLocationEvent(m, 0, GetThis(f), kMethodEntry); |
| 1728 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1729 | } |
| 1730 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1731 | int event_flags = 0; |
| 1732 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1733 | if (IsBreakpoint(m, dex_pc)) { |
| 1734 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1735 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1736 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1737 | // If the debugger is single-stepping one of our threads, check to |
| 1738 | // see if we're that thread and we've reached a step point. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1739 | if (gSingleStepControl.is_active && gSingleStepControl.thread == self) { |
| 1740 | CHECK(!m->IsNative()); |
| 1741 | if (gSingleStepControl.step_depth == JDWP::SD_INTO) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1742 | // Step into method calls. We break when the line number |
| 1743 | // or method pointer changes. If we're in SS_MIN mode, we |
| 1744 | // always stop. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1745 | if (gSingleStepControl.method != m) { |
| 1746 | event_flags |= kSingleStep; |
| 1747 | VLOG(jdwp) << "SS new method"; |
| 1748 | } else if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
| 1749 | event_flags |= kSingleStep; |
| 1750 | VLOG(jdwp) << "SS new instruction"; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1751 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 1752 | event_flags |= kSingleStep; |
| 1753 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1754 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1755 | } else if (gSingleStepControl.step_depth == JDWP::SD_OVER) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1756 | // Step over method calls. We break when the line number is |
| 1757 | // different and the frame depth is <= the original frame |
| 1758 | // depth. (We can't just compare on the method, because we |
| 1759 | // might get unrolled past it by an exception, and it's tricky |
| 1760 | // to identify recursion.) |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1761 | |
| 1762 | // TODO: can we just use the value of 'sp'? |
| 1763 | int stack_depth = GetStackDepth(self); |
| 1764 | |
| 1765 | if (stack_depth < gSingleStepControl.stack_depth) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1766 | // popped up one or more frames, always trigger |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1767 | event_flags |= kSingleStep; |
| 1768 | VLOG(jdwp) << "SS method pop"; |
| 1769 | } else if (stack_depth == gSingleStepControl.stack_depth) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1770 | // same depth, see if we moved |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1771 | if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
| 1772 | event_flags |= kSingleStep; |
| 1773 | VLOG(jdwp) << "SS new instruction"; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1774 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 1775 | event_flags |= kSingleStep; |
| 1776 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | } else { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1780 | CHECK_EQ(gSingleStepControl.step_depth, JDWP::SD_OUT); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1781 | // Return from the current method. We break when the frame |
| 1782 | // depth pops up. |
| 1783 | |
| 1784 | // This differs from the "method exit" break in that it stops |
| 1785 | // with the PC at the next instruction in the returned-to |
| 1786 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1787 | |
| 1788 | // TODO: can we just use the value of 'sp'? |
| 1789 | int stack_depth = GetStackDepth(self); |
| 1790 | if (stack_depth < gSingleStepControl.stack_depth) { |
| 1791 | event_flags |= kSingleStep; |
| 1792 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1793 | } |
| 1794 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1795 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1796 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1797 | // Check to see if this is a "return" instruction. JDWP says we should |
| 1798 | // send the event *after* the code has been executed, but it also says |
| 1799 | // the location we provide is the last instruction. Since the "return" |
| 1800 | // instruction has no interesting side effects, we should be safe. |
| 1801 | // (We can't just move this down to the returnFromMethod label because |
| 1802 | // we potentially need to combine it with other events.) |
| 1803 | // We're also not supposed to generate a method exit event if the method |
| 1804 | // terminates "with a thrown exception". |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1805 | if (dex_pc >= 0) { |
| 1806 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 1807 | CHECK(code_item != NULL); |
| 1808 | CHECK_LT(dex_pc, static_cast<int32_t>(code_item->insns_size_in_code_units_)); |
| 1809 | if (Instruction::At(&code_item->insns_[dex_pc])->IsReturn()) { |
| 1810 | event_flags |= kMethodExit; |
| 1811 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1812 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1813 | |
| 1814 | // If there's something interesting going on, see if it matches one |
| 1815 | // of the debugger filters. |
| 1816 | if (event_flags != 0) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1817 | Dbg::PostLocationEvent(m, dex_pc, GetThis(f), event_flags); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1821 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location) { |
| 1822 | MutexLock mu(gBreakpointsLock); |
| 1823 | Method* m = FromMethodId(location->methodId); |
| 1824 | gBreakpoints.push_back(Breakpoint(m, location->idx)); |
| 1825 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1828 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) { |
| 1829 | MutexLock mu(gBreakpointsLock); |
| 1830 | Method* m = FromMethodId(location->methodId); |
| 1831 | for (size_t i = 0; i < gBreakpoints.size(); ++i) { |
| 1832 | if (gBreakpoints[i].method == m && gBreakpoints[i].pc == location->idx) { |
| 1833 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
| 1834 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 1835 | return; |
| 1836 | } |
| 1837 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1840 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize step_size, JDWP::JdwpStepDepth step_depth) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1841 | Thread* thread = DecodeThread(threadId); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1842 | if (thread == NULL) { |
| 1843 | return JDWP::ERR_INVALID_THREAD; |
| 1844 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1845 | |
| 1846 | // TODO: there's no theoretical reason why we couldn't support single-stepping |
| 1847 | // of multiple threads at once, but we never did so historically. |
| 1848 | if (gSingleStepControl.thread != NULL && thread != gSingleStepControl.thread) { |
| 1849 | LOG(WARNING) << "single-step already active for " << *gSingleStepControl.thread |
| 1850 | << "; switching to " << *thread; |
| 1851 | } |
| 1852 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1853 | // |
| 1854 | // Work out what Method* we're in, the current line number, and how deep the stack currently |
| 1855 | // is for step-out. |
| 1856 | // |
| 1857 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1858 | struct SingleStepStackVisitor : public Thread::StackVisitor { |
| 1859 | SingleStepStackVisitor() { |
| 1860 | gSingleStepControl.method = NULL; |
| 1861 | gSingleStepControl.stack_depth = 0; |
| 1862 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1863 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1864 | // TODO: we'll need to skip callee-save frames too. |
| 1865 | if (f.HasMethod()) { |
| 1866 | ++gSingleStepControl.stack_depth; |
| 1867 | if (gSingleStepControl.method == NULL) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1868 | const Method* m = f.GetMethod(); |
| 1869 | const DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
| 1870 | gSingleStepControl.method = m; |
| 1871 | gSingleStepControl.line_number = -1; |
| 1872 | if (dex_cache != NULL) { |
| 1873 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
| 1874 | gSingleStepControl.line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); |
| 1875 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | }; |
| 1880 | SingleStepStackVisitor visitor; |
| 1881 | thread->WalkStack(&visitor); |
| 1882 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1883 | // |
| 1884 | // Find the dex_pc values that correspond to the current line, for line-based single-stepping. |
| 1885 | // |
| 1886 | |
| 1887 | struct DebugCallbackContext { |
| 1888 | DebugCallbackContext() { |
| 1889 | last_pc_valid = false; |
| 1890 | last_pc = 0; |
| 1891 | gSingleStepControl.dex_pcs.clear(); |
| 1892 | } |
| 1893 | |
| 1894 | static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) { |
| 1895 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
| 1896 | if (static_cast<int32_t>(line_number) == gSingleStepControl.line_number) { |
| 1897 | if (!context->last_pc_valid) { |
| 1898 | // Everything from this address until the next line change is ours. |
| 1899 | context->last_pc = address; |
| 1900 | context->last_pc_valid = true; |
| 1901 | } |
| 1902 | // Otherwise, if we're already in a valid range for this line, |
| 1903 | // just keep going (shouldn't really happen)... |
| 1904 | } else if (context->last_pc_valid) { // and the line number is new |
| 1905 | // Add everything from the last entry up until here to the set |
| 1906 | for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) { |
| 1907 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 1908 | } |
| 1909 | context->last_pc_valid = false; |
| 1910 | } |
| 1911 | return false; // There may be multiple entries for any given line. |
| 1912 | } |
| 1913 | |
| 1914 | ~DebugCallbackContext() { |
| 1915 | // If the line number was the last in the position table... |
| 1916 | if (last_pc_valid) { |
| 1917 | size_t end = MethodHelper(gSingleStepControl.method).GetCodeItem()->insns_size_in_code_units_; |
| 1918 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
| 1919 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | bool last_pc_valid; |
| 1925 | uint32_t last_pc; |
| 1926 | }; |
| 1927 | DebugCallbackContext context; |
| 1928 | const Method* m = gSingleStepControl.method; |
| 1929 | MethodHelper mh(m); |
| 1930 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 1931 | DebugCallbackContext::Callback, NULL, &context); |
| 1932 | |
| 1933 | // |
| 1934 | // Everything else... |
| 1935 | // |
| 1936 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1937 | gSingleStepControl.thread = thread; |
| 1938 | gSingleStepControl.step_size = step_size; |
| 1939 | gSingleStepControl.step_depth = step_depth; |
| 1940 | gSingleStepControl.is_active = true; |
| 1941 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1942 | if (VLOG_IS_ON(jdwp)) { |
| 1943 | VLOG(jdwp) << "Single-step thread: " << *gSingleStepControl.thread; |
| 1944 | VLOG(jdwp) << "Single-step step size: " << gSingleStepControl.step_size; |
| 1945 | VLOG(jdwp) << "Single-step step depth: " << gSingleStepControl.step_depth; |
| 1946 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(gSingleStepControl.method); |
| 1947 | VLOG(jdwp) << "Single-step current line: " << gSingleStepControl.line_number; |
| 1948 | VLOG(jdwp) << "Single-step current stack depth: " << gSingleStepControl.stack_depth; |
| 1949 | VLOG(jdwp) << "Single-step dex_pc values:"; |
| 1950 | for (std::set<uint32_t>::iterator it = gSingleStepControl.dex_pcs.begin() ; it != gSingleStepControl.dex_pcs.end(); ++it) { |
| 1951 | VLOG(jdwp) << " " << *it; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | void Dbg::UnconfigureStep(JDWP::ObjectId threadId) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1959 | gSingleStepControl.is_active = false; |
| 1960 | gSingleStepControl.thread = NULL; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1961 | gSingleStepControl.dex_pcs.clear(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1964 | JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId threadId, JDWP::ObjectId objectId, JDWP::RefTypeId classId, JDWP::MethodId methodId, uint32_t numArgs, uint64_t* argArray, uint32_t options, JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, JDWP::ObjectId* pExceptionId) { |
| 1965 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1966 | |
| 1967 | Thread* targetThread = NULL; |
| 1968 | DebugInvokeReq* req = NULL; |
| 1969 | { |
| 1970 | ScopedThreadListLock thread_list_lock; |
| 1971 | targetThread = DecodeThread(threadId); |
| 1972 | if (targetThread == NULL) { |
| 1973 | LOG(ERROR) << "InvokeMethod request for non-existent thread " << threadId; |
| 1974 | return JDWP::ERR_INVALID_THREAD; |
| 1975 | } |
| 1976 | req = targetThread->GetInvokeReq(); |
| 1977 | if (!req->ready) { |
| 1978 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 1979 | return JDWP::ERR_INVALID_THREAD; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | * We currently have a bug where we don't successfully resume the |
| 1984 | * target thread if the suspend count is too deep. We're expected to |
| 1985 | * require one "resume" for each "suspend", but when asked to execute |
| 1986 | * a method we have to resume fully and then re-suspend it back to the |
| 1987 | * same level. (The easiest way to cause this is to type "suspend" |
| 1988 | * multiple times in jdb.) |
| 1989 | * |
| 1990 | * It's unclear what this means when the event specifies "resume all" |
| 1991 | * and some threads are suspended more deeply than others. This is |
| 1992 | * a rare problem, so for now we just prevent it from hanging forever |
| 1993 | * by rejecting the method invocation request. Without this, we will |
| 1994 | * be stuck waiting on a suspended thread. |
| 1995 | */ |
| 1996 | int suspend_count = targetThread->GetSuspendCount(); |
| 1997 | if (suspend_count > 1) { |
| 1998 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
| 1999 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
| 2000 | } |
| 2001 | |
| 2002 | /* |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2003 | * OLD-TODO: ought to screen the various IDs, and verify that the argument |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2004 | * list is valid. |
| 2005 | */ |
| 2006 | req->receiver_ = gRegistry->Get<Object*>(objectId); |
| 2007 | req->thread_ = gRegistry->Get<Object*>(threadId); |
| 2008 | req->class_ = gRegistry->Get<Class*>(classId); |
| 2009 | req->method_ = FromMethodId(methodId); |
| 2010 | req->num_args_ = numArgs; |
| 2011 | req->arg_array_ = argArray; |
| 2012 | req->options_ = options; |
| 2013 | req->invoke_needed_ = true; |
| 2014 | } |
| 2015 | |
| 2016 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
| 2017 | // away we're sitting high and dry -- but we must release this before the ResumeAllThreads |
| 2018 | // call, and it's unwise to hold it during WaitForSuspend. |
| 2019 | |
| 2020 | { |
| 2021 | /* |
| 2022 | * We change our (JDWP thread) status, which should be THREAD_RUNNING, |
| 2023 | * so the VM can suspend for a GC if the invoke request causes us to |
| 2024 | * run out of memory. It's also a good idea to change it before locking |
| 2025 | * the invokeReq mutex, although that should never be held for long. |
| 2026 | */ |
| 2027 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 2028 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2029 | VLOG(jdwp) << " Transferring control to event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2030 | { |
| 2031 | MutexLock mu(req->lock_); |
| 2032 | |
| 2033 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2034 | VLOG(jdwp) << " Resuming all threads"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2035 | thread_list->ResumeAll(true); |
| 2036 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2037 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2038 | thread_list->Resume(targetThread, true); |
| 2039 | } |
| 2040 | |
| 2041 | // Wait for the request to finish executing. |
| 2042 | while (req->invoke_needed_) { |
| 2043 | req->cond_.Wait(req->lock_); |
| 2044 | } |
| 2045 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2046 | VLOG(jdwp) << " Control has returned from event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2047 | |
| 2048 | /* wait for thread to re-suspend itself */ |
| 2049 | targetThread->WaitUntilSuspended(); |
| 2050 | //dvmWaitForSuspend(targetThread); |
| 2051 | } |
| 2052 | |
| 2053 | /* |
| 2054 | * Suspend the threads. We waited for the target thread to suspend |
| 2055 | * itself, so all we need to do is suspend the others. |
| 2056 | * |
| 2057 | * The suspendAllThreads() call will double-suspend the event thread, |
| 2058 | * so we want to resume the target thread once to keep the books straight. |
| 2059 | */ |
| 2060 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2061 | VLOG(jdwp) << " Suspending all threads"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2062 | thread_list->SuspendAll(true); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2063 | VLOG(jdwp) << " Resuming event thread to balance the count"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2064 | thread_list->Resume(targetThread, true); |
| 2065 | } |
| 2066 | |
| 2067 | // Copy the result. |
| 2068 | *pResultTag = req->result_tag; |
| 2069 | if (IsPrimitiveTag(req->result_tag)) { |
| 2070 | *pResultValue = req->result_value.j; |
| 2071 | } else { |
| 2072 | *pResultValue = gRegistry->Add(req->result_value.l); |
| 2073 | } |
| 2074 | *pExceptionId = req->exception; |
| 2075 | return req->error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2079 | Thread* self = Thread::Current(); |
| 2080 | |
| 2081 | // We can be called while an exception is pending in the VM. We need |
| 2082 | // to preserve that across the method invocation. |
| 2083 | SirtRef<Throwable> old_exception(self->GetException()); |
| 2084 | self->ClearException(); |
| 2085 | |
| 2086 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 2087 | |
| 2088 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
| 2089 | Method* m = pReq->method_; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2090 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2091 | if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) { |
| 2092 | m = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2093 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2094 | } |
| 2095 | CHECK(m != NULL); |
| 2096 | |
| 2097 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 2098 | |
| 2099 | pReq->result_value = InvokeWithJValues(self, pReq->receiver_, m, reinterpret_cast<JValue*>(pReq->arg_array_)); |
| 2100 | |
| 2101 | pReq->exception = gRegistry->Add(self->GetException()); |
| 2102 | pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty()); |
| 2103 | if (pReq->exception != 0) { |
| 2104 | Object* exc = self->GetException(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2105 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exc << " " << PrettyTypeOf(exc); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2106 | self->ClearException(); |
| 2107 | pReq->result_value.j = 0; |
| 2108 | } else if (pReq->result_tag == JDWP::JT_OBJECT) { |
| 2109 | /* if no exception thrown, examine object result more closely */ |
| 2110 | JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.l); |
| 2111 | if (new_tag != pReq->result_tag) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2112 | VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2113 | pReq->result_tag = new_tag; |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Register the object. We don't actually need an ObjectId yet, |
| 2118 | * but we do need to be sure that the GC won't move or discard the |
| 2119 | * object when we switch out of RUNNING. The ObjectId conversion |
| 2120 | * will add the object to the "do not touch" list. |
| 2121 | * |
| 2122 | * We can't use the "tracked allocation" mechanism here because |
| 2123 | * the object is going to be handed off to a different thread. |
| 2124 | */ |
| 2125 | gRegistry->Add(pReq->result_value.l); |
| 2126 | } |
| 2127 | |
| 2128 | if (old_exception.get() != NULL) { |
| 2129 | self->SetException(old_exception.get()); |
| 2130 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2131 | } |
| 2132 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2133 | /* |
| 2134 | * Register an object ID that might not have been registered previously. |
| 2135 | * |
| 2136 | * Normally this wouldn't happen -- the conversion to an ObjectId would |
| 2137 | * have added the object to the registry -- but in some cases (e.g. |
| 2138 | * throwing exceptions) we really want to do the registration late. |
| 2139 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2140 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2141 | gRegistry->Add(reinterpret_cast<Object*>(id)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2144 | /* |
| 2145 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 2146 | * need to process each, accumulate the replies, and ship the whole thing |
| 2147 | * back. |
| 2148 | * |
| 2149 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 2150 | * and includes the chunk type/length, followed by the data. |
| 2151 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2152 | * OLD-TODO: we currently assume that the request and reply include a single |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2153 | * chunk. If this becomes inconvenient we will need to adapt. |
| 2154 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2155 | bool Dbg::DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2156 | CHECK_GE(dataLen, 0); |
| 2157 | |
| 2158 | Thread* self = Thread::Current(); |
| 2159 | JNIEnv* env = self->GetJniEnv(); |
| 2160 | |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 2161 | static jclass Chunk_class = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk"); |
| 2162 | static jclass DdmServer_class = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 2163 | static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2164 | static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B"); |
| 2165 | static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I"); |
| 2166 | static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I"); |
| 2167 | static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I"); |
| 2168 | |
| 2169 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2170 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 2171 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2172 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 2173 | env->ExceptionClear(); |
| 2174 | return false; |
| 2175 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2176 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2177 | |
| 2178 | const int kChunkHdrLen = 8; |
| 2179 | |
| 2180 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2181 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2182 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 2183 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2184 | jint offset = kChunkHdrLen; |
| 2185 | if (offset + length > dataLen) { |
| 2186 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 2187 | return false; |
| 2188 | } |
| 2189 | |
| 2190 | // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)". |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2191 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray.get(), offset, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2192 | if (env->ExceptionCheck()) { |
| 2193 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 2194 | env->ExceptionDescribe(); |
| 2195 | env->ExceptionClear(); |
| 2196 | return false; |
| 2197 | } |
| 2198 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2199 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2200 | return false; |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Pull the pieces out of the chunk. We copy the results into a |
| 2205 | * newly-allocated buffer that the caller can free. We don't want to |
| 2206 | * continue using the Chunk object because nothing has a reference to it. |
| 2207 | * |
| 2208 | * We could avoid this by returning type/data/offset/length and having |
| 2209 | * the caller be aware of the object lifetime issues, but that |
| 2210 | * integrates the JDWP code more tightly into the VM, and doesn't work |
| 2211 | * if we have responses for multiple chunks. |
| 2212 | * |
| 2213 | * So we're pretty much stuck with copying data around multiple times. |
| 2214 | */ |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2215 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), data_fid))); |
| 2216 | length = env->GetIntField(chunk.get(), length_fid); |
| 2217 | offset = env->GetIntField(chunk.get(), offset_fid); |
| 2218 | type = env->GetIntField(chunk.get(), type_fid); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2219 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2220 | VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2221 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2222 | return false; |
| 2223 | } |
| 2224 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2225 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2226 | if (offset + length > replyLength) { |
| 2227 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 2228 | return false; |
| 2229 | } |
| 2230 | |
| 2231 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 2232 | if (reply == NULL) { |
| 2233 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 2234 | return false; |
| 2235 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2236 | JDWP::Set4BE(reply + 0, type); |
| 2237 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2238 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2239 | |
| 2240 | *pReplyBuf = reply; |
| 2241 | *pReplyLen = length + kChunkHdrLen; |
| 2242 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2243 | VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", reinterpret_cast<char*>(reply), reply, length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2244 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2247 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2248 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2249 | |
| 2250 | Thread* self = Thread::Current(); |
| 2251 | if (self->GetState() != Thread::kRunnable) { |
| 2252 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 2253 | /* try anyway? */ |
| 2254 | } |
| 2255 | |
| 2256 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 2257 | static jclass DdmServer_class = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer"); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2258 | static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V"); |
| 2259 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| 2260 | env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event); |
| 2261 | if (env->ExceptionCheck()) { |
| 2262 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 2263 | env->ExceptionDescribe(); |
| 2264 | env->ExceptionClear(); |
| 2265 | } |
| 2266 | } |
| 2267 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2268 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2269 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2273 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2274 | gDdmThreadNotification = false; |
| 2275 | } |
| 2276 | |
| 2277 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2278 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2279 | * |
| 2280 | * Because we broadcast the full set of threads when the notifications are |
| 2281 | * first enabled, it's possible for "thread" to be actively executing. |
| 2282 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2283 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2284 | if (!gDdmThreadNotification) { |
| 2285 | return; |
| 2286 | } |
| 2287 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2288 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2289 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2290 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2291 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2292 | } else { |
| 2293 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 2294 | SirtRef<String> name(t->GetThreadName()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2295 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 2296 | const jchar* chars = name->GetCharArray()->GetData(); |
| 2297 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2298 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2299 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
| 2300 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2301 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 2302 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2306 | static void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2307 | Dbg::DdmSendThreadNotification(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 2311 | // We lock the thread list to avoid sending duplicate events or missing |
| 2312 | // a thread change. We should be okay holding this lock while sending |
| 2313 | // the messages out. (We have to hold it while accessing a live thread.) |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 2314 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2315 | |
| 2316 | gDdmThreadNotification = enable; |
| 2317 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 2318 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2322 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2323 | if (gDebuggerActive) { |
| 2324 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2325 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2326 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2327 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2331 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2335 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2336 | } |
| 2337 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2338 | void Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2339 | CHECK(buf != NULL); |
| 2340 | iovec vec[1]; |
| 2341 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 2342 | vec[0].iov_len = byte_count; |
| 2343 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2346 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 2347 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 2348 | } |
| 2349 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 2350 | void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iov_count) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2351 | if (gJdwpState == NULL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2352 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2353 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 2354 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2355 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2358 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 2359 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 2360 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2361 | return true; |
| 2362 | } |
| 2363 | |
| 2364 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 2365 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 2366 | return false; |
| 2367 | } |
| 2368 | |
| 2369 | gDdmHpifWhen = when; |
| 2370 | return true; |
| 2371 | } |
| 2372 | |
| 2373 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 2374 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 2375 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 2376 | return false; |
| 2377 | } |
| 2378 | |
| 2379 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 2380 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 2381 | return false; |
| 2382 | } |
| 2383 | |
| 2384 | if (native) { |
| 2385 | gDdmNhsgWhen = when; |
| 2386 | gDdmNhsgWhat = what; |
| 2387 | } else { |
| 2388 | gDdmHpsgWhen = when; |
| 2389 | gDdmHpsgWhat = what; |
| 2390 | } |
| 2391 | return true; |
| 2392 | } |
| 2393 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 2394 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 2395 | // If there's a one-shot 'when', reset it. |
| 2396 | if (reason == gDdmHpifWhen) { |
| 2397 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 2398 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | /* |
| 2403 | * Chunk HPIF (client --> server) |
| 2404 | * |
| 2405 | * Heap Info. General information about the heap, |
| 2406 | * suitable for a summary display. |
| 2407 | * |
| 2408 | * [u4]: number of heaps |
| 2409 | * |
| 2410 | * For each heap: |
| 2411 | * [u4]: heap ID |
| 2412 | * [u8]: timestamp in ms since Unix epoch |
| 2413 | * [u1]: capture reason (same as 'when' value from server) |
| 2414 | * [u4]: max heap size in bytes (-Xmx) |
| 2415 | * [u4]: current heap size in bytes |
| 2416 | * [u4]: current number of bytes allocated |
| 2417 | * [u4]: current number of objects allocated |
| 2418 | */ |
| 2419 | uint8_t heap_count = 1; |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2420 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2421 | JDWP::Append4BE(bytes, heap_count); |
| 2422 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| 2423 | JDWP::Append8BE(bytes, MilliTime()); |
| 2424 | JDWP::Append1BE(bytes, reason); |
| 2425 | JDWP::Append4BE(bytes, Heap::GetMaxMemory()); // Max allowed heap size in bytes. |
| 2426 | JDWP::Append4BE(bytes, Heap::GetTotalMemory()); // Current heap size in bytes. |
| 2427 | JDWP::Append4BE(bytes, Heap::GetBytesAllocated()); |
| 2428 | JDWP::Append4BE(bytes, Heap::GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2429 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 2430 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2433 | enum HpsgSolidity { |
| 2434 | SOLIDITY_FREE = 0, |
| 2435 | SOLIDITY_HARD = 1, |
| 2436 | SOLIDITY_SOFT = 2, |
| 2437 | SOLIDITY_WEAK = 3, |
| 2438 | SOLIDITY_PHANTOM = 4, |
| 2439 | SOLIDITY_FINALIZABLE = 5, |
| 2440 | SOLIDITY_SWEEP = 6, |
| 2441 | }; |
| 2442 | |
| 2443 | enum HpsgKind { |
| 2444 | KIND_OBJECT = 0, |
| 2445 | KIND_CLASS_OBJECT = 1, |
| 2446 | KIND_ARRAY_1 = 2, |
| 2447 | KIND_ARRAY_2 = 3, |
| 2448 | KIND_ARRAY_4 = 4, |
| 2449 | KIND_ARRAY_8 = 5, |
| 2450 | KIND_UNKNOWN = 6, |
| 2451 | KIND_NATIVE = 7, |
| 2452 | }; |
| 2453 | |
| 2454 | #define HPSG_PARTIAL (1<<7) |
| 2455 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 2456 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2457 | class HeapChunkContext { |
| 2458 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2459 | // Maximum chunk size. Obtain this from the formula: |
| 2460 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 2461 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2462 | : buf_(16384 - 16), |
| 2463 | type_(0), |
| 2464 | merge_(merge) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2465 | Reset(); |
| 2466 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2467 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2468 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2469 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2474 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2475 | Flush(); |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2480 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2481 | return; |
| 2482 | } |
| 2483 | |
| 2484 | // Start a new HPSx chunk. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2485 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 2486 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2487 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2488 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 2489 | JDWP::Write4BE(&p_, 0); // offset of this piece (relative to the virtual address). |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2490 | // [u4]: length of piece, in allocation units |
| 2491 | // We won't know this until we're done, so save the offset and stuff in a dummy value. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2492 | pieceLenField_ = p_; |
| 2493 | JDWP::Write4BE(&p_, 0x55555555); |
| 2494 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | void Flush() { |
| 2498 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2499 | CHECK_LE(&buf_[0], pieceLenField_); |
| 2500 | CHECK_LE(pieceLenField_, p_); |
| 2501 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2502 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2503 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2504 | Reset(); |
| 2505 | } |
| 2506 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2507 | static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 2508 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2509 | } |
| 2510 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2511 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2512 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 2513 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2514 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2515 | p_ = &buf_[0]; |
| 2516 | totalAllocationUnits_ = 0; |
| 2517 | needHeader_ = true; |
| 2518 | pieceLenField_ = NULL; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2521 | void HeapChunkCallback(void* start, void* end, size_t used_bytes) { |
| 2522 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 2523 | // in the following code not to allocate memory, by ensuring buf_ is of the correct size |
| 2524 | |
| 2525 | const void* user_ptr = used_bytes > 0 ? const_cast<void*>(start) : NULL; |
| 2526 | // from malloc.c mem2chunk(mem) |
| 2527 | const void* chunk_ptr = |
| 2528 | reinterpret_cast<const void*>(reinterpret_cast<const char*>(const_cast<void*>(start)) - |
| 2529 | (2 * sizeof(size_t))); |
| 2530 | // from malloc.c chunksize |
| 2531 | size_t chunk_len = (*reinterpret_cast<size_t* const*>(chunk_ptr))[1] & ~7; |
| 2532 | |
| 2533 | |
| 2534 | //size_t chunk_len = malloc_usable_size(user_ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2535 | CHECK_EQ((chunk_len & (ALLOCATION_UNIT_SIZE-1)), 0U); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2536 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2537 | /* Make sure there's enough room left in the buffer. |
| 2538 | * We need to use two bytes for every fractional 256 |
| 2539 | * allocation units used by the chunk. |
| 2540 | */ |
| 2541 | { |
| 2542 | size_t needed = (((chunk_len/ALLOCATION_UNIT_SIZE + 255) / 256) * 2); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2543 | size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2544 | if (bytesLeft < needed) { |
| 2545 | Flush(); |
| 2546 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2547 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2548 | bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2549 | if (bytesLeft < needed) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2550 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << chunk_len << ", " << needed << " bytes)"; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2551 | return; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | // OLD-TODO: notice when there's a gap and start a new heap, or at least a new range. |
| 2556 | EnsureHeader(chunk_ptr); |
| 2557 | |
| 2558 | // Determine the type of this chunk. |
| 2559 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 2560 | // If it's the same, we should combine them. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2561 | uint8_t state = ExamineObject(reinterpret_cast<const Object*>(user_ptr), (type_ == CHUNK_TYPE("NHSG"))); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2562 | |
| 2563 | // Write out the chunk description. |
| 2564 | chunk_len /= ALLOCATION_UNIT_SIZE; // convert to allocation units |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2565 | totalAllocationUnits_ += chunk_len; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2566 | while (chunk_len > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2567 | *p_++ = state | HPSG_PARTIAL; |
| 2568 | *p_++ = 255; // length - 1 |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2569 | chunk_len -= 256; |
| 2570 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2571 | *p_++ = state; |
| 2572 | *p_++ = chunk_len - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2573 | } |
| 2574 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2575 | uint8_t ExamineObject(const Object* o, bool is_native_heap) { |
| 2576 | if (o == NULL) { |
| 2577 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 2578 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2579 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2580 | // It's an allocated chunk. Figure out what it is. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2581 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2582 | // If we're looking at the native heap, we'll just return |
| 2583 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
| 2584 | if (is_native_heap || !Heap::IsLiveObjectLocked(o)) { |
| 2585 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 2586 | } |
| 2587 | |
| 2588 | Class* c = o->GetClass(); |
| 2589 | if (c == NULL) { |
| 2590 | // The object was probably just created but hasn't been initialized yet. |
| 2591 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 2592 | } |
| 2593 | |
| 2594 | if (!Heap::IsHeapAddress(c)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2595 | LOG(WARNING) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2596 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 2597 | } |
| 2598 | |
| 2599 | if (c->IsClassClass()) { |
| 2600 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 2601 | } |
| 2602 | |
| 2603 | if (c->IsArrayClass()) { |
| 2604 | if (o->IsObjectArray()) { |
| 2605 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 2606 | } |
| 2607 | switch (c->GetComponentSize()) { |
| 2608 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 2609 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 2610 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 2611 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 2612 | } |
| 2613 | } |
| 2614 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2615 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 2616 | } |
| 2617 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2618 | std::vector<uint8_t> buf_; |
| 2619 | uint8_t* p_; |
| 2620 | uint8_t* pieceLenField_; |
| 2621 | size_t totalAllocationUnits_; |
| 2622 | uint32_t type_; |
| 2623 | bool merge_; |
| 2624 | bool needHeader_; |
| 2625 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2626 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 2627 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2628 | |
| 2629 | void Dbg::DdmSendHeapSegments(bool native) { |
| 2630 | Dbg::HpsgWhen when; |
| 2631 | Dbg::HpsgWhat what; |
| 2632 | if (!native) { |
| 2633 | when = gDdmHpsgWhen; |
| 2634 | what = gDdmHpsgWhat; |
| 2635 | } else { |
| 2636 | when = gDdmNhsgWhen; |
| 2637 | what = gDdmNhsgWhat; |
| 2638 | } |
| 2639 | if (when == HPSG_WHEN_NEVER) { |
| 2640 | return; |
| 2641 | } |
| 2642 | |
| 2643 | // Figure out what kind of chunks we'll be sending. |
| 2644 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 2645 | |
| 2646 | // First, send a heap start chunk. |
| 2647 | uint8_t heap_id[4]; |
| 2648 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 2649 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 2650 | |
| 2651 | // Send a series of heap segment chunks. |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2652 | HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 2653 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2654 | // TODO: enable when bionic has moved to dlmalloc 2.8.5 |
| 2655 | // dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context); |
| 2656 | UNIMPLEMENTED(WARNING) << "Native heap send heap segments"; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2657 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2658 | Heap::GetAllocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2659 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2660 | |
| 2661 | // Finally, send a heap end chunk. |
| 2662 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2663 | } |
| 2664 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2665 | void Dbg::SetAllocTrackingEnabled(bool enabled) { |
| 2666 | MutexLock mu(gAllocTrackerLock); |
| 2667 | if (enabled) { |
| 2668 | if (recent_allocation_records_ == NULL) { |
| 2669 | LOG(INFO) << "Enabling alloc tracker (" << kNumAllocRecords << " entries, " |
| 2670 | << kMaxAllocRecordStackDepth << " frames --> " |
| 2671 | << (sizeof(AllocRecord) * kNumAllocRecords) << " bytes)"; |
| 2672 | gAllocRecordHead = gAllocRecordCount = 0; |
| 2673 | recent_allocation_records_ = new AllocRecord[kNumAllocRecords]; |
| 2674 | CHECK(recent_allocation_records_ != NULL); |
| 2675 | } |
| 2676 | } else { |
| 2677 | delete[] recent_allocation_records_; |
| 2678 | recent_allocation_records_ = NULL; |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | struct AllocRecordStackVisitor : public Thread::StackVisitor { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2683 | explicit AllocRecordStackVisitor(AllocRecord* record) : record(record), depth(0) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
| 2687 | if (depth >= kMaxAllocRecordStackDepth) { |
| 2688 | return; |
| 2689 | } |
| 2690 | Method* m = f.GetMethod(); |
| 2691 | if (m == NULL || m->IsCalleeSaveMethod()) { |
| 2692 | return; |
| 2693 | } |
| 2694 | record->stack[depth].method = m; |
| 2695 | record->stack[depth].raw_pc = pc; |
| 2696 | ++depth; |
| 2697 | } |
| 2698 | |
| 2699 | ~AllocRecordStackVisitor() { |
| 2700 | // Clear out any unused stack trace elements. |
| 2701 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
| 2702 | record->stack[depth].method = NULL; |
| 2703 | record->stack[depth].raw_pc = 0; |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | AllocRecord* record; |
| 2708 | size_t depth; |
| 2709 | }; |
| 2710 | |
| 2711 | void Dbg::RecordAllocation(Class* type, size_t byte_count) { |
| 2712 | Thread* self = Thread::Current(); |
| 2713 | CHECK(self != NULL); |
| 2714 | |
| 2715 | MutexLock mu(gAllocTrackerLock); |
| 2716 | if (recent_allocation_records_ == NULL) { |
| 2717 | return; |
| 2718 | } |
| 2719 | |
| 2720 | // Advance and clip. |
| 2721 | if (++gAllocRecordHead == kNumAllocRecords) { |
| 2722 | gAllocRecordHead = 0; |
| 2723 | } |
| 2724 | |
| 2725 | // Fill in the basics. |
| 2726 | AllocRecord* record = &recent_allocation_records_[gAllocRecordHead]; |
| 2727 | record->type = type; |
| 2728 | record->byte_count = byte_count; |
| 2729 | record->thin_lock_id = self->GetThinLockId(); |
| 2730 | |
| 2731 | // Fill in the stack trace. |
| 2732 | AllocRecordStackVisitor visitor(record); |
| 2733 | self->WalkStack(&visitor); |
| 2734 | |
| 2735 | if (gAllocRecordCount < kNumAllocRecords) { |
| 2736 | ++gAllocRecordCount; |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | /* |
| 2741 | * Return the index of the head element. |
| 2742 | * |
| 2743 | * We point at the most-recently-written record, so if allocRecordCount is 1 |
| 2744 | * we want to use the current element. Take "head+1" and subtract count |
| 2745 | * from it. |
| 2746 | * |
| 2747 | * We need to handle underflow in our circular buffer, so we add |
| 2748 | * kNumAllocRecords and then mask it back down. |
| 2749 | */ |
| 2750 | inline static int headIndex() { |
| 2751 | return (gAllocRecordHead+1 + kNumAllocRecords - gAllocRecordCount) & (kNumAllocRecords-1); |
| 2752 | } |
| 2753 | |
| 2754 | void Dbg::DumpRecentAllocations() { |
| 2755 | MutexLock mu(gAllocTrackerLock); |
| 2756 | if (recent_allocation_records_ == NULL) { |
| 2757 | LOG(INFO) << "Not recording tracked allocations"; |
| 2758 | return; |
| 2759 | } |
| 2760 | |
| 2761 | // "i" is the head of the list. We want to start at the end of the |
| 2762 | // list and move forward to the tail. |
| 2763 | size_t i = headIndex(); |
| 2764 | size_t count = gAllocRecordCount; |
| 2765 | |
| 2766 | LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")"; |
| 2767 | while (count--) { |
| 2768 | AllocRecord* record = &recent_allocation_records_[i]; |
| 2769 | |
Elliott Hughes | aa6e1cd | 2012-01-18 19:26:06 -0800 | [diff] [blame] | 2770 | LOG(INFO) << StringPrintf(" T=%-2d %6zd ", record->thin_lock_id, record->byte_count) |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2771 | << PrettyClass(record->type); |
| 2772 | |
| 2773 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
| 2774 | const Method* m = record->stack[stack_frame].method; |
| 2775 | if (m == NULL) { |
| 2776 | break; |
| 2777 | } |
| 2778 | LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber(); |
| 2779 | } |
| 2780 | |
| 2781 | // pause periodically to help logcat catch up |
| 2782 | if ((count % 5) == 0) { |
| 2783 | usleep(40000); |
| 2784 | } |
| 2785 | |
| 2786 | i = (i + 1) & (kNumAllocRecords-1); |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | class StringTable { |
| 2791 | public: |
| 2792 | StringTable() { |
| 2793 | } |
| 2794 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2795 | void Add(const char* s) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2796 | table_.insert(s); |
| 2797 | } |
| 2798 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2799 | size_t IndexOf(const char* s) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2800 | return std::distance(table_.begin(), table_.find(s)); |
| 2801 | } |
| 2802 | |
| 2803 | size_t Size() { |
| 2804 | return table_.size(); |
| 2805 | } |
| 2806 | |
| 2807 | void WriteTo(std::vector<uint8_t>& bytes) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2808 | typedef std::set<const char*>::const_iterator It; // TODO: C++0x auto |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2809 | for (It it = table_.begin(); it != table_.end(); ++it) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2810 | const char* s = *it; |
| 2811 | size_t s_len = CountModifiedUtf8Chars(s); |
| 2812 | UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]); |
| 2813 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 2814 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | private: |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2819 | std::set<const char*> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2820 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 2821 | }; |
| 2822 | |
| 2823 | /* |
| 2824 | * The data we send to DDMS contains everything we have recorded. |
| 2825 | * |
| 2826 | * Message header (all values big-endian): |
| 2827 | * (1b) message header len (to allow future expansion); includes itself |
| 2828 | * (1b) entry header len |
| 2829 | * (1b) stack frame len |
| 2830 | * (2b) number of entries |
| 2831 | * (4b) offset to string table from start of message |
| 2832 | * (2b) number of class name strings |
| 2833 | * (2b) number of method name strings |
| 2834 | * (2b) number of source file name strings |
| 2835 | * For each entry: |
| 2836 | * (4b) total allocation size |
| 2837 | * (2b) threadId |
| 2838 | * (2b) allocated object's class name index |
| 2839 | * (1b) stack depth |
| 2840 | * For each stack frame: |
| 2841 | * (2b) method's class name |
| 2842 | * (2b) method name |
| 2843 | * (2b) method source file |
| 2844 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 2845 | * (xb) class name strings |
| 2846 | * (xb) method name strings |
| 2847 | * (xb) source file strings |
| 2848 | * |
| 2849 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 2850 | * followed by UTF-16 data. |
| 2851 | * |
| 2852 | * We send up 16-bit unsigned indexes into string tables. In theory there |
| 2853 | * can be (kMaxAllocRecordStackDepth * kNumAllocRecords) unique strings in |
| 2854 | * each table, but in practice there should be far fewer. |
| 2855 | * |
| 2856 | * The chief reason for using a string table here is to keep the size of |
| 2857 | * the DDMS message to a minimum. This is partly to make the protocol |
| 2858 | * efficient, but also because we have to form the whole thing up all at |
| 2859 | * once in a memory buffer. |
| 2860 | * |
| 2861 | * We use separate string tables for class names, method names, and source |
| 2862 | * files to keep the indexes small. There will generally be no overlap |
| 2863 | * between the contents of these tables. |
| 2864 | */ |
| 2865 | jbyteArray Dbg::GetRecentAllocations() { |
| 2866 | if (false) { |
| 2867 | DumpRecentAllocations(); |
| 2868 | } |
| 2869 | |
| 2870 | MutexLock mu(gAllocTrackerLock); |
| 2871 | |
| 2872 | /* |
| 2873 | * Part 1: generate string tables. |
| 2874 | */ |
| 2875 | StringTable class_names; |
| 2876 | StringTable method_names; |
| 2877 | StringTable filenames; |
| 2878 | |
| 2879 | int count = gAllocRecordCount; |
| 2880 | int idx = headIndex(); |
| 2881 | while (count--) { |
| 2882 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2883 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 2884 | class_names.Add(ClassHelper(record->type).GetDescriptor()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2885 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2886 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2887 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2888 | Method* m = record->stack[i].method; |
| 2889 | mh.ChangeMethod(m); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2890 | if (m != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2891 | class_names.Add(mh.GetDeclaringClassDescriptor()); |
| 2892 | method_names.Add(mh.GetName()); |
| 2893 | filenames.Add(mh.GetDeclaringClassSourceFile()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2898 | } |
| 2899 | |
| 2900 | LOG(INFO) << "allocation records: " << gAllocRecordCount; |
| 2901 | |
| 2902 | /* |
| 2903 | * Part 2: allocate a buffer and generate the output. |
| 2904 | */ |
| 2905 | std::vector<uint8_t> bytes; |
| 2906 | |
| 2907 | // (1b) message header len (to allow future expansion); includes itself |
| 2908 | // (1b) entry header len |
| 2909 | // (1b) stack frame len |
| 2910 | const int kMessageHeaderLen = 15; |
| 2911 | const int kEntryHeaderLen = 9; |
| 2912 | const int kStackFrameLen = 8; |
| 2913 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 2914 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 2915 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 2916 | |
| 2917 | // (2b) number of entries |
| 2918 | // (4b) offset to string table from start of message |
| 2919 | // (2b) number of class name strings |
| 2920 | // (2b) number of method name strings |
| 2921 | // (2b) number of source file name strings |
| 2922 | JDWP::Append2BE(bytes, gAllocRecordCount); |
| 2923 | size_t string_table_offset = bytes.size(); |
| 2924 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| 2925 | JDWP::Append2BE(bytes, class_names.Size()); |
| 2926 | JDWP::Append2BE(bytes, method_names.Size()); |
| 2927 | JDWP::Append2BE(bytes, filenames.Size()); |
| 2928 | |
| 2929 | count = gAllocRecordCount; |
| 2930 | idx = headIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2931 | ClassHelper kh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2932 | while (count--) { |
| 2933 | // For each entry: |
| 2934 | // (4b) total allocation size |
| 2935 | // (2b) thread id |
| 2936 | // (2b) allocated object's class name index |
| 2937 | // (1b) stack depth |
| 2938 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2939 | size_t stack_depth = record->GetDepth(); |
| 2940 | JDWP::Append4BE(bytes, record->byte_count); |
| 2941 | JDWP::Append2BE(bytes, record->thin_lock_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2942 | kh.ChangeClass(record->type); |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 2943 | JDWP::Append2BE(bytes, class_names.IndexOf(kh.GetDescriptor())); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2944 | JDWP::Append1BE(bytes, stack_depth); |
| 2945 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2946 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2947 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 2948 | // For each stack frame: |
| 2949 | // (2b) method's class name |
| 2950 | // (2b) method name |
| 2951 | // (2b) method source file |
| 2952 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2953 | mh.ChangeMethod(record->stack[stack_frame].method); |
| 2954 | JDWP::Append2BE(bytes, class_names.IndexOf(mh.GetDeclaringClassDescriptor())); |
| 2955 | JDWP::Append2BE(bytes, method_names.IndexOf(mh.GetName())); |
| 2956 | JDWP::Append2BE(bytes, filenames.IndexOf(mh.GetDeclaringClassSourceFile())); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2957 | JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber()); |
| 2958 | } |
| 2959 | |
| 2960 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2961 | } |
| 2962 | |
| 2963 | // (xb) class name strings |
| 2964 | // (xb) method name strings |
| 2965 | // (xb) source file strings |
| 2966 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 2967 | class_names.WriteTo(bytes); |
| 2968 | method_names.WriteTo(bytes); |
| 2969 | filenames.WriteTo(bytes); |
| 2970 | |
| 2971 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 2972 | jbyteArray result = env->NewByteArray(bytes.size()); |
| 2973 | if (result != NULL) { |
| 2974 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 2975 | } |
| 2976 | return result; |
| 2977 | } |
| 2978 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2979 | } // namespace art |