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" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 26 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 27 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 28 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 29 | #include "thread_list.h" |
| 30 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 31 | extern "C" void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*); |
| 32 | #ifndef HAVE_ANDROID_OS |
| 33 | void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*) { |
| 34 | // No-op for glibc. |
| 35 | } |
| 36 | #endif |
| 37 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 38 | namespace art { |
| 39 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 40 | static const size_t kMaxAllocRecordStackDepth = 16; // Max 255. |
| 41 | static const size_t kNumAllocRecords = 512; // Must be power of 2. |
| 42 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 43 | class ObjectRegistry { |
| 44 | public: |
| 45 | ObjectRegistry() : lock_("ObjectRegistry lock") { |
| 46 | } |
| 47 | |
| 48 | JDWP::ObjectId Add(Object* o) { |
| 49 | if (o == NULL) { |
| 50 | return 0; |
| 51 | } |
| 52 | JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o)); |
| 53 | MutexLock mu(lock_); |
| 54 | map_[id] = o; |
| 55 | return id; |
| 56 | } |
| 57 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 58 | void Clear() { |
| 59 | MutexLock mu(lock_); |
| 60 | LOG(DEBUG) << "Debugger has detached; object registry had " << map_.size() << " entries"; |
| 61 | map_.clear(); |
| 62 | } |
| 63 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 64 | bool Contains(JDWP::ObjectId id) { |
| 65 | MutexLock mu(lock_); |
| 66 | return map_.find(id) != map_.end(); |
| 67 | } |
| 68 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 69 | template<typename T> T Get(JDWP::ObjectId id) { |
| 70 | MutexLock mu(lock_); |
| 71 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 72 | It it = map_.find(id); |
| 73 | return (it != map_.end()) ? reinterpret_cast<T>(it->second) : NULL; |
| 74 | } |
| 75 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 76 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 77 | MutexLock mu(lock_); |
| 78 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 79 | for (It it = map_.begin(); it != map_.end(); ++it) { |
| 80 | visitor(it->second, arg); |
| 81 | } |
| 82 | } |
| 83 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 84 | private: |
| 85 | Mutex lock_; |
| 86 | std::map<JDWP::ObjectId, Object*> map_; |
| 87 | }; |
| 88 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 89 | struct AllocRecordStackTraceElement { |
| 90 | const Method* method; |
| 91 | uintptr_t raw_pc; |
| 92 | |
| 93 | int32_t LineNumber() const { |
| 94 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 95 | Class* c = method->GetDeclaringClass(); |
| 96 | DexCache* dex_cache = c->GetDexCache(); |
| 97 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 98 | return dex_file.GetLineNumFromPC(method, method->ToDexPC(raw_pc)); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | struct AllocRecord { |
| 103 | Class* type; |
| 104 | size_t byte_count; |
| 105 | uint16_t thin_lock_id; |
| 106 | AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method. |
| 107 | |
| 108 | size_t GetDepth() { |
| 109 | size_t depth = 0; |
| 110 | while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) { |
| 111 | ++depth; |
| 112 | } |
| 113 | return depth; |
| 114 | } |
| 115 | }; |
| 116 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 117 | // JDWP is allowed unless the Zygote forbids it. |
| 118 | static bool gJdwpAllowed = true; |
| 119 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 120 | // Was there a -Xrunjdwp or -agent argument on the command-line? |
| 121 | static bool gJdwpConfigured = false; |
| 122 | |
| 123 | // Broken-down JDWP options. (Only valid if gJdwpConfigured is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 124 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 125 | |
| 126 | // Runtime JDWP state. |
| 127 | static JDWP::JdwpState* gJdwpState = NULL; |
| 128 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 129 | static bool gDebuggerActive; // debugger is making requests. |
| 130 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 131 | static bool gDdmThreadNotification = false; |
| 132 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 133 | // DDMS GC-related settings. |
| 134 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 135 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 136 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 137 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 138 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 139 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 140 | static ObjectRegistry* gRegistry = NULL; |
| 141 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 142 | // Recent allocation tracking. |
| 143 | static Mutex gAllocTrackerLock("AllocTracker lock"); |
| 144 | AllocRecord* Dbg::recent_allocation_records_ = NULL; // TODO: CircularBuffer<AllocRecord> |
| 145 | static size_t gAllocRecordHead = 0; |
| 146 | static size_t gAllocRecordCount = 0; |
| 147 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 148 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 149 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 150 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 151 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 152 | } |
| 153 | |
| 154 | static JDWP::JdwpTag TagFromClass(Class* c) { |
| 155 | if (c->IsArrayClass()) { |
| 156 | return JDWP::JT_ARRAY; |
| 157 | } |
| 158 | |
| 159 | if (c->IsStringClass()) { |
| 160 | return JDWP::JT_STRING; |
| 161 | } else if (c->IsClassClass()) { |
| 162 | return JDWP::JT_CLASS_OBJECT; |
| 163 | #if 0 // TODO |
| 164 | } else if (dvmInstanceof(clazz, gDvm.classJavaLangThread)) { |
| 165 | return JDWP::JT_THREAD; |
| 166 | } else if (dvmInstanceof(clazz, gDvm.classJavaLangThreadGroup)) { |
| 167 | return JDWP::JT_THREAD_GROUP; |
| 168 | } else if (dvmInstanceof(clazz, gDvm.classJavaLangClassLoader)) { |
| 169 | return JDWP::JT_CLASS_LOADER; |
| 170 | #endif |
| 171 | } else { |
| 172 | return JDWP::JT_OBJECT; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Objects declared to hold Object might actually hold a more specific |
| 178 | * type. The debugger may take a special interest in these (e.g. it |
| 179 | * wants to display the contents of Strings), so we want to return an |
| 180 | * appropriate tag. |
| 181 | * |
| 182 | * Null objects are tagged JT_OBJECT. |
| 183 | */ |
| 184 | static JDWP::JdwpTag TagFromObject(const Object* o) { |
| 185 | return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass()); |
| 186 | } |
| 187 | |
| 188 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 189 | switch (tag) { |
| 190 | case JDWP::JT_BOOLEAN: |
| 191 | case JDWP::JT_BYTE: |
| 192 | case JDWP::JT_CHAR: |
| 193 | case JDWP::JT_FLOAT: |
| 194 | case JDWP::JT_DOUBLE: |
| 195 | case JDWP::JT_INT: |
| 196 | case JDWP::JT_LONG: |
| 197 | case JDWP::JT_SHORT: |
| 198 | case JDWP::JT_VOID: |
| 199 | return true; |
| 200 | default: |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 205 | /* |
| 206 | * Handle one of the JDWP name/value pairs. |
| 207 | * |
| 208 | * JDWP options are: |
| 209 | * help: if specified, show help message and bail |
| 210 | * transport: may be dt_socket or dt_shmem |
| 211 | * address: for dt_socket, "host:port", or just "port" when listening |
| 212 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 213 | * timeout: how long to wait for debugger to connect / listen |
| 214 | * |
| 215 | * Useful with server=n (these aren't supported yet): |
| 216 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 217 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 218 | * launch=<command-line>: launch the debugger itself |
| 219 | * |
| 220 | * The "transport" option is required, as is "address" if server=n. |
| 221 | */ |
| 222 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 223 | if (name == "transport") { |
| 224 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 225 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 226 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 227 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 228 | } else { |
| 229 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 230 | return false; |
| 231 | } |
| 232 | } else if (name == "server") { |
| 233 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 234 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 235 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 236 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 237 | } else { |
| 238 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 239 | return false; |
| 240 | } |
| 241 | } else if (name == "suspend") { |
| 242 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 243 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 244 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 245 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 246 | } else { |
| 247 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 248 | return false; |
| 249 | } |
| 250 | } else if (name == "address") { |
| 251 | /* this is either <port> or <host>:<port> */ |
| 252 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 253 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 254 | std::string::size_type colon = value.find(':'); |
| 255 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 256 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 257 | port_string = value.substr(colon + 1); |
| 258 | } else { |
| 259 | port_string = value; |
| 260 | } |
| 261 | if (port_string.empty()) { |
| 262 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 263 | return false; |
| 264 | } |
| 265 | char* end; |
| 266 | long port = strtol(port_string.c_str(), &end, 10); |
| 267 | if (*end != '\0') { |
| 268 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 269 | return false; |
| 270 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 271 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 272 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 273 | /* valid but unsupported */ |
| 274 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 275 | } else { |
| 276 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 277 | } |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 284 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 285 | */ |
| 286 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 287 | LOG(VERBOSE) << "ParseJdwpOptions: " << options; |
| 288 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 289 | std::vector<std::string> pairs; |
| 290 | Split(options, ',', pairs); |
| 291 | |
| 292 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 293 | std::string::size_type equals = pairs[i].find('='); |
| 294 | if (equals == std::string::npos) { |
| 295 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 296 | return false; |
| 297 | } |
| 298 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 299 | } |
| 300 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 301 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 302 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 303 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 304 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 305 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | gJdwpConfigured = true; |
| 310 | return true; |
| 311 | } |
| 312 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 313 | void Dbg::StartJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 314 | if (!gJdwpAllowed || !gJdwpConfigured) { |
| 315 | // No JDWP for you! |
| 316 | return; |
| 317 | } |
| 318 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 319 | CHECK(gRegistry == NULL); |
| 320 | gRegistry = new ObjectRegistry; |
| 321 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 322 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 323 | // debugger, passively listen for a debugger, or block waiting for a |
| 324 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 325 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 326 | if (gJdwpState == NULL) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 327 | // We probably failed because some other process has the port already, which means that |
| 328 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 329 | // talking to that other process. |
| 330 | LOG(FATAL) << "debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // If a debugger has already attached, send the "welcome" message. |
| 334 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 335 | if (gJdwpState->IsActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 336 | //ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 337 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 338 | LOG(WARNING) << "failed to post 'start' message to debugger"; |
| 339 | } |
| 340 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 343 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 344 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 345 | delete gRegistry; |
| 346 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 349 | void Dbg::GcDidFinish() { |
| 350 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
| 351 | LOG(DEBUG) << "Sending VM heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 352 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 353 | } |
| 354 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
| 355 | LOG(DEBUG) << "Dumping VM heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 356 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 357 | } |
| 358 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
| 359 | LOG(DEBUG) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 360 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 364 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 365 | gJdwpAllowed = allowed; |
| 366 | } |
| 367 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 368 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 369 | return Thread::Current()->GetInvokeReq(); |
| 370 | } |
| 371 | |
| 372 | Thread* Dbg::GetDebugThread() { |
| 373 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 374 | } |
| 375 | |
| 376 | void Dbg::ClearWaitForEventThread() { |
| 377 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 381 | CHECK(!gDebuggerConnected); |
| 382 | LOG(VERBOSE) << "JDWP has attached"; |
| 383 | gDebuggerConnected = true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 386 | void Dbg::GoActive() { |
| 387 | // Enable all debugging features, including scans for breakpoints. |
| 388 | // This is a no-op if we're already active. |
| 389 | // Only called from the JDWP handler thread. |
| 390 | if (gDebuggerActive) { |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | LOG(INFO) << "Debugger is active"; |
| 395 | |
| 396 | // TODO: CHECK we don't have any outstanding breakpoints. |
| 397 | |
| 398 | gDebuggerActive = true; |
| 399 | |
| 400 | //dvmEnableAllSubMode(kSubModeDebuggerActive); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 404 | CHECK(gDebuggerConnected); |
| 405 | |
| 406 | gDebuggerActive = false; |
| 407 | |
| 408 | //dvmDisableAllSubMode(kSubModeDebuggerActive); |
| 409 | |
| 410 | gRegistry->Clear(); |
| 411 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | bool Dbg::IsDebuggerConnected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 415 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | bool Dbg::IsDebuggingEnabled() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 419 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | int64_t Dbg::LastDebuggerActivity() { |
| 423 | UNIMPLEMENTED(WARNING); |
| 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | int Dbg::ThreadRunning() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 428 | return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | int Dbg::ThreadWaiting() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 432 | return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 435 | int Dbg::ThreadContinuing(int new_state) { |
| 436 | 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] | 437 | } |
| 438 | |
| 439 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 440 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void Dbg::Exit(int status) { |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 444 | exit(status); // This is all dalvik did. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 447 | void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 448 | if (gRegistry != NULL) { |
| 449 | gRegistry->VisitRoots(visitor, arg); |
| 450 | } |
| 451 | } |
| 452 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 453 | std::string Dbg::GetClassDescriptor(JDWP::RefTypeId classId) { |
| 454 | Class* c = gRegistry->Get<Class*>(classId); |
| 455 | return c->GetDescriptor()->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) { |
| 459 | UNIMPLEMENTED(FATAL); |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 464 | Class* c = gRegistry->Get<Class*>(id); |
| 465 | return gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) { |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 469 | Object* o = gRegistry->Get<Object*>(id); |
| 470 | return gRegistry->Add(o->GetClass()->GetClassLoader()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) { |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame^] | 474 | Class* c = gRegistry->Get<Class*>(id); |
| 475 | return c->GetAccessFlags() & kAccJavaFlagsMask; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 478 | bool Dbg::IsInterface(JDWP::RefTypeId classId) { |
| 479 | Class* c = gRegistry->Get<Class*>(classId); |
| 480 | return c->IsInterface(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 483 | void Dbg::GetClassList(uint32_t* pClassCount, JDWP::RefTypeId** pClasses) { |
| 484 | // Get the complete list of reference classes (i.e. all classes except |
| 485 | // the primitive types). |
| 486 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 487 | struct ClassListCreator { |
| 488 | static bool Visit(Class* c, void* arg) { |
| 489 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 490 | } |
| 491 | |
| 492 | bool Visit(Class* c) { |
| 493 | if (!c->IsPrimitive()) { |
| 494 | classes.push_back(static_cast<JDWP::RefTypeId>(gRegistry->Add(c))); |
| 495 | } |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | std::vector<JDWP::RefTypeId> classes; |
| 500 | }; |
| 501 | |
| 502 | ClassListCreator clc; |
| 503 | Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc); |
| 504 | *pClassCount = clc.classes.size(); |
| 505 | *pClasses = new JDWP::RefTypeId[clc.classes.size()]; |
| 506 | for (size_t i = 0; i < clc.classes.size(); ++i) { |
| 507 | (*pClasses)[i] = clc.classes[i]; |
| 508 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 512 | UNIMPLEMENTED(FATAL); |
| 513 | } |
| 514 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame^] | 515 | void Dbg::GetClassInfo(JDWP::RefTypeId classId, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 516 | Class* c = gRegistry->Get<Class*>(classId); |
| 517 | if (c->IsArrayClass()) { |
| 518 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 519 | *pTypeTag = JDWP::TT_ARRAY; |
| 520 | } else { |
| 521 | if (c->IsErroneous()) { |
| 522 | *pStatus = JDWP::CS_ERROR; |
| 523 | } else { |
| 524 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 525 | } |
| 526 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 527 | } |
| 528 | |
| 529 | if (pDescriptor != NULL) { |
| 530 | *pDescriptor = c->GetDescriptor()->ToModifiedUtf8(); |
| 531 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame^] | 534 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) { |
| 535 | std::vector<Class*> classes; |
| 536 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
| 537 | ids.clear(); |
| 538 | for (size_t i = 0; i < classes.size(); ++i) { |
| 539 | ids.push_back(gRegistry->Add(classes[i])); |
| 540 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame^] | 543 | void Dbg::GetObjectType(JDWP::ObjectId objectId, JDWP::JdwpTypeTag* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 544 | Object* o = gRegistry->Get<Object*>(objectId); |
| 545 | if (o->GetClass()->IsArrayClass()) { |
| 546 | *pRefTypeTag = JDWP::TT_ARRAY; |
| 547 | } else if (o->GetClass()->IsInterface()) { |
| 548 | *pRefTypeTag = JDWP::TT_INTERFACE; |
| 549 | } else { |
| 550 | *pRefTypeTag = JDWP::TT_CLASS; |
| 551 | } |
| 552 | *pRefTypeId = gRegistry->Add(o->GetClass()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) { |
| 556 | UNIMPLEMENTED(FATAL); |
| 557 | return 0; |
| 558 | } |
| 559 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 560 | std::string Dbg::GetSignature(JDWP::RefTypeId refTypeId) { |
| 561 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 562 | CHECK(c != NULL); |
| 563 | return c->GetDescriptor()->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 566 | bool Dbg::GetSourceFile(JDWP::RefTypeId refTypeId, std::string& result) { |
| 567 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 568 | CHECK(c != NULL); |
| 569 | |
| 570 | String* source_file = c->GetSourceFile(); |
| 571 | if (source_file == NULL) { |
| 572 | return false; |
| 573 | } |
| 574 | result = source_file->ToModifiedUtf8(); |
| 575 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) { |
| 579 | UNIMPLEMENTED(FATAL); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 584 | Object* o = gRegistry->Get<Object*>(objectId); |
| 585 | return TagFromObject(o); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 588 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 589 | switch (tag) { |
| 590 | case JDWP::JT_VOID: |
| 591 | return 0; |
| 592 | case JDWP::JT_BYTE: |
| 593 | case JDWP::JT_BOOLEAN: |
| 594 | return 1; |
| 595 | case JDWP::JT_CHAR: |
| 596 | case JDWP::JT_SHORT: |
| 597 | return 2; |
| 598 | case JDWP::JT_FLOAT: |
| 599 | case JDWP::JT_INT: |
| 600 | return 4; |
| 601 | case JDWP::JT_ARRAY: |
| 602 | case JDWP::JT_OBJECT: |
| 603 | case JDWP::JT_STRING: |
| 604 | case JDWP::JT_THREAD: |
| 605 | case JDWP::JT_THREAD_GROUP: |
| 606 | case JDWP::JT_CLASS_LOADER: |
| 607 | case JDWP::JT_CLASS_OBJECT: |
| 608 | return sizeof(JDWP::ObjectId); |
| 609 | case JDWP::JT_DOUBLE: |
| 610 | case JDWP::JT_LONG: |
| 611 | return 8; |
| 612 | default: |
| 613 | LOG(FATAL) << "unknown tag " << tag; |
| 614 | return -1; |
| 615 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | int Dbg::GetArrayLength(JDWP::ObjectId arrayId) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 619 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 620 | Array* a = o->AsArray(); |
| 621 | return a->GetLength(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 625 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 626 | Array* a = o->AsArray(); |
| 627 | std::string descriptor(a->GetClass()->GetDescriptor()->ToModifiedUtf8()); |
| 628 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 629 | if (!IsPrimitiveTag(tag)) { |
| 630 | tag = TagFromClass(a->GetClass()->GetComponentType()); |
| 631 | } |
| 632 | return tag; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 635 | bool Dbg::OutputArray(JDWP::ObjectId arrayId, int offset, int count, JDWP::ExpandBuf* pReply) { |
| 636 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 637 | Array* a = o->AsArray(); |
| 638 | |
| 639 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 640 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | std::string descriptor(a->GetClass()->GetDescriptor()->ToModifiedUtf8()); |
| 645 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 646 | |
| 647 | if (IsPrimitiveTag(tag)) { |
| 648 | size_t width = GetTagWidth(tag); |
| 649 | const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData()); |
| 650 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 651 | if (width == 8) { |
| 652 | const uint64_t* src8 = reinterpret_cast<const uint64_t*>(src); |
| 653 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 654 | } else if (width == 4) { |
| 655 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 656 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 657 | } else if (width == 2) { |
| 658 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 659 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 660 | } else { |
| 661 | memcpy(dst, &src[offset * width], count * width); |
| 662 | } |
| 663 | } else { |
| 664 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 665 | for (int i = 0; i < count; ++i) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 666 | Object* element = oa->Get(offset + i); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 667 | JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag; |
| 668 | expandBufAdd1(pReply, specific_tag); |
| 669 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 676 | bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int offset, int count, const uint8_t* src) { |
| 677 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 678 | Array* a = o->AsArray(); |
| 679 | |
| 680 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 681 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | std::string descriptor(a->GetClass()->GetDescriptor()->ToModifiedUtf8()); |
| 686 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 687 | |
| 688 | if (IsPrimitiveTag(tag)) { |
| 689 | size_t width = GetTagWidth(tag); |
| 690 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData())[offset * width]); |
| 691 | if (width == 8) { |
| 692 | for (int i = 0; i < count; ++i) { |
| 693 | // Handle potentially non-aligned memory access one byte at a time for ARM's benefit. |
| 694 | uint64_t value; |
| 695 | for (size_t j = 0; j < sizeof(uint64_t); ++j) reinterpret_cast<uint8_t*>(&value)[j] = src[j]; |
| 696 | src += sizeof(uint64_t); |
| 697 | JDWP::Write8BE(&dst, value); |
| 698 | } |
| 699 | } else if (width == 4) { |
| 700 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 701 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[i]); |
| 702 | } else if (width == 2) { |
| 703 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 704 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[i]); |
| 705 | } else { |
| 706 | memcpy(&dst[offset * width], src, count * width); |
| 707 | } |
| 708 | } else { |
| 709 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 710 | for (int i = 0; i < count; ++i) { |
| 711 | JDWP::ObjectId id = JDWP::ReadObjectId(&src); |
| 712 | oa->Set(offset + i, gRegistry->Get<Object*>(id)); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | JDWP::ObjectId Dbg::CreateString(const char* str) { |
| 720 | UNIMPLEMENTED(FATAL); |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) { |
| 725 | UNIMPLEMENTED(FATAL); |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) { |
| 730 | UNIMPLEMENTED(FATAL); |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
| 735 | UNIMPLEMENTED(FATAL); |
| 736 | return false; |
| 737 | } |
| 738 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 739 | JDWP::FieldId ToFieldId(Field* f) { |
| 740 | #ifdef MOVING_GARBAGE_COLLECTOR |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 741 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 742 | #else |
| 743 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| 744 | #endif |
| 745 | } |
| 746 | |
| 747 | JDWP::MethodId ToMethodId(Method* m) { |
| 748 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 749 | UNIMPLEMENTED(FATAL); |
| 750 | #else |
| 751 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
| 752 | #endif |
| 753 | } |
| 754 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 755 | Field* FromFieldId(JDWP::FieldId fid) { |
| 756 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 757 | UNIMPLEMENTED(FATAL); |
| 758 | #else |
| 759 | return reinterpret_cast<Field*>(static_cast<uintptr_t>(fid)); |
| 760 | #endif |
| 761 | } |
| 762 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 763 | Method* FromMethodId(JDWP::MethodId mid) { |
| 764 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 765 | UNIMPLEMENTED(FATAL); |
| 766 | #else |
| 767 | return reinterpret_cast<Method*>(static_cast<uintptr_t>(mid)); |
| 768 | #endif |
| 769 | } |
| 770 | |
| 771 | std::string Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId) { |
| 772 | return FromMethodId(methodId)->GetName()->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 775 | /* |
| 776 | * Augment the access flags for synthetic methods and fields by setting |
| 777 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 778 | * flags not specified by the Java programming language. |
| 779 | */ |
| 780 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 781 | accessFlags &= kAccJavaFlagsMask; |
| 782 | if ((accessFlags & kAccSynthetic) != 0) { |
| 783 | accessFlags |= 0xf0000000; |
| 784 | } |
| 785 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 788 | static const uint16_t kEclipseWorkaroundSlot = 1000; |
| 789 | |
| 790 | /* |
| 791 | * Eclipse appears to expect that the "this" reference is in slot zero. |
| 792 | * If it's not, the "variables" display will show two copies of "this", |
| 793 | * possibly because it gets "this" from SF.ThisObject and then displays |
| 794 | * all locals with nonzero slot numbers. |
| 795 | * |
| 796 | * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On |
| 797 | * SF.GetValues / SF.SetValues we map them back. |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 798 | * |
| 799 | * TODO: jdb uses the value to determine whether a variable is a local or an argument, |
| 800 | * by checking whether it's less than the number of arguments. To make that work, we'd |
| 801 | * 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] | 802 | */ |
| 803 | static uint16_t MangleSlot(uint16_t slot, const char* name) { |
| 804 | uint16_t newSlot = slot; |
| 805 | if (strcmp(name, "this") == 0) { |
| 806 | newSlot = 0; |
| 807 | } else if (slot == 0) { |
| 808 | newSlot = kEclipseWorkaroundSlot; |
| 809 | } |
| 810 | return newSlot; |
| 811 | } |
| 812 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 813 | static uint16_t DemangleSlot(uint16_t slot, Frame& f) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 814 | if (slot == kEclipseWorkaroundSlot) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 815 | return 0; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 816 | } else if (slot == 0) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 817 | Method* m = f.GetMethod(); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 818 | return m->NumRegisters() - m->NumIns(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 819 | } |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 820 | return slot; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 821 | } |
| 822 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 823 | void Dbg::OutputDeclaredFields(JDWP::RefTypeId refTypeId, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 824 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 825 | CHECK(c != NULL); |
| 826 | |
| 827 | size_t instance_field_count = c->NumInstanceFields(); |
| 828 | size_t static_field_count = c->NumStaticFields(); |
| 829 | |
| 830 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 831 | |
| 832 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
| 833 | Field* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count); |
| 834 | |
| 835 | expandBufAddFieldId(pReply, ToFieldId(f)); |
| 836 | expandBufAddUtf8String(pReply, f->GetName()->ToModifiedUtf8().c_str()); |
| 837 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 838 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 839 | static const char genericSignature[1] = ""; |
| 840 | expandBufAddUtf8String(pReply, genericSignature); |
| 841 | } |
| 842 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 843 | } |
| 844 | } |
| 845 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 846 | void Dbg::OutputDeclaredMethods(JDWP::RefTypeId refTypeId, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 847 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 848 | CHECK(c != NULL); |
| 849 | |
| 850 | size_t direct_method_count = c->NumDirectMethods(); |
| 851 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 852 | |
| 853 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 854 | |
| 855 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
| 856 | Method* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count); |
| 857 | |
| 858 | expandBufAddMethodId(pReply, ToMethodId(m)); |
| 859 | expandBufAddUtf8String(pReply, m->GetName()->ToModifiedUtf8().c_str()); |
| 860 | expandBufAddUtf8String(pReply, m->GetSignature()->ToModifiedUtf8().c_str()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 861 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 862 | static const char genericSignature[1] = ""; |
| 863 | expandBufAddUtf8String(pReply, genericSignature); |
| 864 | } |
| 865 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | void Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) { |
| 870 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 871 | CHECK(c != NULL); |
| 872 | size_t interface_count = c->NumInterfaces(); |
| 873 | expandBufAdd4BE(pReply, interface_count); |
| 874 | for (size_t i = 0; i < interface_count; ++i) { |
| 875 | expandBufAddRefTypeId(pReply, gRegistry->Add(c->GetInterface(i))); |
| 876 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 880 | struct DebugCallbackContext { |
| 881 | int numItems; |
| 882 | JDWP::ExpandBuf* pReply; |
| 883 | |
| 884 | static bool Callback(void* context, uint32_t address, uint32_t lineNum) { |
| 885 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 886 | expandBufAdd8BE(pContext->pReply, address); |
| 887 | expandBufAdd4BE(pContext->pReply, lineNum); |
| 888 | pContext->numItems++; |
| 889 | return true; |
| 890 | } |
| 891 | }; |
| 892 | |
| 893 | Method* m = FromMethodId(methodId); |
| 894 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 895 | const DexFile& dex_file = class_linker->FindDexFile(m->GetDeclaringClass()->GetDexCache()); |
| 896 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(m->GetCodeItemOffset()); |
| 897 | |
| 898 | uint64_t start, end; |
| 899 | if (m->IsNative()) { |
| 900 | start = -1; |
| 901 | end = -1; |
| 902 | } else { |
| 903 | start = 0; |
| 904 | end = code_item->insns_size_in_code_units_; // TODO: what are the units supposed to be? *2? |
| 905 | } |
| 906 | |
| 907 | expandBufAdd8BE(pReply, start); |
| 908 | expandBufAdd8BE(pReply, end); |
| 909 | |
| 910 | // Add numLines later |
| 911 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 912 | expandBufAdd4BE(pReply, 0); |
| 913 | |
| 914 | DebugCallbackContext context; |
| 915 | context.numItems = 0; |
| 916 | context.pReply = pReply; |
| 917 | |
| 918 | dex_file.DecodeDebugInfo(code_item, m, DebugCallbackContext::Callback, NULL, &context); |
| 919 | |
| 920 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 923 | 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] | 924 | struct DebugCallbackContext { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 925 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 926 | size_t variable_count; |
| 927 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 928 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 929 | 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] | 930 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 931 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 932 | LOG(VERBOSE) << StringPrintf(" %2d: %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] | 933 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 934 | slot = MangleSlot(slot, name); |
| 935 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 936 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 937 | expandBufAddUtf8String(pContext->pReply, name); |
| 938 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 939 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 940 | expandBufAddUtf8String(pContext->pReply, signature); |
| 941 | } |
| 942 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 943 | expandBufAdd4BE(pContext->pReply, slot); |
| 944 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 945 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 946 | } |
| 947 | }; |
| 948 | |
| 949 | Method* m = FromMethodId(methodId); |
| 950 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 951 | const DexFile& dex_file = class_linker->FindDexFile(m->GetDeclaringClass()->GetDexCache()); |
| 952 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(m->GetCodeItemOffset()); |
| 953 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 954 | // arg_count considers doubles and longs to take 2 units. |
| 955 | // variable_count considers everything to take 1 unit. |
| 956 | std::string shorty(m->GetShorty()->ToModifiedUtf8()); |
| 957 | expandBufAdd4BE(pReply, m->NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 958 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 959 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 960 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 961 | expandBufAdd4BE(pReply, 0); |
| 962 | |
| 963 | DebugCallbackContext context; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 964 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 965 | context.variable_count = 0; |
| 966 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 967 | |
| 968 | dex_file.DecodeDebugInfo(code_item, m, NULL, DebugCallbackContext::Callback, &context); |
| 969 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 970 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 973 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId fieldId) { |
| 974 | return BasicTagFromDescriptor(FromFieldId(fieldId)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 977 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId fieldId) { |
| 978 | return BasicTagFromDescriptor(FromFieldId(fieldId)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 982 | Object* o = gRegistry->Get<Object*>(objectId); |
| 983 | Field* f = FromFieldId(fieldId); |
| 984 | |
| 985 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
| 986 | |
| 987 | if (IsPrimitiveTag(tag)) { |
| 988 | expandBufAdd1(pReply, tag); |
| 989 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 990 | expandBufAdd1(pReply, f->Get32(o)); |
| 991 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 992 | expandBufAdd2BE(pReply, f->Get32(o)); |
| 993 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 994 | expandBufAdd4BE(pReply, f->Get32(o)); |
| 995 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 996 | expandBufAdd8BE(pReply, f->Get64(o)); |
| 997 | } else { |
| 998 | LOG(FATAL) << "unknown tag: " << tag; |
| 999 | } |
| 1000 | } else { |
| 1001 | Object* value = f->GetObject(o); |
| 1002 | expandBufAdd1(pReply, TagFromObject(value)); |
| 1003 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1004 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | void 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] | 1008 | Object* o = gRegistry->Get<Object*>(objectId); |
| 1009 | Field* f = FromFieldId(fieldId); |
| 1010 | |
| 1011 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
| 1012 | |
| 1013 | if (IsPrimitiveTag(tag)) { |
| 1014 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1015 | f->Set64(o, value); |
| 1016 | } else { |
| 1017 | f->Set32(o, value); |
| 1018 | } |
| 1019 | } else { |
| 1020 | f->SetObject(o, gRegistry->Get<Object*>(value)); |
| 1021 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame^] | 1024 | void Dbg::GetStaticFieldValue(JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 1025 | GetFieldValue(0, fieldId, pReply); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame^] | 1028 | void Dbg::SetStaticFieldValue(JDWP::FieldId fieldId, uint64_t value, int width) { |
| 1029 | SetFieldValue(0, fieldId, value, width); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1032 | std::string Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 1033 | String* s = gRegistry->Get<String*>(strId); |
| 1034 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1037 | Thread* DecodeThread(JDWP::ObjectId threadId) { |
| 1038 | Object* thread_peer = gRegistry->Get<Object*>(threadId); |
| 1039 | CHECK(thread_peer != NULL); |
| 1040 | return Thread::FromManagedThread(thread_peer); |
| 1041 | } |
| 1042 | |
| 1043 | bool Dbg::GetThreadName(JDWP::ObjectId threadId, std::string& name) { |
| 1044 | ScopedThreadListLock thread_list_lock; |
| 1045 | Thread* thread = DecodeThread(threadId); |
| 1046 | if (thread == NULL) { |
| 1047 | return false; |
| 1048 | } |
| 1049 | StringAppendF(&name, "<%d> %s", thread->GetThinLockId(), thread->GetName()->ToModifiedUtf8().c_str()); |
| 1050 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1054 | Object* thread = gRegistry->Get<Object*>(threadId); |
| 1055 | CHECK(thread != NULL); |
| 1056 | |
| 1057 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;"); |
| 1058 | CHECK(c != NULL); |
| 1059 | Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;"); |
| 1060 | CHECK(f != NULL); |
| 1061 | Object* group = f->GetObject(thread); |
| 1062 | CHECK(group != NULL); |
| 1063 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1066 | std::string Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 1067 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1068 | CHECK(thread_group != NULL); |
| 1069 | |
| 1070 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1071 | CHECK(c != NULL); |
| 1072 | Field* f = c->FindInstanceField("name", "Ljava/lang/String;"); |
| 1073 | CHECK(f != NULL); |
| 1074 | String* s = reinterpret_cast<String*>(f->GetObject(thread_group)); |
| 1075 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1079 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1080 | CHECK(thread_group != NULL); |
| 1081 | |
| 1082 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1083 | CHECK(c != NULL); |
| 1084 | Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;"); |
| 1085 | CHECK(f != NULL); |
| 1086 | Object* parent = f->GetObject(thread_group); |
| 1087 | return gRegistry->Add(parent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1090 | static Object* GetStaticThreadGroup(const char* field_name) { |
| 1091 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1092 | CHECK(c != NULL); |
| 1093 | Field* f = c->FindStaticField(field_name, "Ljava/lang/ThreadGroup;"); |
| 1094 | CHECK(f != NULL); |
| 1095 | Object* group = f->GetObject(NULL); |
| 1096 | CHECK(group != NULL); |
| 1097 | return group; |
| 1098 | } |
| 1099 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1100 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1101 | return gRegistry->Add(GetStaticThreadGroup("mSystem")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1105 | return gRegistry->Add(GetStaticThreadGroup("mMain")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1108 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* pThreadStatus, uint32_t* pSuspendStatus) { |
| 1109 | ScopedThreadListLock thread_list_lock; |
| 1110 | |
| 1111 | Thread* thread = DecodeThread(threadId); |
| 1112 | if (thread == NULL) { |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | switch (thread->GetState()) { |
| 1117 | case Thread::kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1118 | case Thread::kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1119 | case Thread::kTimedWaiting: *pThreadStatus = JDWP::TS_SLEEPING; break; |
| 1120 | case Thread::kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break; |
| 1121 | case Thread::kWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1122 | case Thread::kInitializing: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1123 | case Thread::kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1124 | case Thread::kNative: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1125 | case Thread::kVmWait: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1126 | case Thread::kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1127 | default: |
| 1128 | LOG(FATAL) << "unknown thread state " << thread->GetState(); |
| 1129 | } |
| 1130 | |
| 1131 | *pSuspendStatus = (thread->IsSuspended() ? JDWP::SUSPEND_STATUS_SUSPENDED : 0); |
| 1132 | |
| 1133 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) { |
| 1137 | UNIMPLEMENTED(FATAL); |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1142 | return DecodeThread(threadId) != NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1146 | return DecodeThread(threadId)->IsSuspended(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | //void Dbg::WaitForSuspend(JDWP::ObjectId threadId); |
| 1150 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1151 | void Dbg::GetThreadGroupThreadsImpl(Object* thread_group, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 1152 | struct ThreadListVisitor { |
| 1153 | static void Visit(Thread* t, void* arg) { |
| 1154 | reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t); |
| 1155 | } |
| 1156 | |
| 1157 | void Visit(Thread* t) { |
| 1158 | if (t == Dbg::GetDebugThread()) { |
| 1159 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 1160 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 1161 | return; |
| 1162 | } |
| 1163 | if (thread_group == NULL || t->GetThreadGroup() == thread_group) { |
| 1164 | threads.push_back(gRegistry->Add(t->GetPeer())); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | Object* thread_group; |
| 1169 | std::vector<JDWP::ObjectId> threads; |
| 1170 | }; |
| 1171 | |
| 1172 | ThreadListVisitor tlv; |
| 1173 | tlv.thread_group = thread_group; |
| 1174 | |
| 1175 | { |
| 1176 | ScopedThreadListLock thread_list_lock; |
| 1177 | Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv); |
| 1178 | } |
| 1179 | |
| 1180 | *pThreadCount = tlv.threads.size(); |
| 1181 | if (*pThreadCount == 0) { |
| 1182 | *ppThreadIds = NULL; |
| 1183 | } else { |
| 1184 | *ppThreadIds = new JDWP::ObjectId[*pThreadCount]; |
| 1185 | for (size_t i = 0; i < *pThreadCount; ++i) { |
| 1186 | (*ppThreadIds)[i] = tlv.threads[i]; |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1191 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1192 | GetThreadGroupThreadsImpl(gRegistry->Get<Object*>(threadGroupId), ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1196 | GetThreadGroupThreadsImpl(NULL, ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1200 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1201 | struct CountStackDepthVisitor : public Thread::StackVisitor { |
| 1202 | CountStackDepthVisitor() : depth(0) {} |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1203 | virtual void VisitFrame(const Frame& f, uintptr_t) { |
| 1204 | // TODO: we'll need to skip callee-save frames too. |
| 1205 | if (f.HasMethod()) { |
| 1206 | ++depth; |
| 1207 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1208 | } |
| 1209 | size_t depth; |
| 1210 | }; |
| 1211 | CountStackDepthVisitor visitor; |
| 1212 | DecodeThread(threadId)->WalkStack(&visitor); |
| 1213 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1216 | bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
| 1217 | ScopedThreadListLock thread_list_lock; |
| 1218 | struct GetFrameVisitor : public Thread::StackVisitor { |
| 1219 | GetFrameVisitor(int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) |
| 1220 | : found(false) ,depth(0), desired_frame_number(desired_frame_number), pFrameId(pFrameId), pLoc(pLoc) { |
| 1221 | } |
| 1222 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1223 | // TODO: we'll need to skip callee-save frames too. |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1224 | if (!f.HasMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1225 | 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] | 1226 | } |
| 1227 | |
| 1228 | if (depth == desired_frame_number) { |
| 1229 | *pFrameId = reinterpret_cast<JDWP::FrameId>(f.GetSP()); |
| 1230 | |
| 1231 | Method* m = f.GetMethod(); |
| 1232 | Class* c = m->GetDeclaringClass(); |
| 1233 | |
| 1234 | pLoc->typeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1235 | pLoc->classId = gRegistry->Add(c); |
| 1236 | pLoc->methodId = ToMethodId(m); |
| 1237 | pLoc->idx = m->IsNative() ? -1 : m->ToDexPC(pc); |
| 1238 | |
| 1239 | found = true; |
| 1240 | } |
| 1241 | ++depth; |
| 1242 | } |
| 1243 | bool found; |
| 1244 | int depth; |
| 1245 | int desired_frame_number; |
| 1246 | JDWP::FrameId* pFrameId; |
| 1247 | JDWP::JdwpLocation* pLoc; |
| 1248 | }; |
| 1249 | GetFrameVisitor visitor(desired_frame_number, pFrameId, pLoc); |
| 1250 | visitor.desired_frame_number = desired_frame_number; |
| 1251 | DecodeThread(threadId)->WalkStack(&visitor); |
| 1252 | return visitor.found; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1256 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1259 | void Dbg::SuspendVM() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1260 | 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] | 1261 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1265 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1269 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1270 | ScopedThreadListLock thread_list_lock; |
| 1271 | Thread* thread = Thread::FromManagedThread(peer); |
| 1272 | if (thread == NULL) { |
| 1273 | LOG(WARNING) << "No such thread for suspend: " << peer; |
| 1274 | return; |
| 1275 | } |
| 1276 | Runtime::Current()->GetThreadList()->Suspend(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1280 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1281 | ScopedThreadListLock thread_list_lock; |
| 1282 | Thread* thread = Thread::FromManagedThread(peer); |
| 1283 | if (thread == NULL) { |
| 1284 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 1285 | return; |
| 1286 | } |
| 1287 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1291 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
| 1295 | UNIMPLEMENTED(FATAL); |
| 1296 | return false; |
| 1297 | } |
| 1298 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1299 | void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t expectedLen) { |
| 1300 | Method** sp = reinterpret_cast<Method**>(frameId); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1301 | Frame f; |
| 1302 | f.SetSP(sp); |
| 1303 | uint16_t reg = DemangleSlot(slot, f); |
| 1304 | Method* m = f.GetMethod(); |
| 1305 | |
| 1306 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1307 | uint32_t vmap_offset; |
| 1308 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
| 1309 | UNIMPLEMENTED(FATAL) << "don't know how to pull locals from callee save frames: " << vmap_offset; |
| 1310 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1311 | |
| 1312 | switch (tag) { |
| 1313 | case JDWP::JT_BOOLEAN: |
| 1314 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1315 | CHECK_EQ(expectedLen, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1316 | uint32_t intVal = f.GetVReg(m, reg); |
| 1317 | LOG(VERBOSE) << "get boolean local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1318 | JDWP::Set1(buf+1, intVal != 0); |
| 1319 | } |
| 1320 | break; |
| 1321 | case JDWP::JT_BYTE: |
| 1322 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1323 | CHECK_EQ(expectedLen, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1324 | uint32_t intVal = f.GetVReg(m, reg); |
| 1325 | LOG(VERBOSE) << "get byte local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1326 | JDWP::Set1(buf+1, intVal); |
| 1327 | } |
| 1328 | break; |
| 1329 | case JDWP::JT_SHORT: |
| 1330 | case JDWP::JT_CHAR: |
| 1331 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1332 | CHECK_EQ(expectedLen, 2U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1333 | uint32_t intVal = f.GetVReg(m, reg); |
| 1334 | LOG(VERBOSE) << "get short/char local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1335 | JDWP::Set2BE(buf+1, intVal); |
| 1336 | } |
| 1337 | break; |
| 1338 | case JDWP::JT_INT: |
| 1339 | case JDWP::JT_FLOAT: |
| 1340 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1341 | CHECK_EQ(expectedLen, 4U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1342 | uint32_t intVal = f.GetVReg(m, reg); |
| 1343 | LOG(VERBOSE) << "get int/float local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1344 | JDWP::Set4BE(buf+1, intVal); |
| 1345 | } |
| 1346 | break; |
| 1347 | case JDWP::JT_ARRAY: |
| 1348 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1349 | CHECK_EQ(expectedLen, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1350 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1351 | LOG(VERBOSE) << "get array local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1352 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1353 | LOG(FATAL) << "reg " << reg << " expected to hold array: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1354 | } |
| 1355 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1356 | } |
| 1357 | break; |
| 1358 | case JDWP::JT_OBJECT: |
| 1359 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1360 | CHECK_EQ(expectedLen, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1361 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1362 | LOG(VERBOSE) << "get object local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1363 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1364 | LOG(FATAL) << "reg " << reg << " expected to hold object: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1365 | } |
| 1366 | tag = TagFromObject(o); |
| 1367 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1368 | } |
| 1369 | break; |
| 1370 | case JDWP::JT_DOUBLE: |
| 1371 | case JDWP::JT_LONG: |
| 1372 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1373 | CHECK_EQ(expectedLen, 8U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1374 | uint32_t lo = f.GetVReg(m, reg); |
| 1375 | uint64_t hi = f.GetVReg(m, reg + 1); |
| 1376 | uint64_t longVal = (hi << 32) | lo; |
| 1377 | LOG(VERBOSE) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1378 | JDWP::Set8BE(buf+1, longVal); |
| 1379 | } |
| 1380 | break; |
| 1381 | default: |
| 1382 | LOG(FATAL) << "unknown tag " << tag; |
| 1383 | break; |
| 1384 | } |
| 1385 | |
| 1386 | // Prepend tag, which may have been updated. |
| 1387 | JDWP::Set1(buf, tag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1390 | void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint64_t value, size_t width) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1391 | UNIMPLEMENTED(FATAL); |
| 1392 | } |
| 1393 | |
| 1394 | void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) { |
| 1395 | UNIMPLEMENTED(FATAL); |
| 1396 | } |
| 1397 | |
| 1398 | void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) { |
| 1399 | UNIMPLEMENTED(FATAL); |
| 1400 | } |
| 1401 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1402 | void Dbg::PostClassPrepare(Class* c) { |
| 1403 | UNIMPLEMENTED(FATAL); |
| 1404 | } |
| 1405 | |
| 1406 | bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 1407 | UNIMPLEMENTED(FATAL); |
| 1408 | return false; |
| 1409 | } |
| 1410 | |
| 1411 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 1412 | UNIMPLEMENTED(FATAL); |
| 1413 | } |
| 1414 | |
| 1415 | bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) { |
| 1416 | UNIMPLEMENTED(FATAL); |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | void Dbg::UnconfigureStep(JDWP::ObjectId threadId) { |
| 1421 | UNIMPLEMENTED(FATAL); |
| 1422 | } |
| 1423 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1424 | 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* pExceptObj) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1425 | UNIMPLEMENTED(FATAL); |
| 1426 | return JDWP::ERR_NONE; |
| 1427 | } |
| 1428 | |
| 1429 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
| 1430 | UNIMPLEMENTED(FATAL); |
| 1431 | } |
| 1432 | |
| 1433 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
| 1434 | UNIMPLEMENTED(FATAL); |
| 1435 | } |
| 1436 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1437 | /* |
| 1438 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 1439 | * need to process each, accumulate the replies, and ship the whole thing |
| 1440 | * back. |
| 1441 | * |
| 1442 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 1443 | * and includes the chunk type/length, followed by the data. |
| 1444 | * |
| 1445 | * TODO: we currently assume that the request and reply include a single |
| 1446 | * chunk. If this becomes inconvenient we will need to adapt. |
| 1447 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1448 | 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] | 1449 | CHECK_GE(dataLen, 0); |
| 1450 | |
| 1451 | Thread* self = Thread::Current(); |
| 1452 | JNIEnv* env = self->GetJniEnv(); |
| 1453 | |
| 1454 | static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk"); |
| 1455 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 1456 | static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", |
| 1457 | "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); |
| 1458 | static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B"); |
| 1459 | static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I"); |
| 1460 | static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I"); |
| 1461 | static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I"); |
| 1462 | |
| 1463 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1464 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 1465 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1466 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 1467 | env->ExceptionClear(); |
| 1468 | return false; |
| 1469 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1470 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1471 | |
| 1472 | const int kChunkHdrLen = 8; |
| 1473 | |
| 1474 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1475 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1476 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 1477 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1478 | jint offset = kChunkHdrLen; |
| 1479 | if (offset + length > dataLen) { |
| 1480 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | // 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] | 1485 | 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] | 1486 | if (env->ExceptionCheck()) { |
| 1487 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 1488 | env->ExceptionDescribe(); |
| 1489 | env->ExceptionClear(); |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1493 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1494 | return false; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * Pull the pieces out of the chunk. We copy the results into a |
| 1499 | * newly-allocated buffer that the caller can free. We don't want to |
| 1500 | * continue using the Chunk object because nothing has a reference to it. |
| 1501 | * |
| 1502 | * We could avoid this by returning type/data/offset/length and having |
| 1503 | * the caller be aware of the object lifetime issues, but that |
| 1504 | * integrates the JDWP code more tightly into the VM, and doesn't work |
| 1505 | * if we have responses for multiple chunks. |
| 1506 | * |
| 1507 | * So we're pretty much stuck with copying data around multiple times. |
| 1508 | */ |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1509 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), data_fid))); |
| 1510 | length = env->GetIntField(chunk.get(), length_fid); |
| 1511 | offset = env->GetIntField(chunk.get(), offset_fid); |
| 1512 | type = env->GetIntField(chunk.get(), type_fid); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1513 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1514 | LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
| 1515 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1516 | return false; |
| 1517 | } |
| 1518 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1519 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1520 | if (offset + length > replyLength) { |
| 1521 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
| 1525 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 1526 | if (reply == NULL) { |
| 1527 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 1528 | return false; |
| 1529 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1530 | JDWP::Set4BE(reply + 0, type); |
| 1531 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1532 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1533 | |
| 1534 | *pReplyBuf = reply; |
| 1535 | *pReplyLen = length + kChunkHdrLen; |
| 1536 | |
| 1537 | LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length); |
| 1538 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1541 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1542 | LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
| 1543 | |
| 1544 | Thread* self = Thread::Current(); |
| 1545 | if (self->GetState() != Thread::kRunnable) { |
| 1546 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 1547 | /* try anyway? */ |
| 1548 | } |
| 1549 | |
| 1550 | JNIEnv* env = self->GetJniEnv(); |
| 1551 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 1552 | static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V"); |
| 1553 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| 1554 | env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event); |
| 1555 | if (env->ExceptionCheck()) { |
| 1556 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 1557 | env->ExceptionDescribe(); |
| 1558 | env->ExceptionClear(); |
| 1559 | } |
| 1560 | } |
| 1561 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1562 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1563 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1567 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1568 | gDdmThreadNotification = false; |
| 1569 | } |
| 1570 | |
| 1571 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1572 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1573 | * |
| 1574 | * Because we broadcast the full set of threads when the notifications are |
| 1575 | * first enabled, it's possible for "thread" to be actively executing. |
| 1576 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1577 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1578 | if (!gDdmThreadNotification) { |
| 1579 | return; |
| 1580 | } |
| 1581 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1582 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1583 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1584 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1585 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1586 | } else { |
| 1587 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
| 1588 | SirtRef<String> name(t->GetName()); |
| 1589 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 1590 | const jchar* chars = name->GetCharArray()->GetData(); |
| 1591 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1592 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1593 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
| 1594 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1595 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 1596 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1600 | static void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1601 | Dbg::DdmSendThreadNotification(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 1605 | // We lock the thread list to avoid sending duplicate events or missing |
| 1606 | // a thread change. We should be okay holding this lock while sending |
| 1607 | // 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] | 1608 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1609 | |
| 1610 | gDdmThreadNotification = enable; |
| 1611 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1612 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1616 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1617 | if (gDebuggerActive) { |
| 1618 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1619 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1620 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1621 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1625 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1629 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1632 | 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] | 1633 | CHECK(buf != NULL); |
| 1634 | iovec vec[1]; |
| 1635 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 1636 | vec[0].iov_len = byte_count; |
| 1637 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1640 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 1641 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 1642 | } |
| 1643 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1644 | void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iovcnt) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 1645 | if (gJdwpState == NULL) { |
| 1646 | LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type; |
| 1647 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1648 | gJdwpState->DdmSendChunkV(type, iov, iovcnt); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 1649 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1652 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 1653 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 1654 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1655 | return true; |
| 1656 | } |
| 1657 | |
| 1658 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 1659 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 1660 | return false; |
| 1661 | } |
| 1662 | |
| 1663 | gDdmHpifWhen = when; |
| 1664 | return true; |
| 1665 | } |
| 1666 | |
| 1667 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 1668 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 1669 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 1670 | return false; |
| 1671 | } |
| 1672 | |
| 1673 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 1674 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 1675 | return false; |
| 1676 | } |
| 1677 | |
| 1678 | if (native) { |
| 1679 | gDdmNhsgWhen = when; |
| 1680 | gDdmNhsgWhat = what; |
| 1681 | } else { |
| 1682 | gDdmHpsgWhen = when; |
| 1683 | gDdmHpsgWhat = what; |
| 1684 | } |
| 1685 | return true; |
| 1686 | } |
| 1687 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 1688 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 1689 | // If there's a one-shot 'when', reset it. |
| 1690 | if (reason == gDdmHpifWhen) { |
| 1691 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 1692 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * Chunk HPIF (client --> server) |
| 1698 | * |
| 1699 | * Heap Info. General information about the heap, |
| 1700 | * suitable for a summary display. |
| 1701 | * |
| 1702 | * [u4]: number of heaps |
| 1703 | * |
| 1704 | * For each heap: |
| 1705 | * [u4]: heap ID |
| 1706 | * [u8]: timestamp in ms since Unix epoch |
| 1707 | * [u1]: capture reason (same as 'when' value from server) |
| 1708 | * [u4]: max heap size in bytes (-Xmx) |
| 1709 | * [u4]: current heap size in bytes |
| 1710 | * [u4]: current number of bytes allocated |
| 1711 | * [u4]: current number of objects allocated |
| 1712 | */ |
| 1713 | uint8_t heap_count = 1; |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1714 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1715 | JDWP::Append4BE(bytes, heap_count); |
| 1716 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| 1717 | JDWP::Append8BE(bytes, MilliTime()); |
| 1718 | JDWP::Append1BE(bytes, reason); |
| 1719 | JDWP::Append4BE(bytes, Heap::GetMaxMemory()); // Max allowed heap size in bytes. |
| 1720 | JDWP::Append4BE(bytes, Heap::GetTotalMemory()); // Current heap size in bytes. |
| 1721 | JDWP::Append4BE(bytes, Heap::GetBytesAllocated()); |
| 1722 | JDWP::Append4BE(bytes, Heap::GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1723 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 1724 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1727 | enum HpsgSolidity { |
| 1728 | SOLIDITY_FREE = 0, |
| 1729 | SOLIDITY_HARD = 1, |
| 1730 | SOLIDITY_SOFT = 2, |
| 1731 | SOLIDITY_WEAK = 3, |
| 1732 | SOLIDITY_PHANTOM = 4, |
| 1733 | SOLIDITY_FINALIZABLE = 5, |
| 1734 | SOLIDITY_SWEEP = 6, |
| 1735 | }; |
| 1736 | |
| 1737 | enum HpsgKind { |
| 1738 | KIND_OBJECT = 0, |
| 1739 | KIND_CLASS_OBJECT = 1, |
| 1740 | KIND_ARRAY_1 = 2, |
| 1741 | KIND_ARRAY_2 = 3, |
| 1742 | KIND_ARRAY_4 = 4, |
| 1743 | KIND_ARRAY_8 = 5, |
| 1744 | KIND_UNKNOWN = 6, |
| 1745 | KIND_NATIVE = 7, |
| 1746 | }; |
| 1747 | |
| 1748 | #define HPSG_PARTIAL (1<<7) |
| 1749 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 1750 | |
| 1751 | struct HeapChunkContext { |
| 1752 | std::vector<uint8_t> buf; |
| 1753 | uint8_t* p; |
| 1754 | uint8_t* pieceLenField; |
| 1755 | size_t totalAllocationUnits; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1756 | uint32_t type; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1757 | bool merge; |
| 1758 | bool needHeader; |
| 1759 | |
| 1760 | // Maximum chunk size. Obtain this from the formula: |
| 1761 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 1762 | HeapChunkContext(bool merge, bool native) |
| 1763 | : buf(16384 - 16), |
| 1764 | type(0), |
| 1765 | merge(merge) { |
| 1766 | Reset(); |
| 1767 | if (native) { |
| 1768 | type = CHUNK_TYPE("NHSG"); |
| 1769 | } else { |
| 1770 | type = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | ~HeapChunkContext() { |
| 1775 | if (p > &buf[0]) { |
| 1776 | Flush(); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | void EnsureHeader(const void* chunk_ptr) { |
| 1781 | if (!needHeader) { |
| 1782 | return; |
| 1783 | } |
| 1784 | |
| 1785 | // Start a new HPSx chunk. |
| 1786 | JDWP::Write4BE(&p, 1); // Heap id (bogus; we only have one heap). |
| 1787 | JDWP::Write1BE(&p, 8); // Size of allocation unit, in bytes. |
| 1788 | |
| 1789 | JDWP::Write4BE(&p, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 1790 | JDWP::Write4BE(&p, 0); // offset of this piece (relative to the virtual address). |
| 1791 | // [u4]: length of piece, in allocation units |
| 1792 | // We won't know this until we're done, so save the offset and stuff in a dummy value. |
| 1793 | pieceLenField = p; |
| 1794 | JDWP::Write4BE(&p, 0x55555555); |
| 1795 | needHeader = false; |
| 1796 | } |
| 1797 | |
| 1798 | void Flush() { |
| 1799 | // Patch the "length of piece" field. |
| 1800 | CHECK_LE(&buf[0], pieceLenField); |
| 1801 | CHECK_LE(pieceLenField, p); |
| 1802 | JDWP::Set4BE(pieceLenField, totalAllocationUnits); |
| 1803 | |
| 1804 | Dbg::DdmSendChunk(type, p - &buf[0], &buf[0]); |
| 1805 | Reset(); |
| 1806 | } |
| 1807 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1808 | static void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len, void* arg) { |
| 1809 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(chunk_ptr, chunk_len, user_ptr, user_len); |
| 1810 | } |
| 1811 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1812 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1813 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 1814 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1815 | void Reset() { |
| 1816 | p = &buf[0]; |
| 1817 | totalAllocationUnits = 0; |
| 1818 | needHeader = true; |
| 1819 | pieceLenField = NULL; |
| 1820 | } |
| 1821 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1822 | void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len) { |
| 1823 | CHECK_EQ((chunk_len & (ALLOCATION_UNIT_SIZE-1)), 0U); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1824 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1825 | /* Make sure there's enough room left in the buffer. |
| 1826 | * We need to use two bytes for every fractional 256 |
| 1827 | * allocation units used by the chunk. |
| 1828 | */ |
| 1829 | { |
| 1830 | size_t needed = (((chunk_len/ALLOCATION_UNIT_SIZE + 255) / 256) * 2); |
| 1831 | size_t bytesLeft = buf.size() - (size_t)(p - &buf[0]); |
| 1832 | if (bytesLeft < needed) { |
| 1833 | Flush(); |
| 1834 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1835 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1836 | bytesLeft = buf.size() - (size_t)(p - &buf[0]); |
| 1837 | if (bytesLeft < needed) { |
| 1838 | LOG(WARNING) << "chunk is too big to transmit (chunk_len=" << chunk_len << ", " << needed << " bytes)"; |
| 1839 | return; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | // OLD-TODO: notice when there's a gap and start a new heap, or at least a new range. |
| 1844 | EnsureHeader(chunk_ptr); |
| 1845 | |
| 1846 | // Determine the type of this chunk. |
| 1847 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 1848 | // If it's the same, we should combine them. |
| 1849 | uint8_t state = ExamineObject(reinterpret_cast<const Object*>(user_ptr), (type == CHUNK_TYPE("NHSG"))); |
| 1850 | |
| 1851 | // Write out the chunk description. |
| 1852 | chunk_len /= ALLOCATION_UNIT_SIZE; // convert to allocation units |
| 1853 | totalAllocationUnits += chunk_len; |
| 1854 | while (chunk_len > 256) { |
| 1855 | *p++ = state | HPSG_PARTIAL; |
| 1856 | *p++ = 255; // length - 1 |
| 1857 | chunk_len -= 256; |
| 1858 | } |
| 1859 | *p++ = state; |
| 1860 | *p++ = chunk_len - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1863 | uint8_t ExamineObject(const Object* o, bool is_native_heap) { |
| 1864 | if (o == NULL) { |
| 1865 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 1866 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1867 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1868 | // It's an allocated chunk. Figure out what it is. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1869 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1870 | // If we're looking at the native heap, we'll just return |
| 1871 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
| 1872 | if (is_native_heap || !Heap::IsLiveObjectLocked(o)) { |
| 1873 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 1874 | } |
| 1875 | |
| 1876 | Class* c = o->GetClass(); |
| 1877 | if (c == NULL) { |
| 1878 | // The object was probably just created but hasn't been initialized yet. |
| 1879 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1880 | } |
| 1881 | |
| 1882 | if (!Heap::IsHeapAddress(c)) { |
| 1883 | LOG(WARNING) << "invalid class for managed heap object: " << o << " " << c; |
| 1884 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 1885 | } |
| 1886 | |
| 1887 | if (c->IsClassClass()) { |
| 1888 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 1889 | } |
| 1890 | |
| 1891 | if (c->IsArrayClass()) { |
| 1892 | if (o->IsObjectArray()) { |
| 1893 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1894 | } |
| 1895 | switch (c->GetComponentSize()) { |
| 1896 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 1897 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 1898 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1899 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 1900 | } |
| 1901 | } |
| 1902 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1903 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1904 | } |
| 1905 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1906 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 1907 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1908 | |
| 1909 | void Dbg::DdmSendHeapSegments(bool native) { |
| 1910 | Dbg::HpsgWhen when; |
| 1911 | Dbg::HpsgWhat what; |
| 1912 | if (!native) { |
| 1913 | when = gDdmHpsgWhen; |
| 1914 | what = gDdmHpsgWhat; |
| 1915 | } else { |
| 1916 | when = gDdmNhsgWhen; |
| 1917 | what = gDdmNhsgWhat; |
| 1918 | } |
| 1919 | if (when == HPSG_WHEN_NEVER) { |
| 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | // Figure out what kind of chunks we'll be sending. |
| 1924 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 1925 | |
| 1926 | // First, send a heap start chunk. |
| 1927 | uint8_t heap_id[4]; |
| 1928 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 1929 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 1930 | |
| 1931 | // Send a series of heap segment chunks. |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1932 | HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 1933 | if (native) { |
| 1934 | dlmalloc_walk_heap(HeapChunkContext::HeapChunkCallback, &context); |
| 1935 | } else { |
| 1936 | Heap::WalkHeap(HeapChunkContext::HeapChunkCallback, &context); |
| 1937 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1938 | |
| 1939 | // Finally, send a heap end chunk. |
| 1940 | 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] | 1941 | } |
| 1942 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1943 | void Dbg::SetAllocTrackingEnabled(bool enabled) { |
| 1944 | MutexLock mu(gAllocTrackerLock); |
| 1945 | if (enabled) { |
| 1946 | if (recent_allocation_records_ == NULL) { |
| 1947 | LOG(INFO) << "Enabling alloc tracker (" << kNumAllocRecords << " entries, " |
| 1948 | << kMaxAllocRecordStackDepth << " frames --> " |
| 1949 | << (sizeof(AllocRecord) * kNumAllocRecords) << " bytes)"; |
| 1950 | gAllocRecordHead = gAllocRecordCount = 0; |
| 1951 | recent_allocation_records_ = new AllocRecord[kNumAllocRecords]; |
| 1952 | CHECK(recent_allocation_records_ != NULL); |
| 1953 | } |
| 1954 | } else { |
| 1955 | delete[] recent_allocation_records_; |
| 1956 | recent_allocation_records_ = NULL; |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | struct AllocRecordStackVisitor : public Thread::StackVisitor { |
| 1961 | AllocRecordStackVisitor(AllocRecord* record) : record(record), depth(0) { |
| 1962 | } |
| 1963 | |
| 1964 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
| 1965 | if (depth >= kMaxAllocRecordStackDepth) { |
| 1966 | return; |
| 1967 | } |
| 1968 | Method* m = f.GetMethod(); |
| 1969 | if (m == NULL || m->IsCalleeSaveMethod()) { |
| 1970 | return; |
| 1971 | } |
| 1972 | record->stack[depth].method = m; |
| 1973 | record->stack[depth].raw_pc = pc; |
| 1974 | ++depth; |
| 1975 | } |
| 1976 | |
| 1977 | ~AllocRecordStackVisitor() { |
| 1978 | // Clear out any unused stack trace elements. |
| 1979 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
| 1980 | record->stack[depth].method = NULL; |
| 1981 | record->stack[depth].raw_pc = 0; |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | AllocRecord* record; |
| 1986 | size_t depth; |
| 1987 | }; |
| 1988 | |
| 1989 | void Dbg::RecordAllocation(Class* type, size_t byte_count) { |
| 1990 | Thread* self = Thread::Current(); |
| 1991 | CHECK(self != NULL); |
| 1992 | |
| 1993 | MutexLock mu(gAllocTrackerLock); |
| 1994 | if (recent_allocation_records_ == NULL) { |
| 1995 | return; |
| 1996 | } |
| 1997 | |
| 1998 | // Advance and clip. |
| 1999 | if (++gAllocRecordHead == kNumAllocRecords) { |
| 2000 | gAllocRecordHead = 0; |
| 2001 | } |
| 2002 | |
| 2003 | // Fill in the basics. |
| 2004 | AllocRecord* record = &recent_allocation_records_[gAllocRecordHead]; |
| 2005 | record->type = type; |
| 2006 | record->byte_count = byte_count; |
| 2007 | record->thin_lock_id = self->GetThinLockId(); |
| 2008 | |
| 2009 | // Fill in the stack trace. |
| 2010 | AllocRecordStackVisitor visitor(record); |
| 2011 | self->WalkStack(&visitor); |
| 2012 | |
| 2013 | if (gAllocRecordCount < kNumAllocRecords) { |
| 2014 | ++gAllocRecordCount; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * Return the index of the head element. |
| 2020 | * |
| 2021 | * We point at the most-recently-written record, so if allocRecordCount is 1 |
| 2022 | * we want to use the current element. Take "head+1" and subtract count |
| 2023 | * from it. |
| 2024 | * |
| 2025 | * We need to handle underflow in our circular buffer, so we add |
| 2026 | * kNumAllocRecords and then mask it back down. |
| 2027 | */ |
| 2028 | inline static int headIndex() { |
| 2029 | return (gAllocRecordHead+1 + kNumAllocRecords - gAllocRecordCount) & (kNumAllocRecords-1); |
| 2030 | } |
| 2031 | |
| 2032 | void Dbg::DumpRecentAllocations() { |
| 2033 | MutexLock mu(gAllocTrackerLock); |
| 2034 | if (recent_allocation_records_ == NULL) { |
| 2035 | LOG(INFO) << "Not recording tracked allocations"; |
| 2036 | return; |
| 2037 | } |
| 2038 | |
| 2039 | // "i" is the head of the list. We want to start at the end of the |
| 2040 | // list and move forward to the tail. |
| 2041 | size_t i = headIndex(); |
| 2042 | size_t count = gAllocRecordCount; |
| 2043 | |
| 2044 | LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")"; |
| 2045 | while (count--) { |
| 2046 | AllocRecord* record = &recent_allocation_records_[i]; |
| 2047 | |
| 2048 | LOG(INFO) << StringPrintf(" T=%-2d %6d ", record->thin_lock_id, record->byte_count) |
| 2049 | << PrettyClass(record->type); |
| 2050 | |
| 2051 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
| 2052 | const Method* m = record->stack[stack_frame].method; |
| 2053 | if (m == NULL) { |
| 2054 | break; |
| 2055 | } |
| 2056 | LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber(); |
| 2057 | } |
| 2058 | |
| 2059 | // pause periodically to help logcat catch up |
| 2060 | if ((count % 5) == 0) { |
| 2061 | usleep(40000); |
| 2062 | } |
| 2063 | |
| 2064 | i = (i + 1) & (kNumAllocRecords-1); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | class StringTable { |
| 2069 | public: |
| 2070 | StringTable() { |
| 2071 | } |
| 2072 | |
| 2073 | void Add(const String* s) { |
| 2074 | table_.insert(s); |
| 2075 | } |
| 2076 | |
| 2077 | size_t IndexOf(const String* s) { |
| 2078 | return std::distance(table_.begin(), table_.find(s)); |
| 2079 | } |
| 2080 | |
| 2081 | size_t Size() { |
| 2082 | return table_.size(); |
| 2083 | } |
| 2084 | |
| 2085 | void WriteTo(std::vector<uint8_t>& bytes) { |
| 2086 | typedef std::set<const String*>::const_iterator It; // TODO: C++0x auto |
| 2087 | for (It it = table_.begin(); it != table_.end(); ++it) { |
| 2088 | const String* s = *it; |
| 2089 | JDWP::AppendUtf16BE(bytes, s->GetCharArray()->GetData(), s->GetLength()); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | private: |
| 2094 | std::set<const String*> table_; |
| 2095 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 2096 | }; |
| 2097 | |
| 2098 | /* |
| 2099 | * The data we send to DDMS contains everything we have recorded. |
| 2100 | * |
| 2101 | * Message header (all values big-endian): |
| 2102 | * (1b) message header len (to allow future expansion); includes itself |
| 2103 | * (1b) entry header len |
| 2104 | * (1b) stack frame len |
| 2105 | * (2b) number of entries |
| 2106 | * (4b) offset to string table from start of message |
| 2107 | * (2b) number of class name strings |
| 2108 | * (2b) number of method name strings |
| 2109 | * (2b) number of source file name strings |
| 2110 | * For each entry: |
| 2111 | * (4b) total allocation size |
| 2112 | * (2b) threadId |
| 2113 | * (2b) allocated object's class name index |
| 2114 | * (1b) stack depth |
| 2115 | * For each stack frame: |
| 2116 | * (2b) method's class name |
| 2117 | * (2b) method name |
| 2118 | * (2b) method source file |
| 2119 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 2120 | * (xb) class name strings |
| 2121 | * (xb) method name strings |
| 2122 | * (xb) source file strings |
| 2123 | * |
| 2124 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 2125 | * followed by UTF-16 data. |
| 2126 | * |
| 2127 | * We send up 16-bit unsigned indexes into string tables. In theory there |
| 2128 | * can be (kMaxAllocRecordStackDepth * kNumAllocRecords) unique strings in |
| 2129 | * each table, but in practice there should be far fewer. |
| 2130 | * |
| 2131 | * The chief reason for using a string table here is to keep the size of |
| 2132 | * the DDMS message to a minimum. This is partly to make the protocol |
| 2133 | * efficient, but also because we have to form the whole thing up all at |
| 2134 | * once in a memory buffer. |
| 2135 | * |
| 2136 | * We use separate string tables for class names, method names, and source |
| 2137 | * files to keep the indexes small. There will generally be no overlap |
| 2138 | * between the contents of these tables. |
| 2139 | */ |
| 2140 | jbyteArray Dbg::GetRecentAllocations() { |
| 2141 | if (false) { |
| 2142 | DumpRecentAllocations(); |
| 2143 | } |
| 2144 | |
| 2145 | MutexLock mu(gAllocTrackerLock); |
| 2146 | |
| 2147 | /* |
| 2148 | * Part 1: generate string tables. |
| 2149 | */ |
| 2150 | StringTable class_names; |
| 2151 | StringTable method_names; |
| 2152 | StringTable filenames; |
| 2153 | |
| 2154 | int count = gAllocRecordCount; |
| 2155 | int idx = headIndex(); |
| 2156 | while (count--) { |
| 2157 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2158 | |
| 2159 | class_names.Add(record->type->GetDescriptor()); |
| 2160 | |
| 2161 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
| 2162 | const Method* m = record->stack[i].method; |
| 2163 | if (m != NULL) { |
| 2164 | class_names.Add(m->GetDeclaringClass()->GetDescriptor()); |
| 2165 | method_names.Add(m->GetName()); |
| 2166 | filenames.Add(m->GetDeclaringClass()->GetSourceFile()); |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2171 | } |
| 2172 | |
| 2173 | LOG(INFO) << "allocation records: " << gAllocRecordCount; |
| 2174 | |
| 2175 | /* |
| 2176 | * Part 2: allocate a buffer and generate the output. |
| 2177 | */ |
| 2178 | std::vector<uint8_t> bytes; |
| 2179 | |
| 2180 | // (1b) message header len (to allow future expansion); includes itself |
| 2181 | // (1b) entry header len |
| 2182 | // (1b) stack frame len |
| 2183 | const int kMessageHeaderLen = 15; |
| 2184 | const int kEntryHeaderLen = 9; |
| 2185 | const int kStackFrameLen = 8; |
| 2186 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 2187 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 2188 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 2189 | |
| 2190 | // (2b) number of entries |
| 2191 | // (4b) offset to string table from start of message |
| 2192 | // (2b) number of class name strings |
| 2193 | // (2b) number of method name strings |
| 2194 | // (2b) number of source file name strings |
| 2195 | JDWP::Append2BE(bytes, gAllocRecordCount); |
| 2196 | size_t string_table_offset = bytes.size(); |
| 2197 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| 2198 | JDWP::Append2BE(bytes, class_names.Size()); |
| 2199 | JDWP::Append2BE(bytes, method_names.Size()); |
| 2200 | JDWP::Append2BE(bytes, filenames.Size()); |
| 2201 | |
| 2202 | count = gAllocRecordCount; |
| 2203 | idx = headIndex(); |
| 2204 | while (count--) { |
| 2205 | // For each entry: |
| 2206 | // (4b) total allocation size |
| 2207 | // (2b) thread id |
| 2208 | // (2b) allocated object's class name index |
| 2209 | // (1b) stack depth |
| 2210 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2211 | size_t stack_depth = record->GetDepth(); |
| 2212 | JDWP::Append4BE(bytes, record->byte_count); |
| 2213 | JDWP::Append2BE(bytes, record->thin_lock_id); |
| 2214 | JDWP::Append2BE(bytes, class_names.IndexOf(record->type->GetDescriptor())); |
| 2215 | JDWP::Append1BE(bytes, stack_depth); |
| 2216 | |
| 2217 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 2218 | // For each stack frame: |
| 2219 | // (2b) method's class name |
| 2220 | // (2b) method name |
| 2221 | // (2b) method source file |
| 2222 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 2223 | const Method* m = record->stack[stack_frame].method; |
| 2224 | JDWP::Append2BE(bytes, class_names.IndexOf(m->GetDeclaringClass()->GetDescriptor())); |
| 2225 | JDWP::Append2BE(bytes, method_names.IndexOf(m->GetName())); |
| 2226 | JDWP::Append2BE(bytes, filenames.IndexOf(m->GetDeclaringClass()->GetSourceFile())); |
| 2227 | JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber()); |
| 2228 | } |
| 2229 | |
| 2230 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2231 | } |
| 2232 | |
| 2233 | // (xb) class name strings |
| 2234 | // (xb) method name strings |
| 2235 | // (xb) source file strings |
| 2236 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 2237 | class_names.WriteTo(bytes); |
| 2238 | method_names.WriteTo(bytes); |
| 2239 | filenames.WriteTo(bytes); |
| 2240 | |
| 2241 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 2242 | jbyteArray result = env->NewByteArray(bytes.size()); |
| 2243 | if (result != NULL) { |
| 2244 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 2245 | } |
| 2246 | return result; |
| 2247 | } |
| 2248 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2249 | } // namespace art |