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) { |
| 474 | UNIMPLEMENTED(FATAL); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | bool Dbg::IsInterface(JDWP::RefTypeId id) { |
| 479 | UNIMPLEMENTED(FATAL); |
| 480 | return false; |
| 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 | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 515 | void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) { |
| 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 | |
| 534 | bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) { |
| 535 | UNIMPLEMENTED(FATAL); |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 540 | Object* o = gRegistry->Get<Object*>(objectId); |
| 541 | if (o->GetClass()->IsArrayClass()) { |
| 542 | *pRefTypeTag = JDWP::TT_ARRAY; |
| 543 | } else if (o->GetClass()->IsInterface()) { |
| 544 | *pRefTypeTag = JDWP::TT_INTERFACE; |
| 545 | } else { |
| 546 | *pRefTypeTag = JDWP::TT_CLASS; |
| 547 | } |
| 548 | *pRefTypeId = gRegistry->Add(o->GetClass()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) { |
| 552 | UNIMPLEMENTED(FATAL); |
| 553 | return 0; |
| 554 | } |
| 555 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 556 | std::string Dbg::GetSignature(JDWP::RefTypeId refTypeId) { |
| 557 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 558 | CHECK(c != NULL); |
| 559 | return c->GetDescriptor()->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 562 | bool Dbg::GetSourceFile(JDWP::RefTypeId refTypeId, std::string& result) { |
| 563 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 564 | CHECK(c != NULL); |
| 565 | |
| 566 | String* source_file = c->GetSourceFile(); |
| 567 | if (source_file == NULL) { |
| 568 | return false; |
| 569 | } |
| 570 | result = source_file->ToModifiedUtf8(); |
| 571 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) { |
| 575 | UNIMPLEMENTED(FATAL); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 580 | Object* o = gRegistry->Get<Object*>(objectId); |
| 581 | return TagFromObject(o); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 584 | size_t Dbg::GetTagWidth(int tag) { |
| 585 | switch (tag) { |
| 586 | case JDWP::JT_VOID: |
| 587 | return 0; |
| 588 | case JDWP::JT_BYTE: |
| 589 | case JDWP::JT_BOOLEAN: |
| 590 | return 1; |
| 591 | case JDWP::JT_CHAR: |
| 592 | case JDWP::JT_SHORT: |
| 593 | return 2; |
| 594 | case JDWP::JT_FLOAT: |
| 595 | case JDWP::JT_INT: |
| 596 | return 4; |
| 597 | case JDWP::JT_ARRAY: |
| 598 | case JDWP::JT_OBJECT: |
| 599 | case JDWP::JT_STRING: |
| 600 | case JDWP::JT_THREAD: |
| 601 | case JDWP::JT_THREAD_GROUP: |
| 602 | case JDWP::JT_CLASS_LOADER: |
| 603 | case JDWP::JT_CLASS_OBJECT: |
| 604 | return sizeof(JDWP::ObjectId); |
| 605 | case JDWP::JT_DOUBLE: |
| 606 | case JDWP::JT_LONG: |
| 607 | return 8; |
| 608 | default: |
| 609 | LOG(FATAL) << "unknown tag " << tag; |
| 610 | return -1; |
| 611 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | int Dbg::GetArrayLength(JDWP::ObjectId arrayId) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 615 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 616 | Array* a = o->AsArray(); |
| 617 | return a->GetLength(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 621 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 622 | Array* a = o->AsArray(); |
| 623 | std::string descriptor(a->GetClass()->GetDescriptor()->ToModifiedUtf8()); |
| 624 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 625 | if (!IsPrimitiveTag(tag)) { |
| 626 | tag = TagFromClass(a->GetClass()->GetComponentType()); |
| 627 | } |
| 628 | return tag; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 631 | bool Dbg::OutputArray(JDWP::ObjectId arrayId, int offset, int count, JDWP::ExpandBuf* pReply) { |
| 632 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 633 | Array* a = o->AsArray(); |
| 634 | |
| 635 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 636 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | std::string descriptor(a->GetClass()->GetDescriptor()->ToModifiedUtf8()); |
| 641 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 642 | |
| 643 | if (IsPrimitiveTag(tag)) { |
| 644 | size_t width = GetTagWidth(tag); |
| 645 | const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData()); |
| 646 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 647 | if (width == 8) { |
| 648 | const uint64_t* src8 = reinterpret_cast<const uint64_t*>(src); |
| 649 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 650 | } else if (width == 4) { |
| 651 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 652 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 653 | } else if (width == 2) { |
| 654 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 655 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 656 | } else { |
| 657 | memcpy(dst, &src[offset * width], count * width); |
| 658 | } |
| 659 | } else { |
| 660 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 661 | for (int i = 0; i < count; ++i) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame^] | 662 | Object* element = oa->Get(offset + i); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 663 | JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag; |
| 664 | expandBufAdd1(pReply, specific_tag); |
| 665 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame^] | 672 | bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int offset, int count, const uint8_t* src) { |
| 673 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 674 | Array* a = o->AsArray(); |
| 675 | |
| 676 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 677 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | std::string descriptor(a->GetClass()->GetDescriptor()->ToModifiedUtf8()); |
| 682 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 683 | |
| 684 | if (IsPrimitiveTag(tag)) { |
| 685 | size_t width = GetTagWidth(tag); |
| 686 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData())[offset * width]); |
| 687 | if (width == 8) { |
| 688 | for (int i = 0; i < count; ++i) { |
| 689 | // Handle potentially non-aligned memory access one byte at a time for ARM's benefit. |
| 690 | uint64_t value; |
| 691 | for (size_t j = 0; j < sizeof(uint64_t); ++j) reinterpret_cast<uint8_t*>(&value)[j] = src[j]; |
| 692 | src += sizeof(uint64_t); |
| 693 | JDWP::Write8BE(&dst, value); |
| 694 | } |
| 695 | } else if (width == 4) { |
| 696 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 697 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[i]); |
| 698 | } else if (width == 2) { |
| 699 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 700 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[i]); |
| 701 | } else { |
| 702 | memcpy(&dst[offset * width], src, count * width); |
| 703 | } |
| 704 | } else { |
| 705 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 706 | for (int i = 0; i < count; ++i) { |
| 707 | JDWP::ObjectId id = JDWP::ReadObjectId(&src); |
| 708 | oa->Set(offset + i, gRegistry->Get<Object*>(id)); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | JDWP::ObjectId Dbg::CreateString(const char* str) { |
| 716 | UNIMPLEMENTED(FATAL); |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) { |
| 721 | UNIMPLEMENTED(FATAL); |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) { |
| 726 | UNIMPLEMENTED(FATAL); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
| 731 | UNIMPLEMENTED(FATAL); |
| 732 | return false; |
| 733 | } |
| 734 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 735 | JDWP::FieldId ToFieldId(Field* f) { |
| 736 | #ifdef MOVING_GARBAGE_COLLECTOR |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 737 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 738 | #else |
| 739 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| 740 | #endif |
| 741 | } |
| 742 | |
| 743 | JDWP::MethodId ToMethodId(Method* m) { |
| 744 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 745 | UNIMPLEMENTED(FATAL); |
| 746 | #else |
| 747 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
| 748 | #endif |
| 749 | } |
| 750 | |
| 751 | Method* FromMethodId(JDWP::MethodId mid) { |
| 752 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 753 | UNIMPLEMENTED(FATAL); |
| 754 | #else |
| 755 | return reinterpret_cast<Method*>(static_cast<uintptr_t>(mid)); |
| 756 | #endif |
| 757 | } |
| 758 | |
| 759 | std::string Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId) { |
| 760 | return FromMethodId(methodId)->GetName()->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 763 | /* |
| 764 | * Augment the access flags for synthetic methods and fields by setting |
| 765 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 766 | * flags not specified by the Java programming language. |
| 767 | */ |
| 768 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 769 | accessFlags &= kAccJavaFlagsMask; |
| 770 | if ((accessFlags & kAccSynthetic) != 0) { |
| 771 | accessFlags |= 0xf0000000; |
| 772 | } |
| 773 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 776 | static const uint16_t kEclipseWorkaroundSlot = 1000; |
| 777 | |
| 778 | /* |
| 779 | * Eclipse appears to expect that the "this" reference is in slot zero. |
| 780 | * If it's not, the "variables" display will show two copies of "this", |
| 781 | * possibly because it gets "this" from SF.ThisObject and then displays |
| 782 | * all locals with nonzero slot numbers. |
| 783 | * |
| 784 | * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On |
| 785 | * SF.GetValues / SF.SetValues we map them back. |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 786 | * |
| 787 | * TODO: jdb uses the value to determine whether a variable is a local or an argument, |
| 788 | * by checking whether it's less than the number of arguments. To make that work, we'd |
| 789 | * 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] | 790 | */ |
| 791 | static uint16_t MangleSlot(uint16_t slot, const char* name) { |
| 792 | uint16_t newSlot = slot; |
| 793 | if (strcmp(name, "this") == 0) { |
| 794 | newSlot = 0; |
| 795 | } else if (slot == 0) { |
| 796 | newSlot = kEclipseWorkaroundSlot; |
| 797 | } |
| 798 | return newSlot; |
| 799 | } |
| 800 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 801 | static uint16_t DemangleSlot(uint16_t slot, Frame& f) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 802 | if (slot == kEclipseWorkaroundSlot) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 803 | return 0; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 804 | } else if (slot == 0) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 805 | Method* m = f.GetMethod(); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 806 | return m->NumRegisters() - m->NumIns(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 807 | } |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 808 | return slot; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 811 | void Dbg::OutputDeclaredFields(JDWP::RefTypeId refTypeId, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 812 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 813 | CHECK(c != NULL); |
| 814 | |
| 815 | size_t instance_field_count = c->NumInstanceFields(); |
| 816 | size_t static_field_count = c->NumStaticFields(); |
| 817 | |
| 818 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 819 | |
| 820 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
| 821 | Field* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count); |
| 822 | |
| 823 | expandBufAddFieldId(pReply, ToFieldId(f)); |
| 824 | expandBufAddUtf8String(pReply, f->GetName()->ToModifiedUtf8().c_str()); |
| 825 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 826 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 827 | static const char genericSignature[1] = ""; |
| 828 | expandBufAddUtf8String(pReply, genericSignature); |
| 829 | } |
| 830 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 831 | } |
| 832 | } |
| 833 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 834 | void Dbg::OutputDeclaredMethods(JDWP::RefTypeId refTypeId, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 835 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 836 | CHECK(c != NULL); |
| 837 | |
| 838 | size_t direct_method_count = c->NumDirectMethods(); |
| 839 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 840 | |
| 841 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 842 | |
| 843 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
| 844 | Method* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count); |
| 845 | |
| 846 | expandBufAddMethodId(pReply, ToMethodId(m)); |
| 847 | expandBufAddUtf8String(pReply, m->GetName()->ToModifiedUtf8().c_str()); |
| 848 | expandBufAddUtf8String(pReply, m->GetSignature()->ToModifiedUtf8().c_str()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 849 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 850 | static const char genericSignature[1] = ""; |
| 851 | expandBufAddUtf8String(pReply, genericSignature); |
| 852 | } |
| 853 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | void Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) { |
| 858 | Class* c = gRegistry->Get<Class*>(refTypeId); |
| 859 | CHECK(c != NULL); |
| 860 | size_t interface_count = c->NumInterfaces(); |
| 861 | expandBufAdd4BE(pReply, interface_count); |
| 862 | for (size_t i = 0; i < interface_count; ++i) { |
| 863 | expandBufAddRefTypeId(pReply, gRegistry->Add(c->GetInterface(i))); |
| 864 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 868 | struct DebugCallbackContext { |
| 869 | int numItems; |
| 870 | JDWP::ExpandBuf* pReply; |
| 871 | |
| 872 | static bool Callback(void* context, uint32_t address, uint32_t lineNum) { |
| 873 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 874 | expandBufAdd8BE(pContext->pReply, address); |
| 875 | expandBufAdd4BE(pContext->pReply, lineNum); |
| 876 | pContext->numItems++; |
| 877 | return true; |
| 878 | } |
| 879 | }; |
| 880 | |
| 881 | Method* m = FromMethodId(methodId); |
| 882 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 883 | const DexFile& dex_file = class_linker->FindDexFile(m->GetDeclaringClass()->GetDexCache()); |
| 884 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(m->GetCodeItemOffset()); |
| 885 | |
| 886 | uint64_t start, end; |
| 887 | if (m->IsNative()) { |
| 888 | start = -1; |
| 889 | end = -1; |
| 890 | } else { |
| 891 | start = 0; |
| 892 | end = code_item->insns_size_in_code_units_; // TODO: what are the units supposed to be? *2? |
| 893 | } |
| 894 | |
| 895 | expandBufAdd8BE(pReply, start); |
| 896 | expandBufAdd8BE(pReply, end); |
| 897 | |
| 898 | // Add numLines later |
| 899 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 900 | expandBufAdd4BE(pReply, 0); |
| 901 | |
| 902 | DebugCallbackContext context; |
| 903 | context.numItems = 0; |
| 904 | context.pReply = pReply; |
| 905 | |
| 906 | dex_file.DecodeDebugInfo(code_item, m, DebugCallbackContext::Callback, NULL, &context); |
| 907 | |
| 908 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 911 | 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] | 912 | struct DebugCallbackContext { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 913 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 914 | size_t variable_count; |
| 915 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 916 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 917 | 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] | 918 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 919 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 920 | 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] | 921 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 922 | slot = MangleSlot(slot, name); |
| 923 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 924 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 925 | expandBufAddUtf8String(pContext->pReply, name); |
| 926 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 927 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 928 | expandBufAddUtf8String(pContext->pReply, signature); |
| 929 | } |
| 930 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 931 | expandBufAdd4BE(pContext->pReply, slot); |
| 932 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 933 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 934 | } |
| 935 | }; |
| 936 | |
| 937 | Method* m = FromMethodId(methodId); |
| 938 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 939 | const DexFile& dex_file = class_linker->FindDexFile(m->GetDeclaringClass()->GetDexCache()); |
| 940 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(m->GetCodeItemOffset()); |
| 941 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 942 | // arg_count considers doubles and longs to take 2 units. |
| 943 | // variable_count considers everything to take 1 unit. |
| 944 | std::string shorty(m->GetShorty()->ToModifiedUtf8()); |
| 945 | expandBufAdd4BE(pReply, m->NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 946 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 947 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 948 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 949 | expandBufAdd4BE(pReply, 0); |
| 950 | |
| 951 | DebugCallbackContext context; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 952 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 953 | context.variable_count = 0; |
| 954 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 955 | |
| 956 | dex_file.DecodeDebugInfo(code_item, m, NULL, DebugCallbackContext::Callback, &context); |
| 957 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 958 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) { |
| 962 | UNIMPLEMENTED(FATAL); |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) { |
| 967 | UNIMPLEMENTED(FATAL); |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 972 | UNIMPLEMENTED(FATAL); |
| 973 | } |
| 974 | |
| 975 | void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) { |
| 976 | UNIMPLEMENTED(FATAL); |
| 977 | } |
| 978 | |
| 979 | void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 980 | UNIMPLEMENTED(FATAL); |
| 981 | } |
| 982 | |
| 983 | void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) { |
| 984 | UNIMPLEMENTED(FATAL); |
| 985 | } |
| 986 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 987 | std::string Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 988 | String* s = gRegistry->Get<String*>(strId); |
| 989 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 992 | Thread* DecodeThread(JDWP::ObjectId threadId) { |
| 993 | Object* thread_peer = gRegistry->Get<Object*>(threadId); |
| 994 | CHECK(thread_peer != NULL); |
| 995 | return Thread::FromManagedThread(thread_peer); |
| 996 | } |
| 997 | |
| 998 | bool Dbg::GetThreadName(JDWP::ObjectId threadId, std::string& name) { |
| 999 | ScopedThreadListLock thread_list_lock; |
| 1000 | Thread* thread = DecodeThread(threadId); |
| 1001 | if (thread == NULL) { |
| 1002 | return false; |
| 1003 | } |
| 1004 | StringAppendF(&name, "<%d> %s", thread->GetThinLockId(), thread->GetName()->ToModifiedUtf8().c_str()); |
| 1005 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1009 | Object* thread = gRegistry->Get<Object*>(threadId); |
| 1010 | CHECK(thread != NULL); |
| 1011 | |
| 1012 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;"); |
| 1013 | CHECK(c != NULL); |
| 1014 | Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;"); |
| 1015 | CHECK(f != NULL); |
| 1016 | Object* group = f->GetObject(thread); |
| 1017 | CHECK(group != NULL); |
| 1018 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1021 | std::string Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 1022 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1023 | CHECK(thread_group != NULL); |
| 1024 | |
| 1025 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1026 | CHECK(c != NULL); |
| 1027 | Field* f = c->FindInstanceField("name", "Ljava/lang/String;"); |
| 1028 | CHECK(f != NULL); |
| 1029 | String* s = reinterpret_cast<String*>(f->GetObject(thread_group)); |
| 1030 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1034 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1035 | CHECK(thread_group != NULL); |
| 1036 | |
| 1037 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1038 | CHECK(c != NULL); |
| 1039 | Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;"); |
| 1040 | CHECK(f != NULL); |
| 1041 | Object* parent = f->GetObject(thread_group); |
| 1042 | return gRegistry->Add(parent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1045 | static Object* GetStaticThreadGroup(const char* field_name) { |
| 1046 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1047 | CHECK(c != NULL); |
| 1048 | Field* f = c->FindStaticField(field_name, "Ljava/lang/ThreadGroup;"); |
| 1049 | CHECK(f != NULL); |
| 1050 | Object* group = f->GetObject(NULL); |
| 1051 | CHECK(group != NULL); |
| 1052 | return group; |
| 1053 | } |
| 1054 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1055 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1056 | return gRegistry->Add(GetStaticThreadGroup("mSystem")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1060 | return gRegistry->Add(GetStaticThreadGroup("mMain")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1063 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* pThreadStatus, uint32_t* pSuspendStatus) { |
| 1064 | ScopedThreadListLock thread_list_lock; |
| 1065 | |
| 1066 | Thread* thread = DecodeThread(threadId); |
| 1067 | if (thread == NULL) { |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | switch (thread->GetState()) { |
| 1072 | case Thread::kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1073 | case Thread::kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1074 | case Thread::kTimedWaiting: *pThreadStatus = JDWP::TS_SLEEPING; break; |
| 1075 | case Thread::kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break; |
| 1076 | case Thread::kWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1077 | case Thread::kInitializing: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1078 | case Thread::kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1079 | case Thread::kNative: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1080 | case Thread::kVmWait: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1081 | case Thread::kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1082 | default: |
| 1083 | LOG(FATAL) << "unknown thread state " << thread->GetState(); |
| 1084 | } |
| 1085 | |
| 1086 | *pSuspendStatus = (thread->IsSuspended() ? JDWP::SUSPEND_STATUS_SUSPENDED : 0); |
| 1087 | |
| 1088 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) { |
| 1092 | UNIMPLEMENTED(FATAL); |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1097 | return DecodeThread(threadId) != NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1101 | return DecodeThread(threadId)->IsSuspended(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | //void Dbg::WaitForSuspend(JDWP::ObjectId threadId); |
| 1105 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1106 | void Dbg::GetThreadGroupThreadsImpl(Object* thread_group, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 1107 | struct ThreadListVisitor { |
| 1108 | static void Visit(Thread* t, void* arg) { |
| 1109 | reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t); |
| 1110 | } |
| 1111 | |
| 1112 | void Visit(Thread* t) { |
| 1113 | if (t == Dbg::GetDebugThread()) { |
| 1114 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 1115 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 1116 | return; |
| 1117 | } |
| 1118 | if (thread_group == NULL || t->GetThreadGroup() == thread_group) { |
| 1119 | threads.push_back(gRegistry->Add(t->GetPeer())); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | Object* thread_group; |
| 1124 | std::vector<JDWP::ObjectId> threads; |
| 1125 | }; |
| 1126 | |
| 1127 | ThreadListVisitor tlv; |
| 1128 | tlv.thread_group = thread_group; |
| 1129 | |
| 1130 | { |
| 1131 | ScopedThreadListLock thread_list_lock; |
| 1132 | Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv); |
| 1133 | } |
| 1134 | |
| 1135 | *pThreadCount = tlv.threads.size(); |
| 1136 | if (*pThreadCount == 0) { |
| 1137 | *ppThreadIds = NULL; |
| 1138 | } else { |
| 1139 | *ppThreadIds = new JDWP::ObjectId[*pThreadCount]; |
| 1140 | for (size_t i = 0; i < *pThreadCount; ++i) { |
| 1141 | (*ppThreadIds)[i] = tlv.threads[i]; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1146 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1147 | GetThreadGroupThreadsImpl(gRegistry->Get<Object*>(threadGroupId), ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1151 | GetThreadGroupThreadsImpl(NULL, ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1155 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1156 | struct CountStackDepthVisitor : public Thread::StackVisitor { |
| 1157 | CountStackDepthVisitor() : depth(0) {} |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1158 | virtual void VisitFrame(const Frame& f, uintptr_t) { |
| 1159 | // TODO: we'll need to skip callee-save frames too. |
| 1160 | if (f.HasMethod()) { |
| 1161 | ++depth; |
| 1162 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1163 | } |
| 1164 | size_t depth; |
| 1165 | }; |
| 1166 | CountStackDepthVisitor visitor; |
| 1167 | DecodeThread(threadId)->WalkStack(&visitor); |
| 1168 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1171 | bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
| 1172 | ScopedThreadListLock thread_list_lock; |
| 1173 | struct GetFrameVisitor : public Thread::StackVisitor { |
| 1174 | GetFrameVisitor(int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) |
| 1175 | : found(false) ,depth(0), desired_frame_number(desired_frame_number), pFrameId(pFrameId), pLoc(pLoc) { |
| 1176 | } |
| 1177 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1178 | // TODO: we'll need to skip callee-save frames too. |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1179 | if (!f.HasMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1180 | 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] | 1181 | } |
| 1182 | |
| 1183 | if (depth == desired_frame_number) { |
| 1184 | *pFrameId = reinterpret_cast<JDWP::FrameId>(f.GetSP()); |
| 1185 | |
| 1186 | Method* m = f.GetMethod(); |
| 1187 | Class* c = m->GetDeclaringClass(); |
| 1188 | |
| 1189 | pLoc->typeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1190 | pLoc->classId = gRegistry->Add(c); |
| 1191 | pLoc->methodId = ToMethodId(m); |
| 1192 | pLoc->idx = m->IsNative() ? -1 : m->ToDexPC(pc); |
| 1193 | |
| 1194 | found = true; |
| 1195 | } |
| 1196 | ++depth; |
| 1197 | } |
| 1198 | bool found; |
| 1199 | int depth; |
| 1200 | int desired_frame_number; |
| 1201 | JDWP::FrameId* pFrameId; |
| 1202 | JDWP::JdwpLocation* pLoc; |
| 1203 | }; |
| 1204 | GetFrameVisitor visitor(desired_frame_number, pFrameId, pLoc); |
| 1205 | visitor.desired_frame_number = desired_frame_number; |
| 1206 | DecodeThread(threadId)->WalkStack(&visitor); |
| 1207 | return visitor.found; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1211 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1214 | void Dbg::SuspendVM() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1215 | 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] | 1216 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1220 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1224 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1225 | ScopedThreadListLock thread_list_lock; |
| 1226 | Thread* thread = Thread::FromManagedThread(peer); |
| 1227 | if (thread == NULL) { |
| 1228 | LOG(WARNING) << "No such thread for suspend: " << peer; |
| 1229 | return; |
| 1230 | } |
| 1231 | Runtime::Current()->GetThreadList()->Suspend(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1235 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1236 | ScopedThreadListLock thread_list_lock; |
| 1237 | Thread* thread = Thread::FromManagedThread(peer); |
| 1238 | if (thread == NULL) { |
| 1239 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 1240 | return; |
| 1241 | } |
| 1242 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1246 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
| 1250 | UNIMPLEMENTED(FATAL); |
| 1251 | return false; |
| 1252 | } |
| 1253 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1254 | void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t expectedLen) { |
| 1255 | Method** sp = reinterpret_cast<Method**>(frameId); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1256 | Frame f; |
| 1257 | f.SetSP(sp); |
| 1258 | uint16_t reg = DemangleSlot(slot, f); |
| 1259 | Method* m = f.GetMethod(); |
| 1260 | |
| 1261 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1262 | uint32_t vmap_offset; |
| 1263 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
| 1264 | UNIMPLEMENTED(FATAL) << "don't know how to pull locals from callee save frames: " << vmap_offset; |
| 1265 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1266 | |
| 1267 | switch (tag) { |
| 1268 | case JDWP::JT_BOOLEAN: |
| 1269 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1270 | CHECK_EQ(expectedLen, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1271 | uint32_t intVal = f.GetVReg(m, reg); |
| 1272 | LOG(VERBOSE) << "get boolean local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1273 | JDWP::Set1(buf+1, intVal != 0); |
| 1274 | } |
| 1275 | break; |
| 1276 | case JDWP::JT_BYTE: |
| 1277 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1278 | CHECK_EQ(expectedLen, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1279 | uint32_t intVal = f.GetVReg(m, reg); |
| 1280 | LOG(VERBOSE) << "get byte local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1281 | JDWP::Set1(buf+1, intVal); |
| 1282 | } |
| 1283 | break; |
| 1284 | case JDWP::JT_SHORT: |
| 1285 | case JDWP::JT_CHAR: |
| 1286 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1287 | CHECK_EQ(expectedLen, 2U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1288 | uint32_t intVal = f.GetVReg(m, reg); |
| 1289 | LOG(VERBOSE) << "get short/char local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1290 | JDWP::Set2BE(buf+1, intVal); |
| 1291 | } |
| 1292 | break; |
| 1293 | case JDWP::JT_INT: |
| 1294 | case JDWP::JT_FLOAT: |
| 1295 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1296 | CHECK_EQ(expectedLen, 4U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1297 | uint32_t intVal = f.GetVReg(m, reg); |
| 1298 | LOG(VERBOSE) << "get int/float local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1299 | JDWP::Set4BE(buf+1, intVal); |
| 1300 | } |
| 1301 | break; |
| 1302 | case JDWP::JT_ARRAY: |
| 1303 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1304 | CHECK_EQ(expectedLen, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1305 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1306 | LOG(VERBOSE) << "get array local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1307 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1308 | LOG(FATAL) << "reg " << reg << " expected to hold array: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1309 | } |
| 1310 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1311 | } |
| 1312 | break; |
| 1313 | case JDWP::JT_OBJECT: |
| 1314 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1315 | CHECK_EQ(expectedLen, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1316 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1317 | LOG(VERBOSE) << "get object local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1318 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1319 | LOG(FATAL) << "reg " << reg << " expected to hold object: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1320 | } |
| 1321 | tag = TagFromObject(o); |
| 1322 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1323 | } |
| 1324 | break; |
| 1325 | case JDWP::JT_DOUBLE: |
| 1326 | case JDWP::JT_LONG: |
| 1327 | { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1328 | CHECK_EQ(expectedLen, 8U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1329 | uint32_t lo = f.GetVReg(m, reg); |
| 1330 | uint64_t hi = f.GetVReg(m, reg + 1); |
| 1331 | uint64_t longVal = (hi << 32) | lo; |
| 1332 | LOG(VERBOSE) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1333 | JDWP::Set8BE(buf+1, longVal); |
| 1334 | } |
| 1335 | break; |
| 1336 | default: |
| 1337 | LOG(FATAL) << "unknown tag " << tag; |
| 1338 | break; |
| 1339 | } |
| 1340 | |
| 1341 | // Prepend tag, which may have been updated. |
| 1342 | JDWP::Set1(buf, tag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1345 | 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] | 1346 | UNIMPLEMENTED(FATAL); |
| 1347 | } |
| 1348 | |
| 1349 | void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) { |
| 1350 | UNIMPLEMENTED(FATAL); |
| 1351 | } |
| 1352 | |
| 1353 | void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) { |
| 1354 | UNIMPLEMENTED(FATAL); |
| 1355 | } |
| 1356 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1357 | void Dbg::PostClassPrepare(Class* c) { |
| 1358 | UNIMPLEMENTED(FATAL); |
| 1359 | } |
| 1360 | |
| 1361 | bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 1362 | UNIMPLEMENTED(FATAL); |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| 1366 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 1367 | UNIMPLEMENTED(FATAL); |
| 1368 | } |
| 1369 | |
| 1370 | bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) { |
| 1371 | UNIMPLEMENTED(FATAL); |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
| 1375 | void Dbg::UnconfigureStep(JDWP::ObjectId threadId) { |
| 1376 | UNIMPLEMENTED(FATAL); |
| 1377 | } |
| 1378 | |
| 1379 | 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, uint8_t* pResultTag, uint64_t* pResultValue, JDWP::ObjectId* pExceptObj) { |
| 1380 | UNIMPLEMENTED(FATAL); |
| 1381 | return JDWP::ERR_NONE; |
| 1382 | } |
| 1383 | |
| 1384 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
| 1385 | UNIMPLEMENTED(FATAL); |
| 1386 | } |
| 1387 | |
| 1388 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
| 1389 | UNIMPLEMENTED(FATAL); |
| 1390 | } |
| 1391 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 1394 | * need to process each, accumulate the replies, and ship the whole thing |
| 1395 | * back. |
| 1396 | * |
| 1397 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 1398 | * and includes the chunk type/length, followed by the data. |
| 1399 | * |
| 1400 | * TODO: we currently assume that the request and reply include a single |
| 1401 | * chunk. If this becomes inconvenient we will need to adapt. |
| 1402 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1403 | 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] | 1404 | CHECK_GE(dataLen, 0); |
| 1405 | |
| 1406 | Thread* self = Thread::Current(); |
| 1407 | JNIEnv* env = self->GetJniEnv(); |
| 1408 | |
| 1409 | static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk"); |
| 1410 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 1411 | static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", |
| 1412 | "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); |
| 1413 | static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B"); |
| 1414 | static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I"); |
| 1415 | static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I"); |
| 1416 | static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I"); |
| 1417 | |
| 1418 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1419 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 1420 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1421 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 1422 | env->ExceptionClear(); |
| 1423 | return false; |
| 1424 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1425 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1426 | |
| 1427 | const int kChunkHdrLen = 8; |
| 1428 | |
| 1429 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1430 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1431 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 1432 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1433 | jint offset = kChunkHdrLen; |
| 1434 | if (offset + length > dataLen) { |
| 1435 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 1436 | return false; |
| 1437 | } |
| 1438 | |
| 1439 | // 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] | 1440 | 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] | 1441 | if (env->ExceptionCheck()) { |
| 1442 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 1443 | env->ExceptionDescribe(); |
| 1444 | env->ExceptionClear(); |
| 1445 | return false; |
| 1446 | } |
| 1447 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1448 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * Pull the pieces out of the chunk. We copy the results into a |
| 1454 | * newly-allocated buffer that the caller can free. We don't want to |
| 1455 | * continue using the Chunk object because nothing has a reference to it. |
| 1456 | * |
| 1457 | * We could avoid this by returning type/data/offset/length and having |
| 1458 | * the caller be aware of the object lifetime issues, but that |
| 1459 | * integrates the JDWP code more tightly into the VM, and doesn't work |
| 1460 | * if we have responses for multiple chunks. |
| 1461 | * |
| 1462 | * So we're pretty much stuck with copying data around multiple times. |
| 1463 | */ |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1464 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), data_fid))); |
| 1465 | length = env->GetIntField(chunk.get(), length_fid); |
| 1466 | offset = env->GetIntField(chunk.get(), offset_fid); |
| 1467 | type = env->GetIntField(chunk.get(), type_fid); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1468 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1469 | LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
| 1470 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1471 | return false; |
| 1472 | } |
| 1473 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1474 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1475 | if (offset + length > replyLength) { |
| 1476 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 1477 | return false; |
| 1478 | } |
| 1479 | |
| 1480 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 1481 | if (reply == NULL) { |
| 1482 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 1483 | return false; |
| 1484 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1485 | JDWP::Set4BE(reply + 0, type); |
| 1486 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1487 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1488 | |
| 1489 | *pReplyBuf = reply; |
| 1490 | *pReplyLen = length + kChunkHdrLen; |
| 1491 | |
| 1492 | LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length); |
| 1493 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1496 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1497 | LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
| 1498 | |
| 1499 | Thread* self = Thread::Current(); |
| 1500 | if (self->GetState() != Thread::kRunnable) { |
| 1501 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 1502 | /* try anyway? */ |
| 1503 | } |
| 1504 | |
| 1505 | JNIEnv* env = self->GetJniEnv(); |
| 1506 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 1507 | static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V"); |
| 1508 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| 1509 | env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event); |
| 1510 | if (env->ExceptionCheck()) { |
| 1511 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 1512 | env->ExceptionDescribe(); |
| 1513 | env->ExceptionClear(); |
| 1514 | } |
| 1515 | } |
| 1516 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1517 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1518 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1522 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1523 | gDdmThreadNotification = false; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1527 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1528 | * |
| 1529 | * Because we broadcast the full set of threads when the notifications are |
| 1530 | * first enabled, it's possible for "thread" to be actively executing. |
| 1531 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1532 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1533 | if (!gDdmThreadNotification) { |
| 1534 | return; |
| 1535 | } |
| 1536 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1537 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1538 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1539 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1540 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1541 | } else { |
| 1542 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
| 1543 | SirtRef<String> name(t->GetName()); |
| 1544 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 1545 | const jchar* chars = name->GetCharArray()->GetData(); |
| 1546 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1547 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1548 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
| 1549 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1550 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 1551 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1555 | static void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1556 | Dbg::DdmSendThreadNotification(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 1560 | // We lock the thread list to avoid sending duplicate events or missing |
| 1561 | // a thread change. We should be okay holding this lock while sending |
| 1562 | // 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] | 1563 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1564 | |
| 1565 | gDdmThreadNotification = enable; |
| 1566 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1567 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1571 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1572 | if (gDebuggerActive) { |
| 1573 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1574 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1575 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1576 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1580 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1584 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1587 | 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] | 1588 | CHECK(buf != NULL); |
| 1589 | iovec vec[1]; |
| 1590 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 1591 | vec[0].iov_len = byte_count; |
| 1592 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1595 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 1596 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 1597 | } |
| 1598 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1599 | void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iovcnt) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 1600 | if (gJdwpState == NULL) { |
| 1601 | LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type; |
| 1602 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1603 | gJdwpState->DdmSendChunkV(type, iov, iovcnt); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 1604 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1607 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 1608 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 1609 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1610 | return true; |
| 1611 | } |
| 1612 | |
| 1613 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 1614 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 1615 | return false; |
| 1616 | } |
| 1617 | |
| 1618 | gDdmHpifWhen = when; |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
| 1622 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 1623 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 1624 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
| 1628 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 1629 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 1630 | return false; |
| 1631 | } |
| 1632 | |
| 1633 | if (native) { |
| 1634 | gDdmNhsgWhen = when; |
| 1635 | gDdmNhsgWhat = what; |
| 1636 | } else { |
| 1637 | gDdmHpsgWhen = when; |
| 1638 | gDdmHpsgWhat = what; |
| 1639 | } |
| 1640 | return true; |
| 1641 | } |
| 1642 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 1643 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 1644 | // If there's a one-shot 'when', reset it. |
| 1645 | if (reason == gDdmHpifWhen) { |
| 1646 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 1647 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * Chunk HPIF (client --> server) |
| 1653 | * |
| 1654 | * Heap Info. General information about the heap, |
| 1655 | * suitable for a summary display. |
| 1656 | * |
| 1657 | * [u4]: number of heaps |
| 1658 | * |
| 1659 | * For each heap: |
| 1660 | * [u4]: heap ID |
| 1661 | * [u8]: timestamp in ms since Unix epoch |
| 1662 | * [u1]: capture reason (same as 'when' value from server) |
| 1663 | * [u4]: max heap size in bytes (-Xmx) |
| 1664 | * [u4]: current heap size in bytes |
| 1665 | * [u4]: current number of bytes allocated |
| 1666 | * [u4]: current number of objects allocated |
| 1667 | */ |
| 1668 | uint8_t heap_count = 1; |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1669 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1670 | JDWP::Append4BE(bytes, heap_count); |
| 1671 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| 1672 | JDWP::Append8BE(bytes, MilliTime()); |
| 1673 | JDWP::Append1BE(bytes, reason); |
| 1674 | JDWP::Append4BE(bytes, Heap::GetMaxMemory()); // Max allowed heap size in bytes. |
| 1675 | JDWP::Append4BE(bytes, Heap::GetTotalMemory()); // Current heap size in bytes. |
| 1676 | JDWP::Append4BE(bytes, Heap::GetBytesAllocated()); |
| 1677 | JDWP::Append4BE(bytes, Heap::GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1678 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 1679 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1682 | enum HpsgSolidity { |
| 1683 | SOLIDITY_FREE = 0, |
| 1684 | SOLIDITY_HARD = 1, |
| 1685 | SOLIDITY_SOFT = 2, |
| 1686 | SOLIDITY_WEAK = 3, |
| 1687 | SOLIDITY_PHANTOM = 4, |
| 1688 | SOLIDITY_FINALIZABLE = 5, |
| 1689 | SOLIDITY_SWEEP = 6, |
| 1690 | }; |
| 1691 | |
| 1692 | enum HpsgKind { |
| 1693 | KIND_OBJECT = 0, |
| 1694 | KIND_CLASS_OBJECT = 1, |
| 1695 | KIND_ARRAY_1 = 2, |
| 1696 | KIND_ARRAY_2 = 3, |
| 1697 | KIND_ARRAY_4 = 4, |
| 1698 | KIND_ARRAY_8 = 5, |
| 1699 | KIND_UNKNOWN = 6, |
| 1700 | KIND_NATIVE = 7, |
| 1701 | }; |
| 1702 | |
| 1703 | #define HPSG_PARTIAL (1<<7) |
| 1704 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 1705 | |
| 1706 | struct HeapChunkContext { |
| 1707 | std::vector<uint8_t> buf; |
| 1708 | uint8_t* p; |
| 1709 | uint8_t* pieceLenField; |
| 1710 | size_t totalAllocationUnits; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1711 | uint32_t type; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1712 | bool merge; |
| 1713 | bool needHeader; |
| 1714 | |
| 1715 | // Maximum chunk size. Obtain this from the formula: |
| 1716 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 1717 | HeapChunkContext(bool merge, bool native) |
| 1718 | : buf(16384 - 16), |
| 1719 | type(0), |
| 1720 | merge(merge) { |
| 1721 | Reset(); |
| 1722 | if (native) { |
| 1723 | type = CHUNK_TYPE("NHSG"); |
| 1724 | } else { |
| 1725 | type = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | ~HeapChunkContext() { |
| 1730 | if (p > &buf[0]) { |
| 1731 | Flush(); |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | void EnsureHeader(const void* chunk_ptr) { |
| 1736 | if (!needHeader) { |
| 1737 | return; |
| 1738 | } |
| 1739 | |
| 1740 | // Start a new HPSx chunk. |
| 1741 | JDWP::Write4BE(&p, 1); // Heap id (bogus; we only have one heap). |
| 1742 | JDWP::Write1BE(&p, 8); // Size of allocation unit, in bytes. |
| 1743 | |
| 1744 | JDWP::Write4BE(&p, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 1745 | JDWP::Write4BE(&p, 0); // offset of this piece (relative to the virtual address). |
| 1746 | // [u4]: length of piece, in allocation units |
| 1747 | // We won't know this until we're done, so save the offset and stuff in a dummy value. |
| 1748 | pieceLenField = p; |
| 1749 | JDWP::Write4BE(&p, 0x55555555); |
| 1750 | needHeader = false; |
| 1751 | } |
| 1752 | |
| 1753 | void Flush() { |
| 1754 | // Patch the "length of piece" field. |
| 1755 | CHECK_LE(&buf[0], pieceLenField); |
| 1756 | CHECK_LE(pieceLenField, p); |
| 1757 | JDWP::Set4BE(pieceLenField, totalAllocationUnits); |
| 1758 | |
| 1759 | Dbg::DdmSendChunk(type, p - &buf[0], &buf[0]); |
| 1760 | Reset(); |
| 1761 | } |
| 1762 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1763 | static void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len, void* arg) { |
| 1764 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(chunk_ptr, chunk_len, user_ptr, user_len); |
| 1765 | } |
| 1766 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1767 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1768 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 1769 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1770 | void Reset() { |
| 1771 | p = &buf[0]; |
| 1772 | totalAllocationUnits = 0; |
| 1773 | needHeader = true; |
| 1774 | pieceLenField = NULL; |
| 1775 | } |
| 1776 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1777 | void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len) { |
| 1778 | CHECK_EQ((chunk_len & (ALLOCATION_UNIT_SIZE-1)), 0U); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1779 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1780 | /* Make sure there's enough room left in the buffer. |
| 1781 | * We need to use two bytes for every fractional 256 |
| 1782 | * allocation units used by the chunk. |
| 1783 | */ |
| 1784 | { |
| 1785 | size_t needed = (((chunk_len/ALLOCATION_UNIT_SIZE + 255) / 256) * 2); |
| 1786 | size_t bytesLeft = buf.size() - (size_t)(p - &buf[0]); |
| 1787 | if (bytesLeft < needed) { |
| 1788 | Flush(); |
| 1789 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1790 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1791 | bytesLeft = buf.size() - (size_t)(p - &buf[0]); |
| 1792 | if (bytesLeft < needed) { |
| 1793 | LOG(WARNING) << "chunk is too big to transmit (chunk_len=" << chunk_len << ", " << needed << " bytes)"; |
| 1794 | return; |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | // OLD-TODO: notice when there's a gap and start a new heap, or at least a new range. |
| 1799 | EnsureHeader(chunk_ptr); |
| 1800 | |
| 1801 | // Determine the type of this chunk. |
| 1802 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 1803 | // If it's the same, we should combine them. |
| 1804 | uint8_t state = ExamineObject(reinterpret_cast<const Object*>(user_ptr), (type == CHUNK_TYPE("NHSG"))); |
| 1805 | |
| 1806 | // Write out the chunk description. |
| 1807 | chunk_len /= ALLOCATION_UNIT_SIZE; // convert to allocation units |
| 1808 | totalAllocationUnits += chunk_len; |
| 1809 | while (chunk_len > 256) { |
| 1810 | *p++ = state | HPSG_PARTIAL; |
| 1811 | *p++ = 255; // length - 1 |
| 1812 | chunk_len -= 256; |
| 1813 | } |
| 1814 | *p++ = state; |
| 1815 | *p++ = chunk_len - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1818 | uint8_t ExamineObject(const Object* o, bool is_native_heap) { |
| 1819 | if (o == NULL) { |
| 1820 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 1821 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1822 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1823 | // It's an allocated chunk. Figure out what it is. |
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 | // If we're looking at the native heap, we'll just return |
| 1826 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
| 1827 | if (is_native_heap || !Heap::IsLiveObjectLocked(o)) { |
| 1828 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 1829 | } |
| 1830 | |
| 1831 | Class* c = o->GetClass(); |
| 1832 | if (c == NULL) { |
| 1833 | // The object was probably just created but hasn't been initialized yet. |
| 1834 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1835 | } |
| 1836 | |
| 1837 | if (!Heap::IsHeapAddress(c)) { |
| 1838 | LOG(WARNING) << "invalid class for managed heap object: " << o << " " << c; |
| 1839 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 1840 | } |
| 1841 | |
| 1842 | if (c->IsClassClass()) { |
| 1843 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 1844 | } |
| 1845 | |
| 1846 | if (c->IsArrayClass()) { |
| 1847 | if (o->IsObjectArray()) { |
| 1848 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1849 | } |
| 1850 | switch (c->GetComponentSize()) { |
| 1851 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 1852 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 1853 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1854 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 1855 | } |
| 1856 | } |
| 1857 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1858 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1859 | } |
| 1860 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1861 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 1862 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1863 | |
| 1864 | void Dbg::DdmSendHeapSegments(bool native) { |
| 1865 | Dbg::HpsgWhen when; |
| 1866 | Dbg::HpsgWhat what; |
| 1867 | if (!native) { |
| 1868 | when = gDdmHpsgWhen; |
| 1869 | what = gDdmHpsgWhat; |
| 1870 | } else { |
| 1871 | when = gDdmNhsgWhen; |
| 1872 | what = gDdmNhsgWhat; |
| 1873 | } |
| 1874 | if (when == HPSG_WHEN_NEVER) { |
| 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | // Figure out what kind of chunks we'll be sending. |
| 1879 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 1880 | |
| 1881 | // First, send a heap start chunk. |
| 1882 | uint8_t heap_id[4]; |
| 1883 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 1884 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 1885 | |
| 1886 | // Send a series of heap segment chunks. |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1887 | HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 1888 | if (native) { |
| 1889 | dlmalloc_walk_heap(HeapChunkContext::HeapChunkCallback, &context); |
| 1890 | } else { |
| 1891 | Heap::WalkHeap(HeapChunkContext::HeapChunkCallback, &context); |
| 1892 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1893 | |
| 1894 | // Finally, send a heap end chunk. |
| 1895 | 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] | 1896 | } |
| 1897 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1898 | void Dbg::SetAllocTrackingEnabled(bool enabled) { |
| 1899 | MutexLock mu(gAllocTrackerLock); |
| 1900 | if (enabled) { |
| 1901 | if (recent_allocation_records_ == NULL) { |
| 1902 | LOG(INFO) << "Enabling alloc tracker (" << kNumAllocRecords << " entries, " |
| 1903 | << kMaxAllocRecordStackDepth << " frames --> " |
| 1904 | << (sizeof(AllocRecord) * kNumAllocRecords) << " bytes)"; |
| 1905 | gAllocRecordHead = gAllocRecordCount = 0; |
| 1906 | recent_allocation_records_ = new AllocRecord[kNumAllocRecords]; |
| 1907 | CHECK(recent_allocation_records_ != NULL); |
| 1908 | } |
| 1909 | } else { |
| 1910 | delete[] recent_allocation_records_; |
| 1911 | recent_allocation_records_ = NULL; |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | struct AllocRecordStackVisitor : public Thread::StackVisitor { |
| 1916 | AllocRecordStackVisitor(AllocRecord* record) : record(record), depth(0) { |
| 1917 | } |
| 1918 | |
| 1919 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
| 1920 | if (depth >= kMaxAllocRecordStackDepth) { |
| 1921 | return; |
| 1922 | } |
| 1923 | Method* m = f.GetMethod(); |
| 1924 | if (m == NULL || m->IsCalleeSaveMethod()) { |
| 1925 | return; |
| 1926 | } |
| 1927 | record->stack[depth].method = m; |
| 1928 | record->stack[depth].raw_pc = pc; |
| 1929 | ++depth; |
| 1930 | } |
| 1931 | |
| 1932 | ~AllocRecordStackVisitor() { |
| 1933 | // Clear out any unused stack trace elements. |
| 1934 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
| 1935 | record->stack[depth].method = NULL; |
| 1936 | record->stack[depth].raw_pc = 0; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | AllocRecord* record; |
| 1941 | size_t depth; |
| 1942 | }; |
| 1943 | |
| 1944 | void Dbg::RecordAllocation(Class* type, size_t byte_count) { |
| 1945 | Thread* self = Thread::Current(); |
| 1946 | CHECK(self != NULL); |
| 1947 | |
| 1948 | MutexLock mu(gAllocTrackerLock); |
| 1949 | if (recent_allocation_records_ == NULL) { |
| 1950 | return; |
| 1951 | } |
| 1952 | |
| 1953 | // Advance and clip. |
| 1954 | if (++gAllocRecordHead == kNumAllocRecords) { |
| 1955 | gAllocRecordHead = 0; |
| 1956 | } |
| 1957 | |
| 1958 | // Fill in the basics. |
| 1959 | AllocRecord* record = &recent_allocation_records_[gAllocRecordHead]; |
| 1960 | record->type = type; |
| 1961 | record->byte_count = byte_count; |
| 1962 | record->thin_lock_id = self->GetThinLockId(); |
| 1963 | |
| 1964 | // Fill in the stack trace. |
| 1965 | AllocRecordStackVisitor visitor(record); |
| 1966 | self->WalkStack(&visitor); |
| 1967 | |
| 1968 | if (gAllocRecordCount < kNumAllocRecords) { |
| 1969 | ++gAllocRecordCount; |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * Return the index of the head element. |
| 1975 | * |
| 1976 | * We point at the most-recently-written record, so if allocRecordCount is 1 |
| 1977 | * we want to use the current element. Take "head+1" and subtract count |
| 1978 | * from it. |
| 1979 | * |
| 1980 | * We need to handle underflow in our circular buffer, so we add |
| 1981 | * kNumAllocRecords and then mask it back down. |
| 1982 | */ |
| 1983 | inline static int headIndex() { |
| 1984 | return (gAllocRecordHead+1 + kNumAllocRecords - gAllocRecordCount) & (kNumAllocRecords-1); |
| 1985 | } |
| 1986 | |
| 1987 | void Dbg::DumpRecentAllocations() { |
| 1988 | MutexLock mu(gAllocTrackerLock); |
| 1989 | if (recent_allocation_records_ == NULL) { |
| 1990 | LOG(INFO) << "Not recording tracked allocations"; |
| 1991 | return; |
| 1992 | } |
| 1993 | |
| 1994 | // "i" is the head of the list. We want to start at the end of the |
| 1995 | // list and move forward to the tail. |
| 1996 | size_t i = headIndex(); |
| 1997 | size_t count = gAllocRecordCount; |
| 1998 | |
| 1999 | LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")"; |
| 2000 | while (count--) { |
| 2001 | AllocRecord* record = &recent_allocation_records_[i]; |
| 2002 | |
| 2003 | LOG(INFO) << StringPrintf(" T=%-2d %6d ", record->thin_lock_id, record->byte_count) |
| 2004 | << PrettyClass(record->type); |
| 2005 | |
| 2006 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
| 2007 | const Method* m = record->stack[stack_frame].method; |
| 2008 | if (m == NULL) { |
| 2009 | break; |
| 2010 | } |
| 2011 | LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber(); |
| 2012 | } |
| 2013 | |
| 2014 | // pause periodically to help logcat catch up |
| 2015 | if ((count % 5) == 0) { |
| 2016 | usleep(40000); |
| 2017 | } |
| 2018 | |
| 2019 | i = (i + 1) & (kNumAllocRecords-1); |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | class StringTable { |
| 2024 | public: |
| 2025 | StringTable() { |
| 2026 | } |
| 2027 | |
| 2028 | void Add(const String* s) { |
| 2029 | table_.insert(s); |
| 2030 | } |
| 2031 | |
| 2032 | size_t IndexOf(const String* s) { |
| 2033 | return std::distance(table_.begin(), table_.find(s)); |
| 2034 | } |
| 2035 | |
| 2036 | size_t Size() { |
| 2037 | return table_.size(); |
| 2038 | } |
| 2039 | |
| 2040 | void WriteTo(std::vector<uint8_t>& bytes) { |
| 2041 | typedef std::set<const String*>::const_iterator It; // TODO: C++0x auto |
| 2042 | for (It it = table_.begin(); it != table_.end(); ++it) { |
| 2043 | const String* s = *it; |
| 2044 | JDWP::AppendUtf16BE(bytes, s->GetCharArray()->GetData(), s->GetLength()); |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | private: |
| 2049 | std::set<const String*> table_; |
| 2050 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 2051 | }; |
| 2052 | |
| 2053 | /* |
| 2054 | * The data we send to DDMS contains everything we have recorded. |
| 2055 | * |
| 2056 | * Message header (all values big-endian): |
| 2057 | * (1b) message header len (to allow future expansion); includes itself |
| 2058 | * (1b) entry header len |
| 2059 | * (1b) stack frame len |
| 2060 | * (2b) number of entries |
| 2061 | * (4b) offset to string table from start of message |
| 2062 | * (2b) number of class name strings |
| 2063 | * (2b) number of method name strings |
| 2064 | * (2b) number of source file name strings |
| 2065 | * For each entry: |
| 2066 | * (4b) total allocation size |
| 2067 | * (2b) threadId |
| 2068 | * (2b) allocated object's class name index |
| 2069 | * (1b) stack depth |
| 2070 | * For each stack frame: |
| 2071 | * (2b) method's class name |
| 2072 | * (2b) method name |
| 2073 | * (2b) method source file |
| 2074 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 2075 | * (xb) class name strings |
| 2076 | * (xb) method name strings |
| 2077 | * (xb) source file strings |
| 2078 | * |
| 2079 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 2080 | * followed by UTF-16 data. |
| 2081 | * |
| 2082 | * We send up 16-bit unsigned indexes into string tables. In theory there |
| 2083 | * can be (kMaxAllocRecordStackDepth * kNumAllocRecords) unique strings in |
| 2084 | * each table, but in practice there should be far fewer. |
| 2085 | * |
| 2086 | * The chief reason for using a string table here is to keep the size of |
| 2087 | * the DDMS message to a minimum. This is partly to make the protocol |
| 2088 | * efficient, but also because we have to form the whole thing up all at |
| 2089 | * once in a memory buffer. |
| 2090 | * |
| 2091 | * We use separate string tables for class names, method names, and source |
| 2092 | * files to keep the indexes small. There will generally be no overlap |
| 2093 | * between the contents of these tables. |
| 2094 | */ |
| 2095 | jbyteArray Dbg::GetRecentAllocations() { |
| 2096 | if (false) { |
| 2097 | DumpRecentAllocations(); |
| 2098 | } |
| 2099 | |
| 2100 | MutexLock mu(gAllocTrackerLock); |
| 2101 | |
| 2102 | /* |
| 2103 | * Part 1: generate string tables. |
| 2104 | */ |
| 2105 | StringTable class_names; |
| 2106 | StringTable method_names; |
| 2107 | StringTable filenames; |
| 2108 | |
| 2109 | int count = gAllocRecordCount; |
| 2110 | int idx = headIndex(); |
| 2111 | while (count--) { |
| 2112 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2113 | |
| 2114 | class_names.Add(record->type->GetDescriptor()); |
| 2115 | |
| 2116 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
| 2117 | const Method* m = record->stack[i].method; |
| 2118 | if (m != NULL) { |
| 2119 | class_names.Add(m->GetDeclaringClass()->GetDescriptor()); |
| 2120 | method_names.Add(m->GetName()); |
| 2121 | filenames.Add(m->GetDeclaringClass()->GetSourceFile()); |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2126 | } |
| 2127 | |
| 2128 | LOG(INFO) << "allocation records: " << gAllocRecordCount; |
| 2129 | |
| 2130 | /* |
| 2131 | * Part 2: allocate a buffer and generate the output. |
| 2132 | */ |
| 2133 | std::vector<uint8_t> bytes; |
| 2134 | |
| 2135 | // (1b) message header len (to allow future expansion); includes itself |
| 2136 | // (1b) entry header len |
| 2137 | // (1b) stack frame len |
| 2138 | const int kMessageHeaderLen = 15; |
| 2139 | const int kEntryHeaderLen = 9; |
| 2140 | const int kStackFrameLen = 8; |
| 2141 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 2142 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 2143 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 2144 | |
| 2145 | // (2b) number of entries |
| 2146 | // (4b) offset to string table from start of message |
| 2147 | // (2b) number of class name strings |
| 2148 | // (2b) number of method name strings |
| 2149 | // (2b) number of source file name strings |
| 2150 | JDWP::Append2BE(bytes, gAllocRecordCount); |
| 2151 | size_t string_table_offset = bytes.size(); |
| 2152 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| 2153 | JDWP::Append2BE(bytes, class_names.Size()); |
| 2154 | JDWP::Append2BE(bytes, method_names.Size()); |
| 2155 | JDWP::Append2BE(bytes, filenames.Size()); |
| 2156 | |
| 2157 | count = gAllocRecordCount; |
| 2158 | idx = headIndex(); |
| 2159 | while (count--) { |
| 2160 | // For each entry: |
| 2161 | // (4b) total allocation size |
| 2162 | // (2b) thread id |
| 2163 | // (2b) allocated object's class name index |
| 2164 | // (1b) stack depth |
| 2165 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2166 | size_t stack_depth = record->GetDepth(); |
| 2167 | JDWP::Append4BE(bytes, record->byte_count); |
| 2168 | JDWP::Append2BE(bytes, record->thin_lock_id); |
| 2169 | JDWP::Append2BE(bytes, class_names.IndexOf(record->type->GetDescriptor())); |
| 2170 | JDWP::Append1BE(bytes, stack_depth); |
| 2171 | |
| 2172 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 2173 | // For each stack frame: |
| 2174 | // (2b) method's class name |
| 2175 | // (2b) method name |
| 2176 | // (2b) method source file |
| 2177 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 2178 | const Method* m = record->stack[stack_frame].method; |
| 2179 | JDWP::Append2BE(bytes, class_names.IndexOf(m->GetDeclaringClass()->GetDescriptor())); |
| 2180 | JDWP::Append2BE(bytes, method_names.IndexOf(m->GetName())); |
| 2181 | JDWP::Append2BE(bytes, filenames.IndexOf(m->GetDeclaringClass()->GetSourceFile())); |
| 2182 | JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber()); |
| 2183 | } |
| 2184 | |
| 2185 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2186 | } |
| 2187 | |
| 2188 | // (xb) class name strings |
| 2189 | // (xb) method name strings |
| 2190 | // (xb) source file strings |
| 2191 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 2192 | class_names.WriteTo(bytes); |
| 2193 | method_names.WriteTo(bytes); |
| 2194 | filenames.WriteTo(bytes); |
| 2195 | |
| 2196 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 2197 | jbyteArray result = env->NewByteArray(bytes.size()); |
| 2198 | if (result != NULL) { |
| 2199 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 2200 | } |
| 2201 | return result; |
| 2202 | } |
| 2203 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2204 | } // namespace art |