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