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