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