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