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