blob: 4640d98435e2fcc450d190826e317d8ab5b3a275 [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);
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800112 VLOG(jdwp) << StringPrintf(" classId=%llx methodId=%x %s.%s", classId, methodId, Dbg::GetClassName(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800113 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 Hughes2435a572012-02-17 16:07:41 -0800125 VLOG(jdwp) << " " << typeTag << StringPrintf("(%zd): 0x%llx", 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 Hughes2435a572012-02-17 16:07:41 -0800155 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" 0x%llx (except=%08llx)", 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.
Elliott Hughes436e3722012-02-17 20:01:47 -0800207 JDWP::JdwpTypeTag type_tag;
208 uint32_t class_status;
209 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
210 if (status != ERR_NONE) {
211 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800212 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213
Elliott Hughes436e3722012-02-17 20:01:47 -0800214 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800215 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800216 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700217 }
218
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700219 return ERR_NONE;
220}
221
222/*
223 * Handle request for the thread IDs of all running threads.
224 *
225 * We exclude ourselves from the list, because we don't allow ourselves
226 * to be suspended, and that violates some JDWP expectations.
227 */
228static JdwpError handleVM_AllThreads(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
229 ObjectId* pThreadIds;
230 uint32_t threadCount;
231 Dbg::GetAllThreads(&pThreadIds, &threadCount);
232
233 expandBufAdd4BE(pReply, threadCount);
234
235 ObjectId* walker = pThreadIds;
236 for (uint32_t i = 0; i < threadCount; i++) {
237 expandBufAddObjectId(pReply, *walker++);
238 }
239
240 free(pThreadIds);
241
242 return ERR_NONE;
243}
244
245/*
246 * List all thread groups that do not have a parent.
247 */
248static JdwpError handleVM_TopLevelThreadGroups(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
249 /*
250 * TODO: maintain a list of parentless thread groups in the VM.
251 *
252 * For now, just return "system". Application threads are created
253 * in "main", which is a child of "system".
254 */
255 uint32_t groups = 1;
256 expandBufAdd4BE(pReply, groups);
257 //threadGroupId = debugGetMainThreadGroup();
258 //expandBufAdd8BE(pReply, threadGroupId);
259 ObjectId threadGroupId = Dbg::GetSystemThreadGroupId();
260 expandBufAddObjectId(pReply, threadGroupId);
261
262 return ERR_NONE;
263}
264
265/*
266 * Respond with the sizes of the basic debugger types.
267 *
268 * All IDs are 8 bytes.
269 */
270static JdwpError handleVM_IDSizes(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
271 expandBufAdd4BE(pReply, sizeof(FieldId));
272 expandBufAdd4BE(pReply, sizeof(MethodId));
273 expandBufAdd4BE(pReply, sizeof(ObjectId));
274 expandBufAdd4BE(pReply, sizeof(RefTypeId));
275 expandBufAdd4BE(pReply, sizeof(FrameId));
276 return ERR_NONE;
277}
278
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700279static JdwpError handleVM_Dispose(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes86964332012-02-15 19:37:42 -0800280 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700281 return ERR_NONE;
282}
283
284/*
285 * Suspend the execution of the application running in the VM (i.e. suspend
286 * all threads).
287 *
288 * This needs to increment the "suspend count" on all threads.
289 */
290static JdwpError handleVM_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700291 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700292 return ERR_NONE;
293}
294
295/*
296 * Resume execution. Decrements the "suspend count" of all threads.
297 */
298static JdwpError handleVM_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
299 Dbg::ResumeVM();
300 return ERR_NONE;
301}
302
303/*
304 * The debugger wants the entire VM to exit.
305 */
306static JdwpError handleVM_Exit(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700307 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308
309 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
310
311 Dbg::Exit(exitCode);
312 return ERR_NOT_IMPLEMENTED; // shouldn't get here
313}
314
315/*
316 * Create a new string in the VM and return its ID.
317 *
318 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
319 * string "java.util.Arrays".)
320 */
321static JdwpError handleVM_CreateString(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800322 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800323 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324 ObjectId stringId = Dbg::CreateString(str);
325 if (stringId == 0) {
326 return ERR_OUT_OF_MEMORY;
327 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328 expandBufAddObjectId(pReply, stringId);
329 return ERR_NONE;
330}
331
332/*
333 * Tell the debugger what we are capable of.
334 */
335static JdwpError handleVM_Capabilities(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
336 expandBufAdd1(pReply, false); /* canWatchFieldModification */
337 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
338 expandBufAdd1(pReply, false); /* canGetBytecodes */
339 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
340 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
341 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
342 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
343 return ERR_NONE;
344}
345
346/*
347 * Return classpath and bootclasspath.
348 */
349static JdwpError handleVM_ClassPaths(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
350 char baseDir[2] = "/";
351
352 /*
353 * TODO: make this real. Not important for remote debugging, but
354 * might be useful for local debugging.
355 */
356 uint32_t classPaths = 1;
357 uint32_t bootClassPaths = 0;
358
Elliott Hughesa2155262011-11-16 16:26:58 -0800359 expandBufAddUtf8String(pReply, baseDir);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700360 expandBufAdd4BE(pReply, classPaths);
361 for (uint32_t i = 0; i < classPaths; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800362 expandBufAddUtf8String(pReply, ".");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700363 }
364
365 expandBufAdd4BE(pReply, bootClassPaths);
366 for (uint32_t i = 0; i < classPaths; i++) {
367 /* add bootclasspath components as strings */
368 }
369
370 return ERR_NONE;
371}
372
373/*
374 * Release a list of object IDs. (Seen in jdb.)
375 *
376 * Currently does nothing.
377 */
378static JdwpError HandleVM_DisposeObjects(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
379 return ERR_NONE;
380}
381
382/*
383 * Tell the debugger what we are capable of.
384 */
385static JdwpError handleVM_CapabilitiesNew(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
386 expandBufAdd1(pReply, false); /* canWatchFieldModification */
387 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
388 expandBufAdd1(pReply, false); /* canGetBytecodes */
389 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
390 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
391 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
392 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
393 expandBufAdd1(pReply, false); /* canRedefineClasses */
394 expandBufAdd1(pReply, false); /* canAddMethod */
395 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
396 expandBufAdd1(pReply, false); /* canPopFrames */
397 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
398 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
399 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
400 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
401 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
402 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
403 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
404 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
405 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
406 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
407
408 /* fill in reserved22 through reserved32; note count started at 1 */
409 for (int i = 22; i <= 32; i++) {
410 expandBufAdd1(pReply, false); /* reservedN */
411 }
412 return ERR_NONE;
413}
414
Elliott Hughes86964332012-02-15 19:37:42 -0800415static JdwpError handleVM_AllClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply, bool descriptor_and_status, bool generic) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800416 std::vector<JDWP::RefTypeId> classes;
417 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700418
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800419 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800421 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800422 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800423 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800424 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800425 uint32_t class_status;
426 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
427 if (status != ERR_NONE) {
428 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800429 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430
Elliott Hughes436e3722012-02-17 20:01:47 -0800431 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800432 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800433 if (descriptor_and_status) {
434 expandBufAddUtf8String(pReply, descriptor);
435 if (generic) {
436 expandBufAddUtf8String(pReply, genericSignature);
437 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800438 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800439 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 }
441
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442 return ERR_NONE;
443}
444
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800445static JdwpError handleVM_AllClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes86964332012-02-15 19:37:42 -0800446 return handleVM_AllClasses(state, buf, dataLen, pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800447}
448
449static JdwpError handleVM_AllClassesWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes86964332012-02-15 19:37:42 -0800450 return handleVM_AllClasses(state, buf, dataLen, pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800451}
452
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453/*
454 * Given a referenceTypeID, return a string with the JNI reference type
455 * signature (e.g. "Ljava/lang/Error;").
456 */
457static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
458 RefTypeId refTypeId = ReadRefTypeId(&buf);
459
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800460 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800461 std::string signature;
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800462
463 JdwpError status = Dbg::GetSignature(refTypeId, signature);
464 if (status != ERR_NONE) {
465 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800466 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800467 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700468 return ERR_NONE;
469}
470
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700471static JdwpError handleRT_Modifiers(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
472 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800473 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474}
475
476/*
477 * Get values from static fields in a reference type.
478 */
479static JdwpError handleRT_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800480 ReadRefTypeId(&buf); // We don't need this, but we need to skip over it in the request.
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700481 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800483 VLOG(jdwp) << " RT_GetValues " << numFields << ":";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484
485 expandBufAdd4BE(pReply, numFields);
486 for (uint32_t i = 0; i < numFields; i++) {
487 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800488 JdwpError status = Dbg::GetStaticFieldValue(fieldId, pReply);
489 if (status != ERR_NONE) {
490 return status;
491 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492 }
493
494 return ERR_NONE;
495}
496
497/*
498 * Get the name of the source file in which a reference type was declared.
499 */
500static JdwpError handleRT_SourceFile(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
501 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800502 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800503 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
504 if (status != ERR_NONE) {
505 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800507 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800508 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509}
510
511/*
512 * Return the current status of the reference type.
513 */
514static JdwpError handleRT_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
515 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800516 JDWP::JdwpTypeTag type_tag;
517 uint32_t class_status;
518 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
519 if (status != ERR_NONE) {
520 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800521 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800522 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523 return ERR_NONE;
524}
525
526/*
527 * Return interfaces implemented directly by this class.
528 */
529static JdwpError handleRT_Interfaces(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
530 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800531 VLOG(jdwp) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800532 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700533}
534
535/*
536 * Return the class object corresponding to this type.
537 */
538static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
539 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800540 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800541 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
542 if (status != ERR_NONE) {
543 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800544 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800545 VLOG(jdwp) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800546 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700547 return ERR_NONE;
548}
549
550/*
551 * Returns the value of the SourceDebugExtension attribute.
552 *
553 * JDB seems interested, but DEX files don't currently support this.
554 */
555static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
556 /* referenceTypeId in, string out */
557 return ERR_ABSENT_INFORMATION;
558}
559
560/*
561 * Like RT_Signature but with the possibility of a "generic signature".
562 */
563static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800564 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700565
566 RefTypeId refTypeId = ReadRefTypeId(&buf);
567
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800568 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800569 std::string signature;
570 if (Dbg::GetSignature(refTypeId, signature)) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800571 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700572 } else {
573 LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId);
Elliott Hughesa2155262011-11-16 16:26:58 -0800574 expandBufAddUtf8String(pReply, "Lunknown;");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575 }
576 expandBufAddUtf8String(pReply, genericSignature);
577
578 return ERR_NONE;
579}
580
581/*
582 * Return the instance of java.lang.ClassLoader that loaded the specified
583 * reference type, or null if it was loaded by the system loader.
584 */
585static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
586 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800587 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588}
589
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800590static std::string Describe(const RefTypeId& refTypeId) {
591 std::string signature("unknown");
592 Dbg::GetSignature(refTypeId, signature);
593 return StringPrintf("refTypeId=0x%llx (%s)", refTypeId, signature.c_str());
594}
595
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700596/*
597 * Given a referenceTypeId, return a block of stuff that describes the
598 * fields declared by a class.
599 */
600static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
601 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800602 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800603 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800604}
605
606// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
607static JdwpError handleRT_Fields(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
608 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800609 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800610 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611}
612
613/*
614 * Given a referenceTypeID, return a block of goodies describing the
615 * methods declared by a class.
616 */
617static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
618 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800619 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800620 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800621}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800623// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
624static JdwpError handleRT_Methods(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
625 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800626 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800627 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700628}
629
630/*
631 * Return the immediate superclass of a class.
632 */
633static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
634 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800635 RefTypeId superClassId;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800636 JdwpError status = Dbg::GetSuperclass(classId, superClassId);
637 if (status != ERR_NONE) {
638 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800639 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700641 return ERR_NONE;
642}
643
644/*
645 * Set static class values.
646 */
647static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
648 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700649 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700650
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800651 VLOG(jdwp) << StringPrintf(" Req to set %d values in classId=%llx", values, classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652
653 for (uint32_t i = 0; i < values; i++) {
654 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800655 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800656 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700657 uint64_t value = jdwpReadValue(&buf, width);
658
Elliott Hughes2435a572012-02-17 16:07:41 -0800659 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800660 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
661 if (status != ERR_NONE) {
662 return status;
663 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700664 }
665
666 return ERR_NONE;
667}
668
669/*
670 * Invoke a static method.
671 *
672 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
673 * values in the "variables" display.
674 */
675static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
676 RefTypeId classId = ReadRefTypeId(&buf);
677 ObjectId threadId = ReadObjectId(&buf);
678 MethodId methodId = ReadMethodId(&buf);
679
680 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
681}
682
683/*
684 * Create a new object of the requested type, and invoke the specified
685 * constructor.
686 *
687 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
688 * see the contents of a byte[] as a string.
689 */
690static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
691 RefTypeId classId = ReadRefTypeId(&buf);
692 ObjectId threadId = ReadObjectId(&buf);
693 MethodId methodId = ReadMethodId(&buf);
694
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800695 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(classId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800696 ObjectId objectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800697 JdwpError status = Dbg::CreateObject(classId, objectId);
698 if (status != ERR_NONE) {
699 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800700 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701 if (objectId == 0) {
702 return ERR_OUT_OF_MEMORY;
703 }
704 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
705}
706
707/*
708 * Create a new array object of the requested type and length.
709 */
710static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
711 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700712 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700713
Elliott Hughes2435a572012-02-17 16:07:41 -0800714 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800715 ObjectId objectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800716 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, objectId);
717 if (status != ERR_NONE) {
718 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800719 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720 if (objectId == 0) {
721 return ERR_OUT_OF_MEMORY;
722 }
723 expandBufAdd1(pReply, JT_ARRAY);
724 expandBufAddObjectId(pReply, objectId);
725 return ERR_NONE;
726}
727
728/*
729 * Return line number information for the method, if present.
730 */
731static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
732 RefTypeId refTypeId = ReadRefTypeId(&buf);
733 MethodId methodId = ReadMethodId(&buf);
734
Elliott Hughes2435a572012-02-17 16:07:41 -0800735 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId,methodId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700736
737 Dbg::OutputLineTable(refTypeId, methodId, pReply);
738
739 return ERR_NONE;
740}
741
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800742static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply, bool generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700743 RefTypeId classId = ReadRefTypeId(&buf);
744 MethodId methodId = ReadMethodId(&buf);
745
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800746 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s", Dbg::GetClassName(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700747
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800748 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
749 // information. That will cause Eclipse to make a best-effort attempt at displaying local
750 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
751 // not showing anything.
752 Dbg::OutputVariableTable(classId, methodId, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753 return ERR_NONE;
754}
755
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800756static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
757 return handleM_VariableTable(state, buf, dataLen, pReply, false);
758}
759
760static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
761 return handleM_VariableTable(state, buf, dataLen, pReply, true);
762}
763
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700764/*
765 * Given an object reference, return the runtime type of the object
766 * (class or array).
767 *
768 * This can get called on different things, e.g. threadId gets
769 * passed in here.
770 */
771static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
772 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800773 VLOG(jdwp) << StringPrintf(" Req for type of objectId=0x%llx", objectId);
Elliott Hughes2435a572012-02-17 16:07:41 -0800774 return Dbg::GetReferenceType(objectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700775}
776
777/*
778 * Get values from the fields of an object.
779 */
780static JdwpError handleOR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
781 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700782 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700783
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800784 VLOG(jdwp) << StringPrintf(" Req for %d fields from objectId=0x%llx", numFields, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700785
786 expandBufAdd4BE(pReply, numFields);
787
788 for (uint32_t i = 0; i < numFields; i++) {
789 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800790 JdwpError status = Dbg::GetFieldValue(objectId, fieldId, pReply);
791 if (status != ERR_NONE) {
792 return status;
793 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700794 }
795
796 return ERR_NONE;
797}
798
799/*
800 * Set values in the fields of an object.
801 */
802static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
803 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700804 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700805
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800806 VLOG(jdwp) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700807
808 for (uint32_t i = 0; i < numFields; i++) {
809 FieldId fieldId = ReadFieldId(&buf);
810
Elliott Hughesaed4be92011-12-02 16:16:23 -0800811 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800812 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700813 uint64_t value = jdwpReadValue(&buf, width);
814
Elliott Hughes2435a572012-02-17 16:07:41 -0800815 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800816 JdwpError status = Dbg::SetFieldValue(objectId, fieldId, value, width);
817 if (status != ERR_NONE) {
818 return status;
819 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700820 }
821
822 return ERR_NONE;
823}
824
825/*
826 * Invoke an instance method. The invocation must occur in the specified
827 * thread, which must have been suspended by an event.
828 *
829 * The call is synchronous. All threads in the VM are resumed, unless the
830 * SINGLE_THREADED flag is set.
831 *
832 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
833 * object), it will try to invoke the object's toString() function. This
834 * feature becomes crucial when examining ArrayLists with Eclipse.
835 */
836static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
837 ObjectId objectId = ReadObjectId(&buf);
838 ObjectId threadId = ReadObjectId(&buf);
839 RefTypeId classId = ReadRefTypeId(&buf);
840 MethodId methodId = ReadMethodId(&buf);
841
842 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
843}
844
845/*
846 * Disable garbage collection of the specified object.
847 */
848static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
849 // this is currently a no-op
850 return ERR_NONE;
851}
852
853/*
854 * Enable garbage collection of the specified object.
855 */
856static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
857 // this is currently a no-op
858 return ERR_NONE;
859}
860
861/*
862 * Determine whether an object has been garbage collected.
863 */
864static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
865 ObjectId objectId;
866
867 objectId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800868 VLOG(jdwp) << StringPrintf(" Req IsCollected(0x%llx)", objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700869
870 // TODO: currently returning false; must integrate with GC
871 expandBufAdd1(pReply, 0);
872
873 return ERR_NONE;
874}
875
876/*
877 * Return the string value in a string object.
878 */
879static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
880 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800881 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800883 VLOG(jdwp) << StringPrintf(" Req for str %llx --> '%s'", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700884
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800885 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700886
887 return ERR_NONE;
888}
889
890/*
891 * Return a thread's name.
892 */
893static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
894 ObjectId threadId = ReadObjectId(&buf);
895
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800896 VLOG(jdwp) << StringPrintf(" Req for name of thread 0x%llx", threadId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800897 std::string name;
898 if (!Dbg::GetThreadName(threadId, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899 return ERR_INVALID_THREAD;
900 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800901 VLOG(jdwp) << StringPrintf(" Name of thread 0x%llx is \"%s\"", threadId, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800902 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903
904 return ERR_NONE;
905}
906
907/*
908 * Suspend the specified thread.
909 *
910 * It's supposed to remain suspended even if interpreted code wants to
911 * resume it; only the JDI is allowed to resume it.
912 */
913static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
914 ObjectId threadId = ReadObjectId(&buf);
915
916 if (threadId == Dbg::GetThreadSelfId()) {
917 LOG(INFO) << " Warning: ignoring request to suspend self";
918 return ERR_THREAD_NOT_SUSPENDED;
919 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800920 VLOG(jdwp) << StringPrintf(" Req to suspend thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921 Dbg::SuspendThread(threadId);
922 return ERR_NONE;
923}
924
925/*
926 * Resume the specified thread.
927 */
928static JdwpError handleTR_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
929 ObjectId threadId = ReadObjectId(&buf);
930
931 if (threadId == Dbg::GetThreadSelfId()) {
932 LOG(INFO) << " Warning: ignoring request to resume self";
933 return ERR_NONE;
934 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800935 VLOG(jdwp) << StringPrintf(" Req to resume thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700936 Dbg::ResumeThread(threadId);
937 return ERR_NONE;
938}
939
940/*
941 * Return status of specified thread.
942 */
943static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
944 ObjectId threadId = ReadObjectId(&buf);
945
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800946 VLOG(jdwp) << StringPrintf(" Req for status of thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800948 JDWP::JdwpThreadStatus threadStatus;
949 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
951 return ERR_INVALID_THREAD;
952 }
953
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800954 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955
956 expandBufAdd4BE(pReply, threadStatus);
957 expandBufAdd4BE(pReply, suspendStatus);
958
959 return ERR_NONE;
960}
961
962/*
963 * Return the thread group that the specified thread is a member of.
964 */
965static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
966 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes2435a572012-02-17 16:07:41 -0800967 return Dbg::GetThreadGroup(threadId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700968}
969
970/*
971 * Return the current call stack of a suspended thread.
972 *
973 * If the thread isn't suspended, the error code isn't defined, but should
974 * be THREAD_NOT_SUSPENDED.
975 */
976static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
977 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800978 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700979 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980
981 if (!Dbg::ThreadExists(threadId)) {
982 return ERR_INVALID_THREAD;
983 }
984 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800985 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986 return ERR_THREAD_NOT_SUSPENDED;
987 }
988
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800989 size_t actual_frame_count = Dbg::GetThreadFrameCount(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800991 VLOG(jdwp) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%zd]", threadId, start_frame, length, actual_frame_count);
992 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700993 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
994 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800996 if (start_frame > actual_frame_count) {
997 return ERR_INVALID_INDEX;
998 }
999 if (length == static_cast<uint32_t>(-1)) {
1000 length = actual_frame_count - start_frame;
1001 }
1002 if (start_frame + length > actual_frame_count) {
1003 return ERR_INVALID_LENGTH;
1004 }
1005
1006 expandBufAdd4BE(pReply, length);
1007 for (uint32_t i = start_frame; i < (start_frame + length); ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001008 FrameId frameId;
1009 JdwpLocation loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001010 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
1011
1012 expandBufAdd8BE(pReply, frameId);
1013 AddLocation(pReply, &loc);
1014
Elliott Hughes2435a572012-02-17 16:07:41 -08001015 VLOG(jdwp) << StringPrintf(" Frame %d: id=%llx ", i, frameId) << loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001016 }
1017
1018 return ERR_NONE;
1019}
1020
1021/*
1022 * Returns the #of frames on the specified thread, which must be suspended.
1023 */
1024static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1025 ObjectId threadId = ReadObjectId(&buf);
1026
1027 if (!Dbg::ThreadExists(threadId)) {
1028 return ERR_INVALID_THREAD;
1029 }
1030 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001031 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001032 return ERR_THREAD_NOT_SUSPENDED;
1033 }
1034
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001035 int frame_count = Dbg::GetThreadFrameCount(threadId);
1036 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001037 return ERR_INVALID_THREAD;
1038 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001039 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001040
1041 return ERR_NONE;
1042}
1043
1044/*
1045 * Get the monitor that the thread is waiting on.
1046 */
1047static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1048 ObjectId threadId;
1049
1050 threadId = ReadObjectId(&buf);
1051
1052 // TODO: create an Object to represent the monitor (we're currently
1053 // just using a raw Monitor struct in the VM)
1054
1055 return ERR_NOT_IMPLEMENTED;
1056}
1057
1058/*
1059 * Return the suspend count for the specified thread.
1060 *
1061 * (The thread *might* still be running -- it might not have examined
1062 * its suspend count recently.)
1063 */
1064static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1065 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes2435a572012-02-17 16:07:41 -08001066 return Dbg::GetThreadSuspendCount(threadId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001067}
1068
1069/*
1070 * Return the name of a thread group.
1071 *
1072 * The Eclipse debugger recognizes "main" and "system" as special.
1073 */
1074static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1075 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001076 VLOG(jdwp) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001077
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001078 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001079
1080 return ERR_NONE;
1081}
1082
1083/*
1084 * Returns the thread group -- if any -- that contains the specified
1085 * thread group.
1086 */
1087static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1088 ObjectId groupId = ReadObjectId(&buf);
1089
1090 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1091 expandBufAddObjectId(pReply, parentGroup);
1092
1093 return ERR_NONE;
1094}
1095
1096/*
1097 * Return the active threads and thread groups that are part of the
1098 * specified thread group.
1099 */
1100static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1101 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001102 VLOG(jdwp) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001103
1104 ObjectId* pThreadIds;
1105 uint32_t threadCount;
1106 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1107
1108 expandBufAdd4BE(pReply, threadCount);
1109
1110 for (uint32_t i = 0; i < threadCount; i++) {
1111 expandBufAddObjectId(pReply, pThreadIds[i]);
1112 }
1113 free(pThreadIds);
1114
1115 /*
1116 * TODO: finish support for child groups
1117 *
1118 * For now, just show that "main" is a child of "system".
1119 */
1120 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1121 expandBufAdd4BE(pReply, 1);
1122 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1123 } else {
1124 expandBufAdd4BE(pReply, 0);
1125 }
1126
1127 return ERR_NONE;
1128}
1129
1130/*
1131 * Return the #of components in the array.
1132 */
1133static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1134 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001135 VLOG(jdwp) << StringPrintf(" Req for length of array 0x%llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001137 int length;
1138 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1139 if (status != ERR_NONE) {
1140 return status;
1141 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001142 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001143
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001144 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001145
1146 return ERR_NONE;
1147}
1148
1149/*
1150 * Return the values from an array.
1151 */
1152static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1153 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001154 uint32_t firstIndex = Read4BE(&buf);
1155 uint32_t length = Read4BE(&buf);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001156 VLOG(jdwp) << StringPrintf(" Req for array values 0x%llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001157
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001158 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001159}
1160
1161/*
1162 * Set values in an array.
1163 */
1164static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1165 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001166 uint32_t firstIndex = Read4BE(&buf);
1167 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001169 VLOG(jdwp) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001170
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001171 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172}
1173
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001174static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1175 ObjectId classLoaderObject;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001176 classLoaderObject = ReadObjectId(&buf);
Elliott Hughes86964332012-02-15 19:37:42 -08001177 // TODO: we should only return classes which have the given class loader as a defining or
1178 // initiating loader. The former would be easy; the latter is hard, because we don't have
1179 // any such notion.
1180 return handleVM_AllClasses(state, buf, dataLen, pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001181}
1182
1183/*
1184 * Set an event trigger.
1185 *
1186 * Reply with a requestID.
1187 */
1188static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1189 const uint8_t* origBuf = buf;
1190
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001191 uint8_t eventKind = Read1(&buf);
1192 uint8_t suspendPolicy = Read1(&buf);
1193 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001194
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001195 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001196 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1197 << " mods=" << modifierCount << ")";
1198
1199 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1200
1201 JdwpEvent* pEvent = EventAlloc(modifierCount);
1202 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1203 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1204 pEvent->modCount = modifierCount;
1205
1206 /*
1207 * Read modifiers. Ordering may be significant (see explanation of Count
1208 * mods in JDWP doc).
1209 */
1210 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001211 JdwpModKind modKind = static_cast<JdwpModKind>(Read1(&buf));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001212
1213 pEvent->mods[idx].modKind = modKind;
1214
1215 switch (modKind) {
1216 case MK_COUNT: /* report once, when "--count" reaches 0 */
1217 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001218 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001219 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001220 if (count == 0) {
1221 return ERR_INVALID_COUNT;
1222 }
1223 pEvent->mods[idx].count.count = count;
1224 }
1225 break;
1226 case MK_CONDITIONAL: /* conditional on expression) */
1227 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001228 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001229 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001230 pEvent->mods[idx].conditional.exprId = exprId;
1231 }
1232 break;
1233 case MK_THREAD_ONLY: /* only report events in specified thread */
1234 {
1235 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001236 VLOG(jdwp) << StringPrintf(" ThreadOnly: %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001237 pEvent->mods[idx].threadOnly.threadId = threadId;
1238 }
1239 break;
1240 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1241 {
1242 RefTypeId clazzId = ReadRefTypeId(&buf);
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001243 VLOG(jdwp) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassName(clazzId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001244 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1245 }
1246 break;
1247 case MK_CLASS_MATCH: /* restrict events to matching classes */
1248 {
Elliott Hughes86964332012-02-15 19:37:42 -08001249 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001250 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001251 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001252 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes86964332012-02-15 19:37:42 -08001253 pEvent->mods[idx].classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001254 }
1255 break;
1256 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1257 {
Elliott Hughes86964332012-02-15 19:37:42 -08001258 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001259 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001260 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001261 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes86964332012-02-15 19:37:42 -08001262 pEvent->mods[idx].classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001263 }
1264 break;
1265 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1266 {
1267 JdwpLocation loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001268 jdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001269 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270 pEvent->mods[idx].locationOnly.loc = loc;
1271 }
1272 break;
1273 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1274 {
1275 RefTypeId exceptionOrNull; /* null == all exceptions */
1276 uint8_t caught, uncaught;
1277
1278 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001279 caught = Read1(&buf);
1280 uncaught = Read1(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001281 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001282 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001283
1284 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1285 pEvent->mods[idx].exceptionOnly.caught = caught;
1286 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1287 }
1288 break;
1289 case MK_FIELD_ONLY: /* for field access/mod events */
1290 {
1291 RefTypeId declaring = ReadRefTypeId(&buf);
1292 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001293 VLOG(jdwp) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001294 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1295 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1296 }
1297 break;
1298 case MK_STEP: /* for use with EK_SINGLE_STEP */
1299 {
1300 ObjectId threadId;
1301 uint32_t size, depth;
1302
1303 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001304 size = Read4BE(&buf);
1305 depth = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001306 VLOG(jdwp) << StringPrintf(" Step: thread=%llx", threadId)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001307 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1308
1309 pEvent->mods[idx].step.threadId = threadId;
1310 pEvent->mods[idx].step.size = size;
1311 pEvent->mods[idx].step.depth = depth;
1312 }
1313 break;
1314 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1315 {
1316 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001317 VLOG(jdwp) << StringPrintf(" InstanceOnly: %llx", instance);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001318 pEvent->mods[idx].instanceOnly.objectId = instance;
1319 }
1320 break;
1321 default:
1322 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1323 break;
1324 }
1325 }
1326
1327 /*
1328 * Make sure we consumed all data. It is possible that the remote side
1329 * has sent us bad stuff, but for now we blame ourselves.
1330 */
1331 if (buf != origBuf + dataLen) {
1332 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1333 }
1334
1335 /*
1336 * We reply with an integer "requestID".
1337 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001338 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001339 expandBufAdd4BE(pReply, requestId);
1340
1341 pEvent->requestId = requestId;
1342
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001343 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001344
1345 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001346 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001347 if (err != ERR_NONE) {
1348 /* registration failed, probably because event is bogus */
1349 EventFree(pEvent);
1350 LOG(WARNING) << "WARNING: event request rejected";
1351 }
1352 return err;
1353}
1354
1355/*
1356 * Clear an event. Failure to find an event with a matching ID is a no-op
1357 * and does not return an error.
1358 */
1359static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1360 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001361 eventKind = Read1(&buf);
1362 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001363
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001364 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001365
Elliott Hughes761928d2011-11-16 18:33:03 -08001366 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001367
1368 return ERR_NONE;
1369}
1370
1371/*
1372 * Return the values of arguments and local variables.
1373 */
1374static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1375 ObjectId threadId = ReadObjectId(&buf);
1376 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001377 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001378
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001379 VLOG(jdwp) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001380
1381 expandBufAdd4BE(pReply, slots); /* "int values" */
1382 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001383 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001384 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001385
Elliott Hughes2435a572012-02-17 16:07:41 -08001386 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001387
Elliott Hughesdbb40792011-11-18 17:05:22 -08001388 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001389 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1390 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1391 }
1392
1393 return ERR_NONE;
1394}
1395
1396/*
1397 * Set the values of arguments and local variables.
1398 */
1399static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1400 ObjectId threadId = ReadObjectId(&buf);
1401 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001402 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001403
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001404 VLOG(jdwp) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405
1406 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001407 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001408 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001409 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001410 uint64_t value = jdwpReadValue(&buf, width);
1411
Elliott Hughes2435a572012-02-17 16:07:41 -08001412 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001413 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1414 }
1415
1416 return ERR_NONE;
1417}
1418
1419/*
1420 * Returns the value of "this" for the specified frame.
1421 */
1422static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08001423 ReadObjectId(&buf); // Skip thread id.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001424 FrameId frameId = ReadFrameId(&buf);
1425
1426 ObjectId objectId;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001427 Dbg::GetThisObject(frameId, &objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001428
1429 uint8_t objectTag = Dbg::GetObjectTag(objectId);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001430 VLOG(jdwp) << StringPrintf(" Req for 'this' in frame=%llx --> %llx '%c'", frameId, objectId, (char)objectTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001431
1432 expandBufAdd1(pReply, objectTag);
1433 expandBufAddObjectId(pReply, objectId);
1434
1435 return ERR_NONE;
1436}
1437
1438/*
1439 * Return the reference type reflected by this class object.
1440 *
1441 * This appears to be required because ReferenceTypeId values are NEVER
1442 * reused, whereas ClassIds can be recycled like any other object. (Either
1443 * that, or I have no idea what this is for.)
1444 */
1445static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1446 RefTypeId classObjectId = ReadRefTypeId(&buf);
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001447 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001448 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001449}
1450
1451/*
1452 * Handle a DDM packet with a single chunk in it.
1453 */
1454static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1455 uint8_t* replyBuf = NULL;
1456 int replyLen = -1;
1457
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001458 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001459
1460 /*
1461 * On first DDM packet, notify all handlers that DDM is running.
1462 */
1463 if (!state->ddmActive) {
1464 state->ddmActive = true;
1465 Dbg::DdmConnected();
1466 }
1467
1468 /*
1469 * If they want to send something back, we copy it into the buffer.
1470 * A no-copy approach would be nicer.
1471 *
1472 * TODO: consider altering the JDWP stuff to hold the packet header
1473 * in a separate buffer. That would allow us to writev() DDM traffic
1474 * instead of copying it into the expanding buffer. The reduction in
1475 * heap requirements is probably more valuable than the efficiency.
1476 */
1477 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1478 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1479 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1480 free(replyBuf);
1481 }
1482 return ERR_NONE;
1483}
1484
1485/*
1486 * Handler map decl.
1487 */
1488typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1489
1490struct JdwpHandlerMap {
1491 uint8_t cmdSet;
1492 uint8_t cmd;
1493 JdwpRequestHandler func;
1494 const char* descr;
1495};
1496
1497/*
1498 * Map commands to functions.
1499 *
1500 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1501 * and 128-256 are vendor-defined.
1502 */
1503static const JdwpHandlerMap gHandlerMap[] = {
1504 /* VirtualMachine command set (1) */
1505 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1506 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
Elliott Hughes1fe7afb2012-02-13 17:23:03 -08001507 { 1, 3, handleVM_AllClasses, "VirtualMachine.AllClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001508 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1509 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1510 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1511 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1512 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1513 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1514 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1515 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1516 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1517 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1518 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001519 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1520 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001521 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001522 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1523 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
1524 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
1525 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001526
1527 /* ReferenceType command set (2) */
1528 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1529 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1530 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001531 { 2, 4, handleRT_Fields, "ReferenceType.Fields" },
1532 { 2, 5, handleRT_Methods, "ReferenceType.Methods" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001533 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1534 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001535 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001536 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1537 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1538 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1539 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1540 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1541 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1542 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001543 { 2, 16, NULL, "ReferenceType.Instances" },
1544 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1545 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001546
1547 /* ClassType command set (3) */
1548 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1549 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1550 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1551 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1552
1553 /* ArrayType command set (4) */
1554 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1555
1556 /* InterfaceType command set (5) */
1557
1558 /* Method command set (6) */
1559 { 6, 1, handleM_LineTable, "Method.LineTable" },
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001560 { 6, 2, handleM_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001561 { 6, 3, NULL, "Method.Bytecodes" },
1562 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001563 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1564
1565 /* Field command set (8) */
1566
1567 /* ObjectReference command set (9) */
1568 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1569 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1570 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001571 { 9, 4, NULL, "ObjectReference.UNUSED" },
1572 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001573 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1574 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1575 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1576 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001577 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001578
1579 /* StringReference command set (10) */
1580 { 10, 1, handleSR_Value, "StringReference.Value" },
1581
1582 /* ThreadReference command set (11) */
1583 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1584 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1585 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1586 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1587 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1588 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1589 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001590 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001592 { 11, 10, NULL, "ThreadReference.Stop" },
1593 { 11, 11, NULL,"ThreadReference.Interrupt" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001594 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001595 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1596 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001597
1598 /* ThreadGroupReference command set (12) */
1599 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1600 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1601 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1602
1603 /* ArrayReference command set (13) */
1604 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1605 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1606 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1607
1608 /* ClassLoaderReference command set (14) */
1609 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1610
1611 /* EventRequest command set (15) */
1612 { 15, 1, handleER_Set, "EventRequest.Set" },
1613 { 15, 2, handleER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001614 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001615
1616 /* StackFrame command set (16) */
1617 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1618 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1619 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001620 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001621
1622 /* ClassObjectReference command set (17) */
1623 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1624
1625 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001626 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001627
1628 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1629};
1630
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001631static const char* GetCommandName(size_t cmdSet, size_t cmd) {
1632 for (int i = 0; i < (int) arraysize(gHandlerMap); i++) {
1633 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1634 return gHandlerMap[i].descr;
1635 }
1636 }
1637 return "?UNKNOWN?";
1638}
1639
1640static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1641 std::string result;
1642 result += "REQ: ";
1643 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1644 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1645 return result;
1646}
1647
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001648/*
1649 * Process a request from the debugger.
1650 *
1651 * On entry, the JDWP thread is in VMWAIT.
1652 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001653void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001654 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001655
1656 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1657 /*
1658 * Activity from a debugger, not merely ddms. Mark us as having an
1659 * active debugger session, and zero out the last-activity timestamp
1660 * so waitForDebugger() doesn't return if we stall for a bit here.
1661 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001662 Dbg::GoActive();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001663 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001664 }
1665
1666 /*
1667 * If a debugger event has fired in another thread, wait until the
1668 * initiating thread has suspended itself before processing messages
1669 * from the debugger. Otherwise we (the JDWP thread) could be told to
1670 * resume the thread before it has suspended.
1671 *
1672 * We call with an argument of zero to wait for the current event
1673 * thread to finish, and then clear the block. Depending on the thread
1674 * suspend policy, this may allow events in other threads to fire,
1675 * but those events have no bearing on what the debugger has sent us
1676 * in the current request.
1677 *
1678 * Note that we MUST clear the event token before waking the event
1679 * thread up, or risk waiting for the thread to suspend after we've
1680 * told it to resume.
1681 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001682 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001683
1684 /*
1685 * Tell the VM that we're running and shouldn't be interrupted by GC.
1686 * Do this after anything that can stall indefinitely.
1687 */
1688 Dbg::ThreadRunning();
1689
1690 expandBufAddSpace(pReply, kJDWPHeaderLen);
1691
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001692 size_t i;
1693 for (i = 0; i < arraysize(gHandlerMap); i++) {
1694 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001695 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001696 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001697 break;
1698 }
1699 }
1700 if (i == arraysize(gHandlerMap)) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001701 LOG(ERROR) << DescribeCommand(pHeader, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001702 if (dataLen > 0) {
1703 HexDump(buf, dataLen);
1704 }
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001705 LOG(ERROR) << "command not implemented";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001706 result = ERR_NOT_IMPLEMENTED;
1707 }
1708
1709 /*
1710 * Set up the reply header.
1711 *
1712 * If we encountered an error, only send the header back.
1713 */
1714 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001715 Set4BE(replyBuf + 4, pHeader->id);
1716 Set1(replyBuf + 8, kJDWPFlagReply);
1717 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001718 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001719 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001720 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001721 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722 }
1723
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001724 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001725 if (false) {
1726 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1727 if (respLen > 0) {
1728 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1729 }
1730 }
1731
1732 /*
1733 * Update last-activity timestamp. We really only need this during
1734 * the initial setup. Only update if this is a non-DDMS packet.
1735 */
1736 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001737 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001738 }
1739
1740 /* tell the VM that GC is okay again */
1741 Dbg::ThreadWaiting();
1742}
1743
1744} // namespace JDWP
1745
1746} // namespace art