blob: d6b9bb34d59ee769432b10a95939bcd5e04f10a5 [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 *
Elliott Hughes45651fd2012-02-21 15:48:20 -080099 * If "is_constructor" is set, this returns "objectId" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100 * 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,
Elliott Hughes45651fd2012-02-21 15:48:20 -0800105 bool is_constructor)
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700106{
Elliott Hughes45651fd2012-02-21 15:48:20 -0800107 CHECK(!is_constructor || objectId != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108
Elliott Hughes45651fd2012-02-21 15:48:20 -0800109 uint32_t arg_count = 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 Hughes45651fd2012-02-21 15:48:20 -0800113 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114
Elliott Hughes45651fd2012-02-21 15:48:20 -0800115 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
116 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
117 for (uint32_t i = 0; i < arg_count; ++i) {
118 argTypes[i] = ReadTag(&buf);
119 size_t width = Dbg::GetTagWidth(argTypes[i]);
120 argValues[i] = jdwpReadValue(&buf, width);
121 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): 0x%llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 }
123
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700124 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800125 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 -0700126
Elliott Hughes45651fd2012-02-21 15:48:20 -0800127 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128 uint64_t resultValue;
129 ObjectId exceptObjId;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800130 JdwpError err = Dbg::InvokeMethod(threadId, objectId, classId, methodId, arg_count, argValues.get(), argTypes.get(), options, &resultTag, &resultValue, &exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800132 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 }
134
135 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800136 if (is_constructor) {
137 // If we invoked a constructor (which actually returns void), return the receiver,
138 // unless we threw, in which case we return NULL.
139 resultTag = JT_OBJECT;
140 resultValue = (exceptObjId == 0) ? objectId : 0;
141 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700142
Elliott Hughes45651fd2012-02-21 15:48:20 -0800143 size_t width = Dbg::GetTagWidth(resultTag);
144 expandBufAdd1(pReply, resultTag);
145 if (width != 0) {
146 jdwpWriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147 }
148 expandBufAdd1(pReply, JT_OBJECT);
149 expandBufAddObjectId(pReply, exceptObjId);
150
Elliott Hughes2435a572012-02-17 16:07:41 -0800151 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" 0x%llx (except=%08llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152
153 /* show detailed debug output */
154 if (resultTag == JT_STRING && exceptObjId == 0) {
155 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800156 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800158 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700159 }
160 }
161 }
162
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700163 return err;
164}
165
166
167/*
168 * Request for version info.
169 */
170static JdwpError handleVM_Version(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
171 /* text information on runtime version */
172 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800173 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174 /* JDWP version numbers */
175 expandBufAdd4BE(pReply, 1); // major
176 expandBufAdd4BE(pReply, 5); // minor
177 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800178 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800180 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181
182 return ERR_NONE;
183}
184
185/*
186 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
187 * referenceTypeID. We need to send back more than one if the class has
188 * been loaded by multiple class loaders.
189 */
190static JdwpError handleVM_ClassesBySignature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800191 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800192 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800194 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800195 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800197 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800199 for (size_t i = 0; i < ids.size(); ++i) {
200 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800201 JDWP::JdwpTypeTag type_tag;
202 uint32_t class_status;
203 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
204 if (status != ERR_NONE) {
205 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800206 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700207
Elliott Hughes436e3722012-02-17 20:01:47 -0800208 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800209 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800210 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211 }
212
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213 return ERR_NONE;
214}
215
216/*
217 * Handle request for the thread IDs of all running threads.
218 *
219 * We exclude ourselves from the list, because we don't allow ourselves
220 * to be suspended, and that violates some JDWP expectations.
221 */
222static JdwpError handleVM_AllThreads(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
223 ObjectId* pThreadIds;
224 uint32_t threadCount;
225 Dbg::GetAllThreads(&pThreadIds, &threadCount);
226
227 expandBufAdd4BE(pReply, threadCount);
228
229 ObjectId* walker = pThreadIds;
230 for (uint32_t i = 0; i < threadCount; i++) {
231 expandBufAddObjectId(pReply, *walker++);
232 }
233
234 free(pThreadIds);
235
236 return ERR_NONE;
237}
238
239/*
240 * List all thread groups that do not have a parent.
241 */
242static JdwpError handleVM_TopLevelThreadGroups(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
243 /*
244 * TODO: maintain a list of parentless thread groups in the VM.
245 *
246 * For now, just return "system". Application threads are created
247 * in "main", which is a child of "system".
248 */
249 uint32_t groups = 1;
250 expandBufAdd4BE(pReply, groups);
251 //threadGroupId = debugGetMainThreadGroup();
252 //expandBufAdd8BE(pReply, threadGroupId);
253 ObjectId threadGroupId = Dbg::GetSystemThreadGroupId();
254 expandBufAddObjectId(pReply, threadGroupId);
255
256 return ERR_NONE;
257}
258
259/*
260 * Respond with the sizes of the basic debugger types.
261 *
262 * All IDs are 8 bytes.
263 */
264static JdwpError handleVM_IDSizes(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
265 expandBufAdd4BE(pReply, sizeof(FieldId));
266 expandBufAdd4BE(pReply, sizeof(MethodId));
267 expandBufAdd4BE(pReply, sizeof(ObjectId));
268 expandBufAdd4BE(pReply, sizeof(RefTypeId));
269 expandBufAdd4BE(pReply, sizeof(FrameId));
270 return ERR_NONE;
271}
272
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700273static JdwpError handleVM_Dispose(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes86964332012-02-15 19:37:42 -0800274 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700275 return ERR_NONE;
276}
277
278/*
279 * Suspend the execution of the application running in the VM (i.e. suspend
280 * all threads).
281 *
282 * This needs to increment the "suspend count" on all threads.
283 */
284static JdwpError handleVM_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700285 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700286 return ERR_NONE;
287}
288
289/*
290 * Resume execution. Decrements the "suspend count" of all threads.
291 */
292static JdwpError handleVM_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
293 Dbg::ResumeVM();
294 return ERR_NONE;
295}
296
297/*
298 * The debugger wants the entire VM to exit.
299 */
300static JdwpError handleVM_Exit(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700301 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700302
303 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
304
305 Dbg::Exit(exitCode);
306 return ERR_NOT_IMPLEMENTED; // shouldn't get here
307}
308
309/*
310 * Create a new string in the VM and return its ID.
311 *
312 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
313 * string "java.util.Arrays".)
314 */
315static JdwpError handleVM_CreateString(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800316 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800317 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700318 ObjectId stringId = Dbg::CreateString(str);
319 if (stringId == 0) {
320 return ERR_OUT_OF_MEMORY;
321 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700322 expandBufAddObjectId(pReply, stringId);
323 return ERR_NONE;
324}
325
326/*
327 * Tell the debugger what we are capable of.
328 */
329static JdwpError handleVM_Capabilities(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
330 expandBufAdd1(pReply, false); /* canWatchFieldModification */
331 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
332 expandBufAdd1(pReply, false); /* canGetBytecodes */
333 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
334 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
335 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
336 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
337 return ERR_NONE;
338}
339
340/*
341 * Return classpath and bootclasspath.
342 */
343static JdwpError handleVM_ClassPaths(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
344 char baseDir[2] = "/";
345
346 /*
347 * TODO: make this real. Not important for remote debugging, but
348 * might be useful for local debugging.
349 */
350 uint32_t classPaths = 1;
351 uint32_t bootClassPaths = 0;
352
Elliott Hughesa2155262011-11-16 16:26:58 -0800353 expandBufAddUtf8String(pReply, baseDir);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354 expandBufAdd4BE(pReply, classPaths);
355 for (uint32_t i = 0; i < classPaths; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800356 expandBufAddUtf8String(pReply, ".");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700357 }
358
359 expandBufAdd4BE(pReply, bootClassPaths);
360 for (uint32_t i = 0; i < classPaths; i++) {
361 /* add bootclasspath components as strings */
362 }
363
364 return ERR_NONE;
365}
366
367/*
368 * Release a list of object IDs. (Seen in jdb.)
369 *
370 * Currently does nothing.
371 */
372static JdwpError HandleVM_DisposeObjects(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
373 return ERR_NONE;
374}
375
376/*
377 * Tell the debugger what we are capable of.
378 */
379static JdwpError handleVM_CapabilitiesNew(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
380 expandBufAdd1(pReply, false); /* canWatchFieldModification */
381 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
382 expandBufAdd1(pReply, false); /* canGetBytecodes */
383 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
384 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
385 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
386 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
387 expandBufAdd1(pReply, false); /* canRedefineClasses */
388 expandBufAdd1(pReply, false); /* canAddMethod */
389 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
390 expandBufAdd1(pReply, false); /* canPopFrames */
391 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
392 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
393 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
394 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
395 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
396 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
397 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
398 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
399 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
400 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
401
402 /* fill in reserved22 through reserved32; note count started at 1 */
403 for (int i = 22; i <= 32; i++) {
404 expandBufAdd1(pReply, false); /* reservedN */
405 }
406 return ERR_NONE;
407}
408
Elliott Hughes86964332012-02-15 19:37:42 -0800409static 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 -0800410 std::vector<JDWP::RefTypeId> classes;
411 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800413 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800415 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800416 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800417 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800418 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800419 uint32_t class_status;
420 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
421 if (status != ERR_NONE) {
422 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800423 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424
Elliott Hughes436e3722012-02-17 20:01:47 -0800425 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800426 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800427 if (descriptor_and_status) {
428 expandBufAddUtf8String(pReply, descriptor);
429 if (generic) {
430 expandBufAddUtf8String(pReply, genericSignature);
431 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800432 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800433 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434 }
435
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436 return ERR_NONE;
437}
438
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800439static JdwpError handleVM_AllClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes86964332012-02-15 19:37:42 -0800440 return handleVM_AllClasses(state, buf, dataLen, pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800441}
442
443static JdwpError handleVM_AllClassesWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes86964332012-02-15 19:37:42 -0800444 return handleVM_AllClasses(state, buf, dataLen, pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800445}
446
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447/*
448 * Given a referenceTypeID, return a string with the JNI reference type
449 * signature (e.g. "Ljava/lang/Error;").
450 */
451static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
452 RefTypeId refTypeId = ReadRefTypeId(&buf);
453
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800454 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800455 std::string signature;
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800456
457 JdwpError status = Dbg::GetSignature(refTypeId, signature);
458 if (status != ERR_NONE) {
459 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800460 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800461 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462 return ERR_NONE;
463}
464
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700465static JdwpError handleRT_Modifiers(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
466 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800467 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700468}
469
470/*
471 * Get values from static fields in a reference type.
472 */
473static JdwpError handleRT_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800474 ReadRefTypeId(&buf); // We don't need this, but we need to skip over it in the request.
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700475 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700476
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800477 VLOG(jdwp) << " RT_GetValues " << numFields << ":";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478
479 expandBufAdd4BE(pReply, numFields);
480 for (uint32_t i = 0; i < numFields; i++) {
481 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800482 JdwpError status = Dbg::GetStaticFieldValue(fieldId, pReply);
483 if (status != ERR_NONE) {
484 return status;
485 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486 }
487
488 return ERR_NONE;
489}
490
491/*
492 * Get the name of the source file in which a reference type was declared.
493 */
494static JdwpError handleRT_SourceFile(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
495 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800496 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800497 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
498 if (status != ERR_NONE) {
499 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800501 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800502 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700503}
504
505/*
506 * Return the current status of the reference type.
507 */
508static JdwpError handleRT_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
509 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800510 JDWP::JdwpTypeTag type_tag;
511 uint32_t class_status;
512 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
513 if (status != ERR_NONE) {
514 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800515 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800516 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700517 return ERR_NONE;
518}
519
520/*
521 * Return interfaces implemented directly by this class.
522 */
523static JdwpError handleRT_Interfaces(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
524 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800525 VLOG(jdwp) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800526 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527}
528
529/*
530 * Return the class object corresponding to this type.
531 */
532static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
533 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800534 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800535 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
536 if (status != ERR_NONE) {
537 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800538 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800539 VLOG(jdwp) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800540 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700541 return ERR_NONE;
542}
543
544/*
545 * Returns the value of the SourceDebugExtension attribute.
546 *
547 * JDB seems interested, but DEX files don't currently support this.
548 */
549static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
550 /* referenceTypeId in, string out */
551 return ERR_ABSENT_INFORMATION;
552}
553
554/*
555 * Like RT_Signature but with the possibility of a "generic signature".
556 */
557static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800558 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700559
560 RefTypeId refTypeId = ReadRefTypeId(&buf);
561
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800562 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800563 std::string signature;
564 if (Dbg::GetSignature(refTypeId, signature)) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800565 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700566 } else {
567 LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId);
Elliott Hughesa2155262011-11-16 16:26:58 -0800568 expandBufAddUtf8String(pReply, "Lunknown;");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700569 }
570 expandBufAddUtf8String(pReply, genericSignature);
571
572 return ERR_NONE;
573}
574
575/*
576 * Return the instance of java.lang.ClassLoader that loaded the specified
577 * reference type, or null if it was loaded by the system loader.
578 */
579static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
580 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800581 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582}
583
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800584static std::string Describe(const RefTypeId& refTypeId) {
585 std::string signature("unknown");
586 Dbg::GetSignature(refTypeId, signature);
587 return StringPrintf("refTypeId=0x%llx (%s)", refTypeId, signature.c_str());
588}
589
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700590/*
591 * Given a referenceTypeId, return a block of stuff that describes the
592 * fields declared by a class.
593 */
594static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
595 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800596 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800597 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800598}
599
600// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
601static JdwpError handleRT_Fields(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
602 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800603 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800604 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605}
606
607/*
608 * Given a referenceTypeID, return a block of goodies describing the
609 * methods declared by a class.
610 */
611static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
612 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800613 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800614 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800615}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800617// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
618static JdwpError handleRT_Methods(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
619 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800620 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800621 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622}
623
624/*
625 * Return the immediate superclass of a class.
626 */
627static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
628 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800629 RefTypeId superClassId;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800630 JdwpError status = Dbg::GetSuperclass(classId, superClassId);
631 if (status != ERR_NONE) {
632 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800633 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700634 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700635 return ERR_NONE;
636}
637
638/*
639 * Set static class values.
640 */
641static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
642 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700643 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700644
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800645 VLOG(jdwp) << StringPrintf(" Req to set %d values in classId=%llx", values, classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646
647 for (uint32_t i = 0; i < values; i++) {
648 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800649 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800650 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651 uint64_t value = jdwpReadValue(&buf, width);
652
Elliott Hughes2435a572012-02-17 16:07:41 -0800653 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800654 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
655 if (status != ERR_NONE) {
656 return status;
657 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658 }
659
660 return ERR_NONE;
661}
662
663/*
664 * Invoke a static method.
665 *
666 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
667 * values in the "variables" display.
668 */
669static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
670 RefTypeId classId = ReadRefTypeId(&buf);
671 ObjectId threadId = ReadObjectId(&buf);
672 MethodId methodId = ReadMethodId(&buf);
673
674 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
675}
676
677/*
678 * Create a new object of the requested type, and invoke the specified
679 * constructor.
680 *
681 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
682 * see the contents of a byte[] as a string.
683 */
684static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
685 RefTypeId classId = ReadRefTypeId(&buf);
686 ObjectId threadId = ReadObjectId(&buf);
687 MethodId methodId = ReadMethodId(&buf);
688
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800689 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(classId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800690 ObjectId objectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800691 JdwpError status = Dbg::CreateObject(classId, objectId);
692 if (status != ERR_NONE) {
693 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800694 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695 if (objectId == 0) {
696 return ERR_OUT_OF_MEMORY;
697 }
698 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
699}
700
701/*
702 * Create a new array object of the requested type and length.
703 */
704static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
705 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700706 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707
Elliott Hughes2435a572012-02-17 16:07:41 -0800708 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800709 ObjectId objectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800710 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, objectId);
711 if (status != ERR_NONE) {
712 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800713 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700714 if (objectId == 0) {
715 return ERR_OUT_OF_MEMORY;
716 }
717 expandBufAdd1(pReply, JT_ARRAY);
718 expandBufAddObjectId(pReply, objectId);
719 return ERR_NONE;
720}
721
722/*
723 * Return line number information for the method, if present.
724 */
725static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
726 RefTypeId refTypeId = ReadRefTypeId(&buf);
727 MethodId methodId = ReadMethodId(&buf);
728
Elliott Hughes2435a572012-02-17 16:07:41 -0800729 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId,methodId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730
731 Dbg::OutputLineTable(refTypeId, methodId, pReply);
732
733 return ERR_NONE;
734}
735
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800736static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply, bool generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700737 RefTypeId classId = ReadRefTypeId(&buf);
738 MethodId methodId = ReadMethodId(&buf);
739
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800740 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 -0700741
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800742 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
743 // information. That will cause Eclipse to make a best-effort attempt at displaying local
744 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
745 // not showing anything.
746 Dbg::OutputVariableTable(classId, methodId, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700747 return ERR_NONE;
748}
749
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800750static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
751 return handleM_VariableTable(state, buf, dataLen, pReply, false);
752}
753
754static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
755 return handleM_VariableTable(state, buf, dataLen, pReply, true);
756}
757
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700758/*
759 * Given an object reference, return the runtime type of the object
760 * (class or array).
761 *
762 * This can get called on different things, e.g. threadId gets
763 * passed in here.
764 */
765static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
766 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800767 VLOG(jdwp) << StringPrintf(" Req for type of objectId=0x%llx", objectId);
Elliott Hughes2435a572012-02-17 16:07:41 -0800768 return Dbg::GetReferenceType(objectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700769}
770
771/*
772 * Get values from the fields of an object.
773 */
774static JdwpError handleOR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
775 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700776 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800778 VLOG(jdwp) << StringPrintf(" Req for %d fields from objectId=0x%llx", numFields, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700779
780 expandBufAdd4BE(pReply, numFields);
781
782 for (uint32_t i = 0; i < numFields; i++) {
783 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800784 JdwpError status = Dbg::GetFieldValue(objectId, fieldId, pReply);
785 if (status != ERR_NONE) {
786 return status;
787 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700788 }
789
790 return ERR_NONE;
791}
792
793/*
794 * Set values in the fields of an object.
795 */
796static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
797 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700798 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700799
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800800 VLOG(jdwp) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801
802 for (uint32_t i = 0; i < numFields; i++) {
803 FieldId fieldId = ReadFieldId(&buf);
804
Elliott Hughesaed4be92011-12-02 16:16:23 -0800805 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800806 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700807 uint64_t value = jdwpReadValue(&buf, width);
808
Elliott Hughes2435a572012-02-17 16:07:41 -0800809 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800810 JdwpError status = Dbg::SetFieldValue(objectId, fieldId, value, width);
811 if (status != ERR_NONE) {
812 return status;
813 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700814 }
815
816 return ERR_NONE;
817}
818
819/*
820 * Invoke an instance method. The invocation must occur in the specified
821 * thread, which must have been suspended by an event.
822 *
823 * The call is synchronous. All threads in the VM are resumed, unless the
824 * SINGLE_THREADED flag is set.
825 *
826 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
827 * object), it will try to invoke the object's toString() function. This
828 * feature becomes crucial when examining ArrayLists with Eclipse.
829 */
830static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
831 ObjectId objectId = ReadObjectId(&buf);
832 ObjectId threadId = ReadObjectId(&buf);
833 RefTypeId classId = ReadRefTypeId(&buf);
834 MethodId methodId = ReadMethodId(&buf);
835
836 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
837}
838
839/*
840 * Disable garbage collection of the specified object.
841 */
842static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
843 // this is currently a no-op
844 return ERR_NONE;
845}
846
847/*
848 * Enable garbage collection of the specified object.
849 */
850static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
851 // this is currently a no-op
852 return ERR_NONE;
853}
854
855/*
856 * Determine whether an object has been garbage collected.
857 */
858static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
859 ObjectId objectId;
860
861 objectId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800862 VLOG(jdwp) << StringPrintf(" Req IsCollected(0x%llx)", objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700863
864 // TODO: currently returning false; must integrate with GC
865 expandBufAdd1(pReply, 0);
866
867 return ERR_NONE;
868}
869
870/*
871 * Return the string value in a string object.
872 */
873static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
874 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800875 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700876
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800877 VLOG(jdwp) << StringPrintf(" Req for str %llx --> '%s'", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700878
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800879 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700880
881 return ERR_NONE;
882}
883
884/*
885 * Return a thread's name.
886 */
887static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
888 ObjectId threadId = ReadObjectId(&buf);
889
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800890 VLOG(jdwp) << StringPrintf(" Req for name of thread 0x%llx", threadId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800891 std::string name;
892 if (!Dbg::GetThreadName(threadId, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700893 return ERR_INVALID_THREAD;
894 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800895 VLOG(jdwp) << StringPrintf(" Name of thread 0x%llx is \"%s\"", threadId, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800896 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700897
898 return ERR_NONE;
899}
900
901/*
902 * Suspend the specified thread.
903 *
904 * It's supposed to remain suspended even if interpreted code wants to
905 * resume it; only the JDI is allowed to resume it.
906 */
907static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
908 ObjectId threadId = ReadObjectId(&buf);
909
910 if (threadId == Dbg::GetThreadSelfId()) {
911 LOG(INFO) << " Warning: ignoring request to suspend self";
912 return ERR_THREAD_NOT_SUSPENDED;
913 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800914 VLOG(jdwp) << StringPrintf(" Req to suspend thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700915 Dbg::SuspendThread(threadId);
916 return ERR_NONE;
917}
918
919/*
920 * Resume the specified thread.
921 */
922static JdwpError handleTR_Resume(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 resume self";
927 return ERR_NONE;
928 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800929 VLOG(jdwp) << StringPrintf(" Req to resume thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700930 Dbg::ResumeThread(threadId);
931 return ERR_NONE;
932}
933
934/*
935 * Return status of specified thread.
936 */
937static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
938 ObjectId threadId = ReadObjectId(&buf);
939
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800940 VLOG(jdwp) << StringPrintf(" Req for status of thread 0x%llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700941
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800942 JDWP::JdwpThreadStatus threadStatus;
943 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700944 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
945 return ERR_INVALID_THREAD;
946 }
947
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800948 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949
950 expandBufAdd4BE(pReply, threadStatus);
951 expandBufAdd4BE(pReply, suspendStatus);
952
953 return ERR_NONE;
954}
955
956/*
957 * Return the thread group that the specified thread is a member of.
958 */
959static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
960 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes2435a572012-02-17 16:07:41 -0800961 return Dbg::GetThreadGroup(threadId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700962}
963
964/*
965 * Return the current call stack of a suspended thread.
966 *
967 * If the thread isn't suspended, the error code isn't defined, but should
968 * be THREAD_NOT_SUSPENDED.
969 */
970static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
971 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800972 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700973 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974
975 if (!Dbg::ThreadExists(threadId)) {
976 return ERR_INVALID_THREAD;
977 }
978 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800979 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980 return ERR_THREAD_NOT_SUSPENDED;
981 }
982
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800983 size_t actual_frame_count = Dbg::GetThreadFrameCount(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700984
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800985 VLOG(jdwp) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%zd]", threadId, start_frame, length, actual_frame_count);
986 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700987 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
988 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700989
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800990 if (start_frame > actual_frame_count) {
991 return ERR_INVALID_INDEX;
992 }
993 if (length == static_cast<uint32_t>(-1)) {
994 length = actual_frame_count - start_frame;
995 }
996 if (start_frame + length > actual_frame_count) {
997 return ERR_INVALID_LENGTH;
998 }
999
1000 expandBufAdd4BE(pReply, length);
1001 for (uint32_t i = start_frame; i < (start_frame + length); ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001002 FrameId frameId;
1003 JdwpLocation loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
1005
1006 expandBufAdd8BE(pReply, frameId);
1007 AddLocation(pReply, &loc);
1008
Elliott Hughes2435a572012-02-17 16:07:41 -08001009 VLOG(jdwp) << StringPrintf(" Frame %d: id=%llx ", i, frameId) << loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001010 }
1011
1012 return ERR_NONE;
1013}
1014
1015/*
1016 * Returns the #of frames on the specified thread, which must be suspended.
1017 */
1018static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1019 ObjectId threadId = ReadObjectId(&buf);
1020
1021 if (!Dbg::ThreadExists(threadId)) {
1022 return ERR_INVALID_THREAD;
1023 }
1024 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001025 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001026 return ERR_THREAD_NOT_SUSPENDED;
1027 }
1028
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001029 int frame_count = Dbg::GetThreadFrameCount(threadId);
1030 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031 return ERR_INVALID_THREAD;
1032 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001033 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034
1035 return ERR_NONE;
1036}
1037
1038/*
1039 * Get the monitor that the thread is waiting on.
1040 */
1041static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1042 ObjectId threadId;
1043
1044 threadId = ReadObjectId(&buf);
1045
1046 // TODO: create an Object to represent the monitor (we're currently
1047 // just using a raw Monitor struct in the VM)
1048
1049 return ERR_NOT_IMPLEMENTED;
1050}
1051
1052/*
1053 * Return the suspend count for the specified thread.
1054 *
1055 * (The thread *might* still be running -- it might not have examined
1056 * its suspend count recently.)
1057 */
1058static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1059 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes2435a572012-02-17 16:07:41 -08001060 return Dbg::GetThreadSuspendCount(threadId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061}
1062
1063/*
1064 * Return the name of a thread group.
1065 *
1066 * The Eclipse debugger recognizes "main" and "system" as special.
1067 */
1068static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1069 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001070 VLOG(jdwp) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001071
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001072 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001073
1074 return ERR_NONE;
1075}
1076
1077/*
1078 * Returns the thread group -- if any -- that contains the specified
1079 * thread group.
1080 */
1081static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1082 ObjectId groupId = ReadObjectId(&buf);
1083
1084 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1085 expandBufAddObjectId(pReply, parentGroup);
1086
1087 return ERR_NONE;
1088}
1089
1090/*
1091 * Return the active threads and thread groups that are part of the
1092 * specified thread group.
1093 */
1094static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1095 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001096 VLOG(jdwp) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001097
1098 ObjectId* pThreadIds;
1099 uint32_t threadCount;
1100 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1101
1102 expandBufAdd4BE(pReply, threadCount);
1103
1104 for (uint32_t i = 0; i < threadCount; i++) {
1105 expandBufAddObjectId(pReply, pThreadIds[i]);
1106 }
1107 free(pThreadIds);
1108
1109 /*
1110 * TODO: finish support for child groups
1111 *
1112 * For now, just show that "main" is a child of "system".
1113 */
1114 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1115 expandBufAdd4BE(pReply, 1);
1116 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1117 } else {
1118 expandBufAdd4BE(pReply, 0);
1119 }
1120
1121 return ERR_NONE;
1122}
1123
1124/*
1125 * Return the #of components in the array.
1126 */
1127static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1128 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001129 VLOG(jdwp) << StringPrintf(" Req for length of array 0x%llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001130
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001131 int length;
1132 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1133 if (status != ERR_NONE) {
1134 return status;
1135 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001136 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001137
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001138 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139
1140 return ERR_NONE;
1141}
1142
1143/*
1144 * Return the values from an array.
1145 */
1146static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1147 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001148 uint32_t firstIndex = Read4BE(&buf);
1149 uint32_t length = Read4BE(&buf);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001150 VLOG(jdwp) << StringPrintf(" Req for array values 0x%llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001151
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001152 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001153}
1154
1155/*
1156 * Set values in an array.
1157 */
1158static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1159 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001160 uint32_t firstIndex = Read4BE(&buf);
1161 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001162
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001163 VLOG(jdwp) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001164
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001165 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001166}
1167
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1169 ObjectId classLoaderObject;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001170 classLoaderObject = ReadObjectId(&buf);
Elliott Hughes86964332012-02-15 19:37:42 -08001171 // TODO: we should only return classes which have the given class loader as a defining or
1172 // initiating loader. The former would be easy; the latter is hard, because we don't have
1173 // any such notion.
1174 return handleVM_AllClasses(state, buf, dataLen, pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001175}
1176
1177/*
1178 * Set an event trigger.
1179 *
1180 * Reply with a requestID.
1181 */
1182static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1183 const uint8_t* origBuf = buf;
1184
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001185 uint8_t eventKind = Read1(&buf);
1186 uint8_t suspendPolicy = Read1(&buf);
1187 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001189 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001190 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1191 << " mods=" << modifierCount << ")";
1192
1193 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1194
1195 JdwpEvent* pEvent = EventAlloc(modifierCount);
1196 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1197 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1198 pEvent->modCount = modifierCount;
1199
1200 /*
1201 * Read modifiers. Ordering may be significant (see explanation of Count
1202 * mods in JDWP doc).
1203 */
1204 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001205 JdwpModKind modKind = static_cast<JdwpModKind>(Read1(&buf));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001206
1207 pEvent->mods[idx].modKind = modKind;
1208
1209 switch (modKind) {
1210 case MK_COUNT: /* report once, when "--count" reaches 0 */
1211 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001212 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001213 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001214 if (count == 0) {
1215 return ERR_INVALID_COUNT;
1216 }
1217 pEvent->mods[idx].count.count = count;
1218 }
1219 break;
1220 case MK_CONDITIONAL: /* conditional on expression) */
1221 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001222 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001223 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001224 pEvent->mods[idx].conditional.exprId = exprId;
1225 }
1226 break;
1227 case MK_THREAD_ONLY: /* only report events in specified thread */
1228 {
1229 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001230 VLOG(jdwp) << StringPrintf(" ThreadOnly: %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001231 pEvent->mods[idx].threadOnly.threadId = threadId;
1232 }
1233 break;
1234 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1235 {
1236 RefTypeId clazzId = ReadRefTypeId(&buf);
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001237 VLOG(jdwp) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassName(clazzId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001238 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1239 }
1240 break;
1241 case MK_CLASS_MATCH: /* restrict events to matching classes */
1242 {
Elliott Hughes86964332012-02-15 19:37:42 -08001243 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001244 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001245 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001246 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes86964332012-02-15 19:37:42 -08001247 pEvent->mods[idx].classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001248 }
1249 break;
1250 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1251 {
Elliott Hughes86964332012-02-15 19:37:42 -08001252 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001253 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001254 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001255 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes86964332012-02-15 19:37:42 -08001256 pEvent->mods[idx].classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001257 }
1258 break;
1259 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1260 {
1261 JdwpLocation loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001262 jdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001263 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001264 pEvent->mods[idx].locationOnly.loc = loc;
1265 }
1266 break;
1267 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1268 {
1269 RefTypeId exceptionOrNull; /* null == all exceptions */
1270 uint8_t caught, uncaught;
1271
1272 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001273 caught = Read1(&buf);
1274 uncaught = Read1(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001275 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001276 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001277
1278 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1279 pEvent->mods[idx].exceptionOnly.caught = caught;
1280 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1281 }
1282 break;
1283 case MK_FIELD_ONLY: /* for field access/mod events */
1284 {
1285 RefTypeId declaring = ReadRefTypeId(&buf);
1286 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001287 VLOG(jdwp) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001288 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1289 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1290 }
1291 break;
1292 case MK_STEP: /* for use with EK_SINGLE_STEP */
1293 {
1294 ObjectId threadId;
1295 uint32_t size, depth;
1296
1297 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001298 size = Read4BE(&buf);
1299 depth = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001300 VLOG(jdwp) << StringPrintf(" Step: thread=%llx", threadId)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001301 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1302
1303 pEvent->mods[idx].step.threadId = threadId;
1304 pEvent->mods[idx].step.size = size;
1305 pEvent->mods[idx].step.depth = depth;
1306 }
1307 break;
1308 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1309 {
1310 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001311 VLOG(jdwp) << StringPrintf(" InstanceOnly: %llx", instance);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 pEvent->mods[idx].instanceOnly.objectId = instance;
1313 }
1314 break;
1315 default:
1316 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1317 break;
1318 }
1319 }
1320
1321 /*
1322 * Make sure we consumed all data. It is possible that the remote side
1323 * has sent us bad stuff, but for now we blame ourselves.
1324 */
1325 if (buf != origBuf + dataLen) {
1326 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1327 }
1328
1329 /*
1330 * We reply with an integer "requestID".
1331 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001332 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001333 expandBufAdd4BE(pReply, requestId);
1334
1335 pEvent->requestId = requestId;
1336
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001337 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001338
1339 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001340 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001341 if (err != ERR_NONE) {
1342 /* registration failed, probably because event is bogus */
1343 EventFree(pEvent);
1344 LOG(WARNING) << "WARNING: event request rejected";
1345 }
1346 return err;
1347}
1348
1349/*
1350 * Clear an event. Failure to find an event with a matching ID is a no-op
1351 * and does not return an error.
1352 */
1353static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1354 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001355 eventKind = Read1(&buf);
1356 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001357
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001358 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001359
Elliott Hughes761928d2011-11-16 18:33:03 -08001360 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001361
1362 return ERR_NONE;
1363}
1364
1365/*
1366 * Return the values of arguments and local variables.
1367 */
1368static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1369 ObjectId threadId = ReadObjectId(&buf);
1370 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001371 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001372
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001373 VLOG(jdwp) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374
1375 expandBufAdd4BE(pReply, slots); /* "int values" */
1376 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001377 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001378 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001379
Elliott Hughes2435a572012-02-17 16:07:41 -08001380 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001381
Elliott Hughesdbb40792011-11-18 17:05:22 -08001382 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001383 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1384 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1385 }
1386
1387 return ERR_NONE;
1388}
1389
1390/*
1391 * Set the values of arguments and local variables.
1392 */
1393static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1394 ObjectId threadId = ReadObjectId(&buf);
1395 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001396 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001398 VLOG(jdwp) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001399
1400 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001401 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001402 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001403 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001404 uint64_t value = jdwpReadValue(&buf, width);
1405
Elliott Hughes2435a572012-02-17 16:07:41 -08001406 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001407 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1408 }
1409
1410 return ERR_NONE;
1411}
1412
1413/*
1414 * Returns the value of "this" for the specified frame.
1415 */
1416static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08001417 ReadObjectId(&buf); // Skip thread id.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418 FrameId frameId = ReadFrameId(&buf);
1419
1420 ObjectId objectId;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001421 Dbg::GetThisObject(frameId, &objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422
1423 uint8_t objectTag = Dbg::GetObjectTag(objectId);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001424 VLOG(jdwp) << StringPrintf(" Req for 'this' in frame=%llx --> %llx '%c'", frameId, objectId, (char)objectTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001425
1426 expandBufAdd1(pReply, objectTag);
1427 expandBufAddObjectId(pReply, objectId);
1428
1429 return ERR_NONE;
1430}
1431
1432/*
1433 * Return the reference type reflected by this class object.
1434 *
1435 * This appears to be required because ReferenceTypeId values are NEVER
1436 * reused, whereas ClassIds can be recycled like any other object. (Either
1437 * that, or I have no idea what this is for.)
1438 */
1439static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1440 RefTypeId classObjectId = ReadRefTypeId(&buf);
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001441 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001442 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001443}
1444
1445/*
1446 * Handle a DDM packet with a single chunk in it.
1447 */
1448static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1449 uint8_t* replyBuf = NULL;
1450 int replyLen = -1;
1451
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001452 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001453
1454 /*
1455 * On first DDM packet, notify all handlers that DDM is running.
1456 */
1457 if (!state->ddmActive) {
1458 state->ddmActive = true;
1459 Dbg::DdmConnected();
1460 }
1461
1462 /*
1463 * If they want to send something back, we copy it into the buffer.
1464 * A no-copy approach would be nicer.
1465 *
1466 * TODO: consider altering the JDWP stuff to hold the packet header
1467 * in a separate buffer. That would allow us to writev() DDM traffic
1468 * instead of copying it into the expanding buffer. The reduction in
1469 * heap requirements is probably more valuable than the efficiency.
1470 */
1471 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1472 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1473 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1474 free(replyBuf);
1475 }
1476 return ERR_NONE;
1477}
1478
1479/*
1480 * Handler map decl.
1481 */
1482typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1483
1484struct JdwpHandlerMap {
1485 uint8_t cmdSet;
1486 uint8_t cmd;
1487 JdwpRequestHandler func;
1488 const char* descr;
1489};
1490
1491/*
1492 * Map commands to functions.
1493 *
1494 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1495 * and 128-256 are vendor-defined.
1496 */
1497static const JdwpHandlerMap gHandlerMap[] = {
1498 /* VirtualMachine command set (1) */
1499 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1500 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
Elliott Hughes1fe7afb2012-02-13 17:23:03 -08001501 { 1, 3, handleVM_AllClasses, "VirtualMachine.AllClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001502 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1503 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1504 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1505 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1506 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1507 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1508 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1509 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1510 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1511 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1512 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001513 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1514 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001515 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001516 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1517 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
1518 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
1519 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001520
1521 /* ReferenceType command set (2) */
1522 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1523 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1524 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001525 { 2, 4, handleRT_Fields, "ReferenceType.Fields" },
1526 { 2, 5, handleRT_Methods, "ReferenceType.Methods" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001527 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1528 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001529 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001530 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1531 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1532 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1533 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1534 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1535 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1536 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001537 { 2, 16, NULL, "ReferenceType.Instances" },
1538 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1539 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001540
1541 /* ClassType command set (3) */
1542 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1543 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1544 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1545 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1546
1547 /* ArrayType command set (4) */
1548 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1549
1550 /* InterfaceType command set (5) */
1551
1552 /* Method command set (6) */
1553 { 6, 1, handleM_LineTable, "Method.LineTable" },
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001554 { 6, 2, handleM_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001555 { 6, 3, NULL, "Method.Bytecodes" },
1556 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001557 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1558
1559 /* Field command set (8) */
1560
1561 /* ObjectReference command set (9) */
1562 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1563 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1564 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001565 { 9, 4, NULL, "ObjectReference.UNUSED" },
1566 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001567 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1568 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1569 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1570 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001571 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001572
1573 /* StringReference command set (10) */
1574 { 10, 1, handleSR_Value, "StringReference.Value" },
1575
1576 /* ThreadReference command set (11) */
1577 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1578 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1579 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1580 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1581 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1582 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1583 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001584 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001585 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001586 { 11, 10, NULL, "ThreadReference.Stop" },
1587 { 11, 11, NULL,"ThreadReference.Interrupt" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001588 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001589 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1590 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591
1592 /* ThreadGroupReference command set (12) */
1593 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1594 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1595 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1596
1597 /* ArrayReference command set (13) */
1598 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1599 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1600 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1601
1602 /* ClassLoaderReference command set (14) */
1603 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1604
1605 /* EventRequest command set (15) */
1606 { 15, 1, handleER_Set, "EventRequest.Set" },
1607 { 15, 2, handleER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001608 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001609
1610 /* StackFrame command set (16) */
1611 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1612 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1613 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001614 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001615
1616 /* ClassObjectReference command set (17) */
1617 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1618
1619 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001620 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001621
1622 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1623};
1624
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001625static const char* GetCommandName(size_t cmdSet, size_t cmd) {
1626 for (int i = 0; i < (int) arraysize(gHandlerMap); i++) {
1627 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1628 return gHandlerMap[i].descr;
1629 }
1630 }
1631 return "?UNKNOWN?";
1632}
1633
1634static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1635 std::string result;
1636 result += "REQ: ";
1637 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1638 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1639 return result;
1640}
1641
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001642/*
1643 * Process a request from the debugger.
1644 *
1645 * On entry, the JDWP thread is in VMWAIT.
1646 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001647void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001648 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001649
1650 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1651 /*
1652 * Activity from a debugger, not merely ddms. Mark us as having an
1653 * active debugger session, and zero out the last-activity timestamp
1654 * so waitForDebugger() doesn't return if we stall for a bit here.
1655 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001656 Dbg::GoActive();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001657 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001658 }
1659
1660 /*
1661 * If a debugger event has fired in another thread, wait until the
1662 * initiating thread has suspended itself before processing messages
1663 * from the debugger. Otherwise we (the JDWP thread) could be told to
1664 * resume the thread before it has suspended.
1665 *
1666 * We call with an argument of zero to wait for the current event
1667 * thread to finish, and then clear the block. Depending on the thread
1668 * suspend policy, this may allow events in other threads to fire,
1669 * but those events have no bearing on what the debugger has sent us
1670 * in the current request.
1671 *
1672 * Note that we MUST clear the event token before waking the event
1673 * thread up, or risk waiting for the thread to suspend after we've
1674 * told it to resume.
1675 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001676 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001677
1678 /*
1679 * Tell the VM that we're running and shouldn't be interrupted by GC.
1680 * Do this after anything that can stall indefinitely.
1681 */
1682 Dbg::ThreadRunning();
1683
1684 expandBufAddSpace(pReply, kJDWPHeaderLen);
1685
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001686 size_t i;
1687 for (i = 0; i < arraysize(gHandlerMap); i++) {
1688 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001689 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001690 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001691 break;
1692 }
1693 }
1694 if (i == arraysize(gHandlerMap)) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001695 LOG(ERROR) << DescribeCommand(pHeader, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001696 if (dataLen > 0) {
1697 HexDump(buf, dataLen);
1698 }
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001699 LOG(ERROR) << "command not implemented";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001700 result = ERR_NOT_IMPLEMENTED;
1701 }
1702
1703 /*
1704 * Set up the reply header.
1705 *
1706 * If we encountered an error, only send the header back.
1707 */
1708 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001709 Set4BE(replyBuf + 4, pHeader->id);
1710 Set1(replyBuf + 8, kJDWPFlagReply);
1711 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001712 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001713 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001714 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001715 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001716 }
1717
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001718 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001719 if (false) {
1720 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1721 if (respLen > 0) {
1722 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1723 }
1724 }
1725
1726 /*
1727 * Update last-activity timestamp. We really only need this during
1728 * the initial setup. Only update if this is a non-DDMS packet.
1729 */
1730 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001731 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001732 }
1733
1734 /* tell the VM that GC is okay again */
1735 Dbg::ThreadWaiting();
1736}
1737
1738} // namespace JDWP
1739
1740} // namespace art