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