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 | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 21 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 22 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 23 | #include "thread_list.h" |
| 24 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 27 | class ObjectRegistry { |
| 28 | public: |
| 29 | ObjectRegistry() : lock_("ObjectRegistry lock") { |
| 30 | } |
| 31 | |
| 32 | JDWP::ObjectId Add(Object* o) { |
| 33 | if (o == NULL) { |
| 34 | return 0; |
| 35 | } |
| 36 | JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o)); |
| 37 | MutexLock mu(lock_); |
| 38 | map_[id] = o; |
| 39 | return id; |
| 40 | } |
| 41 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 42 | void Clear() { |
| 43 | MutexLock mu(lock_); |
| 44 | LOG(DEBUG) << "Debugger has detached; object registry had " << map_.size() << " entries"; |
| 45 | map_.clear(); |
| 46 | } |
| 47 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 48 | bool Contains(JDWP::ObjectId id) { |
| 49 | MutexLock mu(lock_); |
| 50 | return map_.find(id) != map_.end(); |
| 51 | } |
| 52 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame^] | 53 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 54 | MutexLock mu(lock_); |
| 55 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 56 | for (It it = map_.begin(); it != map_.end(); ++it) { |
| 57 | visitor(it->second, arg); |
| 58 | } |
| 59 | } |
| 60 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 61 | private: |
| 62 | Mutex lock_; |
| 63 | std::map<JDWP::ObjectId, Object*> map_; |
| 64 | }; |
| 65 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 66 | // JDWP is allowed unless the Zygote forbids it. |
| 67 | static bool gJdwpAllowed = true; |
| 68 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 69 | // Was there a -Xrunjdwp or -agent argument on the command-line? |
| 70 | static bool gJdwpConfigured = false; |
| 71 | |
| 72 | // Broken-down JDWP options. (Only valid if gJdwpConfigured is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 73 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 74 | |
| 75 | // Runtime JDWP state. |
| 76 | static JDWP::JdwpState* gJdwpState = NULL; |
| 77 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 78 | static bool gDebuggerActive; // debugger is making requests. |
| 79 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 80 | static bool gDdmThreadNotification = false; |
| 81 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 82 | static ObjectRegistry* gRegistry = NULL; |
| 83 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 84 | /* |
| 85 | * Handle one of the JDWP name/value pairs. |
| 86 | * |
| 87 | * JDWP options are: |
| 88 | * help: if specified, show help message and bail |
| 89 | * transport: may be dt_socket or dt_shmem |
| 90 | * address: for dt_socket, "host:port", or just "port" when listening |
| 91 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 92 | * timeout: how long to wait for debugger to connect / listen |
| 93 | * |
| 94 | * Useful with server=n (these aren't supported yet): |
| 95 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 96 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 97 | * launch=<command-line>: launch the debugger itself |
| 98 | * |
| 99 | * The "transport" option is required, as is "address" if server=n. |
| 100 | */ |
| 101 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 102 | if (name == "transport") { |
| 103 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 104 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 105 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 106 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 107 | } else { |
| 108 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 109 | return false; |
| 110 | } |
| 111 | } else if (name == "server") { |
| 112 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 113 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 114 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 115 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 116 | } else { |
| 117 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 118 | return false; |
| 119 | } |
| 120 | } else if (name == "suspend") { |
| 121 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 122 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 123 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 124 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 125 | } else { |
| 126 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 127 | return false; |
| 128 | } |
| 129 | } else if (name == "address") { |
| 130 | /* this is either <port> or <host>:<port> */ |
| 131 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 132 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 133 | std::string::size_type colon = value.find(':'); |
| 134 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 135 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 136 | port_string = value.substr(colon + 1); |
| 137 | } else { |
| 138 | port_string = value; |
| 139 | } |
| 140 | if (port_string.empty()) { |
| 141 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 142 | return false; |
| 143 | } |
| 144 | char* end; |
| 145 | long port = strtol(port_string.c_str(), &end, 10); |
| 146 | if (*end != '\0') { |
| 147 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 148 | return false; |
| 149 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 150 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 151 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 152 | /* valid but unsupported */ |
| 153 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 154 | } else { |
| 155 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 163 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 164 | */ |
| 165 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 166 | LOG(VERBOSE) << "ParseJdwpOptions: " << options; |
| 167 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 168 | std::vector<std::string> pairs; |
| 169 | Split(options, ',', pairs); |
| 170 | |
| 171 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 172 | std::string::size_type equals = pairs[i].find('='); |
| 173 | if (equals == std::string::npos) { |
| 174 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 175 | return false; |
| 176 | } |
| 177 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 178 | } |
| 179 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 180 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 181 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 182 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 183 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 184 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | gJdwpConfigured = true; |
| 189 | return true; |
| 190 | } |
| 191 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 192 | void Dbg::StartJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 193 | if (!gJdwpAllowed || !gJdwpConfigured) { |
| 194 | // No JDWP for you! |
| 195 | return; |
| 196 | } |
| 197 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 198 | CHECK(gRegistry == NULL); |
| 199 | gRegistry = new ObjectRegistry; |
| 200 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 201 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 202 | // debugger, passively listen for a debugger, or block waiting for a |
| 203 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 204 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 205 | if (gJdwpState == NULL) { |
| 206 | LOG(WARNING) << "debugger thread failed to initialize"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 207 | return; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // If a debugger has already attached, send the "welcome" message. |
| 211 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 212 | if (gJdwpState->IsActive()) { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 213 | //ScopedThreadStateChange(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 214 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 215 | LOG(WARNING) << "failed to post 'start' message to debugger"; |
| 216 | } |
| 217 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 220 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 221 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 222 | delete gRegistry; |
| 223 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 226 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 227 | gJdwpAllowed = allowed; |
| 228 | } |
| 229 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 230 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 231 | return Thread::Current()->GetInvokeReq(); |
| 232 | } |
| 233 | |
| 234 | Thread* Dbg::GetDebugThread() { |
| 235 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 236 | } |
| 237 | |
| 238 | void Dbg::ClearWaitForEventThread() { |
| 239 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 243 | CHECK(!gDebuggerConnected); |
| 244 | LOG(VERBOSE) << "JDWP has attached"; |
| 245 | gDebuggerConnected = true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void Dbg::Active() { |
| 249 | UNIMPLEMENTED(FATAL); |
| 250 | } |
| 251 | |
| 252 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 253 | CHECK(gDebuggerConnected); |
| 254 | |
| 255 | gDebuggerActive = false; |
| 256 | |
| 257 | //dvmDisableAllSubMode(kSubModeDebuggerActive); |
| 258 | |
| 259 | gRegistry->Clear(); |
| 260 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | bool Dbg::IsDebuggerConnected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 264 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | bool Dbg::IsDebuggingEnabled() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 268 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | int64_t Dbg::LastDebuggerActivity() { |
| 272 | UNIMPLEMENTED(WARNING); |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | int Dbg::ThreadRunning() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 277 | return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | int Dbg::ThreadWaiting() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 281 | return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 284 | int Dbg::ThreadContinuing(int new_state) { |
| 285 | 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] | 286 | } |
| 287 | |
| 288 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 289 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void Dbg::Exit(int status) { |
| 293 | UNIMPLEMENTED(FATAL); |
| 294 | } |
| 295 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame^] | 296 | void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 297 | if (gRegistry != NULL) { |
| 298 | gRegistry->VisitRoots(visitor, arg); |
| 299 | } |
| 300 | } |
| 301 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 302 | const char* Dbg::GetClassDescriptor(JDWP::RefTypeId id) { |
| 303 | UNIMPLEMENTED(FATAL); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) { |
| 308 | UNIMPLEMENTED(FATAL); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) { |
| 313 | UNIMPLEMENTED(FATAL); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) { |
| 318 | UNIMPLEMENTED(FATAL); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) { |
| 323 | UNIMPLEMENTED(FATAL); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | bool Dbg::IsInterface(JDWP::RefTypeId id) { |
| 328 | UNIMPLEMENTED(FATAL); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | void Dbg::GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 333 | UNIMPLEMENTED(FATAL); |
| 334 | } |
| 335 | |
| 336 | void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 337 | UNIMPLEMENTED(FATAL); |
| 338 | } |
| 339 | |
| 340 | void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature) { |
| 341 | UNIMPLEMENTED(FATAL); |
| 342 | } |
| 343 | |
| 344 | bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) { |
| 345 | UNIMPLEMENTED(FATAL); |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) { |
| 350 | UNIMPLEMENTED(FATAL); |
| 351 | } |
| 352 | |
| 353 | uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) { |
| 354 | UNIMPLEMENTED(FATAL); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | const char* Dbg::GetSignature(JDWP::RefTypeId refTypeId) { |
| 359 | UNIMPLEMENTED(FATAL); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | const char* Dbg::GetSourceFile(JDWP::RefTypeId refTypeId) { |
| 364 | UNIMPLEMENTED(FATAL); |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) { |
| 369 | UNIMPLEMENTED(FATAL); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
| 374 | UNIMPLEMENTED(FATAL); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | int Dbg::GetTagWidth(int tag) { |
| 379 | UNIMPLEMENTED(FATAL); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | int Dbg::GetArrayLength(JDWP::ObjectId arrayId) { |
| 384 | UNIMPLEMENTED(FATAL); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) { |
| 389 | UNIMPLEMENTED(FATAL); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | bool Dbg::OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply) { |
| 394 | UNIMPLEMENTED(FATAL); |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf) { |
| 399 | UNIMPLEMENTED(FATAL); |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | JDWP::ObjectId Dbg::CreateString(const char* str) { |
| 404 | UNIMPLEMENTED(FATAL); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) { |
| 409 | UNIMPLEMENTED(FATAL); |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) { |
| 414 | UNIMPLEMENTED(FATAL); |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
| 419 | UNIMPLEMENTED(FATAL); |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | const char* Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id) { |
| 424 | UNIMPLEMENTED(FATAL); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | void Dbg::OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 429 | UNIMPLEMENTED(FATAL); |
| 430 | } |
| 431 | |
| 432 | void Dbg::OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 433 | UNIMPLEMENTED(FATAL); |
| 434 | } |
| 435 | |
| 436 | void Dbg::OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) { |
| 437 | UNIMPLEMENTED(FATAL); |
| 438 | } |
| 439 | |
| 440 | void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
| 441 | UNIMPLEMENTED(FATAL); |
| 442 | } |
| 443 | |
| 444 | void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply) { |
| 445 | UNIMPLEMENTED(FATAL); |
| 446 | } |
| 447 | |
| 448 | uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) { |
| 449 | UNIMPLEMENTED(FATAL); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) { |
| 454 | UNIMPLEMENTED(FATAL); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 459 | UNIMPLEMENTED(FATAL); |
| 460 | } |
| 461 | |
| 462 | void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) { |
| 463 | UNIMPLEMENTED(FATAL); |
| 464 | } |
| 465 | |
| 466 | void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 467 | UNIMPLEMENTED(FATAL); |
| 468 | } |
| 469 | |
| 470 | void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) { |
| 471 | UNIMPLEMENTED(FATAL); |
| 472 | } |
| 473 | |
| 474 | char* Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 475 | UNIMPLEMENTED(FATAL); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | char* Dbg::GetThreadName(JDWP::ObjectId threadId) { |
| 480 | UNIMPLEMENTED(FATAL); |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) { |
| 485 | UNIMPLEMENTED(FATAL); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | char* Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 490 | UNIMPLEMENTED(FATAL); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
| 495 | UNIMPLEMENTED(FATAL); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
| 500 | UNIMPLEMENTED(FATAL); |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
| 505 | UNIMPLEMENTED(FATAL); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus) { |
| 510 | UNIMPLEMENTED(FATAL); |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) { |
| 515 | UNIMPLEMENTED(FATAL); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
| 520 | UNIMPLEMENTED(FATAL); |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
| 525 | UNIMPLEMENTED(FATAL); |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | //void Dbg::WaitForSuspend(JDWP::ObjectId threadId); |
| 530 | |
| 531 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 532 | UNIMPLEMENTED(FATAL); |
| 533 | } |
| 534 | |
| 535 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 536 | UNIMPLEMENTED(FATAL); |
| 537 | } |
| 538 | |
| 539 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
| 540 | UNIMPLEMENTED(FATAL); |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
| 545 | UNIMPLEMENTED(FATAL); |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 550 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 553 | void Dbg::SuspendVM() { |
| 554 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 558 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
| 562 | UNIMPLEMENTED(FATAL); |
| 563 | } |
| 564 | |
| 565 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
| 566 | UNIMPLEMENTED(FATAL); |
| 567 | } |
| 568 | |
| 569 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 570 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
| 574 | UNIMPLEMENTED(FATAL); |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen) { |
| 579 | UNIMPLEMENTED(FATAL); |
| 580 | } |
| 581 | |
| 582 | void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width) { |
| 583 | UNIMPLEMENTED(FATAL); |
| 584 | } |
| 585 | |
| 586 | void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) { |
| 587 | UNIMPLEMENTED(FATAL); |
| 588 | } |
| 589 | |
| 590 | void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) { |
| 591 | UNIMPLEMENTED(FATAL); |
| 592 | } |
| 593 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 594 | void Dbg::PostClassPrepare(Class* c) { |
| 595 | UNIMPLEMENTED(FATAL); |
| 596 | } |
| 597 | |
| 598 | bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 599 | UNIMPLEMENTED(FATAL); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 604 | UNIMPLEMENTED(FATAL); |
| 605 | } |
| 606 | |
| 607 | bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) { |
| 608 | UNIMPLEMENTED(FATAL); |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | void Dbg::UnconfigureStep(JDWP::ObjectId threadId) { |
| 613 | UNIMPLEMENTED(FATAL); |
| 614 | } |
| 615 | |
| 616 | 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) { |
| 617 | UNIMPLEMENTED(FATAL); |
| 618 | return JDWP::ERR_NONE; |
| 619 | } |
| 620 | |
| 621 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
| 622 | UNIMPLEMENTED(FATAL); |
| 623 | } |
| 624 | |
| 625 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
| 626 | UNIMPLEMENTED(FATAL); |
| 627 | } |
| 628 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 629 | /* |
| 630 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 631 | * need to process each, accumulate the replies, and ship the whole thing |
| 632 | * back. |
| 633 | * |
| 634 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 635 | * and includes the chunk type/length, followed by the data. |
| 636 | * |
| 637 | * TODO: we currently assume that the request and reply include a single |
| 638 | * chunk. If this becomes inconvenient we will need to adapt. |
| 639 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 640 | 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] | 641 | CHECK_GE(dataLen, 0); |
| 642 | |
| 643 | Thread* self = Thread::Current(); |
| 644 | JNIEnv* env = self->GetJniEnv(); |
| 645 | |
| 646 | static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk"); |
| 647 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 648 | static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", |
| 649 | "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); |
| 650 | static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B"); |
| 651 | static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I"); |
| 652 | static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I"); |
| 653 | static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I"); |
| 654 | |
| 655 | // Create a byte[] corresponding to 'buf'. |
| 656 | jbyteArray dataArray = env->NewByteArray(dataLen); |
| 657 | if (dataArray == NULL) { |
| 658 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 659 | env->ExceptionClear(); |
| 660 | return false; |
| 661 | } |
| 662 | env->SetByteArrayRegion(dataArray, 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
| 663 | |
| 664 | const int kChunkHdrLen = 8; |
| 665 | |
| 666 | // Run through and find all chunks. [Currently just find the first.] |
| 667 | ScopedByteArrayRO contents(env, dataArray); |
| 668 | jint type = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 669 | jint length = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
| 670 | jint offset = kChunkHdrLen; |
| 671 | if (offset + length > dataLen) { |
| 672 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)". |
| 677 | jobject chunk = env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray, offset, length); |
| 678 | if (env->ExceptionCheck()) { |
| 679 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 680 | env->ExceptionDescribe(); |
| 681 | env->ExceptionClear(); |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | if (chunk == NULL) { |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Pull the pieces out of the chunk. We copy the results into a |
| 691 | * newly-allocated buffer that the caller can free. We don't want to |
| 692 | * continue using the Chunk object because nothing has a reference to it. |
| 693 | * |
| 694 | * We could avoid this by returning type/data/offset/length and having |
| 695 | * the caller be aware of the object lifetime issues, but that |
| 696 | * integrates the JDWP code more tightly into the VM, and doesn't work |
| 697 | * if we have responses for multiple chunks. |
| 698 | * |
| 699 | * So we're pretty much stuck with copying data around multiple times. |
| 700 | */ |
| 701 | jbyteArray replyData = reinterpret_cast<jbyteArray>(env->GetObjectField(chunk, data_fid)); |
| 702 | length = env->GetIntField(chunk, length_fid); |
| 703 | offset = env->GetIntField(chunk, offset_fid); |
| 704 | type = env->GetIntField(chunk, type_fid); |
| 705 | |
| 706 | LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData, offset, length); |
| 707 | if (length == 0 || replyData == NULL) { |
| 708 | return false; |
| 709 | } |
| 710 | |
| 711 | jsize replyLength = env->GetArrayLength(replyData); |
| 712 | if (offset + length > replyLength) { |
| 713 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 718 | if (reply == NULL) { |
| 719 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 720 | return false; |
| 721 | } |
| 722 | JDWP::set4BE(reply + 0, type); |
| 723 | JDWP::set4BE(reply + 4, length); |
| 724 | env->GetByteArrayRegion(replyData, offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
| 725 | |
| 726 | *pReplyBuf = reply; |
| 727 | *pReplyLen = length + kChunkHdrLen; |
| 728 | |
| 729 | LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length); |
| 730 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 733 | void DdmBroadcast(bool connect) { |
| 734 | LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
| 735 | |
| 736 | Thread* self = Thread::Current(); |
| 737 | if (self->GetState() != Thread::kRunnable) { |
| 738 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 739 | /* try anyway? */ |
| 740 | } |
| 741 | |
| 742 | JNIEnv* env = self->GetJniEnv(); |
| 743 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 744 | static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V"); |
| 745 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| 746 | env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event); |
| 747 | if (env->ExceptionCheck()) { |
| 748 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 749 | env->ExceptionDescribe(); |
| 750 | env->ExceptionClear(); |
| 751 | } |
| 752 | } |
| 753 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 754 | void Dbg::DdmConnected() { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 755 | DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | void Dbg::DdmDisconnected() { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 759 | DdmBroadcast(false); |
| 760 | gDdmThreadNotification = false; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Send a notification when a thread starts or stops. |
| 765 | * |
| 766 | * Because we broadcast the full set of threads when the notifications are |
| 767 | * first enabled, it's possible for "thread" to be actively executing. |
| 768 | */ |
| 769 | void DdmSendThreadNotification(Thread* t, bool started) { |
| 770 | if (!gDdmThreadNotification) { |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | if (started) { |
| 775 | SirtRef<String> name(t->GetName()); |
| 776 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 777 | |
| 778 | size_t byte_count = char_count*2 + sizeof(uint32_t)*2; |
| 779 | std::vector<uint8_t> buf(byte_count); |
| 780 | JDWP::set4BE(&buf[0], t->GetThinLockId()); |
| 781 | JDWP::set4BE(&buf[4], char_count); |
| 782 | if (char_count > 0) { |
| 783 | // Copy the UTF-16 string, transforming to big-endian. |
| 784 | const jchar* src = name->GetCharArray()->GetData(); |
| 785 | jchar* dst = reinterpret_cast<jchar*>(&buf[8]); |
| 786 | while (char_count--) { |
| 787 | JDWP::set2BE(reinterpret_cast<uint8_t*>(dst++), *src++); |
| 788 | } |
| 789 | } |
| 790 | Dbg::DdmSendChunk(CHUNK_TYPE("THCR"), buf.size(), &buf[0]); |
| 791 | } else { |
| 792 | uint8_t buf[4]; |
| 793 | JDWP::set4BE(&buf[0], t->GetThinLockId()); |
| 794 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
| 795 | } |
| 796 | } |
| 797 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame^] | 798 | void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 799 | DdmSendThreadNotification(t, true); |
| 800 | } |
| 801 | |
| 802 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 803 | // We lock the thread list to avoid sending duplicate events or missing |
| 804 | // a thread change. We should be okay holding this lock while sending |
| 805 | // the messages out. (We have to hold it while accessing a live thread.) |
| 806 | ThreadListLock lock; |
| 807 | |
| 808 | gDdmThreadNotification = enable; |
| 809 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame^] | 810 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
| 814 | void PostThreadStartOrStop(Thread* t, bool is_start) { |
| 815 | if (gDebuggerActive) { |
| 816 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
| 817 | gJdwpState->PostThreadChange(id, is_start); |
| 818 | } |
| 819 | DdmSendThreadNotification(t, is_start); |
| 820 | } |
| 821 | |
| 822 | void Dbg::PostThreadStart(Thread* t) { |
| 823 | PostThreadStartOrStop(t, true); |
| 824 | } |
| 825 | |
| 826 | void Dbg::PostThreadDeath(Thread* t) { |
| 827 | PostThreadStartOrStop(t, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 830 | void Dbg::DdmSendChunk(int type, size_t byte_count, const uint8_t* buf) { |
| 831 | CHECK(buf != NULL); |
| 832 | iovec vec[1]; |
| 833 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 834 | vec[0].iov_len = byte_count; |
| 835 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | void Dbg::DdmSendChunkV(int type, const struct iovec* iov, int iovcnt) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 839 | if (gJdwpState == NULL) { |
| 840 | LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type; |
| 841 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 842 | gJdwpState->DdmSendChunkV(type, iov, iovcnt); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 843 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | } // namespace art |