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 | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 21 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 22 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 23 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 24 | #include "thread_list.h" |
| 25 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 26 | extern "C" void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*); |
| 27 | #ifndef HAVE_ANDROID_OS |
| 28 | void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*) { |
| 29 | // No-op for glibc. |
| 30 | } |
| 31 | #endif |
| 32 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 35 | class ObjectRegistry { |
| 36 | public: |
| 37 | ObjectRegistry() : lock_("ObjectRegistry lock") { |
| 38 | } |
| 39 | |
| 40 | JDWP::ObjectId Add(Object* o) { |
| 41 | if (o == NULL) { |
| 42 | return 0; |
| 43 | } |
| 44 | JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o)); |
| 45 | MutexLock mu(lock_); |
| 46 | map_[id] = o; |
| 47 | return id; |
| 48 | } |
| 49 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 50 | void Clear() { |
| 51 | MutexLock mu(lock_); |
| 52 | LOG(DEBUG) << "Debugger has detached; object registry had " << map_.size() << " entries"; |
| 53 | map_.clear(); |
| 54 | } |
| 55 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 56 | bool Contains(JDWP::ObjectId id) { |
| 57 | MutexLock mu(lock_); |
| 58 | return map_.find(id) != map_.end(); |
| 59 | } |
| 60 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 61 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 62 | MutexLock mu(lock_); |
| 63 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 64 | for (It it = map_.begin(); it != map_.end(); ++it) { |
| 65 | visitor(it->second, arg); |
| 66 | } |
| 67 | } |
| 68 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 69 | private: |
| 70 | Mutex lock_; |
| 71 | std::map<JDWP::ObjectId, Object*> map_; |
| 72 | }; |
| 73 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 74 | // JDWP is allowed unless the Zygote forbids it. |
| 75 | static bool gJdwpAllowed = true; |
| 76 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 77 | // Was there a -Xrunjdwp or -agent argument on the command-line? |
| 78 | static bool gJdwpConfigured = false; |
| 79 | |
| 80 | // Broken-down JDWP options. (Only valid if gJdwpConfigured is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 81 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 82 | |
| 83 | // Runtime JDWP state. |
| 84 | static JDWP::JdwpState* gJdwpState = NULL; |
| 85 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 86 | static bool gDebuggerActive; // debugger is making requests. |
| 87 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 88 | static bool gDdmThreadNotification = false; |
| 89 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 90 | // DDMS GC-related settings. |
| 91 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 92 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 93 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 94 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 95 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 96 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 97 | static ObjectRegistry* gRegistry = NULL; |
| 98 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 99 | /* |
| 100 | * Handle one of the JDWP name/value pairs. |
| 101 | * |
| 102 | * JDWP options are: |
| 103 | * help: if specified, show help message and bail |
| 104 | * transport: may be dt_socket or dt_shmem |
| 105 | * address: for dt_socket, "host:port", or just "port" when listening |
| 106 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 107 | * timeout: how long to wait for debugger to connect / listen |
| 108 | * |
| 109 | * Useful with server=n (these aren't supported yet): |
| 110 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 111 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 112 | * launch=<command-line>: launch the debugger itself |
| 113 | * |
| 114 | * The "transport" option is required, as is "address" if server=n. |
| 115 | */ |
| 116 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 117 | if (name == "transport") { |
| 118 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 119 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 120 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 121 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 122 | } else { |
| 123 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 124 | return false; |
| 125 | } |
| 126 | } else if (name == "server") { |
| 127 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 128 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 129 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 130 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 131 | } else { |
| 132 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 133 | return false; |
| 134 | } |
| 135 | } else if (name == "suspend") { |
| 136 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 137 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 138 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 139 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 140 | } else { |
| 141 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 142 | return false; |
| 143 | } |
| 144 | } else if (name == "address") { |
| 145 | /* this is either <port> or <host>:<port> */ |
| 146 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 147 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 148 | std::string::size_type colon = value.find(':'); |
| 149 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 150 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 151 | port_string = value.substr(colon + 1); |
| 152 | } else { |
| 153 | port_string = value; |
| 154 | } |
| 155 | if (port_string.empty()) { |
| 156 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 157 | return false; |
| 158 | } |
| 159 | char* end; |
| 160 | long port = strtol(port_string.c_str(), &end, 10); |
| 161 | if (*end != '\0') { |
| 162 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 163 | return false; |
| 164 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 165 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 166 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 167 | /* valid but unsupported */ |
| 168 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 169 | } else { |
| 170 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 171 | } |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 178 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 179 | */ |
| 180 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 181 | LOG(VERBOSE) << "ParseJdwpOptions: " << options; |
| 182 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 183 | std::vector<std::string> pairs; |
| 184 | Split(options, ',', pairs); |
| 185 | |
| 186 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 187 | std::string::size_type equals = pairs[i].find('='); |
| 188 | if (equals == std::string::npos) { |
| 189 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 190 | return false; |
| 191 | } |
| 192 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 193 | } |
| 194 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 195 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 196 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 197 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 198 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 199 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | gJdwpConfigured = true; |
| 204 | return true; |
| 205 | } |
| 206 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 207 | void Dbg::StartJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 208 | if (!gJdwpAllowed || !gJdwpConfigured) { |
| 209 | // No JDWP for you! |
| 210 | return; |
| 211 | } |
| 212 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 213 | CHECK(gRegistry == NULL); |
| 214 | gRegistry = new ObjectRegistry; |
| 215 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 216 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 217 | // debugger, passively listen for a debugger, or block waiting for a |
| 218 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 219 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 220 | if (gJdwpState == NULL) { |
| 221 | LOG(WARNING) << "debugger thread failed to initialize"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 222 | return; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // If a debugger has already attached, send the "welcome" message. |
| 226 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 227 | if (gJdwpState->IsActive()) { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 228 | //ScopedThreadStateChange(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 229 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 230 | LOG(WARNING) << "failed to post 'start' message to debugger"; |
| 231 | } |
| 232 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 235 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 236 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 237 | delete gRegistry; |
| 238 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 241 | void Dbg::GcDidFinish() { |
| 242 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
| 243 | LOG(DEBUG) << "Sending VM heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 244 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 245 | } |
| 246 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
| 247 | LOG(DEBUG) << "Dumping VM heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 248 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 249 | } |
| 250 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
| 251 | LOG(DEBUG) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 252 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 256 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 257 | gJdwpAllowed = allowed; |
| 258 | } |
| 259 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 260 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 261 | return Thread::Current()->GetInvokeReq(); |
| 262 | } |
| 263 | |
| 264 | Thread* Dbg::GetDebugThread() { |
| 265 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 266 | } |
| 267 | |
| 268 | void Dbg::ClearWaitForEventThread() { |
| 269 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 273 | CHECK(!gDebuggerConnected); |
| 274 | LOG(VERBOSE) << "JDWP has attached"; |
| 275 | gDebuggerConnected = true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void Dbg::Active() { |
| 279 | UNIMPLEMENTED(FATAL); |
| 280 | } |
| 281 | |
| 282 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 283 | CHECK(gDebuggerConnected); |
| 284 | |
| 285 | gDebuggerActive = false; |
| 286 | |
| 287 | //dvmDisableAllSubMode(kSubModeDebuggerActive); |
| 288 | |
| 289 | gRegistry->Clear(); |
| 290 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool Dbg::IsDebuggerConnected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 294 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | bool Dbg::IsDebuggingEnabled() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 298 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | int64_t Dbg::LastDebuggerActivity() { |
| 302 | UNIMPLEMENTED(WARNING); |
| 303 | return -1; |
| 304 | } |
| 305 | |
| 306 | int Dbg::ThreadRunning() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 307 | return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | int Dbg::ThreadWaiting() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 311 | return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 314 | int Dbg::ThreadContinuing(int new_state) { |
| 315 | 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] | 316 | } |
| 317 | |
| 318 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 319 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void Dbg::Exit(int status) { |
| 323 | UNIMPLEMENTED(FATAL); |
| 324 | } |
| 325 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 326 | void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 327 | if (gRegistry != NULL) { |
| 328 | gRegistry->VisitRoots(visitor, arg); |
| 329 | } |
| 330 | } |
| 331 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 332 | const char* Dbg::GetClassDescriptor(JDWP::RefTypeId id) { |
| 333 | UNIMPLEMENTED(FATAL); |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) { |
| 338 | UNIMPLEMENTED(FATAL); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) { |
| 343 | UNIMPLEMENTED(FATAL); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) { |
| 348 | UNIMPLEMENTED(FATAL); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) { |
| 353 | UNIMPLEMENTED(FATAL); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | bool Dbg::IsInterface(JDWP::RefTypeId id) { |
| 358 | UNIMPLEMENTED(FATAL); |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | void Dbg::GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 363 | UNIMPLEMENTED(FATAL); |
| 364 | } |
| 365 | |
| 366 | void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 367 | UNIMPLEMENTED(FATAL); |
| 368 | } |
| 369 | |
| 370 | void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature) { |
| 371 | UNIMPLEMENTED(FATAL); |
| 372 | } |
| 373 | |
| 374 | bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) { |
| 375 | UNIMPLEMENTED(FATAL); |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) { |
| 380 | UNIMPLEMENTED(FATAL); |
| 381 | } |
| 382 | |
| 383 | uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) { |
| 384 | UNIMPLEMENTED(FATAL); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | const char* Dbg::GetSignature(JDWP::RefTypeId refTypeId) { |
| 389 | UNIMPLEMENTED(FATAL); |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | const char* Dbg::GetSourceFile(JDWP::RefTypeId refTypeId) { |
| 394 | UNIMPLEMENTED(FATAL); |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) { |
| 399 | UNIMPLEMENTED(FATAL); |
| 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
| 404 | UNIMPLEMENTED(FATAL); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | int Dbg::GetTagWidth(int tag) { |
| 409 | UNIMPLEMENTED(FATAL); |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | int Dbg::GetArrayLength(JDWP::ObjectId arrayId) { |
| 414 | UNIMPLEMENTED(FATAL); |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) { |
| 419 | UNIMPLEMENTED(FATAL); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | bool Dbg::OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply) { |
| 424 | UNIMPLEMENTED(FATAL); |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf) { |
| 429 | UNIMPLEMENTED(FATAL); |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | JDWP::ObjectId Dbg::CreateString(const char* str) { |
| 434 | UNIMPLEMENTED(FATAL); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) { |
| 439 | UNIMPLEMENTED(FATAL); |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) { |
| 444 | UNIMPLEMENTED(FATAL); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
| 449 | UNIMPLEMENTED(FATAL); |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | const char* Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id) { |
| 454 | UNIMPLEMENTED(FATAL); |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | void Dbg::OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 459 | UNIMPLEMENTED(FATAL); |
| 460 | } |
| 461 | |
| 462 | void Dbg::OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 463 | UNIMPLEMENTED(FATAL); |
| 464 | } |
| 465 | |
| 466 | void Dbg::OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) { |
| 467 | UNIMPLEMENTED(FATAL); |
| 468 | } |
| 469 | |
| 470 | void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
| 471 | UNIMPLEMENTED(FATAL); |
| 472 | } |
| 473 | |
| 474 | void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 475 | UNIMPLEMENTED(FATAL); |
| 476 | } |
| 477 | |
| 478 | uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) { |
| 479 | UNIMPLEMENTED(FATAL); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) { |
| 484 | UNIMPLEMENTED(FATAL); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 489 | UNIMPLEMENTED(FATAL); |
| 490 | } |
| 491 | |
| 492 | void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) { |
| 493 | UNIMPLEMENTED(FATAL); |
| 494 | } |
| 495 | |
| 496 | void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 497 | UNIMPLEMENTED(FATAL); |
| 498 | } |
| 499 | |
| 500 | void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) { |
| 501 | UNIMPLEMENTED(FATAL); |
| 502 | } |
| 503 | |
| 504 | char* Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 505 | UNIMPLEMENTED(FATAL); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | char* Dbg::GetThreadName(JDWP::ObjectId threadId) { |
| 510 | UNIMPLEMENTED(FATAL); |
| 511 | return NULL; |
| 512 | } |
| 513 | |
| 514 | JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) { |
| 515 | UNIMPLEMENTED(FATAL); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | char* Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 520 | UNIMPLEMENTED(FATAL); |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
| 525 | UNIMPLEMENTED(FATAL); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
| 530 | UNIMPLEMENTED(FATAL); |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
| 535 | UNIMPLEMENTED(FATAL); |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus) { |
| 540 | UNIMPLEMENTED(FATAL); |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) { |
| 545 | UNIMPLEMENTED(FATAL); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
| 550 | UNIMPLEMENTED(FATAL); |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
| 555 | UNIMPLEMENTED(FATAL); |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | //void Dbg::WaitForSuspend(JDWP::ObjectId threadId); |
| 560 | |
| 561 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 562 | UNIMPLEMENTED(FATAL); |
| 563 | } |
| 564 | |
| 565 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 566 | UNIMPLEMENTED(FATAL); |
| 567 | } |
| 568 | |
| 569 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
| 570 | UNIMPLEMENTED(FATAL); |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
| 575 | UNIMPLEMENTED(FATAL); |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 580 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 583 | void Dbg::SuspendVM() { |
| 584 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 588 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
| 592 | UNIMPLEMENTED(FATAL); |
| 593 | } |
| 594 | |
| 595 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
| 596 | UNIMPLEMENTED(FATAL); |
| 597 | } |
| 598 | |
| 599 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 600 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
| 604 | UNIMPLEMENTED(FATAL); |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen) { |
| 609 | UNIMPLEMENTED(FATAL); |
| 610 | } |
| 611 | |
| 612 | void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width) { |
| 613 | UNIMPLEMENTED(FATAL); |
| 614 | } |
| 615 | |
| 616 | void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) { |
| 617 | UNIMPLEMENTED(FATAL); |
| 618 | } |
| 619 | |
| 620 | void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) { |
| 621 | UNIMPLEMENTED(FATAL); |
| 622 | } |
| 623 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 624 | void Dbg::PostClassPrepare(Class* c) { |
| 625 | UNIMPLEMENTED(FATAL); |
| 626 | } |
| 627 | |
| 628 | bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 629 | UNIMPLEMENTED(FATAL); |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 634 | UNIMPLEMENTED(FATAL); |
| 635 | } |
| 636 | |
| 637 | bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) { |
| 638 | UNIMPLEMENTED(FATAL); |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | void Dbg::UnconfigureStep(JDWP::ObjectId threadId) { |
| 643 | UNIMPLEMENTED(FATAL); |
| 644 | } |
| 645 | |
| 646 | 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) { |
| 647 | UNIMPLEMENTED(FATAL); |
| 648 | return JDWP::ERR_NONE; |
| 649 | } |
| 650 | |
| 651 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
| 652 | UNIMPLEMENTED(FATAL); |
| 653 | } |
| 654 | |
| 655 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
| 656 | UNIMPLEMENTED(FATAL); |
| 657 | } |
| 658 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 659 | /* |
| 660 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 661 | * need to process each, accumulate the replies, and ship the whole thing |
| 662 | * back. |
| 663 | * |
| 664 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 665 | * and includes the chunk type/length, followed by the data. |
| 666 | * |
| 667 | * TODO: we currently assume that the request and reply include a single |
| 668 | * chunk. If this becomes inconvenient we will need to adapt. |
| 669 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 670 | 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] | 671 | CHECK_GE(dataLen, 0); |
| 672 | |
| 673 | Thread* self = Thread::Current(); |
| 674 | JNIEnv* env = self->GetJniEnv(); |
| 675 | |
| 676 | static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk"); |
| 677 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 678 | static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", |
| 679 | "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); |
| 680 | static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B"); |
| 681 | static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I"); |
| 682 | static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I"); |
| 683 | static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I"); |
| 684 | |
| 685 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 686 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 687 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 688 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 689 | env->ExceptionClear(); |
| 690 | return false; |
| 691 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 692 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 693 | |
| 694 | const int kChunkHdrLen = 8; |
| 695 | |
| 696 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 697 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 698 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 699 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 700 | jint offset = kChunkHdrLen; |
| 701 | if (offset + length > dataLen) { |
| 702 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | // 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] | 707 | 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] | 708 | if (env->ExceptionCheck()) { |
| 709 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 710 | env->ExceptionDescribe(); |
| 711 | env->ExceptionClear(); |
| 712 | return false; |
| 713 | } |
| 714 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 715 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 716 | return false; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Pull the pieces out of the chunk. We copy the results into a |
| 721 | * newly-allocated buffer that the caller can free. We don't want to |
| 722 | * continue using the Chunk object because nothing has a reference to it. |
| 723 | * |
| 724 | * We could avoid this by returning type/data/offset/length and having |
| 725 | * the caller be aware of the object lifetime issues, but that |
| 726 | * integrates the JDWP code more tightly into the VM, and doesn't work |
| 727 | * if we have responses for multiple chunks. |
| 728 | * |
| 729 | * So we're pretty much stuck with copying data around multiple times. |
| 730 | */ |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 731 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), data_fid))); |
| 732 | length = env->GetIntField(chunk.get(), length_fid); |
| 733 | offset = env->GetIntField(chunk.get(), offset_fid); |
| 734 | type = env->GetIntField(chunk.get(), type_fid); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 735 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 736 | LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
| 737 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 738 | return false; |
| 739 | } |
| 740 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 741 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 742 | if (offset + length > replyLength) { |
| 743 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 748 | if (reply == NULL) { |
| 749 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 750 | return false; |
| 751 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 752 | JDWP::Set4BE(reply + 0, type); |
| 753 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 754 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 755 | |
| 756 | *pReplyBuf = reply; |
| 757 | *pReplyLen = length + kChunkHdrLen; |
| 758 | |
| 759 | LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length); |
| 760 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 763 | void DdmBroadcast(bool connect) { |
| 764 | LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
| 765 | |
| 766 | Thread* self = Thread::Current(); |
| 767 | if (self->GetState() != Thread::kRunnable) { |
| 768 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 769 | /* try anyway? */ |
| 770 | } |
| 771 | |
| 772 | JNIEnv* env = self->GetJniEnv(); |
| 773 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 774 | static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V"); |
| 775 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| 776 | env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event); |
| 777 | if (env->ExceptionCheck()) { |
| 778 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 779 | env->ExceptionDescribe(); |
| 780 | env->ExceptionClear(); |
| 781 | } |
| 782 | } |
| 783 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 784 | void Dbg::DdmConnected() { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 785 | DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void Dbg::DdmDisconnected() { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 789 | DdmBroadcast(false); |
| 790 | gDdmThreadNotification = false; |
| 791 | } |
| 792 | |
| 793 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 794 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 795 | * |
| 796 | * Because we broadcast the full set of threads when the notifications are |
| 797 | * first enabled, it's possible for "thread" to be actively executing. |
| 798 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 799 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 800 | if (!gDdmThreadNotification) { |
| 801 | return; |
| 802 | } |
| 803 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 804 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 805 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 806 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 807 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 808 | } else { |
| 809 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
| 810 | SirtRef<String> name(t->GetName()); |
| 811 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 812 | const jchar* chars = name->GetCharArray()->GetData(); |
| 813 | |
| 814 | size_t byte_count = char_count*2 + sizeof(uint32_t)*2; |
| 815 | std::vector<uint8_t> bytes(byte_count); |
| 816 | uint8_t* dst = &bytes[0]; |
| 817 | JDWP::Write4BE(&dst, t->GetThinLockId()); |
| 818 | JDWP::Write4BE(&dst, char_count); |
| 819 | if (char_count > 0) { |
| 820 | // Copy the UTF-16 string, transforming to big-endian. |
| 821 | while (char_count--) { |
| 822 | JDWP::Write2BE(&dst, *chars++); |
| 823 | } |
| 824 | } |
| 825 | Dbg::DdmSendChunk(type, bytes.size(), &bytes[0]); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 829 | void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 830 | Dbg::DdmSendThreadNotification(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 834 | // We lock the thread list to avoid sending duplicate events or missing |
| 835 | // a thread change. We should be okay holding this lock while sending |
| 836 | // 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] | 837 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 838 | |
| 839 | gDdmThreadNotification = enable; |
| 840 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 841 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 845 | void PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 846 | if (gDebuggerActive) { |
| 847 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 848 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 849 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 850 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 854 | PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 858 | PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 861 | 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] | 862 | CHECK(buf != NULL); |
| 863 | iovec vec[1]; |
| 864 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 865 | vec[0].iov_len = byte_count; |
| 866 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 869 | void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iovcnt) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 870 | if (gJdwpState == NULL) { |
| 871 | LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type; |
| 872 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 873 | gJdwpState->DdmSendChunkV(type, iov, iovcnt); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 874 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 877 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 878 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 879 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 880 | return true; |
| 881 | } |
| 882 | |
| 883 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 884 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 885 | return false; |
| 886 | } |
| 887 | |
| 888 | gDdmHpifWhen = when; |
| 889 | return true; |
| 890 | } |
| 891 | |
| 892 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 893 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 894 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 899 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | if (native) { |
| 904 | gDdmNhsgWhen = when; |
| 905 | gDdmNhsgWhat = what; |
| 906 | } else { |
| 907 | gDdmHpsgWhen = when; |
| 908 | gDdmHpsgWhat = what; |
| 909 | } |
| 910 | return true; |
| 911 | } |
| 912 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 913 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 914 | // If there's a one-shot 'when', reset it. |
| 915 | if (reason == gDdmHpifWhen) { |
| 916 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 917 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * Chunk HPIF (client --> server) |
| 923 | * |
| 924 | * Heap Info. General information about the heap, |
| 925 | * suitable for a summary display. |
| 926 | * |
| 927 | * [u4]: number of heaps |
| 928 | * |
| 929 | * For each heap: |
| 930 | * [u4]: heap ID |
| 931 | * [u8]: timestamp in ms since Unix epoch |
| 932 | * [u1]: capture reason (same as 'when' value from server) |
| 933 | * [u4]: max heap size in bytes (-Xmx) |
| 934 | * [u4]: current heap size in bytes |
| 935 | * [u4]: current number of bytes allocated |
| 936 | * [u4]: current number of objects allocated |
| 937 | */ |
| 938 | uint8_t heap_count = 1; |
| 939 | std::vector<uint8_t> bytes(4 + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 940 | uint8_t* dst = &bytes[0]; |
| 941 | JDWP::Write4BE(&dst, heap_count); |
| 942 | JDWP::Write4BE(&dst, 1); // Heap id (bogus; we only have one heap). |
| 943 | JDWP::Write8BE(&dst, MilliTime()); |
| 944 | JDWP::Write1BE(&dst, reason); |
| 945 | JDWP::Write4BE(&dst, Heap::GetMaxMemory()); // Max allowed heap size in bytes. |
| 946 | JDWP::Write4BE(&dst, Heap::GetTotalMemory()); // Current heap size in bytes. |
| 947 | JDWP::Write4BE(&dst, Heap::GetBytesAllocated()); |
| 948 | JDWP::Write4BE(&dst, Heap::GetObjectsAllocated()); |
| 949 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes.size(), &bytes[0]); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 952 | enum HpsgSolidity { |
| 953 | SOLIDITY_FREE = 0, |
| 954 | SOLIDITY_HARD = 1, |
| 955 | SOLIDITY_SOFT = 2, |
| 956 | SOLIDITY_WEAK = 3, |
| 957 | SOLIDITY_PHANTOM = 4, |
| 958 | SOLIDITY_FINALIZABLE = 5, |
| 959 | SOLIDITY_SWEEP = 6, |
| 960 | }; |
| 961 | |
| 962 | enum HpsgKind { |
| 963 | KIND_OBJECT = 0, |
| 964 | KIND_CLASS_OBJECT = 1, |
| 965 | KIND_ARRAY_1 = 2, |
| 966 | KIND_ARRAY_2 = 3, |
| 967 | KIND_ARRAY_4 = 4, |
| 968 | KIND_ARRAY_8 = 5, |
| 969 | KIND_UNKNOWN = 6, |
| 970 | KIND_NATIVE = 7, |
| 971 | }; |
| 972 | |
| 973 | #define HPSG_PARTIAL (1<<7) |
| 974 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 975 | |
| 976 | struct HeapChunkContext { |
| 977 | std::vector<uint8_t> buf; |
| 978 | uint8_t* p; |
| 979 | uint8_t* pieceLenField; |
| 980 | size_t totalAllocationUnits; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame^] | 981 | uint32_t type; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 982 | bool merge; |
| 983 | bool needHeader; |
| 984 | |
| 985 | // Maximum chunk size. Obtain this from the formula: |
| 986 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 987 | HeapChunkContext(bool merge, bool native) |
| 988 | : buf(16384 - 16), |
| 989 | type(0), |
| 990 | merge(merge) { |
| 991 | Reset(); |
| 992 | if (native) { |
| 993 | type = CHUNK_TYPE("NHSG"); |
| 994 | } else { |
| 995 | type = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | ~HeapChunkContext() { |
| 1000 | if (p > &buf[0]) { |
| 1001 | Flush(); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | void EnsureHeader(const void* chunk_ptr) { |
| 1006 | if (!needHeader) { |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | // Start a new HPSx chunk. |
| 1011 | JDWP::Write4BE(&p, 1); // Heap id (bogus; we only have one heap). |
| 1012 | JDWP::Write1BE(&p, 8); // Size of allocation unit, in bytes. |
| 1013 | |
| 1014 | JDWP::Write4BE(&p, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 1015 | JDWP::Write4BE(&p, 0); // offset of this piece (relative to the virtual address). |
| 1016 | // [u4]: length of piece, in allocation units |
| 1017 | // We won't know this until we're done, so save the offset and stuff in a dummy value. |
| 1018 | pieceLenField = p; |
| 1019 | JDWP::Write4BE(&p, 0x55555555); |
| 1020 | needHeader = false; |
| 1021 | } |
| 1022 | |
| 1023 | void Flush() { |
| 1024 | // Patch the "length of piece" field. |
| 1025 | CHECK_LE(&buf[0], pieceLenField); |
| 1026 | CHECK_LE(pieceLenField, p); |
| 1027 | JDWP::Set4BE(pieceLenField, totalAllocationUnits); |
| 1028 | |
| 1029 | Dbg::DdmSendChunk(type, p - &buf[0], &buf[0]); |
| 1030 | Reset(); |
| 1031 | } |
| 1032 | |
| 1033 | private: |
| 1034 | void Reset() { |
| 1035 | p = &buf[0]; |
| 1036 | totalAllocationUnits = 0; |
| 1037 | needHeader = true; |
| 1038 | pieceLenField = NULL; |
| 1039 | } |
| 1040 | |
| 1041 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 1042 | }; |
| 1043 | |
| 1044 | #define ALLOCATION_UNIT_SIZE 8 |
| 1045 | |
| 1046 | uint8_t ExamineObject(const Object* o, bool is_native_heap) { |
| 1047 | if (o == NULL) { |
| 1048 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 1049 | } |
| 1050 | |
| 1051 | // It's an allocated chunk. Figure out what it is. |
| 1052 | |
| 1053 | // If we're looking at the native heap, we'll just return |
| 1054 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
| 1055 | if (is_native_heap || !Heap::IsLiveObjectLocked(o)) { |
| 1056 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 1057 | } |
| 1058 | |
| 1059 | Class* c = o->GetClass(); |
| 1060 | if (c == NULL) { |
| 1061 | // The object was probably just created but hasn't been initialized yet. |
| 1062 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1063 | } |
| 1064 | |
| 1065 | if (!Heap::IsHeapAddress(c)) { |
| 1066 | LOG(WARNING) << "invalid class for managed heap object: " << o << " " << c; |
| 1067 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 1068 | } |
| 1069 | |
| 1070 | if (c->IsClassClass()) { |
| 1071 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 1072 | } |
| 1073 | |
| 1074 | if (c->IsArrayClass()) { |
| 1075 | if (o->IsObjectArray()) { |
| 1076 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1077 | } |
| 1078 | switch (c->GetComponentSize()) { |
| 1079 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 1080 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 1081 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 1082 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 1087 | } |
| 1088 | |
| 1089 | static void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len, void* arg) { |
| 1090 | HeapChunkContext* context = reinterpret_cast<HeapChunkContext*>(arg); |
| 1091 | |
| 1092 | CHECK_EQ((chunk_len & (ALLOCATION_UNIT_SIZE-1)), 0U); |
| 1093 | |
| 1094 | /* Make sure there's enough room left in the buffer. |
| 1095 | * We need to use two bytes for every fractional 256 |
| 1096 | * allocation units used by the chunk. |
| 1097 | */ |
| 1098 | { |
| 1099 | size_t needed = (((chunk_len/ALLOCATION_UNIT_SIZE + 255) / 256) * 2); |
| 1100 | size_t bytesLeft = context->buf.size() - (size_t)(context->p - &context->buf[0]); |
| 1101 | if (bytesLeft < needed) { |
| 1102 | context->Flush(); |
| 1103 | } |
| 1104 | |
| 1105 | bytesLeft = context->buf.size() - (size_t)(context->p - &context->buf[0]); |
| 1106 | if (bytesLeft < needed) { |
| 1107 | LOG(WARNING) << "chunk is too big to transmit (chunk_len=" << chunk_len << ", " << needed << " bytes)"; |
| 1108 | return; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | // OLD-TODO: notice when there's a gap and start a new heap, or at least a new range. |
| 1113 | context->EnsureHeader(chunk_ptr); |
| 1114 | |
| 1115 | // Determine the type of this chunk. |
| 1116 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 1117 | // If it's the same, we should combine them. |
| 1118 | uint8_t state = ExamineObject(reinterpret_cast<const Object*>(user_ptr), (context->type == CHUNK_TYPE("NHSG"))); |
| 1119 | |
| 1120 | // Write out the chunk description. |
| 1121 | chunk_len /= ALLOCATION_UNIT_SIZE; // convert to allocation units |
| 1122 | context->totalAllocationUnits += chunk_len; |
| 1123 | while (chunk_len > 256) { |
| 1124 | *context->p++ = state | HPSG_PARTIAL; |
| 1125 | *context->p++ = 255; // length - 1 |
| 1126 | chunk_len -= 256; |
| 1127 | } |
| 1128 | *context->p++ = state; |
| 1129 | *context->p++ = chunk_len - 1; |
| 1130 | } |
| 1131 | |
| 1132 | static void WalkHeap(bool merge, bool native) { |
| 1133 | HeapChunkContext context(merge, native); |
| 1134 | if (native) { |
| 1135 | dlmalloc_walk_heap(HeapChunkCallback, &context); |
| 1136 | } else { |
| 1137 | Heap::WalkHeap(HeapChunkCallback, &context); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | void Dbg::DdmSendHeapSegments(bool native) { |
| 1142 | Dbg::HpsgWhen when; |
| 1143 | Dbg::HpsgWhat what; |
| 1144 | if (!native) { |
| 1145 | when = gDdmHpsgWhen; |
| 1146 | what = gDdmHpsgWhat; |
| 1147 | } else { |
| 1148 | when = gDdmNhsgWhen; |
| 1149 | what = gDdmNhsgWhat; |
| 1150 | } |
| 1151 | if (when == HPSG_WHEN_NEVER) { |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | // Figure out what kind of chunks we'll be sending. |
| 1156 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 1157 | |
| 1158 | // First, send a heap start chunk. |
| 1159 | uint8_t heap_id[4]; |
| 1160 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 1161 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 1162 | |
| 1163 | // Send a series of heap segment chunks. |
| 1164 | WalkHeap((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 1165 | |
| 1166 | // Finally, send a heap end chunk. |
| 1167 | 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] | 1168 | } |
| 1169 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1170 | } // namespace art |