blob: 73c70cb735e09e6f8f5ad2edcb7b196fa7cebc8c [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 Hughes972a47b2012-02-21 18:16:06 -080055 pLoc->dex_pc = 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);
Elliott Hughes972a47b2012-02-21 18:16:06 -080065 expandBufAdd8BE(pReply, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070066}
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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700102static JdwpError finishInvoke(JdwpState*,
103 const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700104 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 Hughes229feb72012-02-23 13:33:29 -0800111 VLOG(jdwp) << StringPrintf(" --> threadId=%#llx objectId=%#llx", threadId, objectId);
112 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);
Elliott Hughes229feb72012-02-23 13:33:29 -0800121 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#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 Hughes229feb72012-02-23 13:33:29 -0800151 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", 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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700170static JdwpError handleVM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700171 /* 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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700190static JdwpError handleVM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, 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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700222static JdwpError handleVM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700223 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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700242static JdwpError handleVM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243 /*
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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700264static JdwpError handleVM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 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 Hughes1bac54f2012-03-16 12:48:31 -0700273static JdwpError handleVM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*) {
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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700284static JdwpError handleVM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*) {
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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700292static JdwpError handleVM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293 Dbg::ResumeVM();
294 return ERR_NONE;
295}
296
297/*
298 * The debugger wants the entire VM to exit.
299 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700300static JdwpError handleVM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700315static JdwpError handleVM_CreateString(JdwpState*, const uint8_t* buf, int, 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 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700329static JdwpError handleVM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330 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
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700340static JdwpError handleVM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800341 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700342
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800343 std::vector<std::string> class_path;
344 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
345 expandBufAdd4BE(pReply, class_path.size());
346 for (size_t i = 0; i < class_path.size(); ++i) {
347 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700348 }
349
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800350 std::vector<std::string> boot_class_path;
351 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
352 expandBufAdd4BE(pReply, boot_class_path.size());
353 for (size_t i = 0; i < boot_class_path.size(); ++i) {
354 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355 }
356
357 return ERR_NONE;
358}
359
360/*
361 * Release a list of object IDs. (Seen in jdb.)
362 *
363 * Currently does nothing.
364 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700365static JdwpError HandleVM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700366 return ERR_NONE;
367}
368
369/*
370 * Tell the debugger what we are capable of.
371 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700372static JdwpError handleVM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 expandBufAdd1(pReply, false); /* canWatchFieldModification */
374 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
375 expandBufAdd1(pReply, false); /* canGetBytecodes */
376 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
377 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
378 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
379 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
380 expandBufAdd1(pReply, false); /* canRedefineClasses */
381 expandBufAdd1(pReply, false); /* canAddMethod */
382 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
383 expandBufAdd1(pReply, false); /* canPopFrames */
384 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
385 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
386 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
387 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
388 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
389 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
390 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
391 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
392 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
393 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
394
395 /* fill in reserved22 through reserved32; note count started at 1 */
396 for (int i = 22; i <= 32; i++) {
397 expandBufAdd1(pReply, false); /* reservedN */
398 }
399 return ERR_NONE;
400}
401
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700402static JdwpError handleVM_AllClasses(ExpandBuf* pReply, bool descriptor_and_status, bool generic) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800403 std::vector<JDWP::RefTypeId> classes;
404 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800406 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800408 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800409 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800410 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800411 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800412 uint32_t class_status;
413 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
414 if (status != ERR_NONE) {
415 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800416 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417
Elliott Hughes436e3722012-02-17 20:01:47 -0800418 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800419 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800420 if (descriptor_and_status) {
421 expandBufAddUtf8String(pReply, descriptor);
422 if (generic) {
423 expandBufAddUtf8String(pReply, genericSignature);
424 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800425 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800426 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427 }
428
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700429 return ERR_NONE;
430}
431
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700432static JdwpError handleVM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
433 return handleVM_AllClasses(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800434}
435
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700436static JdwpError handleVM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
437 return handleVM_AllClasses(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800438}
439
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700440static JdwpError handleRT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800442 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700443}
444
445/*
446 * Get values from static fields in a reference type.
447 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700448static JdwpError handleRT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800449 RefTypeId refTypeId = ReadRefTypeId(&buf);
450 uint32_t field_count = Read4BE(&buf);
451 expandBufAdd4BE(pReply, field_count);
452 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800454 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800455 if (status != ERR_NONE) {
456 return status;
457 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700458 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700459 return ERR_NONE;
460}
461
462/*
463 * Get the name of the source file in which a reference type was declared.
464 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700465static JdwpError handleRT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700466 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800467 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800468 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
469 if (status != ERR_NONE) {
470 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700471 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800472 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800473 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474}
475
476/*
477 * Return the current status of the reference type.
478 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700479static JdwpError handleRT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700480 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800481 JDWP::JdwpTypeTag type_tag;
482 uint32_t class_status;
483 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
484 if (status != ERR_NONE) {
485 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800486 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800487 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488 return ERR_NONE;
489}
490
491/*
492 * Return interfaces implemented directly by this class.
493 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700494static JdwpError handleRT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800496 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800497 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700498}
499
500/*
501 * Return the class object corresponding to this type.
502 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700503static JdwpError handleRT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800505 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800506 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
507 if (status != ERR_NONE) {
508 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800509 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800510 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800511 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512 return ERR_NONE;
513}
514
515/*
516 * Returns the value of the SourceDebugExtension attribute.
517 *
518 * JDB seems interested, but DEX files don't currently support this.
519 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700520static JdwpError handleRT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 /* referenceTypeId in, string out */
522 return ERR_ABSENT_INFORMATION;
523}
524
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700525static JdwpError handleRT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool with_generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700526 RefTypeId refTypeId = ReadRefTypeId(&buf);
527
Elliott Hughes229feb72012-02-23 13:33:29 -0800528 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800529 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800530
531 JdwpError status = Dbg::GetSignature(refTypeId, signature);
532 if (status != ERR_NONE) {
533 return status;
534 }
535 expandBufAddUtf8String(pReply, signature);
536 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800537 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539 return ERR_NONE;
540}
541
Elliott Hughes98e43f62012-02-24 12:42:35 -0800542static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
543 return handleRT_Signature(state, buf, dataLen, pReply, false);
544}
545
546static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
547 return handleRT_Signature(state, buf, dataLen, pReply, true);
548}
549
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700550/*
551 * Return the instance of java.lang.ClassLoader that loaded the specified
552 * reference type, or null if it was loaded by the system loader.
553 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700554static JdwpError handleRT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700555 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800556 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700557}
558
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800559static std::string Describe(const RefTypeId& refTypeId) {
560 std::string signature("unknown");
561 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800562 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800563}
564
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700565/*
566 * Given a referenceTypeId, return a block of stuff that describes the
567 * fields declared by a class.
568 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700569static JdwpError handleRT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800571 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800572 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800573}
574
575// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700576static JdwpError handleRT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800577 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800578 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800579 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580}
581
582/*
583 * Given a referenceTypeID, return a block of goodies describing the
584 * methods declared by a class.
585 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700586static JdwpError handleRT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800588 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800589 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800590}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700591
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800592// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700593static JdwpError handleRT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800594 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800595 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800596 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700597}
598
599/*
600 * Return the immediate superclass of a class.
601 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700602static JdwpError handleCT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700603 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800604 RefTypeId superClassId;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800605 JdwpError status = Dbg::GetSuperclass(classId, superClassId);
606 if (status != ERR_NONE) {
607 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800608 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700610 return ERR_NONE;
611}
612
613/*
614 * Set static class values.
615 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700616static JdwpError handleCT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700618 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619
Elliott Hughes229feb72012-02-23 13:33:29 -0800620 VLOG(jdwp) << StringPrintf(" Req to set %d values in classId=%#llx", values, classId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621
622 for (uint32_t i = 0; i < values; i++) {
623 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800624 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800625 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700626 uint64_t value = jdwpReadValue(&buf, width);
627
Elliott Hughes2435a572012-02-17 16:07:41 -0800628 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800629 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
630 if (status != ERR_NONE) {
631 return status;
632 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700633 }
634
635 return ERR_NONE;
636}
637
638/*
639 * Invoke a static method.
640 *
641 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
642 * values in the "variables" display.
643 */
644static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
645 RefTypeId classId = ReadRefTypeId(&buf);
646 ObjectId threadId = ReadObjectId(&buf);
647 MethodId methodId = ReadMethodId(&buf);
648
649 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
650}
651
652/*
653 * Create a new object of the requested type, and invoke the specified
654 * constructor.
655 *
656 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
657 * see the contents of a byte[] as a string.
658 */
659static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
660 RefTypeId classId = ReadRefTypeId(&buf);
661 ObjectId threadId = ReadObjectId(&buf);
662 MethodId methodId = ReadMethodId(&buf);
663
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800664 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(classId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800665 ObjectId objectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800666 JdwpError status = Dbg::CreateObject(classId, objectId);
667 if (status != ERR_NONE) {
668 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800669 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700670 if (objectId == 0) {
671 return ERR_OUT_OF_MEMORY;
672 }
673 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
674}
675
676/*
677 * Create a new array object of the requested type and length.
678 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700679static JdwpError handleAT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700680 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700681 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700682
Elliott Hughes2435a572012-02-17 16:07:41 -0800683 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800684 ObjectId objectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800685 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, objectId);
686 if (status != ERR_NONE) {
687 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800688 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700689 if (objectId == 0) {
690 return ERR_OUT_OF_MEMORY;
691 }
692 expandBufAdd1(pReply, JT_ARRAY);
693 expandBufAddObjectId(pReply, objectId);
694 return ERR_NONE;
695}
696
697/*
698 * Return line number information for the method, if present.
699 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700700static JdwpError handleM_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701 RefTypeId refTypeId = ReadRefTypeId(&buf);
702 MethodId methodId = ReadMethodId(&buf);
703
Elliott Hughes2435a572012-02-17 16:07:41 -0800704 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId,methodId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700705
706 Dbg::OutputLineTable(refTypeId, methodId, pReply);
707
708 return ERR_NONE;
709}
710
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700711static JdwpError handleM_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700712 RefTypeId classId = ReadRefTypeId(&buf);
713 MethodId methodId = ReadMethodId(&buf);
714
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800715 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 -0700716
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800717 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
718 // information. That will cause Eclipse to make a best-effort attempt at displaying local
719 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
720 // not showing anything.
721 Dbg::OutputVariableTable(classId, methodId, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700722 return ERR_NONE;
723}
724
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800725static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
726 return handleM_VariableTable(state, buf, dataLen, pReply, false);
727}
728
729static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
730 return handleM_VariableTable(state, buf, dataLen, pReply, true);
731}
732
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700733/*
734 * Given an object reference, return the runtime type of the object
735 * (class or array).
736 *
737 * This can get called on different things, e.g. threadId gets
738 * passed in here.
739 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700740static JdwpError handleOR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700741 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800742 VLOG(jdwp) << StringPrintf(" Req for type of objectId=%#llx", objectId);
Elliott Hughes2435a572012-02-17 16:07:41 -0800743 return Dbg::GetReferenceType(objectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744}
745
746/*
747 * Get values from the fields of an object.
748 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700749static JdwpError handleOR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800751 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700752
Elliott Hughes0cf74332012-02-23 23:14:00 -0800753 VLOG(jdwp) << StringPrintf(" Req for %d fields from objectId=%#llx", field_count, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700754
Elliott Hughes0cf74332012-02-23 23:14:00 -0800755 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700756
Elliott Hughes0cf74332012-02-23 23:14:00 -0800757 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700758 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800759 JdwpError status = Dbg::GetFieldValue(objectId, fieldId, pReply);
760 if (status != ERR_NONE) {
761 return status;
762 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700763 }
764
765 return ERR_NONE;
766}
767
768/*
769 * Set values in the fields of an object.
770 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700771static JdwpError handleOR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800773 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700774
Elliott Hughes0cf74332012-02-23 23:14:00 -0800775 VLOG(jdwp) << StringPrintf(" Req to set %d fields in objectId=%#llx", field_count, objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776
Elliott Hughes0cf74332012-02-23 23:14:00 -0800777 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700778 FieldId fieldId = ReadFieldId(&buf);
779
Elliott Hughesaed4be92011-12-02 16:16:23 -0800780 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800781 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700782 uint64_t value = jdwpReadValue(&buf, width);
783
Elliott Hughes2435a572012-02-17 16:07:41 -0800784 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800785 JdwpError status = Dbg::SetFieldValue(objectId, fieldId, value, width);
786 if (status != ERR_NONE) {
787 return status;
788 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700789 }
790
791 return ERR_NONE;
792}
793
794/*
795 * Invoke an instance method. The invocation must occur in the specified
796 * thread, which must have been suspended by an event.
797 *
798 * The call is synchronous. All threads in the VM are resumed, unless the
799 * SINGLE_THREADED flag is set.
800 *
801 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
802 * object), it will try to invoke the object's toString() function. This
803 * feature becomes crucial when examining ArrayLists with Eclipse.
804 */
805static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
806 ObjectId objectId = ReadObjectId(&buf);
807 ObjectId threadId = ReadObjectId(&buf);
808 RefTypeId classId = ReadRefTypeId(&buf);
809 MethodId methodId = ReadMethodId(&buf);
810
811 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
812}
813
814/*
815 * Disable garbage collection of the specified object.
816 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700817static JdwpError handleOR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818 // this is currently a no-op
819 return ERR_NONE;
820}
821
822/*
823 * Enable garbage collection of the specified object.
824 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700825static JdwpError handleOR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700826 // this is currently a no-op
827 return ERR_NONE;
828}
829
830/*
831 * Determine whether an object has been garbage collected.
832 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700833static JdwpError handleOR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700834 ObjectId objectId;
835
836 objectId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800837 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838
839 // TODO: currently returning false; must integrate with GC
840 expandBufAdd1(pReply, 0);
841
842 return ERR_NONE;
843}
844
845/*
846 * Return the string value in a string object.
847 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700848static JdwpError handleSR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700849 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800850 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700851
Elliott Hughes82914b62012-04-09 15:56:29 -0700852 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800854 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700855
856 return ERR_NONE;
857}
858
859/*
860 * Return a thread's name.
861 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700862static JdwpError handleTR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700863 ObjectId threadId = ReadObjectId(&buf);
864
Elliott Hughes229feb72012-02-23 13:33:29 -0800865 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", threadId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800866 std::string name;
867 if (!Dbg::GetThreadName(threadId, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700868 return ERR_INVALID_THREAD;
869 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800870 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", threadId, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800871 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872
873 return ERR_NONE;
874}
875
876/*
877 * Suspend the specified thread.
878 *
879 * It's supposed to remain suspended even if interpreted code wants to
880 * resume it; only the JDI is allowed to resume it.
881 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700882static JdwpError handleTR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700883 ObjectId threadId = ReadObjectId(&buf);
884
885 if (threadId == Dbg::GetThreadSelfId()) {
886 LOG(INFO) << " Warning: ignoring request to suspend self";
887 return ERR_THREAD_NOT_SUSPENDED;
888 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800889 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700890 Dbg::SuspendThread(threadId);
891 return ERR_NONE;
892}
893
894/*
895 * Resume the specified thread.
896 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700897static JdwpError handleTR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700898 ObjectId threadId = ReadObjectId(&buf);
899
900 if (threadId == Dbg::GetThreadSelfId()) {
901 LOG(INFO) << " Warning: ignoring request to resume self";
902 return ERR_NONE;
903 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800904 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700905 Dbg::ResumeThread(threadId);
906 return ERR_NONE;
907}
908
909/*
910 * Return status of specified thread.
911 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700912static JdwpError handleTR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700913 ObjectId threadId = ReadObjectId(&buf);
914
Elliott Hughes229feb72012-02-23 13:33:29 -0800915 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700916
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800917 JDWP::JdwpThreadStatus threadStatus;
918 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700919 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
920 return ERR_INVALID_THREAD;
921 }
922
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800923 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700924
925 expandBufAdd4BE(pReply, threadStatus);
926 expandBufAdd4BE(pReply, suspendStatus);
927
928 return ERR_NONE;
929}
930
931/*
932 * Return the thread group that the specified thread is a member of.
933 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700934static JdwpError handleTR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700935 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes2435a572012-02-17 16:07:41 -0800936 return Dbg::GetThreadGroup(threadId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937}
938
939/*
940 * Return the current call stack of a suspended thread.
941 *
942 * If the thread isn't suspended, the error code isn't defined, but should
943 * be THREAD_NOT_SUSPENDED.
944 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700945static JdwpError handleTR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700946 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800947 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700948 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949
950 if (!Dbg::ThreadExists(threadId)) {
951 return ERR_INVALID_THREAD;
952 }
953 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughes229feb72012-02-23 13:33:29 -0800954 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955 return ERR_THREAD_NOT_SUSPENDED;
956 }
957
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800958 size_t actual_frame_count = Dbg::GetThreadFrameCount(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700959
Elliott Hughes229feb72012-02-23 13:33:29 -0800960 VLOG(jdwp) << StringPrintf(" Request for frames: threadId=%#llx start=%d length=%d [count=%zd]", threadId, start_frame, length, actual_frame_count);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800961 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700962 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
963 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700964
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800965 if (start_frame > actual_frame_count) {
966 return ERR_INVALID_INDEX;
967 }
968 if (length == static_cast<uint32_t>(-1)) {
969 length = actual_frame_count - start_frame;
970 }
971 if (start_frame + length > actual_frame_count) {
972 return ERR_INVALID_LENGTH;
973 }
974
975 expandBufAdd4BE(pReply, length);
976 for (uint32_t i = start_frame; i < (start_frame + length); ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700977 FrameId frameId;
978 JdwpLocation loc;
Elliott Hughes530fa002012-03-12 11:44:49 -0700979 // TODO: switch to GetThreadFrames so we don't have to search for each frame
980 // even though we only want them in order.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700981 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
982
983 expandBufAdd8BE(pReply, frameId);
984 AddLocation(pReply, &loc);
985
Elliott Hughes229feb72012-02-23 13:33:29 -0800986 VLOG(jdwp) << StringPrintf(" Frame %d: id=%#llx ", i, frameId) << loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700987 }
988
989 return ERR_NONE;
990}
991
992/*
993 * Returns the #of frames on the specified thread, which must be suspended.
994 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700995static JdwpError handleTR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700996 ObjectId threadId = ReadObjectId(&buf);
997
998 if (!Dbg::ThreadExists(threadId)) {
999 return ERR_INVALID_THREAD;
1000 }
1001 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughes229feb72012-02-23 13:33:29 -08001002 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001003 return ERR_THREAD_NOT_SUSPENDED;
1004 }
1005
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001006 int frame_count = Dbg::GetThreadFrameCount(threadId);
1007 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001008 return ERR_INVALID_THREAD;
1009 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001010 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001011
1012 return ERR_NONE;
1013}
1014
1015/*
1016 * Get the monitor that the thread is waiting on.
1017 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001018static JdwpError handleTR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019 ObjectId threadId;
1020
1021 threadId = ReadObjectId(&buf);
1022
1023 // TODO: create an Object to represent the monitor (we're currently
1024 // just using a raw Monitor struct in the VM)
1025
1026 return ERR_NOT_IMPLEMENTED;
1027}
1028
1029/*
1030 * Return the suspend count for the specified thread.
1031 *
1032 * (The thread *might* still be running -- it might not have examined
1033 * its suspend count recently.)
1034 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001035static JdwpError handleTR_SuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001036 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes2435a572012-02-17 16:07:41 -08001037 return Dbg::GetThreadSuspendCount(threadId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001038}
1039
1040/*
1041 * Return the name of a thread group.
1042 *
1043 * The Eclipse debugger recognizes "main" and "system" as special.
1044 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001045static JdwpError handleTGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001047 VLOG(jdwp) << StringPrintf(" Req for name of threadGroupId=%#llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001048
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001049 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050
1051 return ERR_NONE;
1052}
1053
1054/*
1055 * Returns the thread group -- if any -- that contains the specified
1056 * thread group.
1057 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001058static JdwpError handleTGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001059 ObjectId groupId = ReadObjectId(&buf);
1060
1061 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1062 expandBufAddObjectId(pReply, parentGroup);
1063
1064 return ERR_NONE;
1065}
1066
1067/*
1068 * Return the active threads and thread groups that are part of the
1069 * specified thread group.
1070 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001071static JdwpError handleTGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001072 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001073 VLOG(jdwp) << StringPrintf(" Req for threads in threadGroupId=%#llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001074
1075 ObjectId* pThreadIds;
1076 uint32_t threadCount;
1077 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1078
1079 expandBufAdd4BE(pReply, threadCount);
1080
1081 for (uint32_t i = 0; i < threadCount; i++) {
1082 expandBufAddObjectId(pReply, pThreadIds[i]);
1083 }
1084 free(pThreadIds);
1085
1086 /*
1087 * TODO: finish support for child groups
1088 *
1089 * For now, just show that "main" is a child of "system".
1090 */
1091 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1092 expandBufAdd4BE(pReply, 1);
1093 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1094 } else {
1095 expandBufAdd4BE(pReply, 0);
1096 }
1097
1098 return ERR_NONE;
1099}
1100
1101/*
1102 * Return the #of components in the array.
1103 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001104static JdwpError handleAR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001105 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001106 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001107
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001108 int length;
1109 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1110 if (status != ERR_NONE) {
1111 return status;
1112 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001113 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001114
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001115 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001116
1117 return ERR_NONE;
1118}
1119
1120/*
1121 * Return the values from an array.
1122 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001123static JdwpError handleAR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001124 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001125 uint32_t firstIndex = Read4BE(&buf);
1126 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001127 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001128
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001129 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001130}
1131
1132/*
1133 * Set values in an array.
1134 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001135static JdwpError handleAR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001137 uint32_t firstIndex = Read4BE(&buf);
1138 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139
Elliott Hughes229feb72012-02-23 13:33:29 -08001140 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId, firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001141
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001142 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001143}
1144
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001145static JdwpError handleCLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001146 ObjectId classLoaderObject;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001147 classLoaderObject = ReadObjectId(&buf);
Elliott Hughes86964332012-02-15 19:37:42 -08001148 // TODO: we should only return classes which have the given class loader as a defining or
1149 // initiating loader. The former would be easy; the latter is hard, because we don't have
1150 // any such notion.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001151 return handleVM_AllClasses(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001152}
1153
1154/*
1155 * Set an event trigger.
1156 *
1157 * Reply with a requestID.
1158 */
1159static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1160 const uint8_t* origBuf = buf;
1161
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001162 uint8_t eventKind = Read1(&buf);
1163 uint8_t suspendPolicy = Read1(&buf);
1164 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001165
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001166 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001167 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1168 << " mods=" << modifierCount << ")";
1169
1170 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1171
1172 JdwpEvent* pEvent = EventAlloc(modifierCount);
1173 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1174 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1175 pEvent->modCount = modifierCount;
1176
1177 /*
1178 * Read modifiers. Ordering may be significant (see explanation of Count
1179 * mods in JDWP doc).
1180 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001181 for (uint32_t i = 0; i < modifierCount; ++i) {
1182 JdwpEventMod& mod = pEvent->mods[i];
1183 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1184 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001185 case MK_COUNT: /* report once, when "--count" reaches 0 */
1186 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001187 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001188 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189 if (count == 0) {
1190 return ERR_INVALID_COUNT;
1191 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001192 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193 }
1194 break;
1195 case MK_CONDITIONAL: /* conditional on expression) */
1196 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001197 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001198 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001199 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001200 }
1201 break;
1202 case MK_THREAD_ONLY: /* only report events in specified thread */
1203 {
1204 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001205 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", threadId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001206 mod.threadOnly.threadId = threadId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001207 }
1208 break;
1209 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1210 {
Elliott Hughese84278b2012-03-22 10:06:53 -07001211 RefTypeId classId = ReadRefTypeId(&buf);
1212 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", classId, Dbg::GetClassName(classId).c_str());
1213 mod.classOnly.refTypeId = classId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001214 }
1215 break;
1216 case MK_CLASS_MATCH: /* restrict events to matching classes */
1217 {
Elliott Hughes86964332012-02-15 19:37:42 -08001218 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001219 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001220 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001221 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001222 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001223 }
1224 break;
1225 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1226 {
Elliott Hughes86964332012-02-15 19:37:42 -08001227 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001228 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001229 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001230 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001231 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001232 }
1233 break;
1234 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1235 {
1236 JdwpLocation loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001237 jdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001238 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001239 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001240 }
1241 break;
1242 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1243 {
1244 RefTypeId exceptionOrNull; /* null == all exceptions */
1245 uint8_t caught, uncaught;
1246
1247 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001248 caught = Read1(&buf);
1249 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001250 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001251 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001252
Elliott Hughes972a47b2012-02-21 18:16:06 -08001253 mod.exceptionOnly.refTypeId = exceptionOrNull;
1254 mod.exceptionOnly.caught = caught;
1255 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001256 }
1257 break;
1258 case MK_FIELD_ONLY: /* for field access/mod events */
1259 {
1260 RefTypeId declaring = ReadRefTypeId(&buf);
1261 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001262 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001263 mod.fieldOnly.refTypeId = declaring;
1264 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 }
1266 break;
1267 case MK_STEP: /* for use with EK_SINGLE_STEP */
1268 {
1269 ObjectId threadId;
1270 uint32_t size, depth;
1271
1272 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001273 size = Read4BE(&buf);
1274 depth = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001275 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", threadId)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001276 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1277
Elliott Hughes972a47b2012-02-21 18:16:06 -08001278 mod.step.threadId = threadId;
1279 mod.step.size = size;
1280 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001281 }
1282 break;
1283 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1284 {
1285 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001286 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001287 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001288 }
1289 break;
1290 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001291 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001292 break;
1293 }
1294 }
1295
1296 /*
1297 * Make sure we consumed all data. It is possible that the remote side
1298 * has sent us bad stuff, but for now we blame ourselves.
1299 */
1300 if (buf != origBuf + dataLen) {
1301 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1302 }
1303
1304 /*
1305 * We reply with an integer "requestID".
1306 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001307 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 expandBufAdd4BE(pReply, requestId);
1309
1310 pEvent->requestId = requestId;
1311
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001312 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001313
1314 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001315 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316 if (err != ERR_NONE) {
1317 /* registration failed, probably because event is bogus */
1318 EventFree(pEvent);
1319 LOG(WARNING) << "WARNING: event request rejected";
1320 }
1321 return err;
1322}
1323
1324/*
1325 * Clear an event. Failure to find an event with a matching ID is a no-op
1326 * and does not return an error.
1327 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001328static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001329 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001330 eventKind = Read1(&buf);
1331 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001332
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001333 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001334
Elliott Hughes761928d2011-11-16 18:33:03 -08001335 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001336
1337 return ERR_NONE;
1338}
1339
1340/*
1341 * Return the values of arguments and local variables.
1342 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343static JdwpError handleSF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001344 ObjectId threadId = ReadObjectId(&buf);
1345 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001346 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001347
Elliott Hughes229feb72012-02-23 13:33:29 -08001348 VLOG(jdwp) << StringPrintf(" Req for %d slots in threadId=%#llx frameId=%#llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001349
1350 expandBufAdd4BE(pReply, slots); /* "int values" */
1351 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001352 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001353 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001354
Elliott Hughes2435a572012-02-17 16:07:41 -08001355 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001356
Elliott Hughesdbb40792011-11-18 17:05:22 -08001357 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001358 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1359 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1360 }
1361
1362 return ERR_NONE;
1363}
1364
1365/*
1366 * Set the values of arguments and local variables.
1367 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001368static JdwpError handleSF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001369 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 Hughes229feb72012-02-23 13:33:29 -08001373 VLOG(jdwp) << StringPrintf(" Req to set %d slots in threadId=%#llx frameId=%#llx", slots, threadId, frameId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374
1375 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001376 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001377 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001378 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001379 uint64_t value = jdwpReadValue(&buf, width);
1380
Elliott Hughes2435a572012-02-17 16:07:41 -08001381 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001382 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1383 }
1384
1385 return ERR_NONE;
1386}
1387
1388/*
1389 * Returns the value of "this" for the specified frame.
1390 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391static JdwpError handleSF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08001392 ReadObjectId(&buf); // Skip thread id.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001393 FrameId frameId = ReadFrameId(&buf);
1394
1395 ObjectId objectId;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001396 Dbg::GetThisObject(frameId, &objectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397
1398 uint8_t objectTag = Dbg::GetObjectTag(objectId);
Elliott Hughes229feb72012-02-23 13:33:29 -08001399 VLOG(jdwp) << StringPrintf(" Req for 'this' in frame=%#llx --> %#llx '%c'", frameId, objectId, (char)objectTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001400
1401 expandBufAdd1(pReply, objectTag);
1402 expandBufAddObjectId(pReply, objectId);
1403
1404 return ERR_NONE;
1405}
1406
1407/*
1408 * Return the reference type reflected by this class object.
1409 *
1410 * This appears to be required because ReferenceTypeId values are NEVER
1411 * reused, whereas ClassIds can be recycled like any other object. (Either
1412 * that, or I have no idea what this is for.)
1413 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414static JdwpError handleCOR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001415 RefTypeId classObjectId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001416 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId, Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001417 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418}
1419
1420/*
1421 * Handle a DDM packet with a single chunk in it.
1422 */
1423static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1424 uint8_t* replyBuf = NULL;
1425 int replyLen = -1;
1426
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001427 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001428
1429 /*
1430 * On first DDM packet, notify all handlers that DDM is running.
1431 */
1432 if (!state->ddmActive) {
1433 state->ddmActive = true;
1434 Dbg::DdmConnected();
1435 }
1436
1437 /*
1438 * If they want to send something back, we copy it into the buffer.
1439 * A no-copy approach would be nicer.
1440 *
1441 * TODO: consider altering the JDWP stuff to hold the packet header
1442 * in a separate buffer. That would allow us to writev() DDM traffic
1443 * instead of copying it into the expanding buffer. The reduction in
1444 * heap requirements is probably more valuable than the efficiency.
1445 */
1446 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1447 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1448 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1449 free(replyBuf);
1450 }
1451 return ERR_NONE;
1452}
1453
1454/*
1455 * Handler map decl.
1456 */
1457typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1458
1459struct JdwpHandlerMap {
1460 uint8_t cmdSet;
1461 uint8_t cmd;
1462 JdwpRequestHandler func;
1463 const char* descr;
1464};
1465
1466/*
1467 * Map commands to functions.
1468 *
1469 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1470 * and 128-256 are vendor-defined.
1471 */
1472static const JdwpHandlerMap gHandlerMap[] = {
1473 /* VirtualMachine command set (1) */
1474 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1475 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
Elliott Hughes1fe7afb2012-02-13 17:23:03 -08001476 { 1, 3, handleVM_AllClasses, "VirtualMachine.AllClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001477 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1478 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1479 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1480 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1481 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1482 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1483 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1484 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1485 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1486 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1487 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001488 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1489 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001490 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001491 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1492 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
1493 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
1494 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001495
1496 /* ReferenceType command set (2) */
1497 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1498 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1499 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001500 { 2, 4, handleRT_Fields, "ReferenceType.Fields" },
1501 { 2, 5, handleRT_Methods, "ReferenceType.Methods" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001502 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1503 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001504 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001505 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1506 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1507 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1508 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1509 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1510 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1511 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001512 { 2, 16, NULL, "ReferenceType.Instances" },
1513 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1514 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001515
1516 /* ClassType command set (3) */
1517 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1518 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1519 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1520 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1521
1522 /* ArrayType command set (4) */
1523 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1524
1525 /* InterfaceType command set (5) */
1526
1527 /* Method command set (6) */
1528 { 6, 1, handleM_LineTable, "Method.LineTable" },
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001529 { 6, 2, handleM_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001530 { 6, 3, NULL, "Method.Bytecodes" },
1531 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001532 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1533
1534 /* Field command set (8) */
1535
1536 /* ObjectReference command set (9) */
1537 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1538 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1539 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001540 { 9, 4, NULL, "ObjectReference.UNUSED" },
1541 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001542 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1543 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1544 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1545 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001546 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001547
1548 /* StringReference command set (10) */
1549 { 10, 1, handleSR_Value, "StringReference.Value" },
1550
1551 /* ThreadReference command set (11) */
1552 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1553 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1554 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1555 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1556 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1557 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1558 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001559 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001560 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001561 { 11, 10, NULL, "ThreadReference.Stop" },
1562 { 11, 11, NULL,"ThreadReference.Interrupt" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001563 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001564 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1565 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001566
1567 /* ThreadGroupReference command set (12) */
1568 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1569 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1570 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1571
1572 /* ArrayReference command set (13) */
1573 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1574 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1575 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1576
1577 /* ClassLoaderReference command set (14) */
1578 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1579
1580 /* EventRequest command set (15) */
1581 { 15, 1, handleER_Set, "EventRequest.Set" },
1582 { 15, 2, handleER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001583 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001584
1585 /* StackFrame command set (16) */
1586 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1587 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1588 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001589 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001590
1591 /* ClassObjectReference command set (17) */
1592 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1593
1594 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001595 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001596
1597 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1598};
1599
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001600static const char* GetCommandName(size_t cmdSet, size_t cmd) {
1601 for (int i = 0; i < (int) arraysize(gHandlerMap); i++) {
1602 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1603 return gHandlerMap[i].descr;
1604 }
1605 }
1606 return "?UNKNOWN?";
1607}
1608
1609static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1610 std::string result;
1611 result += "REQ: ";
1612 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1613 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1614 return result;
1615}
1616
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001617/*
1618 * Process a request from the debugger.
1619 *
1620 * On entry, the JDWP thread is in VMWAIT.
1621 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001622void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001623 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001624
1625 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1626 /*
1627 * Activity from a debugger, not merely ddms. Mark us as having an
1628 * active debugger session, and zero out the last-activity timestamp
1629 * so waitForDebugger() doesn't return if we stall for a bit here.
1630 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001631 Dbg::GoActive();
Elliott Hughes7c6169d2012-05-02 16:11:48 -07001632 QuasiAtomic::Swap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001633 }
1634
1635 /*
1636 * If a debugger event has fired in another thread, wait until the
1637 * initiating thread has suspended itself before processing messages
1638 * from the debugger. Otherwise we (the JDWP thread) could be told to
1639 * resume the thread before it has suspended.
1640 *
1641 * We call with an argument of zero to wait for the current event
1642 * thread to finish, and then clear the block. Depending on the thread
1643 * suspend policy, this may allow events in other threads to fire,
1644 * but those events have no bearing on what the debugger has sent us
1645 * in the current request.
1646 *
1647 * Note that we MUST clear the event token before waking the event
1648 * thread up, or risk waiting for the thread to suspend after we've
1649 * told it to resume.
1650 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001651 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001652
1653 /*
1654 * Tell the VM that we're running and shouldn't be interrupted by GC.
1655 * Do this after anything that can stall indefinitely.
1656 */
1657 Dbg::ThreadRunning();
1658
1659 expandBufAddSpace(pReply, kJDWPHeaderLen);
1660
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001661 size_t i;
1662 for (i = 0; i < arraysize(gHandlerMap); i++) {
1663 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001664 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001665 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001666 break;
1667 }
1668 }
1669 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001670 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1671 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001672 result = ERR_NOT_IMPLEMENTED;
1673 }
1674
1675 /*
1676 * Set up the reply header.
1677 *
1678 * If we encountered an error, only send the header back.
1679 */
1680 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001681 Set4BE(replyBuf + 4, pHeader->id);
1682 Set1(replyBuf + 8, kJDWPFlagReply);
1683 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001684 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001685 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001686 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001687 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001688 }
1689
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001690 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001691 if (false) {
1692 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001693 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001694 }
1695
1696 /*
1697 * Update last-activity timestamp. We really only need this during
1698 * the initial setup. Only update if this is a non-DDMS packet.
1699 */
1700 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7c6169d2012-05-02 16:11:48 -07001701 QuasiAtomic::Swap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001702 }
1703
1704 /* tell the VM that GC is okay again */
1705 Dbg::ThreadWaiting();
1706}
1707
1708} // namespace JDWP
1709
1710} // namespace art