blob: 7914aa7ce636a85fada42205f567c07d1c5a8909 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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
43namespace art {
44
45namespace JDWP {
46
47/*
48 * Helper function: read a "location" from an input buffer.
49 */
50static void jdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
51 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughesd07986f2011-12-06 18:27:45 -080052 pLoc->typeTag = ReadTypeTag(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070053 pLoc->classId = ReadObjectId(pBuf);
54 pLoc->methodId = ReadMethodId(pBuf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -070055 pLoc->idx = Read8BE(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056}
57
58/*
59 * Helper function: write a "location" into the reply buffer.
60 */
61void 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 Hughesdbb40792011-11-18 17:05:22 -080071static uint64_t jdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072 uint64_t value = -1;
73 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070074 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 Hughes872d4ec2011-10-21 17:07:15 -070078 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 */
86static 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 */
102static 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 Hughesf7c3b662011-10-27 12:04:56 -0700109 uint32_t numArgs = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700110
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800111 VLOG(jdwp) << StringPrintf(" --> threadId=%llx objectId=%llx", threadId, objectId);
112 VLOG(jdwp) << StringPrintf(" classId=%llx methodId=%x %s.%s", classId, methodId, Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
113 VLOG(jdwp) << StringPrintf(" %d args:", numArgs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114
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 Hughesaed4be92011-12-02 16:16:23 -0800121 JDWP::JdwpTag typeTag = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800122 size_t width = Dbg::GetTagWidth(typeTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 uint64_t value = jdwpReadValue(&buf, width);
124
Elliott Hughesabd04b92012-01-18 22:46:41 -0800125 VLOG(jdwp) << StringPrintf(" '%c'(%zd): 0x%llx", typeTag, width, value);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 argArray[i] = value;
127 }
128
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700129 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800130 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options, (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "", (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131
Elliott Hughesaed4be92011-12-02 16:16:23 -0800132 JDWP::JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 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 Hughesdbb40792011-11-18 17:05:22 -0800145 size_t width = Dbg::GetTagWidth(resultTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146
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 Hughes4dd9b4d2011-12-12 18:29:24 -0800155 VLOG(jdwp) << StringPrintf(" --> returned '%c' 0x%llx (except=%08llx)", resultTag, resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156
157 /* show detailed debug output */
158 if (resultTag == JT_STRING && exceptObjId == 0) {
159 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800160 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800162 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700163 }
164 }
165 }
166
167bail:
168 free(argArray);
169 return err;
170}
171
172
173/*
174 * Request for version info.
175 */
176static 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 Hughes4740cdf2011-12-07 14:07:12 -0800179 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 /* JDWP version numbers */
181 expandBufAdd4BE(pReply, 1); // major
182 expandBufAdd4BE(pReply, 5); // minor
183 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800184 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800186 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187
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 */
196static JdwpError handleVM_ClassesBySignature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800197 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800198 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800200 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800201 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800203 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700204
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800205 for (size_t i = 0; i < ids.size(); ++i) {
206 // Get class vs. interface and status flags.
207 JDWP::JdwpTypeTag typeTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208 uint32_t status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800209 if (!Dbg::GetClassInfo(ids[i], &typeTag, &status, NULL)) {
210 return ERR_INVALID_CLASS;
211 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212
213 expandBufAdd1(pReply, typeTag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800214 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 expandBufAdd4BE(pReply, status);
216 }
217
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218 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 */
227static 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 */
247static 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 */
269static 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
278/*
279 * The debugger is politely asking to disconnect. We're good with that.
280 *
281 * We could resume threads and clean up pinned references, but we can do
282 * that when the TCP connection drops.
283 */
284static JdwpError handleVM_Dispose(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
285 return ERR_NONE;
286}
287
288/*
289 * Suspend the execution of the application running in the VM (i.e. suspend
290 * all threads).
291 *
292 * This needs to increment the "suspend count" on all threads.
293 */
294static JdwpError handleVM_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700295 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700296 return ERR_NONE;
297}
298
299/*
300 * Resume execution. Decrements the "suspend count" of all threads.
301 */
302static JdwpError handleVM_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
303 Dbg::ResumeVM();
304 return ERR_NONE;
305}
306
307/*
308 * The debugger wants the entire VM to exit.
309 */
310static JdwpError handleVM_Exit(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700311 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700312
313 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
314
315 Dbg::Exit(exitCode);
316 return ERR_NOT_IMPLEMENTED; // shouldn't get here
317}
318
319/*
320 * Create a new string in the VM and return its ID.
321 *
322 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
323 * string "java.util.Arrays".)
324 */
325static JdwpError handleVM_CreateString(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800326 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800327 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328 ObjectId stringId = Dbg::CreateString(str);
329 if (stringId == 0) {
330 return ERR_OUT_OF_MEMORY;
331 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700332 expandBufAddObjectId(pReply, stringId);
333 return ERR_NONE;
334}
335
336/*
337 * Tell the debugger what we are capable of.
338 */
339static JdwpError handleVM_Capabilities(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
340 expandBufAdd1(pReply, false); /* canWatchFieldModification */
341 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
342 expandBufAdd1(pReply, false); /* canGetBytecodes */
343 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
344 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
345 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
346 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
347 return ERR_NONE;
348}
349
350/*
351 * Return classpath and bootclasspath.
352 */
353static JdwpError handleVM_ClassPaths(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
354 char baseDir[2] = "/";
355
356 /*
357 * TODO: make this real. Not important for remote debugging, but
358 * might be useful for local debugging.
359 */
360 uint32_t classPaths = 1;
361 uint32_t bootClassPaths = 0;
362
Elliott Hughesa2155262011-11-16 16:26:58 -0800363 expandBufAddUtf8String(pReply, baseDir);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700364 expandBufAdd4BE(pReply, classPaths);
365 for (uint32_t i = 0; i < classPaths; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800366 expandBufAddUtf8String(pReply, ".");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367 }
368
369 expandBufAdd4BE(pReply, bootClassPaths);
370 for (uint32_t i = 0; i < classPaths; i++) {
371 /* add bootclasspath components as strings */
372 }
373
374 return ERR_NONE;
375}
376
377/*
378 * Release a list of object IDs. (Seen in jdb.)
379 *
380 * Currently does nothing.
381 */
382static JdwpError HandleVM_DisposeObjects(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
383 return ERR_NONE;
384}
385
386/*
387 * Tell the debugger what we are capable of.
388 */
389static JdwpError handleVM_CapabilitiesNew(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
390 expandBufAdd1(pReply, false); /* canWatchFieldModification */
391 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
392 expandBufAdd1(pReply, false); /* canGetBytecodes */
393 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
394 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
395 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
396 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
397 expandBufAdd1(pReply, false); /* canRedefineClasses */
398 expandBufAdd1(pReply, false); /* canAddMethod */
399 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
400 expandBufAdd1(pReply, false); /* canPopFrames */
401 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
402 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
403 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
404 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
405 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
406 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
407 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
408 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
409 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
410 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
411
412 /* fill in reserved22 through reserved32; note count started at 1 */
413 for (int i = 22; i <= 32; i++) {
414 expandBufAdd1(pReply, false); /* reservedN */
415 }
416 return ERR_NONE;
417}
418
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800419static JdwpError handleVM_AllClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply, bool generic) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800420 std::vector<JDWP::RefTypeId> classes;
421 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700422
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800423 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800425 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800426 static const char genericSignature[1] = "";
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800427 JDWP::JdwpTypeTag refTypeTag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800428 std::string descriptor;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700429 uint32_t status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800430 if (!Dbg::GetClassInfo(classes[i], &refTypeTag, &status, &descriptor)) {
431 return ERR_INVALID_CLASS;
432 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433
434 expandBufAdd1(pReply, refTypeTag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800435 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800436 expandBufAddUtf8String(pReply, descriptor);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800437 if (generic) {
438 expandBufAddUtf8String(pReply, genericSignature);
439 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 expandBufAdd4BE(pReply, status);
441 }
442
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700443 return ERR_NONE;
444}
445
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800446static JdwpError handleVM_AllClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
447 return handleVM_AllClasses(state, buf, dataLen, pReply, false);
448}
449
450static JdwpError handleVM_AllClassesWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
451 return handleVM_AllClasses(state, buf, dataLen, pReply, true);
452}
453
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454/*
455 * Given a referenceTypeID, return a string with the JNI reference type
456 * signature (e.g. "Ljava/lang/Error;").
457 */
458static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
459 RefTypeId refTypeId = ReadRefTypeId(&buf);
460
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800461 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800462 std::string signature;
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800463
464 JdwpError status = Dbg::GetSignature(refTypeId, signature);
465 if (status != ERR_NONE) {
466 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800467 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800468 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 return ERR_NONE;
470}
471
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472static JdwpError handleRT_Modifiers(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
473 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800474 uint32_t access_flags;
475 if (!Dbg::GetAccessFlags(refTypeId, access_flags)) {
476 return ERR_INVALID_CLASS;
477 }
Elliott Hughesb3057702012-02-14 10:44:29 -0800478
479 // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set.
480 // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
481 access_flags |= kAccSuper;
482
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800483 expandBufAdd4BE(pReply, access_flags);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484 return ERR_NONE;
485}
486
487/*
488 * Get values from static fields in a reference type.
489 */
490static JdwpError handleRT_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800491 ReadRefTypeId(&buf); // We don't need this, but we need to skip over it in the request.
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700492 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800494 VLOG(jdwp) << " RT_GetValues " << numFields << ":";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495
496 expandBufAdd4BE(pReply, numFields);
497 for (uint32_t i = 0; i < numFields; i++) {
498 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800499 Dbg::GetStaticFieldValue(fieldId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 }
501
502 return ERR_NONE;
503}
504
505/*
506 * Get the name of the source file in which a reference type was declared.
507 */
508static JdwpError handleRT_SourceFile(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
509 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800510 std::string source_file;
511 if (!Dbg::GetSourceFile(refTypeId, source_file)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512 return ERR_ABSENT_INFORMATION;
513 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800514 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800515 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516}
517
518/*
519 * Return the current status of the reference type.
520 */
521static JdwpError handleRT_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
522 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800523 JDWP::JdwpTypeTag typeTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524 uint32_t status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800525 if (!Dbg::GetClassInfo(refTypeId, &typeTag, &status, NULL)) {
526 return ERR_INVALID_CLASS;
527 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700528 expandBufAdd4BE(pReply, status);
529 return ERR_NONE;
530}
531
532/*
533 * Return interfaces implemented directly by this class.
534 */
535static JdwpError handleRT_Interfaces(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
536 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800537 VLOG(jdwp) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId, Dbg::GetClassDescriptor(refTypeId).c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800538 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply) ? ERR_NONE : ERR_INVALID_CLASS;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539}
540
541/*
542 * Return the class object corresponding to this type.
543 */
544static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
545 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800546 ObjectId classObjectId;
547 if (!Dbg::GetClassObject(refTypeId, classObjectId)) {
548 return ERR_INVALID_CLASS;
549 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800550 VLOG(jdwp) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800551 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700552 return ERR_NONE;
553}
554
555/*
556 * Returns the value of the SourceDebugExtension attribute.
557 *
558 * JDB seems interested, but DEX files don't currently support this.
559 */
560static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
561 /* referenceTypeId in, string out */
562 return ERR_ABSENT_INFORMATION;
563}
564
565/*
566 * Like RT_Signature but with the possibility of a "generic signature".
567 */
568static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800569 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570
571 RefTypeId refTypeId = ReadRefTypeId(&buf);
572
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800573 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800574 std::string signature;
575 if (Dbg::GetSignature(refTypeId, signature)) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800576 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700577 } else {
578 LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId);
Elliott Hughesa2155262011-11-16 16:26:58 -0800579 expandBufAddUtf8String(pReply, "Lunknown;");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580 }
581 expandBufAddUtf8String(pReply, genericSignature);
582
583 return ERR_NONE;
584}
585
586/*
587 * Return the instance of java.lang.ClassLoader that loaded the specified
588 * reference type, or null if it was loaded by the system loader.
589 */
590static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
591 RefTypeId refTypeId = ReadRefTypeId(&buf);
592
593 expandBufAddObjectId(pReply, Dbg::GetClassLoader(refTypeId));
594
595 return ERR_NONE;
596}
597
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800598static std::string Describe(const RefTypeId& refTypeId) {
599 std::string signature("unknown");
600 Dbg::GetSignature(refTypeId, signature);
601 return StringPrintf("refTypeId=0x%llx (%s)", refTypeId, signature.c_str());
602}
603
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604/*
605 * Given a referenceTypeId, return a block of stuff that describes the
606 * fields declared by a class.
607 */
608static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
609 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800610 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800611 return Dbg::OutputDeclaredFields(refTypeId, true, pReply) ? ERR_NONE : ERR_INVALID_CLASS;
612}
613
614// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
615static JdwpError handleRT_Fields(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
616 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800617 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800618 return Dbg::OutputDeclaredFields(refTypeId, false, pReply) ? ERR_NONE : ERR_INVALID_CLASS;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619}
620
621/*
622 * Given a referenceTypeID, return a block of goodies describing the
623 * methods declared by a class.
624 */
625static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
626 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800627 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800628 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply) ? ERR_NONE : ERR_INVALID_CLASS;
629}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700630
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800631// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
632static JdwpError handleRT_Methods(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
633 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800634 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800635 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply) ? ERR_NONE : ERR_INVALID_CLASS;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700636}
637
638/*
639 * Return the immediate superclass of a class.
640 */
641static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
642 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800643 RefTypeId superClassId;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800644 JdwpError status = Dbg::GetSuperclass(classId, superClassId);
645 if (status != ERR_NONE) {
646 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800647 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700649 return ERR_NONE;
650}
651
652/*
653 * Set static class values.
654 */
655static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
656 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700657 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800659 VLOG(jdwp) << StringPrintf(" Req to set %d values in classId=%llx", values, classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700660
661 for (uint32_t i = 0; i < values; i++) {
662 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800663 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800664 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700665 uint64_t value = jdwpReadValue(&buf, width);
666
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800667 VLOG(jdwp) << StringPrintf(" --> field=%x tag=%c -> %lld", fieldId, fieldTag, value);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800668 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
669 if (status != ERR_NONE) {
670 return status;
671 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672 }
673
674 return ERR_NONE;
675}
676
677/*
678 * Invoke a static method.
679 *
680 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
681 * values in the "variables" display.
682 */
683static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
684 RefTypeId classId = ReadRefTypeId(&buf);
685 ObjectId threadId = ReadObjectId(&buf);
686 MethodId methodId = ReadMethodId(&buf);
687
688 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
689}
690
691/*
692 * Create a new object of the requested type, and invoke the specified
693 * constructor.
694 *
695 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
696 * see the contents of a byte[] as a string.
697 */
698static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
699 RefTypeId classId = ReadRefTypeId(&buf);
700 ObjectId threadId = ReadObjectId(&buf);
701 MethodId methodId = ReadMethodId(&buf);
702
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800703 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassDescriptor(classId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800704 ObjectId objectId;
705 if (!Dbg::CreateObject(classId, objectId)) {
706 return ERR_INVALID_CLASS;
707 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700708 if (objectId == 0) {
709 return ERR_OUT_OF_MEMORY;
710 }
711 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
712}
713
714/*
715 * Create a new array object of the requested type and length.
716 */
717static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
718 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700719 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800721 VLOG(jdwp) << StringPrintf("Creating array %s[%u]", Dbg::GetClassDescriptor(arrayTypeId).c_str(), length);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800722 ObjectId objectId;
723 if (!Dbg::CreateArrayObject(arrayTypeId, length, objectId)) {
724 return ERR_INVALID_CLASS;
725 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726 if (objectId == 0) {
727 return ERR_OUT_OF_MEMORY;
728 }
729 expandBufAdd1(pReply, JT_ARRAY);
730 expandBufAddObjectId(pReply, objectId);
731 return ERR_NONE;
732}
733
734/*
735 * Return line number information for the method, if present.
736 */
737static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
738 RefTypeId refTypeId = ReadRefTypeId(&buf);
739 MethodId methodId = ReadMethodId(&buf);
740
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800741 VLOG(jdwp) << StringPrintf(" Req for line table in %s.%s", Dbg::GetClassDescriptor(refTypeId).c_str(), Dbg::GetMethodName(refTypeId,methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
743 Dbg::OutputLineTable(refTypeId, methodId, pReply);
744
745 return ERR_NONE;
746}
747
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800748static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply, bool generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700749 RefTypeId classId = ReadRefTypeId(&buf);
750 MethodId methodId = ReadMethodId(&buf);
751
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800752 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s", Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800754 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
755 // information. That will cause Eclipse to make a best-effort attempt at displaying local
756 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
757 // not showing anything.
758 Dbg::OutputVariableTable(classId, methodId, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700759 return ERR_NONE;
760}
761
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800762static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
763 return handleM_VariableTable(state, buf, dataLen, pReply, false);
764}
765
766static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
767 return handleM_VariableTable(state, buf, dataLen, pReply, true);
768}
769
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700770/*
771 * Given an object reference, return the runtime type of the object
772 * (class or array).
773 *
774 * This can get called on different things, e.g. threadId gets
775 * passed in here.
776 */
777static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
778 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800779 VLOG(jdwp) << StringPrintf(" Req for type of objectId=0x%llx", objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700780
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800781 JDWP::JdwpTypeTag refTypeTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700782 RefTypeId typeId;
783 Dbg::GetObjectType(objectId, &refTypeTag, &typeId);
784
785 expandBufAdd1(pReply, refTypeTag);
786 expandBufAddRefTypeId(pReply, typeId);
787
788 return ERR_NONE;
789}
790
791/*
792 * Get values from the fields of an object.
793 */
794static JdwpError handleOR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
795 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700796 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700797
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800798 VLOG(jdwp) << StringPrintf(" Req for %d fields from objectId=0x%llx", numFields, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700799
800 expandBufAdd4BE(pReply, numFields);
801
802 for (uint32_t i = 0; i < numFields; i++) {
803 FieldId fieldId = ReadFieldId(&buf);
804 Dbg::GetFieldValue(objectId, fieldId, pReply);
805 }
806
807 return ERR_NONE;
808}
809
810/*
811 * Set values in the fields of an object.
812 */
813static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
814 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700815 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700816
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800817 VLOG(jdwp) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818
819 for (uint32_t i = 0; i < numFields; i++) {
820 FieldId fieldId = ReadFieldId(&buf);
821
Elliott Hughesaed4be92011-12-02 16:16:23 -0800822 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800823 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824 uint64_t value = jdwpReadValue(&buf, width);
825
Elliott Hughesabd04b92012-01-18 22:46:41 -0800826 VLOG(jdwp) << StringPrintf(" --> fieldId=%x tag='%c'(%zd) value=%lld", fieldId, fieldTag, width, value);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700827
828 Dbg::SetFieldValue(objectId, fieldId, value, width);
829 }
830
831 return ERR_NONE;
832}
833
834/*
835 * Invoke an instance method. The invocation must occur in the specified
836 * thread, which must have been suspended by an event.
837 *
838 * The call is synchronous. All threads in the VM are resumed, unless the
839 * SINGLE_THREADED flag is set.
840 *
841 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
842 * object), it will try to invoke the object's toString() function. This
843 * feature becomes crucial when examining ArrayLists with Eclipse.
844 */
845static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
846 ObjectId objectId = ReadObjectId(&buf);
847 ObjectId threadId = ReadObjectId(&buf);
848 RefTypeId classId = ReadRefTypeId(&buf);
849 MethodId methodId = ReadMethodId(&buf);
850
851 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
852}
853
854/*
855 * Disable garbage collection of the specified object.
856 */
857static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
858 // this is currently a no-op
859 return ERR_NONE;
860}
861
862/*
863 * Enable garbage collection of the specified object.
864 */
865static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
866 // this is currently a no-op
867 return ERR_NONE;
868}
869
870/*
871 * Determine whether an object has been garbage collected.
872 */
873static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
874 ObjectId objectId;
875
876 objectId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800877 VLOG(jdwp) << StringPrintf(" Req IsCollected(0x%llx)", objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700878
879 // TODO: currently returning false; must integrate with GC
880 expandBufAdd1(pReply, 0);
881
882 return ERR_NONE;
883}
884
885/*
886 * Return the string value in a string object.
887 */
888static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
889 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800890 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700891
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800892 VLOG(jdwp) << StringPrintf(" Req for str %llx --> '%s'", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700893
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800894 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700895
896 return ERR_NONE;
897}
898
899/*
900 * Return a thread's name.
901 */
902static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
903 ObjectId threadId = ReadObjectId(&buf);
904
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800905 VLOG(jdwp) << StringPrintf(" Req for name of thread 0x%llx", threadId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800906 std::string name;
907 if (!Dbg::GetThreadName(threadId, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700908 return ERR_INVALID_THREAD;
909 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800910 VLOG(jdwp) << StringPrintf(" Name of thread 0x%llx is \"%s\"", threadId, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800911 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912
913 return ERR_NONE;
914}
915
916/*
917 * Suspend the specified thread.
918 *
919 * It's supposed to remain suspended even if interpreted code wants to
920 * resume it; only the JDI is allowed to resume it.
921 */
922static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
923 ObjectId threadId = ReadObjectId(&buf);
924
925 if (threadId == Dbg::GetThreadSelfId()) {
926 LOG(INFO) << " Warning: ignoring request to suspend self";
927 return ERR_THREAD_NOT_SUSPENDED;
928 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800929 VLOG(jdwp) << StringPrintf(" Req to suspend thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700930 Dbg::SuspendThread(threadId);
931 return ERR_NONE;
932}
933
934/*
935 * Resume the specified thread.
936 */
937static JdwpError handleTR_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
938 ObjectId threadId = ReadObjectId(&buf);
939
940 if (threadId == Dbg::GetThreadSelfId()) {
941 LOG(INFO) << " Warning: ignoring request to resume self";
942 return ERR_NONE;
943 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800944 VLOG(jdwp) << StringPrintf(" Req to resume thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700945 Dbg::ResumeThread(threadId);
946 return ERR_NONE;
947}
948
949/*
950 * Return status of specified thread.
951 */
952static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
953 ObjectId threadId = ReadObjectId(&buf);
954
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800955 VLOG(jdwp) << StringPrintf(" Req for status of thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700956
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800957 JDWP::JdwpThreadStatus threadStatus;
958 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700959 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
960 return ERR_INVALID_THREAD;
961 }
962
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800963 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700964
965 expandBufAdd4BE(pReply, threadStatus);
966 expandBufAdd4BE(pReply, suspendStatus);
967
968 return ERR_NONE;
969}
970
971/*
972 * Return the thread group that the specified thread is a member of.
973 */
974static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
975 ObjectId threadId = ReadObjectId(&buf);
976
977 /* currently not handling these */
978 ObjectId threadGroupId = Dbg::GetThreadGroup(threadId);
979 expandBufAddObjectId(pReply, threadGroupId);
980
981 return ERR_NONE;
982}
983
984/*
985 * Return the current call stack of a suspended thread.
986 *
987 * If the thread isn't suspended, the error code isn't defined, but should
988 * be THREAD_NOT_SUSPENDED.
989 */
990static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
991 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700992 uint32_t startFrame = Read4BE(&buf);
993 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700994
995 if (!Dbg::ThreadExists(threadId)) {
996 return ERR_INVALID_THREAD;
997 }
998 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800999 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001000 return ERR_THREAD_NOT_SUSPENDED;
1001 }
1002
Elliott Hughes761928d2011-11-16 18:33:03 -08001003 size_t frameCount = Dbg::GetThreadFrameCount(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004
Elliott Hughesabd04b92012-01-18 22:46:41 -08001005 VLOG(jdwp) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%zd]", threadId, startFrame, length, frameCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001006 if (frameCount <= 0) {
1007 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1008 }
1009 if (length == (uint32_t) -1) {
1010 length = frameCount;
1011 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001012 CHECK_GE(startFrame, 0U);
1013 CHECK_LT(startFrame, frameCount);
1014 CHECK_LE(startFrame + length, frameCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001015
1016 uint32_t frames = length;
1017 expandBufAdd4BE(pReply, frames);
1018 for (uint32_t i = startFrame; i < (startFrame+length); i++) {
1019 FrameId frameId;
1020 JdwpLocation loc;
1021
1022 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
1023
1024 expandBufAdd8BE(pReply, frameId);
1025 AddLocation(pReply, &loc);
1026
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001027 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 Hughes872d4ec2011-10-21 17:07:15 -07001028 }
1029
1030 return ERR_NONE;
1031}
1032
1033/*
1034 * Returns the #of frames on the specified thread, which must be suspended.
1035 */
1036static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1037 ObjectId threadId = ReadObjectId(&buf);
1038
1039 if (!Dbg::ThreadExists(threadId)) {
1040 return ERR_INVALID_THREAD;
1041 }
1042 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001043 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001044 return ERR_THREAD_NOT_SUSPENDED;
1045 }
1046
1047 int frameCount = Dbg::GetThreadFrameCount(threadId);
1048 if (frameCount < 0) {
1049 return ERR_INVALID_THREAD;
1050 }
1051 expandBufAdd4BE(pReply, (uint32_t)frameCount);
1052
1053 return ERR_NONE;
1054}
1055
1056/*
1057 * Get the monitor that the thread is waiting on.
1058 */
1059static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1060 ObjectId threadId;
1061
1062 threadId = ReadObjectId(&buf);
1063
1064 // TODO: create an Object to represent the monitor (we're currently
1065 // just using a raw Monitor struct in the VM)
1066
1067 return ERR_NOT_IMPLEMENTED;
1068}
1069
1070/*
1071 * Return the suspend count for the specified thread.
1072 *
1073 * (The thread *might* still be running -- it might not have examined
1074 * its suspend count recently.)
1075 */
1076static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1077 ObjectId threadId = ReadObjectId(&buf);
1078
1079 uint32_t suspendCount = Dbg::GetThreadSuspendCount(threadId);
1080 expandBufAdd4BE(pReply, suspendCount);
1081
1082 return ERR_NONE;
1083}
1084
1085/*
1086 * Return the name of a thread group.
1087 *
1088 * The Eclipse debugger recognizes "main" and "system" as special.
1089 */
1090static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1091 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001092 VLOG(jdwp) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001093
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001094 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001095
1096 return ERR_NONE;
1097}
1098
1099/*
1100 * Returns the thread group -- if any -- that contains the specified
1101 * thread group.
1102 */
1103static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1104 ObjectId groupId = ReadObjectId(&buf);
1105
1106 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1107 expandBufAddObjectId(pReply, parentGroup);
1108
1109 return ERR_NONE;
1110}
1111
1112/*
1113 * Return the active threads and thread groups that are part of the
1114 * specified thread group.
1115 */
1116static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1117 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001118 VLOG(jdwp) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001119
1120 ObjectId* pThreadIds;
1121 uint32_t threadCount;
1122 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1123
1124 expandBufAdd4BE(pReply, threadCount);
1125
1126 for (uint32_t i = 0; i < threadCount; i++) {
1127 expandBufAddObjectId(pReply, pThreadIds[i]);
1128 }
1129 free(pThreadIds);
1130
1131 /*
1132 * TODO: finish support for child groups
1133 *
1134 * For now, just show that "main" is a child of "system".
1135 */
1136 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1137 expandBufAdd4BE(pReply, 1);
1138 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1139 } else {
1140 expandBufAdd4BE(pReply, 0);
1141 }
1142
1143 return ERR_NONE;
1144}
1145
1146/*
1147 * Return the #of components in the array.
1148 */
1149static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1150 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001151 VLOG(jdwp) << StringPrintf(" Req for length of array 0x%llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001152
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001153 int length;
1154 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1155 if (status != ERR_NONE) {
1156 return status;
1157 }
1158 VLOG(jdwp) << StringPrintf(" --> %d", length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001159
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001160 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001161
1162 return ERR_NONE;
1163}
1164
1165/*
1166 * Return the values from an array.
1167 */
1168static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1169 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001170 uint32_t firstIndex = Read4BE(&buf);
1171 uint32_t length = Read4BE(&buf);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001172 VLOG(jdwp) << StringPrintf(" Req for array values 0x%llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001173
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001174 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001175}
1176
1177/*
1178 * Set values in an array.
1179 */
1180static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1181 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001182 uint32_t firstIndex = Read4BE(&buf);
1183 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001184
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001185 VLOG(jdwp) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001187 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188}
1189
1190/*
1191 * Return the set of classes visible to a class loader. All classes which
1192 * have the class loader as a defining or initiating loader are returned.
1193 */
1194static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1195 ObjectId classLoaderObject;
1196 uint32_t numClasses = 0;
1197 RefTypeId* classRefBuf = NULL;
1198 int i;
1199
1200 classLoaderObject = ReadObjectId(&buf);
1201
1202 Dbg::GetVisibleClassList(classLoaderObject, &numClasses, &classRefBuf);
1203
1204 expandBufAdd4BE(pReply, numClasses);
1205 for (i = 0; i < (int) numClasses; i++) {
1206 uint8_t refTypeTag = Dbg::GetClassObjectType(classRefBuf[i]);
1207
1208 expandBufAdd1(pReply, refTypeTag);
1209 expandBufAddRefTypeId(pReply, classRefBuf[i]);
1210 }
1211
1212 return ERR_NONE;
1213}
1214
1215/*
1216 * Return a newly-allocated string in which all occurrences of '.' have
1217 * been changed to '/'. If we find a '/' in the original string, NULL
1218 * is returned to avoid ambiguity.
1219 */
1220char* dvmDotToSlash(const char* str) {
1221 char* newStr = strdup(str);
1222 char* cp = newStr;
1223
1224 if (newStr == NULL) {
1225 return NULL;
1226 }
1227
1228 while (*cp != '\0') {
1229 if (*cp == '/') {
1230 CHECK(false);
1231 return NULL;
1232 }
1233 if (*cp == '.') {
1234 *cp = '/';
1235 }
1236 cp++;
1237 }
1238
1239 return newStr;
1240}
1241
1242/*
1243 * Set an event trigger.
1244 *
1245 * Reply with a requestID.
1246 */
1247static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1248 const uint8_t* origBuf = buf;
1249
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001250 uint8_t eventKind = Read1(&buf);
1251 uint8_t suspendPolicy = Read1(&buf);
1252 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001253
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001254 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001255 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1256 << " mods=" << modifierCount << ")";
1257
1258 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1259
1260 JdwpEvent* pEvent = EventAlloc(modifierCount);
1261 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1262 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1263 pEvent->modCount = modifierCount;
1264
1265 /*
1266 * Read modifiers. Ordering may be significant (see explanation of Count
1267 * mods in JDWP doc).
1268 */
1269 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001270 JdwpModKind modKind = static_cast<JdwpModKind>(Read1(&buf));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001271
1272 pEvent->mods[idx].modKind = modKind;
1273
1274 switch (modKind) {
1275 case MK_COUNT: /* report once, when "--count" reaches 0 */
1276 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001277 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001278 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001279 if (count == 0) {
1280 return ERR_INVALID_COUNT;
1281 }
1282 pEvent->mods[idx].count.count = count;
1283 }
1284 break;
1285 case MK_CONDITIONAL: /* conditional on expression) */
1286 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001287 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001288 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289 pEvent->mods[idx].conditional.exprId = exprId;
1290 }
1291 break;
1292 case MK_THREAD_ONLY: /* only report events in specified thread */
1293 {
1294 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001295 VLOG(jdwp) << StringPrintf(" ThreadOnly: %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001296 pEvent->mods[idx].threadOnly.threadId = threadId;
1297 }
1298 break;
1299 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1300 {
1301 RefTypeId clazzId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001302 VLOG(jdwp) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassDescriptor(clazzId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1304 }
1305 break;
1306 case MK_CLASS_MATCH: /* restrict events to matching classes */
1307 {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001308 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001309 VLOG(jdwp) << StringPrintf(" ClassMatch: '%s'", pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001310 /* pattern is "java.foo.*", we want "java/foo/ *" */
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001311 pEvent->mods[idx].classMatch.classPattern = dvmDotToSlash(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 }
1313 break;
1314 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1315 {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001316 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001317 VLOG(jdwp) << StringPrintf(" ClassExclude: '%s'", pattern.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001318 pEvent->mods[idx].classExclude.classPattern = dvmDotToSlash(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001319 }
1320 break;
1321 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1322 {
1323 JdwpLocation loc;
1324
1325 jdwpReadLocation(&buf, &loc);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001326 VLOG(jdwp) << StringPrintf(" LocationOnly: typeTag=%d classId=%llx methodId=%x idx=%llx",
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001327 loc.typeTag, loc.classId, loc.methodId, loc.idx);
1328 pEvent->mods[idx].locationOnly.loc = loc;
1329 }
1330 break;
1331 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1332 {
1333 RefTypeId exceptionOrNull; /* null == all exceptions */
1334 uint8_t caught, uncaught;
1335
1336 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001337 caught = Read1(&buf);
1338 uncaught = Read1(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001339 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
Elliott Hughesa2155262011-11-16 16:26:58 -08001340 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassDescriptor(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001341
1342 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1343 pEvent->mods[idx].exceptionOnly.caught = caught;
1344 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1345 }
1346 break;
1347 case MK_FIELD_ONLY: /* for field access/mod events */
1348 {
1349 RefTypeId declaring = ReadRefTypeId(&buf);
1350 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001351 VLOG(jdwp) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001352 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1353 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1354 }
1355 break;
1356 case MK_STEP: /* for use with EK_SINGLE_STEP */
1357 {
1358 ObjectId threadId;
1359 uint32_t size, depth;
1360
1361 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001362 size = Read4BE(&buf);
1363 depth = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001364 VLOG(jdwp) << StringPrintf(" Step: thread=%llx", threadId)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001365 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1366
1367 pEvent->mods[idx].step.threadId = threadId;
1368 pEvent->mods[idx].step.size = size;
1369 pEvent->mods[idx].step.depth = depth;
1370 }
1371 break;
1372 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1373 {
1374 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001375 VLOG(jdwp) << StringPrintf(" InstanceOnly: %llx", instance);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001376 pEvent->mods[idx].instanceOnly.objectId = instance;
1377 }
1378 break;
1379 default:
1380 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1381 break;
1382 }
1383 }
1384
1385 /*
1386 * Make sure we consumed all data. It is possible that the remote side
1387 * has sent us bad stuff, but for now we blame ourselves.
1388 */
1389 if (buf != origBuf + dataLen) {
1390 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1391 }
1392
1393 /*
1394 * We reply with an integer "requestID".
1395 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001396 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397 expandBufAdd4BE(pReply, requestId);
1398
1399 pEvent->requestId = requestId;
1400
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001401 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001402
1403 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001404 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405 if (err != ERR_NONE) {
1406 /* registration failed, probably because event is bogus */
1407 EventFree(pEvent);
1408 LOG(WARNING) << "WARNING: event request rejected";
1409 }
1410 return err;
1411}
1412
1413/*
1414 * Clear an event. Failure to find an event with a matching ID is a no-op
1415 * and does not return an error.
1416 */
1417static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1418 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001419 eventKind = Read1(&buf);
1420 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001421
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001422 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001423
Elliott Hughes761928d2011-11-16 18:33:03 -08001424 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001425
1426 return ERR_NONE;
1427}
1428
1429/*
1430 * Return the values of arguments and local variables.
1431 */
1432static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1433 ObjectId threadId = ReadObjectId(&buf);
1434 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001435 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001436
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001437 VLOG(jdwp) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001438
1439 expandBufAdd4BE(pReply, slots); /* "int values" */
1440 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001441 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001442 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001443
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001444 VLOG(jdwp) << StringPrintf(" --> slot %d '%c'", slot, reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001445
Elliott Hughesdbb40792011-11-18 17:05:22 -08001446 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001447 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1448 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1449 }
1450
1451 return ERR_NONE;
1452}
1453
1454/*
1455 * Set the values of arguments and local variables.
1456 */
1457static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1458 ObjectId threadId = ReadObjectId(&buf);
1459 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001460 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001461
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001462 VLOG(jdwp) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001463
1464 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001465 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001466 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001467 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001468 uint64_t value = jdwpReadValue(&buf, width);
1469
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001470 VLOG(jdwp) << StringPrintf(" --> slot %d '%c' %llx", slot, sigByte, value);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001471 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1472 }
1473
1474 return ERR_NONE;
1475}
1476
1477/*
1478 * Returns the value of "this" for the specified frame.
1479 */
1480static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08001481 ReadObjectId(&buf); // Skip thread id.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001482 FrameId frameId = ReadFrameId(&buf);
1483
1484 ObjectId objectId;
Elliott Hughesd07986f2011-12-06 18:27:45 -08001485 if (!Dbg::GetThisObject(frameId, &objectId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001486 return ERR_INVALID_FRAMEID;
1487 }
1488
1489 uint8_t objectTag = Dbg::GetObjectTag(objectId);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001490 VLOG(jdwp) << StringPrintf(" Req for 'this' in frame=%llx --> %llx '%c'", frameId, objectId, (char)objectTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001491
1492 expandBufAdd1(pReply, objectTag);
1493 expandBufAddObjectId(pReply, objectId);
1494
1495 return ERR_NONE;
1496}
1497
1498/*
1499 * Return the reference type reflected by this class object.
1500 *
1501 * This appears to be required because ReferenceTypeId values are NEVER
1502 * reused, whereas ClassIds can be recycled like any other object. (Either
1503 * that, or I have no idea what this is for.)
1504 */
1505static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1506 RefTypeId classObjectId = ReadRefTypeId(&buf);
1507
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001508 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassDescriptor(classObjectId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001509
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001510 bool is_interface;
1511 if (!Dbg::IsInterface(classObjectId, is_interface)) {
1512 return ERR_INVALID_CLASS;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001513 }
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001514
1515 expandBufAdd1(pReply, is_interface ? TT_INTERFACE : TT_CLASS);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001516 expandBufAddRefTypeId(pReply, classObjectId);
1517
1518 return ERR_NONE;
1519}
1520
1521/*
1522 * Handle a DDM packet with a single chunk in it.
1523 */
1524static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1525 uint8_t* replyBuf = NULL;
1526 int replyLen = -1;
1527
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001528 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001529
1530 /*
1531 * On first DDM packet, notify all handlers that DDM is running.
1532 */
1533 if (!state->ddmActive) {
1534 state->ddmActive = true;
1535 Dbg::DdmConnected();
1536 }
1537
1538 /*
1539 * If they want to send something back, we copy it into the buffer.
1540 * A no-copy approach would be nicer.
1541 *
1542 * TODO: consider altering the JDWP stuff to hold the packet header
1543 * in a separate buffer. That would allow us to writev() DDM traffic
1544 * instead of copying it into the expanding buffer. The reduction in
1545 * heap requirements is probably more valuable than the efficiency.
1546 */
1547 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1548 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1549 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1550 free(replyBuf);
1551 }
1552 return ERR_NONE;
1553}
1554
1555/*
1556 * Handler map decl.
1557 */
1558typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1559
1560struct JdwpHandlerMap {
1561 uint8_t cmdSet;
1562 uint8_t cmd;
1563 JdwpRequestHandler func;
1564 const char* descr;
1565};
1566
1567/*
1568 * Map commands to functions.
1569 *
1570 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1571 * and 128-256 are vendor-defined.
1572 */
1573static const JdwpHandlerMap gHandlerMap[] = {
1574 /* VirtualMachine command set (1) */
1575 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1576 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
Elliott Hughes1fe7afb2012-02-13 17:23:03 -08001577 { 1, 3, handleVM_AllClasses, "VirtualMachine.AllClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001578 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1579 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1580 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1581 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1582 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1583 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1584 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1585 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1586 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1587 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1588 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001589 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1590 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001592 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1593 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
1594 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
1595 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001596
1597 /* ReferenceType command set (2) */
1598 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1599 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1600 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001601 { 2, 4, handleRT_Fields, "ReferenceType.Fields" },
1602 { 2, 5, handleRT_Methods, "ReferenceType.Methods" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001603 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1604 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001605 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001606 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1607 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1608 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1609 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1610 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1611 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1612 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001613 { 2, 16, NULL, "ReferenceType.Instances" },
1614 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1615 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001616
1617 /* ClassType command set (3) */
1618 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1619 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1620 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1621 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1622
1623 /* ArrayType command set (4) */
1624 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1625
1626 /* InterfaceType command set (5) */
1627
1628 /* Method command set (6) */
1629 { 6, 1, handleM_LineTable, "Method.LineTable" },
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001630 { 6, 2, handleM_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001631 { 6, 3, NULL, "Method.Bytecodes" },
1632 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001633 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1634
1635 /* Field command set (8) */
1636
1637 /* ObjectReference command set (9) */
1638 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1639 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1640 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001641 { 9, 4, NULL, "ObjectReference.UNUSED" },
1642 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001643 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1644 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1645 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1646 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001647 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001648
1649 /* StringReference command set (10) */
1650 { 10, 1, handleSR_Value, "StringReference.Value" },
1651
1652 /* ThreadReference command set (11) */
1653 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1654 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1655 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1656 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1657 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1658 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1659 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001660 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001661 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001662 { 11, 10, NULL, "ThreadReference.Stop" },
1663 { 11, 11, NULL,"ThreadReference.Interrupt" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001664 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001665 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1666 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001667
1668 /* ThreadGroupReference command set (12) */
1669 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1670 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1671 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1672
1673 /* ArrayReference command set (13) */
1674 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1675 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1676 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1677
1678 /* ClassLoaderReference command set (14) */
1679 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1680
1681 /* EventRequest command set (15) */
1682 { 15, 1, handleER_Set, "EventRequest.Set" },
1683 { 15, 2, handleER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001684 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001685
1686 /* StackFrame command set (16) */
1687 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1688 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1689 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001690 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001691
1692 /* ClassObjectReference command set (17) */
1693 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1694
1695 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001696 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001697
1698 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1699};
1700
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001701static const char* GetCommandName(size_t cmdSet, size_t cmd) {
1702 for (int i = 0; i < (int) arraysize(gHandlerMap); i++) {
1703 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1704 return gHandlerMap[i].descr;
1705 }
1706 }
1707 return "?UNKNOWN?";
1708}
1709
1710static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1711 std::string result;
1712 result += "REQ: ";
1713 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1714 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1715 return result;
1716}
1717
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001718/*
1719 * Process a request from the debugger.
1720 *
1721 * On entry, the JDWP thread is in VMWAIT.
1722 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001723void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001724 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001725
1726 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1727 /*
1728 * Activity from a debugger, not merely ddms. Mark us as having an
1729 * active debugger session, and zero out the last-activity timestamp
1730 * so waitForDebugger() doesn't return if we stall for a bit here.
1731 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001732 Dbg::GoActive();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001733 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001734 }
1735
1736 /*
1737 * If a debugger event has fired in another thread, wait until the
1738 * initiating thread has suspended itself before processing messages
1739 * from the debugger. Otherwise we (the JDWP thread) could be told to
1740 * resume the thread before it has suspended.
1741 *
1742 * We call with an argument of zero to wait for the current event
1743 * thread to finish, and then clear the block. Depending on the thread
1744 * suspend policy, this may allow events in other threads to fire,
1745 * but those events have no bearing on what the debugger has sent us
1746 * in the current request.
1747 *
1748 * Note that we MUST clear the event token before waking the event
1749 * thread up, or risk waiting for the thread to suspend after we've
1750 * told it to resume.
1751 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001752 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001753
1754 /*
1755 * Tell the VM that we're running and shouldn't be interrupted by GC.
1756 * Do this after anything that can stall indefinitely.
1757 */
1758 Dbg::ThreadRunning();
1759
1760 expandBufAddSpace(pReply, kJDWPHeaderLen);
1761
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001762 size_t i;
1763 for (i = 0; i < arraysize(gHandlerMap); i++) {
1764 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001765 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001766 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001767 break;
1768 }
1769 }
1770 if (i == arraysize(gHandlerMap)) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001771 LOG(ERROR) << DescribeCommand(pHeader, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001772 if (dataLen > 0) {
1773 HexDump(buf, dataLen);
1774 }
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001775 LOG(ERROR) << "command not implemented";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001776 result = ERR_NOT_IMPLEMENTED;
1777 }
1778
1779 /*
1780 * Set up the reply header.
1781 *
1782 * If we encountered an error, only send the header back.
1783 */
1784 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001785 Set4BE(replyBuf + 4, pHeader->id);
1786 Set1(replyBuf + 8, kJDWPFlagReply);
1787 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001788 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001789 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001790 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001791 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001792 }
1793
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001794 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001795 if (false) {
1796 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1797 if (respLen > 0) {
1798 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1799 }
1800 }
1801
1802 /*
1803 * Update last-activity timestamp. We really only need this during
1804 * the initial setup. Only update if this is a non-DDMS packet.
1805 */
1806 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001807 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001808 }
1809
1810 /* tell the VM that GC is okay again */
1811 Dbg::ThreadWaiting();
1812}
1813
1814} // namespace JDWP
1815
1816} // namespace art