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 | /* |
| 18 | * Handle messages from debugger. |
| 19 | * |
| 20 | * GENERAL NOTE: we're not currently testing the message length for |
| 21 | * correctness. This is usually a bad idea, but here we can probably |
| 22 | * get away with it so long as the debugger isn't broken. We can |
| 23 | * change the "read" macros to use "dataLen" to avoid wandering into |
| 24 | * bad territory, and have a single "is dataLen correct" check at the |
| 25 | * end of each function. Not needed at this time. |
| 26 | */ |
| 27 | |
| 28 | #include "atomic.h" |
| 29 | #include "debugger.h" |
| 30 | #include "jdwp/jdwp_priv.h" |
| 31 | #include "jdwp/jdwp_handler.h" |
| 32 | #include "jdwp/jdwp_event.h" |
| 33 | #include "jdwp/jdwp_constants.h" |
| 34 | #include "jdwp/jdwp_expand_buf.h" |
| 35 | #include "logging.h" |
| 36 | #include "macros.h" |
| 37 | #include "stringprintf.h" |
| 38 | |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | namespace art { |
| 44 | |
| 45 | namespace JDWP { |
| 46 | |
| 47 | /* |
| 48 | * Helper function: read a "location" from an input buffer. |
| 49 | */ |
| 50 | static void jdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) { |
| 51 | memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */ |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 52 | pLoc->typeTag = ReadTypeTag(pBuf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 53 | pLoc->classId = ReadObjectId(pBuf); |
| 54 | pLoc->methodId = ReadMethodId(pBuf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 55 | pLoc->idx = Read8BE(pBuf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Helper function: write a "location" into the reply buffer. |
| 60 | */ |
| 61 | void AddLocation(ExpandBuf* pReply, const JdwpLocation* pLoc) { |
| 62 | expandBufAdd1(pReply, pLoc->typeTag); |
| 63 | expandBufAddObjectId(pReply, pLoc->classId); |
| 64 | expandBufAddMethodId(pReply, pLoc->methodId); |
| 65 | expandBufAdd8BE(pReply, pLoc->idx); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Helper function: read a variable-width value from the input buffer. |
| 70 | */ |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 71 | static uint64_t jdwpReadValue(const uint8_t** pBuf, size_t width) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 72 | uint64_t value = -1; |
| 73 | switch (width) { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 74 | case 1: value = Read1(pBuf); break; |
| 75 | case 2: value = Read2BE(pBuf); break; |
| 76 | case 4: value = Read4BE(pBuf); break; |
| 77 | case 8: value = Read8BE(pBuf); break; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 78 | default: LOG(FATAL) << width; break; |
| 79 | } |
| 80 | return value; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Helper function: write a variable-width value into the output input buffer. |
| 85 | */ |
| 86 | static void jdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) { |
| 87 | switch (width) { |
| 88 | case 1: expandBufAdd1(pReply, value); break; |
| 89 | case 2: expandBufAdd2BE(pReply, value); break; |
| 90 | case 4: expandBufAdd4BE(pReply, value); break; |
| 91 | case 8: expandBufAdd8BE(pReply, value); break; |
| 92 | default: LOG(FATAL) << width; break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Common code for *_InvokeMethod requests. |
| 98 | * |
| 99 | * If "isConstructor" is set, this returns "objectId" rather than the |
| 100 | * expected-to-be-void return value of the called function. |
| 101 | */ |
| 102 | static JdwpError finishInvoke(JdwpState* state, |
| 103 | const uint8_t* buf, int dataLen, ExpandBuf* pReply, |
| 104 | ObjectId threadId, ObjectId objectId, RefTypeId classId, MethodId methodId, |
| 105 | bool isConstructor) |
| 106 | { |
| 107 | CHECK(!isConstructor || objectId != 0); |
| 108 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 109 | uint32_t numArgs = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 110 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 111 | VLOG(jdwp) << StringPrintf(" --> threadId=%llx objectId=%llx", threadId, objectId); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 112 | VLOG(jdwp) << StringPrintf(" classId=%llx methodId=%x %s.%s", classId, methodId, Dbg::GetClassName(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str()); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 113 | VLOG(jdwp) << StringPrintf(" %d args:", numArgs); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 114 | |
| 115 | uint64_t* argArray = NULL; |
| 116 | if (numArgs > 0) { |
| 117 | argArray = (ObjectId*) malloc(sizeof(ObjectId) * numArgs); |
| 118 | } |
| 119 | |
| 120 | for (uint32_t i = 0; i < numArgs; i++) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 121 | JDWP::JdwpTag typeTag = ReadTag(&buf); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 122 | size_t width = Dbg::GetTagWidth(typeTag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 123 | uint64_t value = jdwpReadValue(&buf, width); |
| 124 | |
Elliott Hughes | abd04b9 | 2012-01-18 22:46:41 -0800 | [diff] [blame] | 125 | VLOG(jdwp) << StringPrintf(" '%c'(%zd): 0x%llx", typeTag, width, value); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 126 | argArray[i] = value; |
| 127 | } |
| 128 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 129 | uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 130 | VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options, (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "", (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : ""); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 131 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 132 | JDWP::JdwpTag resultTag; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 133 | uint64_t resultValue; |
| 134 | ObjectId exceptObjId; |
| 135 | JdwpError err = Dbg::InvokeMethod(threadId, objectId, classId, methodId, numArgs, argArray, options, &resultTag, &resultValue, &exceptObjId); |
| 136 | if (err != ERR_NONE) { |
| 137 | goto bail; |
| 138 | } |
| 139 | |
| 140 | if (err == ERR_NONE) { |
| 141 | if (isConstructor) { |
| 142 | expandBufAdd1(pReply, JT_OBJECT); |
| 143 | expandBufAddObjectId(pReply, objectId); |
| 144 | } else { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 145 | size_t width = Dbg::GetTagWidth(resultTag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 146 | |
| 147 | expandBufAdd1(pReply, resultTag); |
| 148 | if (width != 0) { |
| 149 | jdwpWriteValue(pReply, width, resultValue); |
| 150 | } |
| 151 | } |
| 152 | expandBufAdd1(pReply, JT_OBJECT); |
| 153 | expandBufAddObjectId(pReply, exceptObjId); |
| 154 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 155 | VLOG(jdwp) << StringPrintf(" --> returned '%c' 0x%llx (except=%08llx)", resultTag, resultValue, exceptObjId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 156 | |
| 157 | /* show detailed debug output */ |
| 158 | if (resultTag == JT_STRING && exceptObjId == 0) { |
| 159 | if (resultValue != 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 160 | VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 161 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 162 | VLOG(jdwp) << " string (null)"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | bail: |
| 168 | free(argArray); |
| 169 | return err; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* |
| 174 | * Request for version info. |
| 175 | */ |
| 176 | static JdwpError handleVM_Version(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 177 | /* text information on runtime version */ |
| 178 | std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion())); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 179 | expandBufAddUtf8String(pReply, version); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 180 | /* JDWP version numbers */ |
| 181 | expandBufAdd4BE(pReply, 1); // major |
| 182 | expandBufAdd4BE(pReply, 5); // minor |
| 183 | /* VM JRE version */ |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 184 | expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 185 | /* target VM name */ |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 186 | expandBufAddUtf8String(pReply, "DalvikVM"); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 187 | |
| 188 | return ERR_NONE; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the |
| 193 | * referenceTypeID. We need to send back more than one if the class has |
| 194 | * been loaded by multiple class loaders. |
| 195 | */ |
| 196 | static JdwpError handleVM_ClassesBySignature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 197 | std::string classDescriptor(ReadNewUtf8String(&buf)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 198 | VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 199 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 200 | std::vector<RefTypeId> ids; |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 201 | Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 202 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 203 | expandBufAdd4BE(pReply, ids.size()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 204 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 205 | for (size_t i = 0; i < ids.size(); ++i) { |
| 206 | // Get class vs. interface and status flags. |
| 207 | JDWP::JdwpTypeTag typeTag; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 208 | uint32_t status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 209 | if (!Dbg::GetClassInfo(ids[i], &typeTag, &status, NULL)) { |
| 210 | return ERR_INVALID_CLASS; |
| 211 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 212 | |
| 213 | expandBufAdd1(pReply, typeTag); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 214 | expandBufAddRefTypeId(pReply, ids[i]); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 215 | expandBufAdd4BE(pReply, status); |
| 216 | } |
| 217 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 218 | return ERR_NONE; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Handle request for the thread IDs of all running threads. |
| 223 | * |
| 224 | * We exclude ourselves from the list, because we don't allow ourselves |
| 225 | * to be suspended, and that violates some JDWP expectations. |
| 226 | */ |
| 227 | static JdwpError handleVM_AllThreads(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 228 | ObjectId* pThreadIds; |
| 229 | uint32_t threadCount; |
| 230 | Dbg::GetAllThreads(&pThreadIds, &threadCount); |
| 231 | |
| 232 | expandBufAdd4BE(pReply, threadCount); |
| 233 | |
| 234 | ObjectId* walker = pThreadIds; |
| 235 | for (uint32_t i = 0; i < threadCount; i++) { |
| 236 | expandBufAddObjectId(pReply, *walker++); |
| 237 | } |
| 238 | |
| 239 | free(pThreadIds); |
| 240 | |
| 241 | return ERR_NONE; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * List all thread groups that do not have a parent. |
| 246 | */ |
| 247 | static JdwpError handleVM_TopLevelThreadGroups(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 248 | /* |
| 249 | * TODO: maintain a list of parentless thread groups in the VM. |
| 250 | * |
| 251 | * For now, just return "system". Application threads are created |
| 252 | * in "main", which is a child of "system". |
| 253 | */ |
| 254 | uint32_t groups = 1; |
| 255 | expandBufAdd4BE(pReply, groups); |
| 256 | //threadGroupId = debugGetMainThreadGroup(); |
| 257 | //expandBufAdd8BE(pReply, threadGroupId); |
| 258 | ObjectId threadGroupId = Dbg::GetSystemThreadGroupId(); |
| 259 | expandBufAddObjectId(pReply, threadGroupId); |
| 260 | |
| 261 | return ERR_NONE; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Respond with the sizes of the basic debugger types. |
| 266 | * |
| 267 | * All IDs are 8 bytes. |
| 268 | */ |
| 269 | static JdwpError handleVM_IDSizes(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 270 | expandBufAdd4BE(pReply, sizeof(FieldId)); |
| 271 | expandBufAdd4BE(pReply, sizeof(MethodId)); |
| 272 | expandBufAdd4BE(pReply, sizeof(ObjectId)); |
| 273 | expandBufAdd4BE(pReply, sizeof(RefTypeId)); |
| 274 | expandBufAdd4BE(pReply, sizeof(FrameId)); |
| 275 | return ERR_NONE; |
| 276 | } |
| 277 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 278 | static JdwpError handleVM_Dispose(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 279 | Dbg::Disposed(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 280 | return ERR_NONE; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Suspend the execution of the application running in the VM (i.e. suspend |
| 285 | * all threads). |
| 286 | * |
| 287 | * This needs to increment the "suspend count" on all threads. |
| 288 | */ |
| 289 | static JdwpError handleVM_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 290 | Dbg::SuspendVM(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 291 | return ERR_NONE; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Resume execution. Decrements the "suspend count" of all threads. |
| 296 | */ |
| 297 | static JdwpError handleVM_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 298 | Dbg::ResumeVM(); |
| 299 | return ERR_NONE; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * The debugger wants the entire VM to exit. |
| 304 | */ |
| 305 | static JdwpError handleVM_Exit(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 306 | uint32_t exitCode = Get4BE(buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 307 | |
| 308 | LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode; |
| 309 | |
| 310 | Dbg::Exit(exitCode); |
| 311 | return ERR_NOT_IMPLEMENTED; // shouldn't get here |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Create a new string in the VM and return its ID. |
| 316 | * |
| 317 | * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the |
| 318 | * string "java.util.Arrays".) |
| 319 | */ |
| 320 | static JdwpError handleVM_CreateString(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 321 | std::string str(ReadNewUtf8String(&buf)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 322 | VLOG(jdwp) << " Req to create string '" << str << "'"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 323 | ObjectId stringId = Dbg::CreateString(str); |
| 324 | if (stringId == 0) { |
| 325 | return ERR_OUT_OF_MEMORY; |
| 326 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 327 | expandBufAddObjectId(pReply, stringId); |
| 328 | return ERR_NONE; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Tell the debugger what we are capable of. |
| 333 | */ |
| 334 | static JdwpError handleVM_Capabilities(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 335 | expandBufAdd1(pReply, false); /* canWatchFieldModification */ |
| 336 | expandBufAdd1(pReply, false); /* canWatchFieldAccess */ |
| 337 | expandBufAdd1(pReply, false); /* canGetBytecodes */ |
| 338 | expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */ |
| 339 | expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */ |
| 340 | expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */ |
| 341 | expandBufAdd1(pReply, false); /* canGetMonitorInfo */ |
| 342 | return ERR_NONE; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Return classpath and bootclasspath. |
| 347 | */ |
| 348 | static JdwpError handleVM_ClassPaths(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 349 | char baseDir[2] = "/"; |
| 350 | |
| 351 | /* |
| 352 | * TODO: make this real. Not important for remote debugging, but |
| 353 | * might be useful for local debugging. |
| 354 | */ |
| 355 | uint32_t classPaths = 1; |
| 356 | uint32_t bootClassPaths = 0; |
| 357 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 358 | expandBufAddUtf8String(pReply, baseDir); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 359 | expandBufAdd4BE(pReply, classPaths); |
| 360 | for (uint32_t i = 0; i < classPaths; i++) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 361 | expandBufAddUtf8String(pReply, "."); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | expandBufAdd4BE(pReply, bootClassPaths); |
| 365 | for (uint32_t i = 0; i < classPaths; i++) { |
| 366 | /* add bootclasspath components as strings */ |
| 367 | } |
| 368 | |
| 369 | return ERR_NONE; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Release a list of object IDs. (Seen in jdb.) |
| 374 | * |
| 375 | * Currently does nothing. |
| 376 | */ |
| 377 | static JdwpError HandleVM_DisposeObjects(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 378 | return ERR_NONE; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Tell the debugger what we are capable of. |
| 383 | */ |
| 384 | static JdwpError handleVM_CapabilitiesNew(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 385 | expandBufAdd1(pReply, false); /* canWatchFieldModification */ |
| 386 | expandBufAdd1(pReply, false); /* canWatchFieldAccess */ |
| 387 | expandBufAdd1(pReply, false); /* canGetBytecodes */ |
| 388 | expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */ |
| 389 | expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */ |
| 390 | expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */ |
| 391 | expandBufAdd1(pReply, false); /* canGetMonitorInfo */ |
| 392 | expandBufAdd1(pReply, false); /* canRedefineClasses */ |
| 393 | expandBufAdd1(pReply, false); /* canAddMethod */ |
| 394 | expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */ |
| 395 | expandBufAdd1(pReply, false); /* canPopFrames */ |
| 396 | expandBufAdd1(pReply, false); /* canUseInstanceFilters */ |
| 397 | expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */ |
| 398 | expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */ |
| 399 | expandBufAdd1(pReply, false); /* canSetDefaultStratum */ |
| 400 | expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */ |
| 401 | expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */ |
| 402 | expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */ |
| 403 | expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */ |
| 404 | expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */ |
| 405 | expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */ |
| 406 | |
| 407 | /* fill in reserved22 through reserved32; note count started at 1 */ |
| 408 | for (int i = 22; i <= 32; i++) { |
| 409 | expandBufAdd1(pReply, false); /* reservedN */ |
| 410 | } |
| 411 | return ERR_NONE; |
| 412 | } |
| 413 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 414 | static JdwpError handleVM_AllClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply, bool descriptor_and_status, bool generic) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 415 | std::vector<JDWP::RefTypeId> classes; |
| 416 | Dbg::GetClassList(classes); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 417 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 418 | expandBufAdd4BE(pReply, classes.size()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 419 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 420 | for (size_t i = 0; i < classes.size(); ++i) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 421 | static const char genericSignature[1] = ""; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 422 | JDWP::JdwpTypeTag refTypeTag; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 423 | std::string descriptor; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 424 | uint32_t status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 425 | if (!Dbg::GetClassInfo(classes[i], &refTypeTag, &status, &descriptor)) { |
| 426 | return ERR_INVALID_CLASS; |
| 427 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 428 | |
| 429 | expandBufAdd1(pReply, refTypeTag); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 430 | expandBufAddRefTypeId(pReply, classes[i]); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 431 | if (descriptor_and_status) { |
| 432 | expandBufAddUtf8String(pReply, descriptor); |
| 433 | if (generic) { |
| 434 | expandBufAddUtf8String(pReply, genericSignature); |
| 435 | } |
| 436 | expandBufAdd4BE(pReply, status); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 437 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 440 | return ERR_NONE; |
| 441 | } |
| 442 | |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 443 | static JdwpError handleVM_AllClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 444 | return handleVM_AllClasses(state, buf, dataLen, pReply, true, false); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static JdwpError handleVM_AllClassesWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 448 | return handleVM_AllClasses(state, buf, dataLen, pReply, true, true); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 451 | /* |
| 452 | * Given a referenceTypeID, return a string with the JNI reference type |
| 453 | * signature (e.g. "Ljava/lang/Error;"). |
| 454 | */ |
| 455 | static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 456 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
| 457 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 458 | VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 459 | std::string signature; |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 460 | |
| 461 | JdwpError status = Dbg::GetSignature(refTypeId, signature); |
| 462 | if (status != ERR_NONE) { |
| 463 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 464 | } |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 465 | expandBufAddUtf8String(pReply, signature); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 466 | return ERR_NONE; |
| 467 | } |
| 468 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 469 | static JdwpError handleRT_Modifiers(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 470 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 471 | uint32_t access_flags; |
| 472 | if (!Dbg::GetAccessFlags(refTypeId, access_flags)) { |
| 473 | return ERR_INVALID_CLASS; |
| 474 | } |
Elliott Hughes | b305770 | 2012-02-14 10:44:29 -0800 | [diff] [blame] | 475 | |
| 476 | // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set. |
| 477 | // Class.getModifiers doesn't return it, but JDWP does, so we set it here. |
| 478 | access_flags |= kAccSuper; |
| 479 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 480 | expandBufAdd4BE(pReply, access_flags); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 481 | return ERR_NONE; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Get values from static fields in a reference type. |
| 486 | */ |
| 487 | static JdwpError handleRT_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 488 | ReadRefTypeId(&buf); // We don't need this, but we need to skip over it in the request. |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 489 | uint32_t numFields = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 490 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 491 | VLOG(jdwp) << " RT_GetValues " << numFields << ":"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 492 | |
| 493 | expandBufAdd4BE(pReply, numFields); |
| 494 | for (uint32_t i = 0; i < numFields; i++) { |
| 495 | FieldId fieldId = ReadFieldId(&buf); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 496 | Dbg::GetStaticFieldValue(fieldId, pReply); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | return ERR_NONE; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Get the name of the source file in which a reference type was declared. |
| 504 | */ |
| 505 | static JdwpError handleRT_SourceFile(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 506 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 507 | std::string source_file; |
| 508 | if (!Dbg::GetSourceFile(refTypeId, source_file)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 509 | return ERR_ABSENT_INFORMATION; |
| 510 | } |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 511 | expandBufAddUtf8String(pReply, source_file); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 512 | return ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
| 516 | * Return the current status of the reference type. |
| 517 | */ |
| 518 | static JdwpError handleRT_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 519 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 520 | JDWP::JdwpTypeTag typeTag; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 521 | uint32_t status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 522 | if (!Dbg::GetClassInfo(refTypeId, &typeTag, &status, NULL)) { |
| 523 | return ERR_INVALID_CLASS; |
| 524 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 525 | expandBufAdd4BE(pReply, status); |
| 526 | return ERR_NONE; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Return interfaces implemented directly by this class. |
| 531 | */ |
| 532 | static JdwpError handleRT_Interfaces(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 533 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 534 | VLOG(jdwp) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str()); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 535 | return Dbg::OutputDeclaredInterfaces(refTypeId, pReply) ? ERR_NONE : ERR_INVALID_CLASS; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Return the class object corresponding to this type. |
| 540 | */ |
| 541 | static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 542 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 543 | ObjectId classObjectId; |
| 544 | if (!Dbg::GetClassObject(refTypeId, classObjectId)) { |
| 545 | return ERR_INVALID_CLASS; |
| 546 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 547 | VLOG(jdwp) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjectId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 548 | expandBufAddObjectId(pReply, classObjectId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 549 | return ERR_NONE; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Returns the value of the SourceDebugExtension attribute. |
| 554 | * |
| 555 | * JDB seems interested, but DEX files don't currently support this. |
| 556 | */ |
| 557 | static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 558 | /* referenceTypeId in, string out */ |
| 559 | return ERR_ABSENT_INFORMATION; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Like RT_Signature but with the possibility of a "generic signature". |
| 564 | */ |
| 565 | static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 566 | static const char genericSignature[1] = ""; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 567 | |
| 568 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
| 569 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 570 | VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 571 | std::string signature; |
| 572 | if (Dbg::GetSignature(refTypeId, signature)) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 573 | expandBufAddUtf8String(pReply, signature); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 574 | } else { |
| 575 | LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 576 | expandBufAddUtf8String(pReply, "Lunknown;"); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 577 | } |
| 578 | expandBufAddUtf8String(pReply, genericSignature); |
| 579 | |
| 580 | return ERR_NONE; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Return the instance of java.lang.ClassLoader that loaded the specified |
| 585 | * reference type, or null if it was loaded by the system loader. |
| 586 | */ |
| 587 | static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 588 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
| 589 | |
| 590 | expandBufAddObjectId(pReply, Dbg::GetClassLoader(refTypeId)); |
| 591 | |
| 592 | return ERR_NONE; |
| 593 | } |
| 594 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 595 | static std::string Describe(const RefTypeId& refTypeId) { |
| 596 | std::string signature("unknown"); |
| 597 | Dbg::GetSignature(refTypeId, signature); |
| 598 | return StringPrintf("refTypeId=0x%llx (%s)", refTypeId, signature.c_str()); |
| 599 | } |
| 600 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 601 | /* |
| 602 | * Given a referenceTypeId, return a block of stuff that describes the |
| 603 | * fields declared by a class. |
| 604 | */ |
| 605 | static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 606 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 607 | VLOG(jdwp) << " Req for fields in " << Describe(refTypeId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 608 | return Dbg::OutputDeclaredFields(refTypeId, true, pReply) ? ERR_NONE : ERR_INVALID_CLASS; |
| 609 | } |
| 610 | |
| 611 | // Obsolete equivalent of FieldsWithGeneric, without the generic type information. |
| 612 | static JdwpError handleRT_Fields(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 613 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 614 | VLOG(jdwp) << " Req for fields in " << Describe(refTypeId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 615 | return Dbg::OutputDeclaredFields(refTypeId, false, pReply) ? ERR_NONE : ERR_INVALID_CLASS; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Given a referenceTypeID, return a block of goodies describing the |
| 620 | * methods declared by a class. |
| 621 | */ |
| 622 | static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 623 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 624 | VLOG(jdwp) << " Req for methods in " << Describe(refTypeId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 625 | return Dbg::OutputDeclaredMethods(refTypeId, true, pReply) ? ERR_NONE : ERR_INVALID_CLASS; |
| 626 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 627 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 628 | // Obsolete equivalent of MethodsWithGeneric, without the generic type information. |
| 629 | static JdwpError handleRT_Methods(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 630 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 631 | VLOG(jdwp) << " Req for methods in " << Describe(refTypeId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 632 | return Dbg::OutputDeclaredMethods(refTypeId, false, pReply) ? ERR_NONE : ERR_INVALID_CLASS; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Return the immediate superclass of a class. |
| 637 | */ |
| 638 | static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 639 | RefTypeId classId = ReadRefTypeId(&buf); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 640 | RefTypeId superClassId; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 641 | JdwpError status = Dbg::GetSuperclass(classId, superClassId); |
| 642 | if (status != ERR_NONE) { |
| 643 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 644 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 645 | expandBufAddRefTypeId(pReply, superClassId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 646 | return ERR_NONE; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Set static class values. |
| 651 | */ |
| 652 | static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 653 | RefTypeId classId = ReadRefTypeId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 654 | uint32_t values = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 655 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 656 | VLOG(jdwp) << StringPrintf(" Req to set %d values in classId=%llx", values, classId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 657 | |
| 658 | for (uint32_t i = 0; i < values; i++) { |
| 659 | FieldId fieldId = ReadFieldId(&buf); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 660 | JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 661 | size_t width = Dbg::GetTagWidth(fieldTag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 662 | uint64_t value = jdwpReadValue(&buf, width); |
| 663 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 664 | VLOG(jdwp) << StringPrintf(" --> field=%x tag=%c -> %lld", fieldId, fieldTag, value); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 665 | JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width); |
| 666 | if (status != ERR_NONE) { |
| 667 | return status; |
| 668 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | return ERR_NONE; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Invoke a static method. |
| 676 | * |
| 677 | * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on |
| 678 | * values in the "variables" display. |
| 679 | */ |
| 680 | static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 681 | RefTypeId classId = ReadRefTypeId(&buf); |
| 682 | ObjectId threadId = ReadObjectId(&buf); |
| 683 | MethodId methodId = ReadMethodId(&buf); |
| 684 | |
| 685 | return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Create a new object of the requested type, and invoke the specified |
| 690 | * constructor. |
| 691 | * |
| 692 | * Example: in IntelliJ, create a watch on "new String(myByteArray)" to |
| 693 | * see the contents of a byte[] as a string. |
| 694 | */ |
| 695 | static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 696 | RefTypeId classId = ReadRefTypeId(&buf); |
| 697 | ObjectId threadId = ReadObjectId(&buf); |
| 698 | MethodId methodId = ReadMethodId(&buf); |
| 699 | |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 700 | VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(classId); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 701 | ObjectId objectId; |
| 702 | if (!Dbg::CreateObject(classId, objectId)) { |
| 703 | return ERR_INVALID_CLASS; |
| 704 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 705 | if (objectId == 0) { |
| 706 | return ERR_OUT_OF_MEMORY; |
| 707 | } |
| 708 | return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true); |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Create a new array object of the requested type and length. |
| 713 | */ |
| 714 | static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 715 | RefTypeId arrayTypeId = ReadRefTypeId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 716 | uint32_t length = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 717 | |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 718 | VLOG(jdwp) << StringPrintf("Creating array %s[%u]", Dbg::GetClassName(arrayTypeId).c_str(), length); |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 719 | ObjectId objectId; |
| 720 | if (!Dbg::CreateArrayObject(arrayTypeId, length, objectId)) { |
| 721 | return ERR_INVALID_CLASS; |
| 722 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 723 | if (objectId == 0) { |
| 724 | return ERR_OUT_OF_MEMORY; |
| 725 | } |
| 726 | expandBufAdd1(pReply, JT_ARRAY); |
| 727 | expandBufAddObjectId(pReply, objectId); |
| 728 | return ERR_NONE; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Return line number information for the method, if present. |
| 733 | */ |
| 734 | static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 735 | RefTypeId refTypeId = ReadRefTypeId(&buf); |
| 736 | MethodId methodId = ReadMethodId(&buf); |
| 737 | |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 738 | VLOG(jdwp) << StringPrintf(" Req for line table in %s.%s", Dbg::GetClassName(refTypeId).c_str(), Dbg::GetMethodName(refTypeId,methodId).c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 739 | |
| 740 | Dbg::OutputLineTable(refTypeId, methodId, pReply); |
| 741 | |
| 742 | return ERR_NONE; |
| 743 | } |
| 744 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 745 | static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply, bool generic) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 746 | RefTypeId classId = ReadRefTypeId(&buf); |
| 747 | MethodId methodId = ReadMethodId(&buf); |
| 748 | |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 749 | VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s", Dbg::GetClassName(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 750 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 751 | // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable |
| 752 | // information. That will cause Eclipse to make a best-effort attempt at displaying local |
| 753 | // variables anonymously. However, the attempt isn't very good, so we're probably better off just |
| 754 | // not showing anything. |
| 755 | Dbg::OutputVariableTable(classId, methodId, generic, pReply); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 756 | return ERR_NONE; |
| 757 | } |
| 758 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 759 | static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 760 | return handleM_VariableTable(state, buf, dataLen, pReply, false); |
| 761 | } |
| 762 | |
| 763 | static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 764 | return handleM_VariableTable(state, buf, dataLen, pReply, true); |
| 765 | } |
| 766 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 767 | /* |
| 768 | * Given an object reference, return the runtime type of the object |
| 769 | * (class or array). |
| 770 | * |
| 771 | * This can get called on different things, e.g. threadId gets |
| 772 | * passed in here. |
| 773 | */ |
| 774 | static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 775 | ObjectId objectId = ReadObjectId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 776 | VLOG(jdwp) << StringPrintf(" Req for type of objectId=0x%llx", objectId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 777 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 778 | JDWP::JdwpTypeTag refTypeTag; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 779 | RefTypeId typeId; |
| 780 | Dbg::GetObjectType(objectId, &refTypeTag, &typeId); |
| 781 | |
| 782 | expandBufAdd1(pReply, refTypeTag); |
| 783 | expandBufAddRefTypeId(pReply, typeId); |
| 784 | |
| 785 | return ERR_NONE; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Get values from the fields of an object. |
| 790 | */ |
| 791 | static JdwpError handleOR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 792 | ObjectId objectId = ReadObjectId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 793 | uint32_t numFields = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 794 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 795 | VLOG(jdwp) << StringPrintf(" Req for %d fields from objectId=0x%llx", numFields, objectId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 796 | |
| 797 | expandBufAdd4BE(pReply, numFields); |
| 798 | |
| 799 | for (uint32_t i = 0; i < numFields; i++) { |
| 800 | FieldId fieldId = ReadFieldId(&buf); |
| 801 | Dbg::GetFieldValue(objectId, fieldId, pReply); |
| 802 | } |
| 803 | |
| 804 | return ERR_NONE; |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Set values in the fields of an object. |
| 809 | */ |
| 810 | static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 811 | ObjectId objectId = ReadObjectId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 812 | uint32_t numFields = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 813 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 814 | VLOG(jdwp) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 815 | |
| 816 | for (uint32_t i = 0; i < numFields; i++) { |
| 817 | FieldId fieldId = ReadFieldId(&buf); |
| 818 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 819 | JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 820 | size_t width = Dbg::GetTagWidth(fieldTag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 821 | uint64_t value = jdwpReadValue(&buf, width); |
| 822 | |
Elliott Hughes | abd04b9 | 2012-01-18 22:46:41 -0800 | [diff] [blame] | 823 | VLOG(jdwp) << StringPrintf(" --> fieldId=%x tag='%c'(%zd) value=%lld", fieldId, fieldTag, width, value); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 824 | |
| 825 | Dbg::SetFieldValue(objectId, fieldId, value, width); |
| 826 | } |
| 827 | |
| 828 | return ERR_NONE; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Invoke an instance method. The invocation must occur in the specified |
| 833 | * thread, which must have been suspended by an event. |
| 834 | * |
| 835 | * The call is synchronous. All threads in the VM are resumed, unless the |
| 836 | * SINGLE_THREADED flag is set. |
| 837 | * |
| 838 | * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an |
| 839 | * object), it will try to invoke the object's toString() function. This |
| 840 | * feature becomes crucial when examining ArrayLists with Eclipse. |
| 841 | */ |
| 842 | static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 843 | ObjectId objectId = ReadObjectId(&buf); |
| 844 | ObjectId threadId = ReadObjectId(&buf); |
| 845 | RefTypeId classId = ReadRefTypeId(&buf); |
| 846 | MethodId methodId = ReadMethodId(&buf); |
| 847 | |
| 848 | return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false); |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Disable garbage collection of the specified object. |
| 853 | */ |
| 854 | static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 855 | // this is currently a no-op |
| 856 | return ERR_NONE; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Enable garbage collection of the specified object. |
| 861 | */ |
| 862 | static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 863 | // this is currently a no-op |
| 864 | return ERR_NONE; |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * Determine whether an object has been garbage collected. |
| 869 | */ |
| 870 | static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 871 | ObjectId objectId; |
| 872 | |
| 873 | objectId = ReadObjectId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 874 | VLOG(jdwp) << StringPrintf(" Req IsCollected(0x%llx)", objectId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 875 | |
| 876 | // TODO: currently returning false; must integrate with GC |
| 877 | expandBufAdd1(pReply, 0); |
| 878 | |
| 879 | return ERR_NONE; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Return the string value in a string object. |
| 884 | */ |
| 885 | static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 886 | ObjectId stringObject = ReadObjectId(&buf); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 887 | std::string str(Dbg::StringToUtf8(stringObject)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 888 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 889 | VLOG(jdwp) << StringPrintf(" Req for str %llx --> '%s'", stringObject, PrintableString(str).c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 890 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 891 | expandBufAddUtf8String(pReply, str); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 892 | |
| 893 | return ERR_NONE; |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Return a thread's name. |
| 898 | */ |
| 899 | static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 900 | ObjectId threadId = ReadObjectId(&buf); |
| 901 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 902 | VLOG(jdwp) << StringPrintf(" Req for name of thread 0x%llx", threadId); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 903 | std::string name; |
| 904 | if (!Dbg::GetThreadName(threadId, name)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 905 | return ERR_INVALID_THREAD; |
| 906 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 907 | VLOG(jdwp) << StringPrintf(" Name of thread 0x%llx is \"%s\"", threadId, name.c_str()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 908 | expandBufAddUtf8String(pReply, name); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 909 | |
| 910 | return ERR_NONE; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Suspend the specified thread. |
| 915 | * |
| 916 | * It's supposed to remain suspended even if interpreted code wants to |
| 917 | * resume it; only the JDI is allowed to resume it. |
| 918 | */ |
| 919 | static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 920 | ObjectId threadId = ReadObjectId(&buf); |
| 921 | |
| 922 | if (threadId == Dbg::GetThreadSelfId()) { |
| 923 | LOG(INFO) << " Warning: ignoring request to suspend self"; |
| 924 | return ERR_THREAD_NOT_SUSPENDED; |
| 925 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 926 | VLOG(jdwp) << StringPrintf(" Req to suspend thread 0x%llx", threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 927 | Dbg::SuspendThread(threadId); |
| 928 | return ERR_NONE; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Resume the specified thread. |
| 933 | */ |
| 934 | static JdwpError handleTR_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 935 | ObjectId threadId = ReadObjectId(&buf); |
| 936 | |
| 937 | if (threadId == Dbg::GetThreadSelfId()) { |
| 938 | LOG(INFO) << " Warning: ignoring request to resume self"; |
| 939 | return ERR_NONE; |
| 940 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 941 | VLOG(jdwp) << StringPrintf(" Req to resume thread 0x%llx", threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 942 | Dbg::ResumeThread(threadId); |
| 943 | return ERR_NONE; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * Return status of specified thread. |
| 948 | */ |
| 949 | static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 950 | ObjectId threadId = ReadObjectId(&buf); |
| 951 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 952 | VLOG(jdwp) << StringPrintf(" Req for status of thread 0x%llx", threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 953 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 954 | JDWP::JdwpThreadStatus threadStatus; |
| 955 | JDWP::JdwpSuspendStatus suspendStatus; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 956 | if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) { |
| 957 | return ERR_INVALID_THREAD; |
| 958 | } |
| 959 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 960 | VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 961 | |
| 962 | expandBufAdd4BE(pReply, threadStatus); |
| 963 | expandBufAdd4BE(pReply, suspendStatus); |
| 964 | |
| 965 | return ERR_NONE; |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * Return the thread group that the specified thread is a member of. |
| 970 | */ |
| 971 | static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 972 | ObjectId threadId = ReadObjectId(&buf); |
| 973 | |
| 974 | /* currently not handling these */ |
| 975 | ObjectId threadGroupId = Dbg::GetThreadGroup(threadId); |
| 976 | expandBufAddObjectId(pReply, threadGroupId); |
| 977 | |
| 978 | return ERR_NONE; |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Return the current call stack of a suspended thread. |
| 983 | * |
| 984 | * If the thread isn't suspended, the error code isn't defined, but should |
| 985 | * be THREAD_NOT_SUSPENDED. |
| 986 | */ |
| 987 | static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 988 | ObjectId threadId = ReadObjectId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 989 | uint32_t startFrame = Read4BE(&buf); |
| 990 | uint32_t length = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 991 | |
| 992 | if (!Dbg::ThreadExists(threadId)) { |
| 993 | return ERR_INVALID_THREAD; |
| 994 | } |
| 995 | if (!Dbg::IsSuspended(threadId)) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 996 | LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 997 | return ERR_THREAD_NOT_SUSPENDED; |
| 998 | } |
| 999 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1000 | size_t frameCount = Dbg::GetThreadFrameCount(threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1001 | |
Elliott Hughes | abd04b9 | 2012-01-18 22:46:41 -0800 | [diff] [blame] | 1002 | VLOG(jdwp) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%zd]", threadId, startFrame, length, frameCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1003 | if (frameCount <= 0) { |
| 1004 | return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */ |
| 1005 | } |
| 1006 | if (length == (uint32_t) -1) { |
| 1007 | length = frameCount; |
| 1008 | } |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1009 | CHECK_GE(startFrame, 0U); |
| 1010 | CHECK_LT(startFrame, frameCount); |
| 1011 | CHECK_LE(startFrame + length, frameCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1012 | |
| 1013 | uint32_t frames = length; |
| 1014 | expandBufAdd4BE(pReply, frames); |
| 1015 | for (uint32_t i = startFrame; i < (startFrame+length); i++) { |
| 1016 | FrameId frameId; |
| 1017 | JdwpLocation loc; |
| 1018 | |
| 1019 | Dbg::GetThreadFrame(threadId, i, &frameId, &loc); |
| 1020 | |
| 1021 | expandBufAdd8BE(pReply, frameId); |
| 1022 | AddLocation(pReply, &loc); |
| 1023 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1024 | VLOG(jdwp) << StringPrintf(" Frame %d: id=%llx loc={type=%d cls=%llx mth=%x loc=%llx}", i, frameId, loc.typeTag, loc.classId, loc.methodId, loc.idx); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | return ERR_NONE; |
| 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * Returns the #of frames on the specified thread, which must be suspended. |
| 1032 | */ |
| 1033 | static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1034 | ObjectId threadId = ReadObjectId(&buf); |
| 1035 | |
| 1036 | if (!Dbg::ThreadExists(threadId)) { |
| 1037 | return ERR_INVALID_THREAD; |
| 1038 | } |
| 1039 | if (!Dbg::IsSuspended(threadId)) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1040 | LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1041 | return ERR_THREAD_NOT_SUSPENDED; |
| 1042 | } |
| 1043 | |
| 1044 | int frameCount = Dbg::GetThreadFrameCount(threadId); |
| 1045 | if (frameCount < 0) { |
| 1046 | return ERR_INVALID_THREAD; |
| 1047 | } |
| 1048 | expandBufAdd4BE(pReply, (uint32_t)frameCount); |
| 1049 | |
| 1050 | return ERR_NONE; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Get the monitor that the thread is waiting on. |
| 1055 | */ |
| 1056 | static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1057 | ObjectId threadId; |
| 1058 | |
| 1059 | threadId = ReadObjectId(&buf); |
| 1060 | |
| 1061 | // TODO: create an Object to represent the monitor (we're currently |
| 1062 | // just using a raw Monitor struct in the VM) |
| 1063 | |
| 1064 | return ERR_NOT_IMPLEMENTED; |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * Return the suspend count for the specified thread. |
| 1069 | * |
| 1070 | * (The thread *might* still be running -- it might not have examined |
| 1071 | * its suspend count recently.) |
| 1072 | */ |
| 1073 | static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1074 | ObjectId threadId = ReadObjectId(&buf); |
| 1075 | |
| 1076 | uint32_t suspendCount = Dbg::GetThreadSuspendCount(threadId); |
| 1077 | expandBufAdd4BE(pReply, suspendCount); |
| 1078 | |
| 1079 | return ERR_NONE; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
| 1083 | * Return the name of a thread group. |
| 1084 | * |
| 1085 | * The Eclipse debugger recognizes "main" and "system" as special. |
| 1086 | */ |
| 1087 | static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1088 | ObjectId threadGroupId = ReadObjectId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1089 | VLOG(jdwp) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1090 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1091 | expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1092 | |
| 1093 | return ERR_NONE; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * Returns the thread group -- if any -- that contains the specified |
| 1098 | * thread group. |
| 1099 | */ |
| 1100 | static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1101 | ObjectId groupId = ReadObjectId(&buf); |
| 1102 | |
| 1103 | ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId); |
| 1104 | expandBufAddObjectId(pReply, parentGroup); |
| 1105 | |
| 1106 | return ERR_NONE; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * Return the active threads and thread groups that are part of the |
| 1111 | * specified thread group. |
| 1112 | */ |
| 1113 | static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1114 | ObjectId threadGroupId = ReadObjectId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1115 | VLOG(jdwp) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1116 | |
| 1117 | ObjectId* pThreadIds; |
| 1118 | uint32_t threadCount; |
| 1119 | Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount); |
| 1120 | |
| 1121 | expandBufAdd4BE(pReply, threadCount); |
| 1122 | |
| 1123 | for (uint32_t i = 0; i < threadCount; i++) { |
| 1124 | expandBufAddObjectId(pReply, pThreadIds[i]); |
| 1125 | } |
| 1126 | free(pThreadIds); |
| 1127 | |
| 1128 | /* |
| 1129 | * TODO: finish support for child groups |
| 1130 | * |
| 1131 | * For now, just show that "main" is a child of "system". |
| 1132 | */ |
| 1133 | if (threadGroupId == Dbg::GetSystemThreadGroupId()) { |
| 1134 | expandBufAdd4BE(pReply, 1); |
| 1135 | expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId()); |
| 1136 | } else { |
| 1137 | expandBufAdd4BE(pReply, 0); |
| 1138 | } |
| 1139 | |
| 1140 | return ERR_NONE; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Return the #of components in the array. |
| 1145 | */ |
| 1146 | static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1147 | ObjectId arrayId = ReadObjectId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1148 | VLOG(jdwp) << StringPrintf(" Req for length of array 0x%llx", arrayId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1149 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1150 | int length; |
| 1151 | JdwpError status = Dbg::GetArrayLength(arrayId, length); |
| 1152 | if (status != ERR_NONE) { |
| 1153 | return status; |
| 1154 | } |
| 1155 | VLOG(jdwp) << StringPrintf(" --> %d", length); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1156 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1157 | expandBufAdd4BE(pReply, length); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1158 | |
| 1159 | return ERR_NONE; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Return the values from an array. |
| 1164 | */ |
| 1165 | static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1166 | ObjectId arrayId = ReadObjectId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1167 | uint32_t firstIndex = Read4BE(&buf); |
| 1168 | uint32_t length = Read4BE(&buf); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1169 | VLOG(jdwp) << StringPrintf(" Req for array values 0x%llx first=%d len=%d", arrayId, firstIndex, length); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1170 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1171 | return Dbg::OutputArray(arrayId, firstIndex, length, pReply); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Set values in an array. |
| 1176 | */ |
| 1177 | static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1178 | ObjectId arrayId = ReadObjectId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1179 | uint32_t firstIndex = Read4BE(&buf); |
| 1180 | uint32_t values = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1181 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1182 | VLOG(jdwp) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1183 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1184 | return Dbg::SetArrayElements(arrayId, firstIndex, values, buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1187 | static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1188 | ObjectId classLoaderObject; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1189 | classLoaderObject = ReadObjectId(&buf); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1190 | // TODO: we should only return classes which have the given class loader as a defining or |
| 1191 | // initiating loader. The former would be easy; the latter is hard, because we don't have |
| 1192 | // any such notion. |
| 1193 | return handleVM_AllClasses(state, buf, dataLen, pReply, false, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Set an event trigger. |
| 1198 | * |
| 1199 | * Reply with a requestID. |
| 1200 | */ |
| 1201 | static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1202 | const uint8_t* origBuf = buf; |
| 1203 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1204 | uint8_t eventKind = Read1(&buf); |
| 1205 | uint8_t suspendPolicy = Read1(&buf); |
| 1206 | uint32_t modifierCount = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1207 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1208 | VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind) |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1209 | << " suspend=" << JdwpSuspendPolicy(suspendPolicy) |
| 1210 | << " mods=" << modifierCount << ")"; |
| 1211 | |
| 1212 | CHECK_LT(modifierCount, 256U); /* reasonableness check */ |
| 1213 | |
| 1214 | JdwpEvent* pEvent = EventAlloc(modifierCount); |
| 1215 | pEvent->eventKind = static_cast<JdwpEventKind>(eventKind); |
| 1216 | pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy); |
| 1217 | pEvent->modCount = modifierCount; |
| 1218 | |
| 1219 | /* |
| 1220 | * Read modifiers. Ordering may be significant (see explanation of Count |
| 1221 | * mods in JDWP doc). |
| 1222 | */ |
| 1223 | for (uint32_t idx = 0; idx < modifierCount; idx++) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1224 | JdwpModKind modKind = static_cast<JdwpModKind>(Read1(&buf)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1225 | |
| 1226 | pEvent->mods[idx].modKind = modKind; |
| 1227 | |
| 1228 | switch (modKind) { |
| 1229 | case MK_COUNT: /* report once, when "--count" reaches 0 */ |
| 1230 | { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1231 | uint32_t count = Read4BE(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1232 | VLOG(jdwp) << " Count: " << count; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1233 | if (count == 0) { |
| 1234 | return ERR_INVALID_COUNT; |
| 1235 | } |
| 1236 | pEvent->mods[idx].count.count = count; |
| 1237 | } |
| 1238 | break; |
| 1239 | case MK_CONDITIONAL: /* conditional on expression) */ |
| 1240 | { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1241 | uint32_t exprId = Read4BE(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1242 | VLOG(jdwp) << " Conditional: " << exprId; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1243 | pEvent->mods[idx].conditional.exprId = exprId; |
| 1244 | } |
| 1245 | break; |
| 1246 | case MK_THREAD_ONLY: /* only report events in specified thread */ |
| 1247 | { |
| 1248 | ObjectId threadId = ReadObjectId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1249 | VLOG(jdwp) << StringPrintf(" ThreadOnly: %llx", threadId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1250 | pEvent->mods[idx].threadOnly.threadId = threadId; |
| 1251 | } |
| 1252 | break; |
| 1253 | case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */ |
| 1254 | { |
| 1255 | RefTypeId clazzId = ReadRefTypeId(&buf); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 1256 | VLOG(jdwp) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassName(clazzId).c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1257 | pEvent->mods[idx].classOnly.refTypeId = clazzId; |
| 1258 | } |
| 1259 | break; |
| 1260 | case MK_CLASS_MATCH: /* restrict events to matching classes */ |
| 1261 | { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1262 | // pattern is "java.foo.*", we want "java/foo/*". |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1263 | std::string pattern(ReadNewUtf8String(&buf)); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1264 | std::replace(pattern.begin(), pattern.end(), '.', '/'); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1265 | VLOG(jdwp) << StringPrintf(" ClassMatch: '%s'", pattern.c_str()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1266 | pEvent->mods[idx].classMatch.classPattern = strdup(pattern.c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1267 | } |
| 1268 | break; |
| 1269 | case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */ |
| 1270 | { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1271 | // pattern is "java.foo.*", we want "java/foo/*". |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1272 | std::string pattern(ReadNewUtf8String(&buf)); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1273 | std::replace(pattern.begin(), pattern.end(), '.', '/'); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1274 | VLOG(jdwp) << StringPrintf(" ClassExclude: '%s'", pattern.c_str()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1275 | pEvent->mods[idx].classExclude.classPattern = strdup(pattern.c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1276 | } |
| 1277 | break; |
| 1278 | case MK_LOCATION_ONLY: /* restrict certain events based on loc */ |
| 1279 | { |
| 1280 | JdwpLocation loc; |
| 1281 | |
| 1282 | jdwpReadLocation(&buf, &loc); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1283 | VLOG(jdwp) << StringPrintf(" LocationOnly: typeTag=%d classId=%llx methodId=%x idx=%llx", |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1284 | loc.typeTag, loc.classId, loc.methodId, loc.idx); |
| 1285 | pEvent->mods[idx].locationOnly.loc = loc; |
| 1286 | } |
| 1287 | break; |
| 1288 | case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */ |
| 1289 | { |
| 1290 | RefTypeId exceptionOrNull; /* null == all exceptions */ |
| 1291 | uint8_t caught, uncaught; |
| 1292 | |
| 1293 | exceptionOrNull = ReadRefTypeId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1294 | caught = Read1(&buf); |
| 1295 | uncaught = Read1(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1296 | VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d", |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 1297 | exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1298 | |
| 1299 | pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull; |
| 1300 | pEvent->mods[idx].exceptionOnly.caught = caught; |
| 1301 | pEvent->mods[idx].exceptionOnly.uncaught = uncaught; |
| 1302 | } |
| 1303 | break; |
| 1304 | case MK_FIELD_ONLY: /* for field access/mod events */ |
| 1305 | { |
| 1306 | RefTypeId declaring = ReadRefTypeId(&buf); |
| 1307 | FieldId fieldId = ReadFieldId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1308 | VLOG(jdwp) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1309 | pEvent->mods[idx].fieldOnly.refTypeId = declaring; |
| 1310 | pEvent->mods[idx].fieldOnly.fieldId = fieldId; |
| 1311 | } |
| 1312 | break; |
| 1313 | case MK_STEP: /* for use with EK_SINGLE_STEP */ |
| 1314 | { |
| 1315 | ObjectId threadId; |
| 1316 | uint32_t size, depth; |
| 1317 | |
| 1318 | threadId = ReadObjectId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1319 | size = Read4BE(&buf); |
| 1320 | depth = Read4BE(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1321 | VLOG(jdwp) << StringPrintf(" Step: thread=%llx", threadId) |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1322 | << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth); |
| 1323 | |
| 1324 | pEvent->mods[idx].step.threadId = threadId; |
| 1325 | pEvent->mods[idx].step.size = size; |
| 1326 | pEvent->mods[idx].step.depth = depth; |
| 1327 | } |
| 1328 | break; |
| 1329 | case MK_INSTANCE_ONLY: /* report events related to a specific obj */ |
| 1330 | { |
| 1331 | ObjectId instance = ReadObjectId(&buf); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1332 | VLOG(jdwp) << StringPrintf(" InstanceOnly: %llx", instance); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1333 | pEvent->mods[idx].instanceOnly.objectId = instance; |
| 1334 | } |
| 1335 | break; |
| 1336 | default: |
| 1337 | LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind; |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Make sure we consumed all data. It is possible that the remote side |
| 1344 | * has sent us bad stuff, but for now we blame ourselves. |
| 1345 | */ |
| 1346 | if (buf != origBuf + dataLen) { |
| 1347 | LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf); |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * We reply with an integer "requestID". |
| 1352 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1353 | uint32_t requestId = state->NextEventSerial(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1354 | expandBufAdd4BE(pReply, requestId); |
| 1355 | |
| 1356 | pEvent->requestId = requestId; |
| 1357 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1358 | VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1359 | |
| 1360 | /* add it to the list */ |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1361 | JdwpError err = state->RegisterEvent(pEvent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1362 | if (err != ERR_NONE) { |
| 1363 | /* registration failed, probably because event is bogus */ |
| 1364 | EventFree(pEvent); |
| 1365 | LOG(WARNING) << "WARNING: event request rejected"; |
| 1366 | } |
| 1367 | return err; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * Clear an event. Failure to find an event with a matching ID is a no-op |
| 1372 | * and does not return an error. |
| 1373 | */ |
| 1374 | static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1375 | uint8_t eventKind; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1376 | eventKind = Read1(&buf); |
| 1377 | uint32_t requestId = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1378 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1379 | VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1380 | |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1381 | state->UnregisterEventById(requestId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1382 | |
| 1383 | return ERR_NONE; |
| 1384 | } |
| 1385 | |
| 1386 | /* |
| 1387 | * Return the values of arguments and local variables. |
| 1388 | */ |
| 1389 | static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1390 | ObjectId threadId = ReadObjectId(&buf); |
| 1391 | FrameId frameId = ReadFrameId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1392 | uint32_t slots = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1393 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1394 | VLOG(jdwp) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1395 | |
| 1396 | expandBufAdd4BE(pReply, slots); /* "int values" */ |
| 1397 | for (uint32_t i = 0; i < slots; i++) { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1398 | uint32_t slot = Read4BE(&buf); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1399 | JDWP::JdwpTag reqSigByte = ReadTag(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1400 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1401 | VLOG(jdwp) << StringPrintf(" --> slot %d '%c'", slot, reqSigByte); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1402 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1403 | size_t width = Dbg::GetTagWidth(reqSigByte); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1404 | uint8_t* ptr = expandBufAddSpace(pReply, width+1); |
| 1405 | Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width); |
| 1406 | } |
| 1407 | |
| 1408 | return ERR_NONE; |
| 1409 | } |
| 1410 | |
| 1411 | /* |
| 1412 | * Set the values of arguments and local variables. |
| 1413 | */ |
| 1414 | static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1415 | ObjectId threadId = ReadObjectId(&buf); |
| 1416 | FrameId frameId = ReadFrameId(&buf); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1417 | uint32_t slots = Read4BE(&buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1418 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1419 | VLOG(jdwp) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1420 | |
| 1421 | for (uint32_t i = 0; i < slots; i++) { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1422 | uint32_t slot = Read4BE(&buf); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1423 | JDWP::JdwpTag sigByte = ReadTag(&buf); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1424 | size_t width = Dbg::GetTagWidth(sigByte); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1425 | uint64_t value = jdwpReadValue(&buf, width); |
| 1426 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1427 | VLOG(jdwp) << StringPrintf(" --> slot %d '%c' %llx", slot, sigByte, value); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1428 | Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width); |
| 1429 | } |
| 1430 | |
| 1431 | return ERR_NONE; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * Returns the value of "this" for the specified frame. |
| 1436 | */ |
| 1437 | static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1438 | ReadObjectId(&buf); // Skip thread id. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1439 | FrameId frameId = ReadFrameId(&buf); |
| 1440 | |
| 1441 | ObjectId objectId; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1442 | Dbg::GetThisObject(frameId, &objectId); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1443 | |
| 1444 | uint8_t objectTag = Dbg::GetObjectTag(objectId); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1445 | VLOG(jdwp) << StringPrintf(" Req for 'this' in frame=%llx --> %llx '%c'", frameId, objectId, (char)objectTag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1446 | |
| 1447 | expandBufAdd1(pReply, objectTag); |
| 1448 | expandBufAddObjectId(pReply, objectId); |
| 1449 | |
| 1450 | return ERR_NONE; |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * Return the reference type reflected by this class object. |
| 1455 | * |
| 1456 | * This appears to be required because ReferenceTypeId values are NEVER |
| 1457 | * reused, whereas ClassIds can be recycled like any other object. (Either |
| 1458 | * that, or I have no idea what this is for.) |
| 1459 | */ |
| 1460 | static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1461 | RefTypeId classObjectId = ReadRefTypeId(&buf); |
| 1462 | |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame^] | 1463 | VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassName(classObjectId).c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1464 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1465 | bool is_interface; |
| 1466 | if (!Dbg::IsInterface(classObjectId, is_interface)) { |
| 1467 | return ERR_INVALID_CLASS; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1468 | } |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1469 | |
| 1470 | expandBufAdd1(pReply, is_interface ? TT_INTERFACE : TT_CLASS); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1471 | expandBufAddRefTypeId(pReply, classObjectId); |
| 1472 | |
| 1473 | return ERR_NONE; |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * Handle a DDM packet with a single chunk in it. |
| 1478 | */ |
| 1479 | static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
| 1480 | uint8_t* replyBuf = NULL; |
| 1481 | int replyLen = -1; |
| 1482 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1483 | VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1484 | |
| 1485 | /* |
| 1486 | * On first DDM packet, notify all handlers that DDM is running. |
| 1487 | */ |
| 1488 | if (!state->ddmActive) { |
| 1489 | state->ddmActive = true; |
| 1490 | Dbg::DdmConnected(); |
| 1491 | } |
| 1492 | |
| 1493 | /* |
| 1494 | * If they want to send something back, we copy it into the buffer. |
| 1495 | * A no-copy approach would be nicer. |
| 1496 | * |
| 1497 | * TODO: consider altering the JDWP stuff to hold the packet header |
| 1498 | * in a separate buffer. That would allow us to writev() DDM traffic |
| 1499 | * instead of copying it into the expanding buffer. The reduction in |
| 1500 | * heap requirements is probably more valuable than the efficiency. |
| 1501 | */ |
| 1502 | if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) { |
| 1503 | CHECK(replyLen > 0 && replyLen < 1*1024*1024); |
| 1504 | memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen); |
| 1505 | free(replyBuf); |
| 1506 | } |
| 1507 | return ERR_NONE; |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * Handler map decl. |
| 1512 | */ |
| 1513 | typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply); |
| 1514 | |
| 1515 | struct JdwpHandlerMap { |
| 1516 | uint8_t cmdSet; |
| 1517 | uint8_t cmd; |
| 1518 | JdwpRequestHandler func; |
| 1519 | const char* descr; |
| 1520 | }; |
| 1521 | |
| 1522 | /* |
| 1523 | * Map commands to functions. |
| 1524 | * |
| 1525 | * Command sets 0-63 are incoming requests, 64-127 are outbound requests, |
| 1526 | * and 128-256 are vendor-defined. |
| 1527 | */ |
| 1528 | static const JdwpHandlerMap gHandlerMap[] = { |
| 1529 | /* VirtualMachine command set (1) */ |
| 1530 | { 1, 1, handleVM_Version, "VirtualMachine.Version" }, |
| 1531 | { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" }, |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1532 | { 1, 3, handleVM_AllClasses, "VirtualMachine.AllClasses" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1533 | { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" }, |
| 1534 | { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" }, |
| 1535 | { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" }, |
| 1536 | { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" }, |
| 1537 | { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" }, |
| 1538 | { 1, 9, handleVM_Resume, "VirtualMachine.Resume" }, |
| 1539 | { 1, 10, handleVM_Exit, "VirtualMachine.Exit" }, |
| 1540 | { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" }, |
| 1541 | { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" }, |
| 1542 | { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" }, |
| 1543 | { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1544 | { 1, 15, NULL, "VirtualMachine.HoldEvents" }, |
| 1545 | { 1, 16, NULL, "VirtualMachine.ReleaseEvents" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1546 | { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1547 | { 1, 18, NULL, "VirtualMachine.RedefineClasses" }, |
| 1548 | { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" }, |
| 1549 | { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" }, |
| 1550 | { 1, 21, NULL, "VirtualMachine.InstanceCounts" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1551 | |
| 1552 | /* ReferenceType command set (2) */ |
| 1553 | { 2, 1, handleRT_Signature, "ReferenceType.Signature" }, |
| 1554 | { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" }, |
| 1555 | { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" }, |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1556 | { 2, 4, handleRT_Fields, "ReferenceType.Fields" }, |
| 1557 | { 2, 5, handleRT_Methods, "ReferenceType.Methods" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1558 | { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" }, |
| 1559 | { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1560 | { 2, 8, NULL, "ReferenceType.NestedTypes" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1561 | { 2, 9, handleRT_Status, "ReferenceType.Status" }, |
| 1562 | { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" }, |
| 1563 | { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" }, |
| 1564 | { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" }, |
| 1565 | { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" }, |
| 1566 | { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" }, |
| 1567 | { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1568 | { 2, 16, NULL, "ReferenceType.Instances" }, |
| 1569 | { 2, 17, NULL, "ReferenceType.ClassFileVersion" }, |
| 1570 | { 2, 18, NULL, "ReferenceType.ConstantPool" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1571 | |
| 1572 | /* ClassType command set (3) */ |
| 1573 | { 3, 1, handleCT_Superclass, "ClassType.Superclass" }, |
| 1574 | { 3, 2, handleCT_SetValues, "ClassType.SetValues" }, |
| 1575 | { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" }, |
| 1576 | { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" }, |
| 1577 | |
| 1578 | /* ArrayType command set (4) */ |
| 1579 | { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" }, |
| 1580 | |
| 1581 | /* InterfaceType command set (5) */ |
| 1582 | |
| 1583 | /* Method command set (6) */ |
| 1584 | { 6, 1, handleM_LineTable, "Method.LineTable" }, |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1585 | { 6, 2, handleM_VariableTable, "Method.VariableTable" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1586 | { 6, 3, NULL, "Method.Bytecodes" }, |
| 1587 | { 6, 4, NULL, "Method.IsObsolete" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1588 | { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" }, |
| 1589 | |
| 1590 | /* Field command set (8) */ |
| 1591 | |
| 1592 | /* ObjectReference command set (9) */ |
| 1593 | { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" }, |
| 1594 | { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" }, |
| 1595 | { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1596 | { 9, 4, NULL, "ObjectReference.UNUSED" }, |
| 1597 | { 9, 5, NULL, "ObjectReference.MonitorInfo" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1598 | { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" }, |
| 1599 | { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" }, |
| 1600 | { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" }, |
| 1601 | { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1602 | { 9, 10, NULL, "ObjectReference.ReferringObjects" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1603 | |
| 1604 | /* StringReference command set (10) */ |
| 1605 | { 10, 1, handleSR_Value, "StringReference.Value" }, |
| 1606 | |
| 1607 | /* ThreadReference command set (11) */ |
| 1608 | { 11, 1, handleTR_Name, "ThreadReference.Name" }, |
| 1609 | { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" }, |
| 1610 | { 11, 3, handleTR_Resume, "ThreadReference.Resume" }, |
| 1611 | { 11, 4, handleTR_Status, "ThreadReference.Status" }, |
| 1612 | { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" }, |
| 1613 | { 11, 6, handleTR_Frames, "ThreadReference.Frames" }, |
| 1614 | { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1615 | { 11, 8, NULL, "ThreadReference.OwnedMonitors" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1616 | { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1617 | { 11, 10, NULL, "ThreadReference.Stop" }, |
| 1618 | { 11, 11, NULL,"ThreadReference.Interrupt" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1619 | { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1620 | { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" }, |
| 1621 | { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1622 | |
| 1623 | /* ThreadGroupReference command set (12) */ |
| 1624 | { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" }, |
| 1625 | { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" }, |
| 1626 | { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" }, |
| 1627 | |
| 1628 | /* ArrayReference command set (13) */ |
| 1629 | { 13, 1, handleAR_Length, "ArrayReference.Length" }, |
| 1630 | { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" }, |
| 1631 | { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" }, |
| 1632 | |
| 1633 | /* ClassLoaderReference command set (14) */ |
| 1634 | { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" }, |
| 1635 | |
| 1636 | /* EventRequest command set (15) */ |
| 1637 | { 15, 1, handleER_Set, "EventRequest.Set" }, |
| 1638 | { 15, 2, handleER_Clear, "EventRequest.Clear" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1639 | { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1640 | |
| 1641 | /* StackFrame command set (16) */ |
| 1642 | { 16, 1, handleSF_GetValues, "StackFrame.GetValues" }, |
| 1643 | { 16, 2, handleSF_SetValues, "StackFrame.SetValues" }, |
| 1644 | { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" }, |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1645 | { 16, 4, NULL, "StackFrame.PopFrames" }, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1646 | |
| 1647 | /* ClassObjectReference command set (17) */ |
| 1648 | { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" }, |
| 1649 | |
| 1650 | /* Event command set (64) */ |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1651 | { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1652 | |
| 1653 | { 199, 1, handleDDM_Chunk, "DDM.Chunk" }, |
| 1654 | }; |
| 1655 | |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1656 | static const char* GetCommandName(size_t cmdSet, size_t cmd) { |
| 1657 | for (int i = 0; i < (int) arraysize(gHandlerMap); i++) { |
| 1658 | if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) { |
| 1659 | return gHandlerMap[i].descr; |
| 1660 | } |
| 1661 | } |
| 1662 | return "?UNKNOWN?"; |
| 1663 | } |
| 1664 | |
| 1665 | static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) { |
| 1666 | std::string result; |
| 1667 | result += "REQ: "; |
| 1668 | result += GetCommandName(pHeader->cmdSet, pHeader->cmd); |
| 1669 | result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id); |
| 1670 | return result; |
| 1671 | } |
| 1672 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1673 | /* |
| 1674 | * Process a request from the debugger. |
| 1675 | * |
| 1676 | * On entry, the JDWP thread is in VMWAIT. |
| 1677 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1678 | void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1679 | JdwpError result = ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1680 | |
| 1681 | if (pHeader->cmdSet != kJDWPDdmCmdSet) { |
| 1682 | /* |
| 1683 | * Activity from a debugger, not merely ddms. Mark us as having an |
| 1684 | * active debugger session, and zero out the last-activity timestamp |
| 1685 | * so waitForDebugger() doesn't return if we stall for a bit here. |
| 1686 | */ |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1687 | Dbg::GoActive(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1688 | QuasiAtomicSwap64(0, &lastActivityWhen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * If a debugger event has fired in another thread, wait until the |
| 1693 | * initiating thread has suspended itself before processing messages |
| 1694 | * from the debugger. Otherwise we (the JDWP thread) could be told to |
| 1695 | * resume the thread before it has suspended. |
| 1696 | * |
| 1697 | * We call with an argument of zero to wait for the current event |
| 1698 | * thread to finish, and then clear the block. Depending on the thread |
| 1699 | * suspend policy, this may allow events in other threads to fire, |
| 1700 | * but those events have no bearing on what the debugger has sent us |
| 1701 | * in the current request. |
| 1702 | * |
| 1703 | * Note that we MUST clear the event token before waking the event |
| 1704 | * thread up, or risk waiting for the thread to suspend after we've |
| 1705 | * told it to resume. |
| 1706 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1707 | SetWaitForEventThread(0); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1708 | |
| 1709 | /* |
| 1710 | * Tell the VM that we're running and shouldn't be interrupted by GC. |
| 1711 | * Do this after anything that can stall indefinitely. |
| 1712 | */ |
| 1713 | Dbg::ThreadRunning(); |
| 1714 | |
| 1715 | expandBufAddSpace(pReply, kJDWPHeaderLen); |
| 1716 | |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1717 | size_t i; |
| 1718 | for (i = 0; i < arraysize(gHandlerMap); i++) { |
| 1719 | if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1720 | VLOG(jdwp) << DescribeCommand(pHeader, dataLen); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 1721 | result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1722 | break; |
| 1723 | } |
| 1724 | } |
| 1725 | if (i == arraysize(gHandlerMap)) { |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1726 | LOG(ERROR) << DescribeCommand(pHeader, dataLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1727 | if (dataLen > 0) { |
| 1728 | HexDump(buf, dataLen); |
| 1729 | } |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1730 | LOG(ERROR) << "command not implemented"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1731 | result = ERR_NOT_IMPLEMENTED; |
| 1732 | } |
| 1733 | |
| 1734 | /* |
| 1735 | * Set up the reply header. |
| 1736 | * |
| 1737 | * If we encountered an error, only send the header back. |
| 1738 | */ |
| 1739 | uint8_t* replyBuf = expandBufGetBuffer(pReply); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1740 | Set4BE(replyBuf + 4, pHeader->id); |
| 1741 | Set1(replyBuf + 8, kJDWPFlagReply); |
| 1742 | Set2BE(replyBuf + 9, result); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1743 | if (result == ERR_NONE) { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1744 | Set4BE(replyBuf + 0, expandBufGetLength(pReply)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1745 | } else { |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1746 | Set4BE(replyBuf + 0, kJDWPHeaderLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Elliott Hughes | a3c24aa | 2011-12-07 15:34:09 -0800 | [diff] [blame] | 1749 | size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1750 | if (false) { |
| 1751 | LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : ""); |
| 1752 | if (respLen > 0) { |
| 1753 | HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * Update last-activity timestamp. We really only need this during |
| 1759 | * the initial setup. Only update if this is a non-DDMS packet. |
| 1760 | */ |
| 1761 | if (pHeader->cmdSet != kJDWPDdmCmdSet) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 1762 | QuasiAtomicSwap64(MilliTime(), &lastActivityWhen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | /* tell the VM that GC is okay again */ |
| 1766 | Dbg::ThreadWaiting(); |
| 1767 | } |
| 1768 | |
| 1769 | } // namespace JDWP |
| 1770 | |
| 1771 | } // namespace art |