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