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 | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 24 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 25 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 26 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 27 | #include "thread_list.h" |
| 28 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 29 | extern "C" void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*); |
| 30 | #ifndef HAVE_ANDROID_OS |
| 31 | void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*) { |
| 32 | // No-op for glibc. |
| 33 | } |
| 34 | #endif |
| 35 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 36 | namespace art { |
| 37 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 38 | static const size_t kMaxAllocRecordStackDepth = 16; // Max 255. |
| 39 | static const size_t kNumAllocRecords = 512; // Must be power of 2. |
| 40 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 41 | class ObjectRegistry { |
| 42 | public: |
| 43 | ObjectRegistry() : lock_("ObjectRegistry lock") { |
| 44 | } |
| 45 | |
| 46 | JDWP::ObjectId Add(Object* o) { |
| 47 | if (o == NULL) { |
| 48 | return 0; |
| 49 | } |
| 50 | JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o)); |
| 51 | MutexLock mu(lock_); |
| 52 | map_[id] = o; |
| 53 | return id; |
| 54 | } |
| 55 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 56 | void Clear() { |
| 57 | MutexLock mu(lock_); |
| 58 | LOG(DEBUG) << "Debugger has detached; object registry had " << map_.size() << " entries"; |
| 59 | map_.clear(); |
| 60 | } |
| 61 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 62 | bool Contains(JDWP::ObjectId id) { |
| 63 | MutexLock mu(lock_); |
| 64 | return map_.find(id) != map_.end(); |
| 65 | } |
| 66 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 67 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 68 | MutexLock mu(lock_); |
| 69 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 70 | for (It it = map_.begin(); it != map_.end(); ++it) { |
| 71 | visitor(it->second, arg); |
| 72 | } |
| 73 | } |
| 74 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 75 | private: |
| 76 | Mutex lock_; |
| 77 | std::map<JDWP::ObjectId, Object*> map_; |
| 78 | }; |
| 79 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 80 | struct AllocRecordStackTraceElement { |
| 81 | const Method* method; |
| 82 | uintptr_t raw_pc; |
| 83 | |
| 84 | int32_t LineNumber() const { |
| 85 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 86 | Class* c = method->GetDeclaringClass(); |
| 87 | DexCache* dex_cache = c->GetDexCache(); |
| 88 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 89 | return dex_file.GetLineNumFromPC(method, method->ToDexPC(raw_pc)); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | struct AllocRecord { |
| 94 | Class* type; |
| 95 | size_t byte_count; |
| 96 | uint16_t thin_lock_id; |
| 97 | AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method. |
| 98 | |
| 99 | size_t GetDepth() { |
| 100 | size_t depth = 0; |
| 101 | while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) { |
| 102 | ++depth; |
| 103 | } |
| 104 | return depth; |
| 105 | } |
| 106 | }; |
| 107 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 108 | // JDWP is allowed unless the Zygote forbids it. |
| 109 | static bool gJdwpAllowed = true; |
| 110 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 111 | // Was there a -Xrunjdwp or -agent argument on the command-line? |
| 112 | static bool gJdwpConfigured = false; |
| 113 | |
| 114 | // Broken-down JDWP options. (Only valid if gJdwpConfigured is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 115 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 116 | |
| 117 | // Runtime JDWP state. |
| 118 | static JDWP::JdwpState* gJdwpState = NULL; |
| 119 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 120 | static bool gDebuggerActive; // debugger is making requests. |
| 121 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 122 | static bool gDdmThreadNotification = false; |
| 123 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 124 | // DDMS GC-related settings. |
| 125 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 126 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 127 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 128 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 129 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 130 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 131 | static ObjectRegistry* gRegistry = NULL; |
| 132 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 133 | // Recent allocation tracking. |
| 134 | static Mutex gAllocTrackerLock("AllocTracker lock"); |
| 135 | AllocRecord* Dbg::recent_allocation_records_ = NULL; // TODO: CircularBuffer<AllocRecord> |
| 136 | static size_t gAllocRecordHead = 0; |
| 137 | static size_t gAllocRecordCount = 0; |
| 138 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 139 | /* |
| 140 | * Handle one of the JDWP name/value pairs. |
| 141 | * |
| 142 | * JDWP options are: |
| 143 | * help: if specified, show help message and bail |
| 144 | * transport: may be dt_socket or dt_shmem |
| 145 | * address: for dt_socket, "host:port", or just "port" when listening |
| 146 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 147 | * timeout: how long to wait for debugger to connect / listen |
| 148 | * |
| 149 | * Useful with server=n (these aren't supported yet): |
| 150 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 151 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 152 | * launch=<command-line>: launch the debugger itself |
| 153 | * |
| 154 | * The "transport" option is required, as is "address" if server=n. |
| 155 | */ |
| 156 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 157 | if (name == "transport") { |
| 158 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 159 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 160 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 161 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 162 | } else { |
| 163 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 164 | return false; |
| 165 | } |
| 166 | } else if (name == "server") { |
| 167 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 168 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 169 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 170 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 171 | } else { |
| 172 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 173 | return false; |
| 174 | } |
| 175 | } else if (name == "suspend") { |
| 176 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 177 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 178 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 179 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 180 | } else { |
| 181 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 182 | return false; |
| 183 | } |
| 184 | } else if (name == "address") { |
| 185 | /* this is either <port> or <host>:<port> */ |
| 186 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 187 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 188 | std::string::size_type colon = value.find(':'); |
| 189 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 190 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 191 | port_string = value.substr(colon + 1); |
| 192 | } else { |
| 193 | port_string = value; |
| 194 | } |
| 195 | if (port_string.empty()) { |
| 196 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 197 | return false; |
| 198 | } |
| 199 | char* end; |
| 200 | long port = strtol(port_string.c_str(), &end, 10); |
| 201 | if (*end != '\0') { |
| 202 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 203 | return false; |
| 204 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 205 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 206 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 207 | /* valid but unsupported */ |
| 208 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 209 | } else { |
| 210 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 211 | } |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 218 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 219 | */ |
| 220 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 221 | LOG(VERBOSE) << "ParseJdwpOptions: " << options; |
| 222 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 223 | std::vector<std::string> pairs; |
| 224 | Split(options, ',', pairs); |
| 225 | |
| 226 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 227 | std::string::size_type equals = pairs[i].find('='); |
| 228 | if (equals == std::string::npos) { |
| 229 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 230 | return false; |
| 231 | } |
| 232 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 233 | } |
| 234 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 235 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 236 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 237 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 238 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 239 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | gJdwpConfigured = true; |
| 244 | return true; |
| 245 | } |
| 246 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 247 | void Dbg::StartJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 248 | if (!gJdwpAllowed || !gJdwpConfigured) { |
| 249 | // No JDWP for you! |
| 250 | return; |
| 251 | } |
| 252 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 253 | CHECK(gRegistry == NULL); |
| 254 | gRegistry = new ObjectRegistry; |
| 255 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 256 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 257 | // debugger, passively listen for a debugger, or block waiting for a |
| 258 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 259 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 260 | if (gJdwpState == NULL) { |
| 261 | LOG(WARNING) << "debugger thread failed to initialize"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 262 | return; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // If a debugger has already attached, send the "welcome" message. |
| 266 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 267 | if (gJdwpState->IsActive()) { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 268 | //ScopedThreadStateChange(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 269 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 270 | LOG(WARNING) << "failed to post 'start' message to debugger"; |
| 271 | } |
| 272 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 275 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 276 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 277 | delete gRegistry; |
| 278 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 281 | void Dbg::GcDidFinish() { |
| 282 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
| 283 | LOG(DEBUG) << "Sending VM heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 284 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 285 | } |
| 286 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
| 287 | LOG(DEBUG) << "Dumping VM heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 288 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 289 | } |
| 290 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
| 291 | LOG(DEBUG) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 292 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 296 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 297 | gJdwpAllowed = allowed; |
| 298 | } |
| 299 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 300 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 301 | return Thread::Current()->GetInvokeReq(); |
| 302 | } |
| 303 | |
| 304 | Thread* Dbg::GetDebugThread() { |
| 305 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 306 | } |
| 307 | |
| 308 | void Dbg::ClearWaitForEventThread() { |
| 309 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 313 | CHECK(!gDebuggerConnected); |
| 314 | LOG(VERBOSE) << "JDWP has attached"; |
| 315 | gDebuggerConnected = true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void Dbg::Active() { |
| 319 | UNIMPLEMENTED(FATAL); |
| 320 | } |
| 321 | |
| 322 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 323 | CHECK(gDebuggerConnected); |
| 324 | |
| 325 | gDebuggerActive = false; |
| 326 | |
| 327 | //dvmDisableAllSubMode(kSubModeDebuggerActive); |
| 328 | |
| 329 | gRegistry->Clear(); |
| 330 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | bool Dbg::IsDebuggerConnected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 334 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | bool Dbg::IsDebuggingEnabled() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 338 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | int64_t Dbg::LastDebuggerActivity() { |
| 342 | UNIMPLEMENTED(WARNING); |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | int Dbg::ThreadRunning() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 347 | return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int Dbg::ThreadWaiting() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 351 | return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 354 | int Dbg::ThreadContinuing(int new_state) { |
| 355 | 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] | 356 | } |
| 357 | |
| 358 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 359 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void Dbg::Exit(int status) { |
| 363 | UNIMPLEMENTED(FATAL); |
| 364 | } |
| 365 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 366 | void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 367 | if (gRegistry != NULL) { |
| 368 | gRegistry->VisitRoots(visitor, arg); |
| 369 | } |
| 370 | } |
| 371 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 372 | const char* Dbg::GetClassDescriptor(JDWP::RefTypeId id) { |
| 373 | UNIMPLEMENTED(FATAL); |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) { |
| 378 | UNIMPLEMENTED(FATAL); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) { |
| 383 | UNIMPLEMENTED(FATAL); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) { |
| 388 | UNIMPLEMENTED(FATAL); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) { |
| 393 | UNIMPLEMENTED(FATAL); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | bool Dbg::IsInterface(JDWP::RefTypeId id) { |
| 398 | UNIMPLEMENTED(FATAL); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | void Dbg::GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 403 | UNIMPLEMENTED(FATAL); |
| 404 | } |
| 405 | |
| 406 | void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 407 | UNIMPLEMENTED(FATAL); |
| 408 | } |
| 409 | |
| 410 | void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature) { |
| 411 | UNIMPLEMENTED(FATAL); |
| 412 | } |
| 413 | |
| 414 | bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) { |
| 415 | UNIMPLEMENTED(FATAL); |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) { |
| 420 | UNIMPLEMENTED(FATAL); |
| 421 | } |
| 422 | |
| 423 | uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) { |
| 424 | UNIMPLEMENTED(FATAL); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | const char* Dbg::GetSignature(JDWP::RefTypeId refTypeId) { |
| 429 | UNIMPLEMENTED(FATAL); |
| 430 | return NULL; |
| 431 | } |
| 432 | |
| 433 | const char* Dbg::GetSourceFile(JDWP::RefTypeId refTypeId) { |
| 434 | UNIMPLEMENTED(FATAL); |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) { |
| 439 | UNIMPLEMENTED(FATAL); |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
| 444 | UNIMPLEMENTED(FATAL); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int Dbg::GetTagWidth(int tag) { |
| 449 | UNIMPLEMENTED(FATAL); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | int Dbg::GetArrayLength(JDWP::ObjectId arrayId) { |
| 454 | UNIMPLEMENTED(FATAL); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) { |
| 459 | UNIMPLEMENTED(FATAL); |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | bool Dbg::OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply) { |
| 464 | UNIMPLEMENTED(FATAL); |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf) { |
| 469 | UNIMPLEMENTED(FATAL); |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | JDWP::ObjectId Dbg::CreateString(const char* str) { |
| 474 | UNIMPLEMENTED(FATAL); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) { |
| 479 | UNIMPLEMENTED(FATAL); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) { |
| 484 | UNIMPLEMENTED(FATAL); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
| 489 | UNIMPLEMENTED(FATAL); |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | const char* Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id) { |
| 494 | UNIMPLEMENTED(FATAL); |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | void Dbg::OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 499 | UNIMPLEMENTED(FATAL); |
| 500 | } |
| 501 | |
| 502 | void Dbg::OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 503 | UNIMPLEMENTED(FATAL); |
| 504 | } |
| 505 | |
| 506 | void Dbg::OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) { |
| 507 | UNIMPLEMENTED(FATAL); |
| 508 | } |
| 509 | |
| 510 | void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
| 511 | UNIMPLEMENTED(FATAL); |
| 512 | } |
| 513 | |
| 514 | void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 515 | UNIMPLEMENTED(FATAL); |
| 516 | } |
| 517 | |
| 518 | uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) { |
| 519 | UNIMPLEMENTED(FATAL); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) { |
| 524 | UNIMPLEMENTED(FATAL); |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 529 | UNIMPLEMENTED(FATAL); |
| 530 | } |
| 531 | |
| 532 | void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) { |
| 533 | UNIMPLEMENTED(FATAL); |
| 534 | } |
| 535 | |
| 536 | void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 537 | UNIMPLEMENTED(FATAL); |
| 538 | } |
| 539 | |
| 540 | void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) { |
| 541 | UNIMPLEMENTED(FATAL); |
| 542 | } |
| 543 | |
| 544 | char* Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 545 | UNIMPLEMENTED(FATAL); |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | char* Dbg::GetThreadName(JDWP::ObjectId threadId) { |
| 550 | UNIMPLEMENTED(FATAL); |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) { |
| 555 | UNIMPLEMENTED(FATAL); |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | char* Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 560 | UNIMPLEMENTED(FATAL); |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
| 565 | UNIMPLEMENTED(FATAL); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
| 570 | UNIMPLEMENTED(FATAL); |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
| 575 | UNIMPLEMENTED(FATAL); |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus) { |
| 580 | UNIMPLEMENTED(FATAL); |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) { |
| 585 | UNIMPLEMENTED(FATAL); |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
| 590 | UNIMPLEMENTED(FATAL); |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
| 595 | UNIMPLEMENTED(FATAL); |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | //void Dbg::WaitForSuspend(JDWP::ObjectId threadId); |
| 600 | |
| 601 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 602 | UNIMPLEMENTED(FATAL); |
| 603 | } |
| 604 | |
| 605 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 606 | UNIMPLEMENTED(FATAL); |
| 607 | } |
| 608 | |
| 609 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
| 610 | UNIMPLEMENTED(FATAL); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
| 615 | UNIMPLEMENTED(FATAL); |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 620 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 623 | void Dbg::SuspendVM() { |
| 624 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 628 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
| 632 | UNIMPLEMENTED(FATAL); |
| 633 | } |
| 634 | |
| 635 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
| 636 | UNIMPLEMENTED(FATAL); |
| 637 | } |
| 638 | |
| 639 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 640 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
| 644 | UNIMPLEMENTED(FATAL); |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen) { |
| 649 | UNIMPLEMENTED(FATAL); |
| 650 | } |
| 651 | |
| 652 | void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width) { |
| 653 | UNIMPLEMENTED(FATAL); |
| 654 | } |
| 655 | |
| 656 | void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) { |
| 657 | UNIMPLEMENTED(FATAL); |
| 658 | } |
| 659 | |
| 660 | void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) { |
| 661 | UNIMPLEMENTED(FATAL); |
| 662 | } |
| 663 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 664 | void Dbg::PostClassPrepare(Class* c) { |
| 665 | UNIMPLEMENTED(FATAL); |
| 666 | } |
| 667 | |
| 668 | bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 669 | UNIMPLEMENTED(FATAL); |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 674 | UNIMPLEMENTED(FATAL); |
| 675 | } |
| 676 | |
| 677 | bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) { |
| 678 | UNIMPLEMENTED(FATAL); |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | void Dbg::UnconfigureStep(JDWP::ObjectId threadId) { |
| 683 | UNIMPLEMENTED(FATAL); |
| 684 | } |
| 685 | |
| 686 | 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) { |
| 687 | UNIMPLEMENTED(FATAL); |
| 688 | return JDWP::ERR_NONE; |
| 689 | } |
| 690 | |
| 691 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
| 692 | UNIMPLEMENTED(FATAL); |
| 693 | } |
| 694 | |
| 695 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
| 696 | UNIMPLEMENTED(FATAL); |
| 697 | } |
| 698 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 699 | /* |
| 700 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 701 | * need to process each, accumulate the replies, and ship the whole thing |
| 702 | * back. |
| 703 | * |
| 704 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 705 | * and includes the chunk type/length, followed by the data. |
| 706 | * |
| 707 | * TODO: we currently assume that the request and reply include a single |
| 708 | * chunk. If this becomes inconvenient we will need to adapt. |
| 709 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 710 | 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] | 711 | CHECK_GE(dataLen, 0); |
| 712 | |
| 713 | Thread* self = Thread::Current(); |
| 714 | JNIEnv* env = self->GetJniEnv(); |
| 715 | |
| 716 | static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk"); |
| 717 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 718 | static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", |
| 719 | "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); |
| 720 | static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B"); |
| 721 | static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I"); |
| 722 | static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I"); |
| 723 | static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I"); |
| 724 | |
| 725 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 726 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 727 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 728 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 729 | env->ExceptionClear(); |
| 730 | return false; |
| 731 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 732 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 733 | |
| 734 | const int kChunkHdrLen = 8; |
| 735 | |
| 736 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 737 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 738 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 739 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 740 | jint offset = kChunkHdrLen; |
| 741 | if (offset + length > dataLen) { |
| 742 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | // 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] | 747 | 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] | 748 | if (env->ExceptionCheck()) { |
| 749 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 750 | env->ExceptionDescribe(); |
| 751 | env->ExceptionClear(); |
| 752 | return false; |
| 753 | } |
| 754 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 755 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 756 | return false; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Pull the pieces out of the chunk. We copy the results into a |
| 761 | * newly-allocated buffer that the caller can free. We don't want to |
| 762 | * continue using the Chunk object because nothing has a reference to it. |
| 763 | * |
| 764 | * We could avoid this by returning type/data/offset/length and having |
| 765 | * the caller be aware of the object lifetime issues, but that |
| 766 | * integrates the JDWP code more tightly into the VM, and doesn't work |
| 767 | * if we have responses for multiple chunks. |
| 768 | * |
| 769 | * So we're pretty much stuck with copying data around multiple times. |
| 770 | */ |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 771 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), data_fid))); |
| 772 | length = env->GetIntField(chunk.get(), length_fid); |
| 773 | offset = env->GetIntField(chunk.get(), offset_fid); |
| 774 | type = env->GetIntField(chunk.get(), type_fid); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 775 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 776 | LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
| 777 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 778 | return false; |
| 779 | } |
| 780 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 781 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 782 | if (offset + length > replyLength) { |
| 783 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 788 | if (reply == NULL) { |
| 789 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 790 | return false; |
| 791 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 792 | JDWP::Set4BE(reply + 0, type); |
| 793 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 794 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 795 | |
| 796 | *pReplyBuf = reply; |
| 797 | *pReplyLen = length + kChunkHdrLen; |
| 798 | |
| 799 | LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length); |
| 800 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 803 | void DdmBroadcast(bool connect) { |
| 804 | LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
| 805 | |
| 806 | Thread* self = Thread::Current(); |
| 807 | if (self->GetState() != Thread::kRunnable) { |
| 808 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 809 | /* try anyway? */ |
| 810 | } |
| 811 | |
| 812 | JNIEnv* env = self->GetJniEnv(); |
| 813 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 814 | static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V"); |
| 815 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| 816 | env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event); |
| 817 | if (env->ExceptionCheck()) { |
| 818 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 819 | env->ExceptionDescribe(); |
| 820 | env->ExceptionClear(); |
| 821 | } |
| 822 | } |
| 823 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 824 | void Dbg::DdmConnected() { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 825 | DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | void Dbg::DdmDisconnected() { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 829 | DdmBroadcast(false); |
| 830 | gDdmThreadNotification = false; |
| 831 | } |
| 832 | |
| 833 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 834 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 835 | * |
| 836 | * Because we broadcast the full set of threads when the notifications are |
| 837 | * first enabled, it's possible for "thread" to be actively executing. |
| 838 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 839 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 840 | if (!gDdmThreadNotification) { |
| 841 | return; |
| 842 | } |
| 843 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 844 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 845 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 846 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 847 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 848 | } else { |
| 849 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
| 850 | SirtRef<String> name(t->GetName()); |
| 851 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 852 | const jchar* chars = name->GetCharArray()->GetData(); |
| 853 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame^] | 854 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 855 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
| 856 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame^] | 857 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 858 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 862 | void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 863 | Dbg::DdmSendThreadNotification(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 867 | // We lock the thread list to avoid sending duplicate events or missing |
| 868 | // a thread change. We should be okay holding this lock while sending |
| 869 | // 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] | 870 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 871 | |
| 872 | gDdmThreadNotification = enable; |
| 873 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 874 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 878 | void PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 879 | if (gDebuggerActive) { |
| 880 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 881 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 882 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 883 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 887 | PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 891 | PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 894 | 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] | 895 | CHECK(buf != NULL); |
| 896 | iovec vec[1]; |
| 897 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 898 | vec[0].iov_len = byte_count; |
| 899 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame^] | 902 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 903 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 904 | } |
| 905 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 906 | void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iovcnt) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 907 | if (gJdwpState == NULL) { |
| 908 | LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type; |
| 909 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 910 | gJdwpState->DdmSendChunkV(type, iov, iovcnt); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 911 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 914 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 915 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 916 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 917 | return true; |
| 918 | } |
| 919 | |
| 920 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 921 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | gDdmHpifWhen = when; |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 930 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 931 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 932 | return false; |
| 933 | } |
| 934 | |
| 935 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 936 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | if (native) { |
| 941 | gDdmNhsgWhen = when; |
| 942 | gDdmNhsgWhat = what; |
| 943 | } else { |
| 944 | gDdmHpsgWhen = when; |
| 945 | gDdmHpsgWhat = what; |
| 946 | } |
| 947 | return true; |
| 948 | } |
| 949 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 950 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 951 | // If there's a one-shot 'when', reset it. |
| 952 | if (reason == gDdmHpifWhen) { |
| 953 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 954 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Chunk HPIF (client --> server) |
| 960 | * |
| 961 | * Heap Info. General information about the heap, |
| 962 | * suitable for a summary display. |
| 963 | * |
| 964 | * [u4]: number of heaps |
| 965 | * |
| 966 | * For each heap: |
| 967 | * [u4]: heap ID |
| 968 | * [u8]: timestamp in ms since Unix epoch |
| 969 | * [u1]: capture reason (same as 'when' value from server) |
| 970 | * [u4]: max heap size in bytes (-Xmx) |
| 971 | * [u4]: current heap size in bytes |
| 972 | * [u4]: current number of bytes allocated |
| 973 | * [u4]: current number of objects allocated |
| 974 | */ |
| 975 | uint8_t heap_count = 1; |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame^] | 976 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 977 | JDWP::Append4BE(bytes, heap_count); |
| 978 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| 979 | JDWP::Append8BE(bytes, MilliTime()); |
| 980 | JDWP::Append1BE(bytes, reason); |
| 981 | JDWP::Append4BE(bytes, Heap::GetMaxMemory()); // Max allowed heap size in bytes. |
| 982 | JDWP::Append4BE(bytes, Heap::GetTotalMemory()); // Current heap size in bytes. |
| 983 | JDWP::Append4BE(bytes, Heap::GetBytesAllocated()); |
| 984 | JDWP::Append4BE(bytes, Heap::GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame^] | 985 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 986 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 989 | enum HpsgSolidity { |
| 990 | SOLIDITY_FREE = 0, |
| 991 | SOLIDITY_HARD = 1, |
| 992 | SOLIDITY_SOFT = 2, |
| 993 | SOLIDITY_WEAK = 3, |
| 994 | SOLIDITY_PHANTOM = 4, |
| 995 | SOLIDITY_FINALIZABLE = 5, |
| 996 | SOLIDITY_SWEEP = 6, |
| 997 | }; |
| 998 | |
| 999 | enum HpsgKind { |
| 1000 | KIND_OBJECT = 0, |
| 1001 | KIND_CLASS_OBJECT = 1, |
| 1002 | KIND_ARRAY_1 = 2, |
| 1003 | KIND_ARRAY_2 = 3, |
| 1004 | KIND_ARRAY_4 = 4, |
| 1005 | KIND_ARRAY_8 = 5, |
| 1006 | KIND_UNKNOWN = 6, |
| 1007 | KIND_NATIVE = 7, |
| 1008 | }; |
| 1009 | |
| 1010 | #define HPSG_PARTIAL (1<<7) |
| 1011 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 1012 | |
| 1013 | struct HeapChunkContext { |
| 1014 | std::vector<uint8_t> buf; |
| 1015 | uint8_t* p; |
| 1016 | uint8_t* pieceLenField; |
| 1017 | size_t totalAllocationUnits; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1018 | uint32_t type; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1019 | bool merge; |
| 1020 | bool needHeader; |
| 1021 | |
| 1022 | // Maximum chunk size. Obtain this from the formula: |
| 1023 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 1024 | HeapChunkContext(bool merge, bool native) |
| 1025 | : buf(16384 - 16), |
| 1026 | type(0), |
| 1027 | merge(merge) { |
| 1028 | Reset(); |
| 1029 | if (native) { |
| 1030 | type = CHUNK_TYPE("NHSG"); |
| 1031 | } else { |
| 1032 | type = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | ~HeapChunkContext() { |
| 1037 | if (p > &buf[0]) { |
| 1038 | Flush(); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | void EnsureHeader(const void* chunk_ptr) { |
| 1043 | if (!needHeader) { |
| 1044 | return; |
| 1045 | } |
| 1046 | |
| 1047 | // Start a new HPSx chunk. |
| 1048 | JDWP::Write4BE(&p, 1); // Heap id (bogus; we only have one heap). |
| 1049 | JDWP::Write1BE(&p, 8); // Size of allocation unit, in bytes. |
| 1050 | |
| 1051 | JDWP::Write4BE(&p, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 1052 | JDWP::Write4BE(&p, 0); // offset of this piece (relative to the virtual address). |
| 1053 | // [u4]: length of piece, in allocation units |
| 1054 | // We won't know this until we're done, so save the offset and stuff in a dummy value. |
| 1055 | pieceLenField = p; |
| 1056 | JDWP::Write4BE(&p, 0x55555555); |
| 1057 | needHeader = false; |
| 1058 | } |
| 1059 | |
| 1060 | void Flush() { |
| 1061 | // Patch the "length of piece" field. |
| 1062 | CHECK_LE(&buf[0], pieceLenField); |
| 1063 | CHECK_LE(pieceLenField, p); |
| 1064 | JDWP::Set4BE(pieceLenField, totalAllocationUnits); |
| 1065 | |
| 1066 | Dbg::DdmSendChunk(type, p - &buf[0], &buf[0]); |
| 1067 | Reset(); |
| 1068 | } |
| 1069 | |
| 1070 | private: |
| 1071 | void Reset() { |
| 1072 | p = &buf[0]; |
| 1073 | totalAllocationUnits = 0; |
| 1074 | needHeader = true; |
| 1075 | pieceLenField = NULL; |
| 1076 | } |
| 1077 | |
| 1078 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 1079 | }; |
| 1080 | |
| 1081 | #define ALLOCATION_UNIT_SIZE 8 |
| 1082 | |
| 1083 | uint8_t ExamineObject(const Object* o, bool is_native_heap) { |
| 1084 | if (o == NULL) { |
| 1085 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 1086 | } |
| 1087 | |
| 1088 | // It's an allocated chunk. Figure out what it is. |
| 1089 | |
| 1090 | // If we're looking at the native heap, we'll just return |
| 1091 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
| 1092 | if (is_native_heap || !Heap::IsLiveObjectLocked(o)) { |
| 1093 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 1094 | } |
| 1095 | |
| 1096 | Class* c = o->GetClass(); |
| 1097 | if (c == NULL) { |
| 1098 | // The object was probably just created but hasn't been initialized yet. |
| 1099 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1100 | } |
| 1101 | |
| 1102 | if (!Heap::IsHeapAddress(c)) { |
| 1103 | LOG(WARNING) << "invalid class for managed heap object: " << o << " " << c; |
| 1104 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 1105 | } |
| 1106 | |
| 1107 | if (c->IsClassClass()) { |
| 1108 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 1109 | } |
| 1110 | |
| 1111 | if (c->IsArrayClass()) { |
| 1112 | if (o->IsObjectArray()) { |
| 1113 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1114 | } |
| 1115 | switch (c->GetComponentSize()) { |
| 1116 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 1117 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 1118 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1119 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1124 | } |
| 1125 | |
| 1126 | static void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len, void* arg) { |
| 1127 | HeapChunkContext* context = reinterpret_cast<HeapChunkContext*>(arg); |
| 1128 | |
| 1129 | CHECK_EQ((chunk_len & (ALLOCATION_UNIT_SIZE-1)), 0U); |
| 1130 | |
| 1131 | /* Make sure there's enough room left in the buffer. |
| 1132 | * We need to use two bytes for every fractional 256 |
| 1133 | * allocation units used by the chunk. |
| 1134 | */ |
| 1135 | { |
| 1136 | size_t needed = (((chunk_len/ALLOCATION_UNIT_SIZE + 255) / 256) * 2); |
| 1137 | size_t bytesLeft = context->buf.size() - (size_t)(context->p - &context->buf[0]); |
| 1138 | if (bytesLeft < needed) { |
| 1139 | context->Flush(); |
| 1140 | } |
| 1141 | |
| 1142 | bytesLeft = context->buf.size() - (size_t)(context->p - &context->buf[0]); |
| 1143 | if (bytesLeft < needed) { |
| 1144 | LOG(WARNING) << "chunk is too big to transmit (chunk_len=" << chunk_len << ", " << needed << " bytes)"; |
| 1145 | return; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | // OLD-TODO: notice when there's a gap and start a new heap, or at least a new range. |
| 1150 | context->EnsureHeader(chunk_ptr); |
| 1151 | |
| 1152 | // Determine the type of this chunk. |
| 1153 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 1154 | // If it's the same, we should combine them. |
| 1155 | uint8_t state = ExamineObject(reinterpret_cast<const Object*>(user_ptr), (context->type == CHUNK_TYPE("NHSG"))); |
| 1156 | |
| 1157 | // Write out the chunk description. |
| 1158 | chunk_len /= ALLOCATION_UNIT_SIZE; // convert to allocation units |
| 1159 | context->totalAllocationUnits += chunk_len; |
| 1160 | while (chunk_len > 256) { |
| 1161 | *context->p++ = state | HPSG_PARTIAL; |
| 1162 | *context->p++ = 255; // length - 1 |
| 1163 | chunk_len -= 256; |
| 1164 | } |
| 1165 | *context->p++ = state; |
| 1166 | *context->p++ = chunk_len - 1; |
| 1167 | } |
| 1168 | |
| 1169 | static void WalkHeap(bool merge, bool native) { |
| 1170 | HeapChunkContext context(merge, native); |
| 1171 | if (native) { |
| 1172 | dlmalloc_walk_heap(HeapChunkCallback, &context); |
| 1173 | } else { |
| 1174 | Heap::WalkHeap(HeapChunkCallback, &context); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | void Dbg::DdmSendHeapSegments(bool native) { |
| 1179 | Dbg::HpsgWhen when; |
| 1180 | Dbg::HpsgWhat what; |
| 1181 | if (!native) { |
| 1182 | when = gDdmHpsgWhen; |
| 1183 | what = gDdmHpsgWhat; |
| 1184 | } else { |
| 1185 | when = gDdmNhsgWhen; |
| 1186 | what = gDdmNhsgWhat; |
| 1187 | } |
| 1188 | if (when == HPSG_WHEN_NEVER) { |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | // Figure out what kind of chunks we'll be sending. |
| 1193 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 1194 | |
| 1195 | // First, send a heap start chunk. |
| 1196 | uint8_t heap_id[4]; |
| 1197 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 1198 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 1199 | |
| 1200 | // Send a series of heap segment chunks. |
| 1201 | WalkHeap((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 1202 | |
| 1203 | // Finally, send a heap end chunk. |
| 1204 | 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] | 1205 | } |
| 1206 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1207 | void Dbg::SetAllocTrackingEnabled(bool enabled) { |
| 1208 | MutexLock mu(gAllocTrackerLock); |
| 1209 | if (enabled) { |
| 1210 | if (recent_allocation_records_ == NULL) { |
| 1211 | LOG(INFO) << "Enabling alloc tracker (" << kNumAllocRecords << " entries, " |
| 1212 | << kMaxAllocRecordStackDepth << " frames --> " |
| 1213 | << (sizeof(AllocRecord) * kNumAllocRecords) << " bytes)"; |
| 1214 | gAllocRecordHead = gAllocRecordCount = 0; |
| 1215 | recent_allocation_records_ = new AllocRecord[kNumAllocRecords]; |
| 1216 | CHECK(recent_allocation_records_ != NULL); |
| 1217 | } |
| 1218 | } else { |
| 1219 | delete[] recent_allocation_records_; |
| 1220 | recent_allocation_records_ = NULL; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | struct AllocRecordStackVisitor : public Thread::StackVisitor { |
| 1225 | AllocRecordStackVisitor(AllocRecord* record) : record(record), depth(0) { |
| 1226 | } |
| 1227 | |
| 1228 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
| 1229 | if (depth >= kMaxAllocRecordStackDepth) { |
| 1230 | return; |
| 1231 | } |
| 1232 | Method* m = f.GetMethod(); |
| 1233 | if (m == NULL || m->IsCalleeSaveMethod()) { |
| 1234 | return; |
| 1235 | } |
| 1236 | record->stack[depth].method = m; |
| 1237 | record->stack[depth].raw_pc = pc; |
| 1238 | ++depth; |
| 1239 | } |
| 1240 | |
| 1241 | ~AllocRecordStackVisitor() { |
| 1242 | // Clear out any unused stack trace elements. |
| 1243 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
| 1244 | record->stack[depth].method = NULL; |
| 1245 | record->stack[depth].raw_pc = 0; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | AllocRecord* record; |
| 1250 | size_t depth; |
| 1251 | }; |
| 1252 | |
| 1253 | void Dbg::RecordAllocation(Class* type, size_t byte_count) { |
| 1254 | Thread* self = Thread::Current(); |
| 1255 | CHECK(self != NULL); |
| 1256 | |
| 1257 | MutexLock mu(gAllocTrackerLock); |
| 1258 | if (recent_allocation_records_ == NULL) { |
| 1259 | return; |
| 1260 | } |
| 1261 | |
| 1262 | // Advance and clip. |
| 1263 | if (++gAllocRecordHead == kNumAllocRecords) { |
| 1264 | gAllocRecordHead = 0; |
| 1265 | } |
| 1266 | |
| 1267 | // Fill in the basics. |
| 1268 | AllocRecord* record = &recent_allocation_records_[gAllocRecordHead]; |
| 1269 | record->type = type; |
| 1270 | record->byte_count = byte_count; |
| 1271 | record->thin_lock_id = self->GetThinLockId(); |
| 1272 | |
| 1273 | // Fill in the stack trace. |
| 1274 | AllocRecordStackVisitor visitor(record); |
| 1275 | self->WalkStack(&visitor); |
| 1276 | |
| 1277 | if (gAllocRecordCount < kNumAllocRecords) { |
| 1278 | ++gAllocRecordCount; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Return the index of the head element. |
| 1284 | * |
| 1285 | * We point at the most-recently-written record, so if allocRecordCount is 1 |
| 1286 | * we want to use the current element. Take "head+1" and subtract count |
| 1287 | * from it. |
| 1288 | * |
| 1289 | * We need to handle underflow in our circular buffer, so we add |
| 1290 | * kNumAllocRecords and then mask it back down. |
| 1291 | */ |
| 1292 | inline static int headIndex() { |
| 1293 | return (gAllocRecordHead+1 + kNumAllocRecords - gAllocRecordCount) & (kNumAllocRecords-1); |
| 1294 | } |
| 1295 | |
| 1296 | void Dbg::DumpRecentAllocations() { |
| 1297 | MutexLock mu(gAllocTrackerLock); |
| 1298 | if (recent_allocation_records_ == NULL) { |
| 1299 | LOG(INFO) << "Not recording tracked allocations"; |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | // "i" is the head of the list. We want to start at the end of the |
| 1304 | // list and move forward to the tail. |
| 1305 | size_t i = headIndex(); |
| 1306 | size_t count = gAllocRecordCount; |
| 1307 | |
| 1308 | LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")"; |
| 1309 | while (count--) { |
| 1310 | AllocRecord* record = &recent_allocation_records_[i]; |
| 1311 | |
| 1312 | LOG(INFO) << StringPrintf(" T=%-2d %6d ", record->thin_lock_id, record->byte_count) |
| 1313 | << PrettyClass(record->type); |
| 1314 | |
| 1315 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
| 1316 | const Method* m = record->stack[stack_frame].method; |
| 1317 | if (m == NULL) { |
| 1318 | break; |
| 1319 | } |
| 1320 | LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber(); |
| 1321 | } |
| 1322 | |
| 1323 | // pause periodically to help logcat catch up |
| 1324 | if ((count % 5) == 0) { |
| 1325 | usleep(40000); |
| 1326 | } |
| 1327 | |
| 1328 | i = (i + 1) & (kNumAllocRecords-1); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | class StringTable { |
| 1333 | public: |
| 1334 | StringTable() { |
| 1335 | } |
| 1336 | |
| 1337 | void Add(const String* s) { |
| 1338 | table_.insert(s); |
| 1339 | } |
| 1340 | |
| 1341 | size_t IndexOf(const String* s) { |
| 1342 | return std::distance(table_.begin(), table_.find(s)); |
| 1343 | } |
| 1344 | |
| 1345 | size_t Size() { |
| 1346 | return table_.size(); |
| 1347 | } |
| 1348 | |
| 1349 | void WriteTo(std::vector<uint8_t>& bytes) { |
| 1350 | typedef std::set<const String*>::const_iterator It; // TODO: C++0x auto |
| 1351 | for (It it = table_.begin(); it != table_.end(); ++it) { |
| 1352 | const String* s = *it; |
| 1353 | JDWP::AppendUtf16BE(bytes, s->GetCharArray()->GetData(), s->GetLength()); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | private: |
| 1358 | std::set<const String*> table_; |
| 1359 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 1360 | }; |
| 1361 | |
| 1362 | /* |
| 1363 | * The data we send to DDMS contains everything we have recorded. |
| 1364 | * |
| 1365 | * Message header (all values big-endian): |
| 1366 | * (1b) message header len (to allow future expansion); includes itself |
| 1367 | * (1b) entry header len |
| 1368 | * (1b) stack frame len |
| 1369 | * (2b) number of entries |
| 1370 | * (4b) offset to string table from start of message |
| 1371 | * (2b) number of class name strings |
| 1372 | * (2b) number of method name strings |
| 1373 | * (2b) number of source file name strings |
| 1374 | * For each entry: |
| 1375 | * (4b) total allocation size |
| 1376 | * (2b) threadId |
| 1377 | * (2b) allocated object's class name index |
| 1378 | * (1b) stack depth |
| 1379 | * For each stack frame: |
| 1380 | * (2b) method's class name |
| 1381 | * (2b) method name |
| 1382 | * (2b) method source file |
| 1383 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 1384 | * (xb) class name strings |
| 1385 | * (xb) method name strings |
| 1386 | * (xb) source file strings |
| 1387 | * |
| 1388 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 1389 | * followed by UTF-16 data. |
| 1390 | * |
| 1391 | * We send up 16-bit unsigned indexes into string tables. In theory there |
| 1392 | * can be (kMaxAllocRecordStackDepth * kNumAllocRecords) unique strings in |
| 1393 | * each table, but in practice there should be far fewer. |
| 1394 | * |
| 1395 | * The chief reason for using a string table here is to keep the size of |
| 1396 | * the DDMS message to a minimum. This is partly to make the protocol |
| 1397 | * efficient, but also because we have to form the whole thing up all at |
| 1398 | * once in a memory buffer. |
| 1399 | * |
| 1400 | * We use separate string tables for class names, method names, and source |
| 1401 | * files to keep the indexes small. There will generally be no overlap |
| 1402 | * between the contents of these tables. |
| 1403 | */ |
| 1404 | jbyteArray Dbg::GetRecentAllocations() { |
| 1405 | if (false) { |
| 1406 | DumpRecentAllocations(); |
| 1407 | } |
| 1408 | |
| 1409 | MutexLock mu(gAllocTrackerLock); |
| 1410 | |
| 1411 | /* |
| 1412 | * Part 1: generate string tables. |
| 1413 | */ |
| 1414 | StringTable class_names; |
| 1415 | StringTable method_names; |
| 1416 | StringTable filenames; |
| 1417 | |
| 1418 | int count = gAllocRecordCount; |
| 1419 | int idx = headIndex(); |
| 1420 | while (count--) { |
| 1421 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 1422 | |
| 1423 | class_names.Add(record->type->GetDescriptor()); |
| 1424 | |
| 1425 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
| 1426 | const Method* m = record->stack[i].method; |
| 1427 | if (m != NULL) { |
| 1428 | class_names.Add(m->GetDeclaringClass()->GetDescriptor()); |
| 1429 | method_names.Add(m->GetName()); |
| 1430 | filenames.Add(m->GetDeclaringClass()->GetSourceFile()); |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 1435 | } |
| 1436 | |
| 1437 | LOG(INFO) << "allocation records: " << gAllocRecordCount; |
| 1438 | |
| 1439 | /* |
| 1440 | * Part 2: allocate a buffer and generate the output. |
| 1441 | */ |
| 1442 | std::vector<uint8_t> bytes; |
| 1443 | |
| 1444 | // (1b) message header len (to allow future expansion); includes itself |
| 1445 | // (1b) entry header len |
| 1446 | // (1b) stack frame len |
| 1447 | const int kMessageHeaderLen = 15; |
| 1448 | const int kEntryHeaderLen = 9; |
| 1449 | const int kStackFrameLen = 8; |
| 1450 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 1451 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 1452 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 1453 | |
| 1454 | // (2b) number of entries |
| 1455 | // (4b) offset to string table from start of message |
| 1456 | // (2b) number of class name strings |
| 1457 | // (2b) number of method name strings |
| 1458 | // (2b) number of source file name strings |
| 1459 | JDWP::Append2BE(bytes, gAllocRecordCount); |
| 1460 | size_t string_table_offset = bytes.size(); |
| 1461 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| 1462 | JDWP::Append2BE(bytes, class_names.Size()); |
| 1463 | JDWP::Append2BE(bytes, method_names.Size()); |
| 1464 | JDWP::Append2BE(bytes, filenames.Size()); |
| 1465 | |
| 1466 | count = gAllocRecordCount; |
| 1467 | idx = headIndex(); |
| 1468 | while (count--) { |
| 1469 | // For each entry: |
| 1470 | // (4b) total allocation size |
| 1471 | // (2b) thread id |
| 1472 | // (2b) allocated object's class name index |
| 1473 | // (1b) stack depth |
| 1474 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 1475 | size_t stack_depth = record->GetDepth(); |
| 1476 | JDWP::Append4BE(bytes, record->byte_count); |
| 1477 | JDWP::Append2BE(bytes, record->thin_lock_id); |
| 1478 | JDWP::Append2BE(bytes, class_names.IndexOf(record->type->GetDescriptor())); |
| 1479 | JDWP::Append1BE(bytes, stack_depth); |
| 1480 | |
| 1481 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 1482 | // For each stack frame: |
| 1483 | // (2b) method's class name |
| 1484 | // (2b) method name |
| 1485 | // (2b) method source file |
| 1486 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 1487 | const Method* m = record->stack[stack_frame].method; |
| 1488 | JDWP::Append2BE(bytes, class_names.IndexOf(m->GetDeclaringClass()->GetDescriptor())); |
| 1489 | JDWP::Append2BE(bytes, method_names.IndexOf(m->GetName())); |
| 1490 | JDWP::Append2BE(bytes, filenames.IndexOf(m->GetDeclaringClass()->GetSourceFile())); |
| 1491 | JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber()); |
| 1492 | } |
| 1493 | |
| 1494 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 1495 | } |
| 1496 | |
| 1497 | // (xb) class name strings |
| 1498 | // (xb) method name strings |
| 1499 | // (xb) source file strings |
| 1500 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 1501 | class_names.WriteTo(bytes); |
| 1502 | method_names.WriteTo(bytes); |
| 1503 | filenames.WriteTo(bytes); |
| 1504 | |
| 1505 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 1506 | jbyteArray result = env->NewByteArray(bytes.size()); |
| 1507 | if (result != NULL) { |
| 1508 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 1509 | } |
| 1510 | return result; |
| 1511 | } |
| 1512 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1513 | } // namespace art |